fix(language-service): apply suggestion (#34177)

PR Close #34177
This commit is contained in:
ivanwonder 2019-12-03 10:45:17 +08:00 committed by Andrew Kushnir
parent 148a060daa
commit 7a86a32040
4 changed files with 16 additions and 12 deletions

View File

@ -249,8 +249,8 @@ class TypeWrapper implements Symbol {
if (symbol) { if (symbol) {
return symbol.name; return symbol.name;
} else { } else {
// the js primitive type(e.g. 'string') doesn't have Symbol. // A primitive type (e.g. 'string') doesn't have Symbol,
// use the ts.TypeChecker to get the type name. // so use the ts.TypeChecker to get the type name.
return this.context.checker.typeToString(this.tsType); return this.context.checker.typeToString(this.tsType);
} }
} }
@ -314,7 +314,9 @@ class TypeWrapper implements Symbol {
} }
} }
class StringIndexTypeWrappr extends TypeWrapper { // If stringIndexType a primitive type(e.g. 'string'), the Symbol is undefined;
// and in AstType.resolvePropertyRead method, the Symbol.type should get the right type.
class StringIndexTypeWrapper extends TypeWrapper {
public readonly type = new TypeWrapper(this.tsType, this.context); public readonly type = new TypeWrapper(this.tsType, this.context);
} }
@ -516,11 +518,7 @@ 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.
return new StringIndexTypeWrapper(this.stringIndexType, this.context);
// if stringIndexType is js primitive type(e.g. 'string'), the Symbol is undefined;
// 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

@ -146,6 +146,13 @@ describe('completions', () => {
const completions = ngLS.getCompletionsAt(TEST_TEMPLATE, marker.start); const completions = ngLS.getCompletionsAt(TEST_TEMPLATE, marker.start);
expectContain(completions, CompletionKind.PROPERTY, ['id', 'name']); expectContain(completions, CompletionKind.PROPERTY, ['id', 'name']);
}); });
it('should work with dot notation if stringIndexType is a primitive type', () => {
mockHost.override(TEST_TEMPLATE, `{{ primitiveIndexType.test.~{string-primitive-type}}}`);
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'string-primitive-type');
const completions = ngLS.getCompletionsAt(TEST_TEMPLATE, marker.start);
expectContain(completions, CompletionKind.METHOD, ['substring']);
});
}); });
}); });

View File

@ -173,10 +173,9 @@ describe('diagnostics', () => {
.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', it('should not produce errors with dot notation if stringIndexType is a primitive type',
() => { () => {
mockHost.override(TEST_TEMPLATE, ` mockHost.override(TEST_TEMPLATE, `{{primitiveIndexType.test}}`);
{{primitiveType.test}}`);
const diags = ngLS.getDiagnostics(TEST_TEMPLATE); const diags = ngLS.getDiagnostics(TEST_TEMPLATE);
expect(diags.length).toBe(0); expect(diags.length).toBe(0);
}); });

View File

@ -192,7 +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} = {}; primitiveIndexType: {[name: string]: string} = {};
anyValue: any; anyValue: any;
myClick(event: any) {} myClick(event: any) {}
} }