Skip to content

Commit 3aad86e

Browse files
committed
fix(@schematics/angular): report invalid entry point names with a descriptive error
The entry-point schematic accepts any name that is a valid npm export subpath, but the final path segment is also used to name the generated class, selector, and source files. A segment that cannot be used that way fails deep inside the component schematic with an error about something the user never provided: `ng generate entry-point 3d` fails with 'Selector "lib-3d" is invalid.' and `ng generate entry-point svg~icons` fails with a raw schema validation dump of the internal component options. Validate the final path segment up front and report the entry point name together with the allowed shape. Dots remain supported, as `classify` removes them when deriving the class name ('testing.v2' generates the class `TestingV2`); a test now covers that case to keep it working.
1 parent 63a1c6a commit 3aad86e

2 files changed

Lines changed: 47 additions & 0 deletions

File tree

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

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,20 @@ export default createProjectSchematic<EntryPointOptions>((options, { project, tr
5858
throw new SchematicsException('Entry point name "package.json" is reserved.');
5959
}
6060

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+
6175
const libDir = project.root;
6276
const packageJsonPath = join(libDir, 'package.json');
6377
if (!tree.exists(packageJsonPath)) {

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

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -473,6 +473,39 @@ 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+
}
493+
});
494+
495+
it('should allow a final name segment containing dots', async () => {
496+
const tree = await schematicRunner.runSchematic(
497+
'entry-point',
498+
{
499+
name: 'testing.v2',
500+
project: 'foo',
501+
},
502+
workspaceTree,
503+
);
504+
505+
const packageJson = getJsonFileContent(tree, '/projects/foo/package.json');
506+
expect(packageJson.exports['./testing.v2']).toBeDefined();
507+
});
508+
476509
it('should throw if the entry point already exists in package.json exports', async () => {
477510
const tree = await schematicRunner.runSchematic('entry-point', defaultOptions, workspaceTree);
478511

0 commit comments

Comments
 (0)