Skip to content

Commit c33a576

Browse files
chuckjazjasonaden
authored andcommitted
fix(compiler): recognize @NgModule with a redundant @Injectable (angular#20320)
The compiler now, again, recognizes `@NgModule()` decorators on classes with a redundant `@Injectable()` decorator. Fixes: angular#19544 PR Close angular#20320
1 parent cf618c5 commit c33a576

File tree

2 files changed

+40
-4
lines changed

2 files changed

+40
-4
lines changed

packages/compiler-cli/test/ngc_spec.ts

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1392,6 +1392,42 @@ describe('ngc transformer command-line', () => {
13921392
});
13931393

13941394
describe('regressions', () => {
1395+
//#19544
1396+
it('should recognize @NgModule() directive with a redundant @Injectable()', () => {
1397+
write('src/tsconfig.json', `{
1398+
"extends": "../tsconfig-base.json",
1399+
"compilerOptions": {
1400+
"outDir": "../dist",
1401+
"rootDir": ".",
1402+
"rootDirs": [
1403+
".",
1404+
"../dist"
1405+
]
1406+
},
1407+
"files": ["test-module.ts"]
1408+
}`);
1409+
write('src/test.component.ts', `
1410+
import {Component} from '@angular/core';
1411+
1412+
@Component({
1413+
template: '<p>hello</p>',
1414+
})
1415+
export class TestComponent {}
1416+
`);
1417+
write('src/test-module.ts', `
1418+
import {Injectable, NgModule} from '@angular/core';
1419+
import {TestComponent} from './test.component';
1420+
1421+
@NgModule({declarations: [TestComponent]})
1422+
@Injectable()
1423+
export class TestModule {}
1424+
`);
1425+
const messages: string[] = [];
1426+
const exitCode =
1427+
main(['-p', path.join(basePath, 'src/tsconfig.json')], message => messages.push(message));
1428+
expect(exitCode).toBe(0, 'Compile failed unexpectedly.\n ' + messages.join('\n '));
1429+
});
1430+
13951431
// #19765
13961432
it('should not report an error when the resolved .css file is in outside rootDir', () => {
13971433
write('src/tsconfig.json', `{

packages/compiler/src/aot/compiler.ts

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,15 +709,15 @@ export function analyzeFile(
709709
} else if (metadataResolver.isPipe(symbol)) {
710710
isNgSymbol = true;
711711
pipes.push(symbol);
712-
} else if (metadataResolver.isInjectable(symbol)) {
713-
isNgSymbol = true;
714-
injectables.push(symbol);
715-
} else {
712+
} else if (metadataResolver.isNgModule(symbol)) {
716713
const ngModule = metadataResolver.getNgModuleMetadata(symbol, false);
717714
if (ngModule) {
718715
isNgSymbol = true;
719716
ngModules.push(ngModule);
720717
}
718+
} else if (metadataResolver.isInjectable(symbol)) {
719+
isNgSymbol = true;
720+
injectables.push(symbol);
721721
}
722722
}
723723
if (!isNgSymbol) {

0 commit comments

Comments
 (0)