From 30bec78da32f149d85aed7079e2b0aa7475885ce Mon Sep 17 00:00:00 2001 From: Chuck Jazdzewski Date: Wed, 6 Jul 2016 14:26:31 -0700 Subject: [PATCH] fix(compiler): Missing metadata files should result in undefined (#9704) RelectorHost threw an exception when metadata was requested for a .d.ts file that didn't have a .metadata.json file. Changed it to return undefined. Fixes #9678 --- modules/@angular/compiler-cli/src/reflector_host.ts | 13 ++++++------- .../compiler-cli/test/reflector_host_spec.ts | 8 ++++++-- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/modules/@angular/compiler-cli/src/reflector_host.ts b/modules/@angular/compiler-cli/src/reflector_host.ts index 44318e7835..29daaf9cdd 100644 --- a/modules/@angular/compiler-cli/src/reflector_host.ts +++ b/modules/@angular/compiler-cli/src/reflector_host.ts @@ -184,14 +184,13 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator { if (this.context.exists(metadataPath)) { return this.readMetadata(metadataPath); } + } else { + const sf = this.program.getSourceFile(filePath); + if (!sf) { + throw new Error(`Source file ${filePath} not present in program.`); + } + return this.metadataCollector.getMetadata(sf); } - - let sf = this.program.getSourceFile(filePath); - if (!sf) { - throw new Error(`Source file ${filePath} not present in program.`); - } - const metadata = this.metadataCollector.getMetadata(sf); - return metadata; } readMetadata(filePath: string) { diff --git a/modules/@angular/compiler-cli/test/reflector_host_spec.ts b/modules/@angular/compiler-cli/test/reflector_host_spec.ts index 4805fe10ba..b7a778423b 100644 --- a/modules/@angular/compiler-cli/test/reflector_host_spec.ts +++ b/modules/@angular/compiler-cli/test/reflector_host_spec.ts @@ -92,6 +92,10 @@ describe('reflector_host', () => { () => { expect(reflectorHost.getMetadataFor('node_modules/@angular/core.d.ts')) .toEqual({__symbolic: 'module', version: 1, metadata: {foo: {__symbolic: 'class'}}})}); + + it('should be able to read metadata from an otherwise unused .d.ts file ', () => { + expect(reflectorHost.getMetadataFor('node_modules/@angular/unused.d.ts')).toBeUndefined(); + }); }); const dummyModule = 'export let foo: any[];' @@ -115,8 +119,8 @@ const FILES: Entry = { 'core.d.ts': dummyModule, 'core.metadata.json': `{"__symbolic":"module", "version": 1, "metadata": {"foo": {"__symbolic": "class"}}}`, - 'router-deprecated': - {'index.d.ts': dummyModule, 'src': {'providers.d.ts': dummyModule}} + 'router-deprecated': {'index.d.ts': dummyModule, 'src': {'providers.d.ts': dummyModule}}, + 'unused.d.ts': dummyModule } } }