Skip to content

Commit c0d833e

Browse files
committed
fixup! fix(@schematics/angular): report invalid entry point names with a descriptive error
1 parent 3aad86e commit c0d833e

2 files changed

Lines changed: 102 additions & 33 deletions

File tree

‎packages/schematics/angular/entry-point/index.ts‎

Lines changed: 29 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,28 @@ function updateTsConfig(packageName: string, ...paths: string[]): Rule {
4040
};
4141
}
4242

43+
/**
44+
* Creates a name usable for the generated class, selector, and file names from an
45+
* entry-point name. Entry-point names are npm export subpaths and may contain characters
46+
* (such as `~`) or digit-led words (such as `3d`) that cannot appear in a class name or
47+
* in a selector segment. Returns null when the result would not start with a letter.
48+
*/
49+
function toArtifactName(name: string): string | null {
50+
const artifactName = name
51+
// `~` and `/` cannot appear in a class name, selector, or file name.
52+
.replace(/[~/]/g, '-')
53+
// A word that starts with digits but contains more than digits cannot start a class
54+
// name or a selector segment (`lib-foo-3d` is an invalid selector); merging it into
55+
// the preceding word (`foo3d`) keeps both valid. All-digit words stay separate, as
56+
// trailing digit segments are valid in a selector (`lib-foo-2`).
57+
.replace(/[-_]+(?=\d+[a-zA-Z.])/g, '')
58+
// Separator runs and leading or trailing separators produce empty selector segments.
59+
.replace(/[-_]{2,}/g, '-')
60+
.replace(/^[-_.]+|[-_.]+$/g, '');
61+
62+
return /^[a-zA-Z]/.test(artifactName) ? artifactName : null;
63+
}
64+
4365
export default createProjectSchematic<EntryPointOptions>((options, { project, tree }) => {
4466
if (project.extensions['projectType'] !== ProjectType.Library) {
4567
throw new SchematicsException(`Project "${options.project}" is not a library project.`);
@@ -58,20 +80,6 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
5880
throw new SchematicsException('Entry point name "package.json" is reserved.');
5981
}
6082

61-
// The final segment names the generated class, selector, and files. Without this check,
62-
// a name that is a valid export subpath but not usable as an identifier fails later with
63-
// an error about a class or selector the user never provided.
64-
if (
65-
options.name !== '.' &&
66-
!/^[a-zA-Z][a-zA-Z0-9_-]*(?:\.[a-zA-Z0-9_-]+)*$/.test(basename(options.name))
67-
) {
68-
throw new SchematicsException(
69-
`Entry point name "${options.name}" is invalid. The final path segment is used to name ` +
70-
'the generated source files and must start with a letter and contain only ' +
71-
'alphanumeric characters, dots, dashes, and underscores.',
72-
);
73-
}
74-
7583
const libDir = project.root;
7684
const packageJsonPath = join(libDir, 'package.json');
7785
if (!tree.exists(packageJsonPath)) {
@@ -128,11 +136,16 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
128136

129137
const prefix = (options.prefix ?? project.prefix) || 'lib';
130138
const sourceDir = isPrimary ? join(entryPointDir, 'lib') : entryPointDir;
131-
const artifactName = isPrimary
139+
const baseName = isPrimary
132140
? /^@.*\/.*/.test(options.project)
133141
? options.project.split('/')[1]
134142
: options.project
135-
: basename(options.name);
143+
: options.name;
144+
const artifactName =
145+
toArtifactName(basename(baseName)) ??
146+
toArtifactName(baseName) ??
147+
toArtifactName(`${prefix}-${baseName}`) ??
148+
'entry';
136149

137150
const buildOptions = project.targets.get('build')?.options;
138151
const distRoot =

‎packages/schematics/angular/entry-point/index_spec.ts‎

Lines changed: 73 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -473,23 +473,79 @@ describe('EntryPoint Schematic', () => {
473473
}
474474
});
475475

476-
it('should throw a descriptive error when the final name segment cannot name source files', async () => {
477-
for (const invalidName of ['3d', 'feature/3d', 'svg~icons']) {
478-
await expectAsync(
479-
schematicRunner.runSchematic(
480-
'entry-point',
481-
{
482-
name: invalidName,
483-
project: 'foo',
484-
},
485-
workspaceTree,
486-
),
487-
).toBeRejectedWithError(
488-
`Entry point name "${invalidName}" is invalid. The final path segment is used to name ` +
489-
'the generated source files and must start with a letter and contain only ' +
490-
'alphanumeric characters, dots, dashes, and underscores.',
491-
);
492-
}
476+
it('should create a usable class and selector when the name starts with digits', async () => {
477+
const tree = await schematicRunner.runSchematic(
478+
'entry-point',
479+
{
480+
name: '3d',
481+
project: 'foo',
482+
},
483+
workspaceTree,
484+
);
485+
486+
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
487+
expect(pkgJson.exports['./3d']).toBe('./3d/src/public-api.ts');
488+
489+
const componentContent = tree.readContent('/projects/foo/3d/src/lib3d.ts');
490+
expect(componentContent).toContain('export class Lib3d');
491+
expect(componentContent).toContain("selector: 'lib-lib3d'");
492+
expect(tree.readContent('/projects/foo/3d/src/public-api.ts')).toContain(
493+
"export * from './lib3d';",
494+
);
495+
});
496+
497+
it('should name artifacts from the parent segments when the final segment starts with digits', async () => {
498+
const tree = await schematicRunner.runSchematic(
499+
'entry-point',
500+
{
501+
name: 'feature/3d',
502+
project: 'foo',
503+
},
504+
workspaceTree,
505+
);
506+
507+
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
508+
expect(pkgJson.exports['./feature/3d']).toBe('./feature/3d/src/public-api.ts');
509+
510+
const componentContent = tree.readContent('/projects/foo/feature/3d/src/feature3d.ts');
511+
expect(componentContent).toContain('export class Feature3d');
512+
expect(componentContent).toContain("selector: 'lib-feature3d'");
513+
});
514+
515+
it('should merge a digit-led word into the preceding word', async () => {
516+
const tree = await schematicRunner.runSchematic(
517+
'entry-point',
518+
{
519+
name: 'my-3d',
520+
project: 'foo',
521+
},
522+
workspaceTree,
523+
);
524+
525+
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
526+
expect(pkgJson.exports['./my-3d']).toBe('./my-3d/src/public-api.ts');
527+
528+
const componentContent = tree.readContent('/projects/foo/my-3d/src/my3d.ts');
529+
expect(componentContent).toContain('export class My3d');
530+
expect(componentContent).toContain("selector: 'lib-my3d'");
531+
});
532+
533+
it('should replace characters that cannot appear in a class name or selector', async () => {
534+
const tree = await schematicRunner.runSchematic(
535+
'entry-point',
536+
{
537+
name: 'svg~icons',
538+
project: 'foo',
539+
},
540+
workspaceTree,
541+
);
542+
543+
const pkgJson = getJsonFileContent(tree, '/projects/foo/package.json');
544+
expect(pkgJson.exports['./svg~icons']).toBe('./svg~icons/src/public-api.ts');
545+
546+
const componentContent = tree.readContent('/projects/foo/svg~icons/src/svg-icons.ts');
547+
expect(componentContent).toContain('export class SvgIcons');
548+
expect(componentContent).toContain("selector: 'lib-svg-icons'");
493549
});
494550

495551
it('should allow a final name segment containing dots', async () => {

0 commit comments

Comments
 (0)