fix(compiler): Ignore references to declared modules and unneeded types (#9776)

Fixes: #9670
This commit is contained in:
Chuck Jazdzewski
2016-07-11 17:26:35 -07:00
committed by GitHub
parent eb5763c23f
commit 4ef86891a3
7 changed files with 70 additions and 46 deletions

View File

@ -123,7 +123,10 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
const filePath = this.resolve(module, containingFile);
if (!filePath) {
throw new Error(`Could not resolve module ${module} relative to ${containingFile}`);
// If the file cannot be found the module is probably referencing a declared module
// for which there is no disambiguating file and we also don't need to track
// re-exports. Just use the module name.
return this.getStaticSymbol(module, symbolName);
}
const tc = this.program.getTypeChecker();
@ -174,10 +177,12 @@ export class ReflectorHost implements StaticReflectorHost, ImportGenerator {
return result;
}
// TODO(alexeagle): take a statictype
getMetadataFor(filePath: string): ModuleMetadata {
if (!this.context.exists(filePath)) {
throw new Error(`No such file '${filePath}'`);
// If the file doesn't exists then we cannot return metadata for the file.
// This will occur if the user refernced a declared module for which no file
// exists for the module (i.e. jQuery or angularjs).
return;
}
if (DTS.test(filePath)) {
const metadataPath = filePath.replace(DTS, '.metadata.json');

View File

@ -474,7 +474,7 @@ export class StaticReflector implements ReflectorReader {
let message = produceErrorMessage(expression);
if (expression['line']) {
message =
`${message} (position ${expression['line']}:${expression['character']} in the original .ts file)`;
`${message} (position ${expression['line']+1}:${expression['character']+1} in the original .ts file)`;
}
throw new Error(message);
}

View File

@ -82,12 +82,17 @@ describe('reflector_host', () => {
.toBeDefined();
});
it('should be produce the same symbol if asked twice', () => {
let foo1 = reflectorHost.getStaticSymbol('main.ts', 'foo');
let foo2 = reflectorHost.getStaticSymbol('main.ts', 'foo');
expect(foo1).toBe(foo2);
});
it('should be able to produce a symbol for a module with no file', () => {
expect(reflectorHost.getStaticSymbol('angularjs', 'SomeAngularSymbol')).toBeDefined();
});
it('should be able to read a metadata file',
() => {
expect(reflectorHost.getMetadataFor('node_modules/@angular/core.d.ts'))
@ -96,6 +101,10 @@ describe('reflector_host', () => {
it('should be able to read metadata from an otherwise unused .d.ts file ', () => {
expect(reflectorHost.getMetadataFor('node_modules/@angular/unused.d.ts')).toBeUndefined();
});
it('should return undefined for missing modules', () => {
expect(reflectorHost.getMetadataFor('node_modules/@angular/missing.d.ts')).toBeUndefined();
});
});
const dummyModule = 'export let foo: any[];'

View File

@ -112,7 +112,7 @@ describe('StaticReflector', () => {
let SomeClass = host.findDeclaration('src/error-reporting', 'SomeClass');
expect(() => reflector.annotations(SomeClass))
.toThrow(new Error(
'Error encountered resolving symbol values statically. A reasonable error message (position 12:33 in the original .ts file), resolving symbol ErrorSym in /tmp/src/error-references.d.ts, resolving symbol Link2 in /tmp/src/error-references.d.ts, resolving symbol Link1 in /tmp/src/error-references.d.ts, resolving symbol SomeClass in /tmp/src/error-reporting.d.ts, resolving symbol SomeClass in /tmp/src/error-reporting.d.ts'));
'Error encountered resolving symbol values statically. A reasonable error message (position 13:34 in the original .ts file), resolving symbol ErrorSym in /tmp/src/error-references.d.ts, resolving symbol Link2 in /tmp/src/error-references.d.ts, resolving symbol Link1 in /tmp/src/error-references.d.ts, resolving symbol SomeClass in /tmp/src/error-reporting.d.ts, resolving symbol SomeClass in /tmp/src/error-reporting.d.ts'));
});
it('should simplify primitive into itself', () => {