From c33a57666b9a60a0fff8e7dbfbe2beddcd529666 Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Thu, 9 Nov 2017 16:52:19 -0800 Subject: [PATCH] fix(compiler): recognize @NgModule with a redundant @Injectable (#20320) The compiler now, again, recognizes `@NgModule()` decorators on classes with a redundant `@Injectable()` decorator. Fixes: #19544 PR Close #20320 --- packages/compiler-cli/test/ngc_spec.ts | 36 ++++++++++++++++++++++++++ packages/compiler/src/aot/compiler.ts | 8 +++--- 2 files changed, 40 insertions(+), 4 deletions(-) diff --git a/packages/compiler-cli/test/ngc_spec.ts b/packages/compiler-cli/test/ngc_spec.ts index b142b41bf6..e92ee8ba8b 100644 --- a/packages/compiler-cli/test/ngc_spec.ts +++ b/packages/compiler-cli/test/ngc_spec.ts @@ -1392,6 +1392,42 @@ describe('ngc transformer command-line', () => { }); describe('regressions', () => { + //#19544 + it('should recognize @NgModule() directive with a redundant @Injectable()', () => { + write('src/tsconfig.json', `{ + "extends": "../tsconfig-base.json", + "compilerOptions": { + "outDir": "../dist", + "rootDir": ".", + "rootDirs": [ + ".", + "../dist" + ] + }, + "files": ["test-module.ts"] + }`); + write('src/test.component.ts', ` + import {Component} from '@angular/core'; + + @Component({ + template: '

hello

', + }) + export class TestComponent {} + `); + write('src/test-module.ts', ` + import {Injectable, NgModule} from '@angular/core'; + import {TestComponent} from './test.component'; + + @NgModule({declarations: [TestComponent]}) + @Injectable() + export class TestModule {} + `); + const messages: string[] = []; + const exitCode = + main(['-p', path.join(basePath, 'src/tsconfig.json')], message => messages.push(message)); + expect(exitCode).toBe(0, 'Compile failed unexpectedly.\n ' + messages.join('\n ')); + }); + // #19765 it('should not report an error when the resolved .css file is in outside rootDir', () => { write('src/tsconfig.json', `{ diff --git a/packages/compiler/src/aot/compiler.ts b/packages/compiler/src/aot/compiler.ts index 22be0661ae..07b5ccc517 100644 --- a/packages/compiler/src/aot/compiler.ts +++ b/packages/compiler/src/aot/compiler.ts @@ -709,15 +709,15 @@ export function analyzeFile( } else if (metadataResolver.isPipe(symbol)) { isNgSymbol = true; pipes.push(symbol); - } else if (metadataResolver.isInjectable(symbol)) { - isNgSymbol = true; - injectables.push(symbol); - } else { + } else if (metadataResolver.isNgModule(symbol)) { const ngModule = metadataResolver.getNgModuleMetadata(symbol, false); if (ngModule) { isNgSymbol = true; ngModules.push(ngModule); } + } else if (metadataResolver.isInjectable(symbol)) { + isNgSymbol = true; + injectables.push(symbol); } } if (!isNgSymbol) {