fix(language-service): bug of accessing a string index signature using dot notation (#34177)

PR Close #34177
This commit is contained in:
ivanwonder 2019-12-02 15:22:07 +08:00 committed by Andrew Kushnir
parent 0d95c08cda
commit 2ebaa51514
3 changed files with 18 additions and 4 deletions

View File

@ -308,6 +308,10 @@ class TypeWrapper implements Symbol {
} }
} }
class StringIndexTypeWrappr extends TypeWrapper {
public readonly type = new TypeWrapper(this.tsType, this.context);
}
class SymbolWrapper implements Symbol { class SymbolWrapper implements Symbol {
private symbol: ts.Symbol; private symbol: ts.Symbol;
private _members?: SymbolTable; private _members?: SymbolTable;
@ -506,10 +510,11 @@ class SymbolTableWrapper implements SymbolTable {
// obj.stringIndex // equivalent to obj['stringIndex']; // obj.stringIndex // equivalent to obj['stringIndex'];
// //
// In this case, return the type indexed by an arbitrary string key. // In this case, return the type indexed by an arbitrary string key.
const symbol = this.stringIndexType.getSymbol();
if (symbol) { // if stringIndexType is js primitive type(e.g. 'string'), the Symbol is undefined;
return new SymbolWrapper(symbol, this.context, this.stringIndexType); // and In AstType.resolvePropertyRead method, the Symbol.type should get the right type.
} // so I add a new Symbol type, 'StringIndexTypeWrappr'
return new StringIndexTypeWrappr(this.stringIndexType, this.context);
} }
return undefined; return undefined;

View File

@ -172,6 +172,14 @@ describe('diagnostics', () => {
expect(diags[0].messageText) expect(diags[0].messageText)
.toBe(`Identifier 'badProperty' is not defined. 'Hero' does not contain such a member`); .toBe(`Identifier 'badProperty' is not defined. 'Hero' does not contain such a member`);
}); });
it('should not produce errors with dot notation if stringIndexType is js primitive type',
() => {
mockHost.override(TEST_TEMPLATE, `
{{primitiveType.test}}`);
const diags = ngLS.getDiagnostics(TEST_TEMPLATE);
expect(diags.length).toBe(0);
});
}); });
}); });

View File

@ -192,6 +192,7 @@ export class TemplateReference {
tupleArray: [string, Hero] = ['test', this.hero]; tupleArray: [string, Hero] = ['test', this.hero];
league: Hero[][] = [this.heroes]; league: Hero[][] = [this.heroes];
heroesByName: {[name: string]: Hero} = {}; heroesByName: {[name: string]: Hero} = {};
primitiveType: {[name: string]: string} = {};
anyValue: any; anyValue: any;
myClick(event: any) {} myClick(event: any) {}
} }