Skip to content

Commit da8987b

Browse files
devversionjosephperrott
authored andcommitted
fix(schematics): incorrectly throws if NgModule uses namespaced decorator (#15298)
Currently if the developer defines the primary app `NgModule` using a namespaced import (e.g. "@ngCore.NgModule`), the Material schematics will incorrectly not detect that module as the logic that resolves the decorator name of a class declaration is flawed. This is because it incorrectly traverses a `PropertyAccessExpression`, while the accessed property name is exposed at the root level `PropertyAccessExpression`. This commit fixes this and also adds additional unit tests for the `hasNgModuleImport` utility function. (We have integration tests for this; but we don't test that special case)
1 parent ae41a0a commit da8987b

File tree

2 files changed

+39
-1
lines changed

2 files changed

+39
-1
lines changed
Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
import {HostTree} from '@angular-devkit/schematics';
2+
import {UnitTestTree} from '@angular-devkit/schematics/testing';
3+
import {hasNgModuleImport} from './ng-module-imports';
4+
5+
describe('NgModule import utils', () => {
6+
7+
let host: UnitTestTree;
8+
9+
beforeEach(() => {
10+
host = new UnitTestTree(new HostTree());
11+
});
12+
13+
describe('hasNgModuleImport', () => {
14+
it('should properly detect imports', () => {
15+
host.create('/test.ts', `
16+
@NgModule({
17+
imports: [TestModule],
18+
})
19+
export class MyModule {}
20+
`);
21+
22+
expect(hasNgModuleImport(host, '/test.ts', 'TestModule')).toBe(true);
23+
expect(hasNgModuleImport(host, '/test.ts', 'NotExistent')).toBe(false);
24+
});
25+
26+
it(`should detect imports for NgModule's using a namespaced identifier`, () => {
27+
host.create('/test.ts', `
28+
@myImport.NgModule({
29+
imports: [TestModule],
30+
})
31+
export class MyModule {}
32+
`);
33+
34+
expect(hasNgModuleImport(host, '/test.ts', 'TestModule')).toBe(true);
35+
expect(hasNgModuleImport(host, '/test.ts', 'NotExistent')).toBe(false);
36+
});
37+
});
38+
});

src/cdk/schematics/utils/ast/ng-module-imports.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ function resolveIdentifierOfExpression(expression: ts.Expression): ts.Identifier
4949
if (ts.isIdentifier(expression)) {
5050
return expression;
5151
} else if (ts.isPropertyAccessExpression(expression)) {
52-
return resolveIdentifierOfExpression(expression.expression);
52+
return expression.name;
5353
}
5454
return null;
5555
}

0 commit comments

Comments
 (0)