fix(compiler-cli): do not emit invalid .metadata.json files

If no metadata is collected the `ngc` would generate file
that contained `[null]` instead of eliding the `.metadata.json`
file.

Fixes: #20479
This commit is contained in:
Chuck Jazdzewski 2017-12-15 17:30:41 -08:00 committed by Alex Rickabaugh
parent e4c53f8529
commit 30208759cd
2 changed files with 25 additions and 3 deletions

View File

@ -336,9 +336,11 @@ class AngularCompilerProgram implements Program {
if (!sf.isDeclarationFile && !GENERATED_FILES.test(sf.fileName)) {
metadataJsonCount++;
const metadata = this.metadataCache.getMetadata(sf);
const metadataText = JSON.stringify([metadata]);
const outFileName = srcToOutPath(sf.fileName.replace(/\.tsx?$/, '.metadata.json'));
this.writeFile(outFileName, metadataText, false, undefined, undefined, [sf]);
if (metadata) {
const metadataText = JSON.stringify([metadata]);
const outFileName = srcToOutPath(sf.fileName.replace(/\.tsx?$/, '.metadata.json'));
this.writeFile(outFileName, metadataText, false, undefined, undefined, [sf]);
}
}
});
}

View File

@ -1474,6 +1474,26 @@ describe('ngc transformer command-line', () => {
});
describe('regressions', () => {
//#20479
it('should not generate an invalid metadata file', () => {
write('src/tsconfig.json', `{
"extends": "../tsconfig-base.json",
"files": ["lib.ts"],
"angularCompilerOptions": {
"skipTemplateCodegen": true
}
}`);
write('src/lib.ts', `
export namespace A{
export class C1 {
}
export interface I1{
}
}`);
expect(main(['-p', path.join(basePath, 'src/tsconfig.json')])).toBe(0);
shouldNotExist('src/lib.metadata.json');
});
//#19544
it('should recognize @NgModule() directive with a redundant @Injectable()', () => {
write('src/tsconfig.json', `{