fix(compiler): always check summaries first before falling back to metadata from .d.ts files (#18788)

PR Close #18788
This commit is contained in:
Tobias Bosch 2017-08-22 16:18:18 -07:00 committed by Miško Hevery
parent 0262e37301
commit f83b819bea
2 changed files with 22 additions and 11 deletions

View File

@ -78,20 +78,22 @@ export class StaticSymbolResolver {
if (staticSymbol.members.length > 0) { if (staticSymbol.members.length > 0) {
return this._resolveSymbolMembers(staticSymbol) !; return this._resolveSymbolMembers(staticSymbol) !;
} }
let result = this.resolvedSymbols.get(staticSymbol); // Note: always ask for a summary first,
if (result) { // as we might have read shallow metadata via a .d.ts file
return result; // for the symbol.
const resultFromSummary = this._resolveSymbolFromSummary(staticSymbol) !;
if (resultFromSummary) {
return resultFromSummary;
} }
result = this._resolveSymbolFromSummary(staticSymbol) !; const resultFromCache = this.resolvedSymbols.get(staticSymbol);
if (result) { if (resultFromCache) {
return result; return resultFromCache;
} }
// Note: Some users use libraries that were not compiled with ngc, i.e. they don't // Note: Some users use libraries that were not compiled with ngc, i.e. they don't
// have summaries, only .d.ts files. So we always need to check both, the summary // have summaries, only .d.ts files. So we always need to check both, the summary
// and metadata. // and metadata.
this._createSymbolsOf(staticSymbol.filePath); this._createSymbolsOf(staticSymbol.filePath);
result = this.resolvedSymbols.get(staticSymbol) !; return this.resolvedSymbols.get(staticSymbol) !;
return result;
} }
/** /**

View File

@ -104,9 +104,18 @@ describe('StaticSymbolResolver', () => {
}); });
it('should use summaries in resolveSymbol and prefer them over regular metadata', () => { it('should use summaries in resolveSymbol and prefer them over regular metadata', () => {
const someSymbol = symbolCache.get('/test.ts', 'a'); const symbolA = symbolCache.get('/test.ts', 'a');
init({'/test.ts': 'export var a = 2'}, [{symbol: someSymbol, metadata: 1}]); const symbolB = symbolCache.get('/test.ts', 'b');
expect(symbolResolver.resolveSymbol(someSymbol).metadata).toBe(1); const symbolC = symbolCache.get('/test.ts', 'c');
init({'/test.ts': 'export var a = 2; export var b = 2; export var c = 2;'}, [
{symbol: symbolA, metadata: 1},
{symbol: symbolB, metadata: 1},
]);
// reading the metadata of a symbol without a summary first,
// to test whether summaries are still preferred after this.
expect(symbolResolver.resolveSymbol(symbolC).metadata).toBe(2);
expect(symbolResolver.resolveSymbol(symbolA).metadata).toBe(1);
expect(symbolResolver.resolveSymbol(symbolB).metadata).toBe(1);
}); });
it('should be able to get all exported symbols of a file', () => { it('should be able to get all exported symbols of a file', () => {