diff --git a/src/cdk/schematics/utils/ast/ng-module-imports.spec.ts b/src/cdk/schematics/utils/ast/ng-module-imports.spec.ts new file mode 100644 index 000000000000..a9f80fe60287 --- /dev/null +++ b/src/cdk/schematics/utils/ast/ng-module-imports.spec.ts @@ -0,0 +1,38 @@ +import {HostTree} from '@angular-devkit/schematics'; +import {UnitTestTree} from '@angular-devkit/schematics/testing'; +import {hasNgModuleImport} from './ng-module-imports'; + +describe('NgModule import utils', () => { + + let host: UnitTestTree; + + beforeEach(() => { + host = new UnitTestTree(new HostTree()); + }); + + describe('hasNgModuleImport', () => { + it('should properly detect imports', () => { + host.create('/test.ts', ` + @NgModule({ + imports: [TestModule], + }) + export class MyModule {} + `); + + expect(hasNgModuleImport(host, '/test.ts', 'TestModule')).toBe(true); + expect(hasNgModuleImport(host, '/test.ts', 'NotExistent')).toBe(false); + }); + + it(`should detect imports for NgModule's using a namespaced identifier`, () => { + host.create('/test.ts', ` + @myImport.NgModule({ + imports: [TestModule], + }) + export class MyModule {} + `); + + expect(hasNgModuleImport(host, '/test.ts', 'TestModule')).toBe(true); + expect(hasNgModuleImport(host, '/test.ts', 'NotExistent')).toBe(false); + }); + }); +}); diff --git a/src/cdk/schematics/utils/ast/ng-module-imports.ts b/src/cdk/schematics/utils/ast/ng-module-imports.ts index 41ee271d70c9..f7586006392c 100644 --- a/src/cdk/schematics/utils/ast/ng-module-imports.ts +++ b/src/cdk/schematics/utils/ast/ng-module-imports.ts @@ -49,7 +49,7 @@ function resolveIdentifierOfExpression(expression: ts.Expression): ts.Identifier if (ts.isIdentifier(expression)) { return expression; } else if (ts.isPropertyAccessExpression(expression)) { - return resolveIdentifierOfExpression(expression.expression); + return expression.name; } return null; }