Skip to content

Commit 7a86a32

Browse files
ivanwonderAndrewKushnir
authored andcommitted
fix(language-service): apply suggestion (angular#34177)
PR Close angular#34177
1 parent 148a060 commit 7a86a32

File tree

4 files changed

+16
-12
lines changed

4 files changed

+16
-12
lines changed

packages/language-service/src/typescript_symbols.ts

Lines changed: 6 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -249,8 +249,8 @@ class TypeWrapper implements Symbol {
249249
if (symbol) {
250250
return symbol.name;
251251
} else {
252-
// the js primitive type(e.g. 'string') doesn't have Symbol.
253-
// use the ts.TypeChecker to get the type name.
252+
// A primitive type (e.g. 'string') doesn't have Symbol,
253+
// so use the ts.TypeChecker to get the type name.
254254
return this.context.checker.typeToString(this.tsType);
255255
}
256256
}
@@ -314,7 +314,9 @@ class TypeWrapper implements Symbol {
314314
}
315315
}
316316

317-
class StringIndexTypeWrappr extends TypeWrapper {
317+
// If stringIndexType a primitive type(e.g. 'string'), the Symbol is undefined;
318+
// and in AstType.resolvePropertyRead method, the Symbol.type should get the right type.
319+
class StringIndexTypeWrapper extends TypeWrapper {
318320
public readonly type = new TypeWrapper(this.tsType, this.context);
319321
}
320322

@@ -516,11 +518,7 @@ class SymbolTableWrapper implements SymbolTable {
516518
// obj.stringIndex // equivalent to obj['stringIndex'];
517519
//
518520
// In this case, return the type indexed by an arbitrary string key.
519-
520-
// if stringIndexType is js primitive type(e.g. 'string'), the Symbol is undefined;
521-
// and In AstType.resolvePropertyRead method, the Symbol.type should get the right type.
522-
// so I add a new Symbol type, 'StringIndexTypeWrappr'
523-
return new StringIndexTypeWrappr(this.stringIndexType, this.context);
521+
return new StringIndexTypeWrapper(this.stringIndexType, this.context);
524522
}
525523

526524
return undefined;

packages/language-service/test/completions_spec.ts

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,13 @@ describe('completions', () => {
146146
const completions = ngLS.getCompletionsAt(TEST_TEMPLATE, marker.start);
147147
expectContain(completions, CompletionKind.PROPERTY, ['id', 'name']);
148148
});
149+
150+
it('should work with dot notation if stringIndexType is a primitive type', () => {
151+
mockHost.override(TEST_TEMPLATE, `{{ primitiveIndexType.test.~{string-primitive-type}}}`);
152+
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'string-primitive-type');
153+
const completions = ngLS.getCompletionsAt(TEST_TEMPLATE, marker.start);
154+
expectContain(completions, CompletionKind.METHOD, ['substring']);
155+
});
149156
});
150157
});
151158

packages/language-service/test/diagnostics_spec.ts

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -173,10 +173,9 @@ describe('diagnostics', () => {
173173
.toBe(`Identifier 'badProperty' is not defined. 'Hero' does not contain such a member`);
174174
});
175175

176-
it('should not produce errors with dot notation if stringIndexType is js primitive type',
176+
it('should not produce errors with dot notation if stringIndexType is a primitive type',
177177
() => {
178-
mockHost.override(TEST_TEMPLATE, `
179-
{{primitiveType.test}}`);
178+
mockHost.override(TEST_TEMPLATE, `{{primitiveIndexType.test}}`);
180179
const diags = ngLS.getDiagnostics(TEST_TEMPLATE);
181180
expect(diags.length).toBe(0);
182181
});

packages/language-service/test/project/app/parsing-cases.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,7 @@ export class TemplateReference {
192192
tupleArray: [string, Hero] = ['test', this.hero];
193193
league: Hero[][] = [this.heroes];
194194
heroesByName: {[name: string]: Hero} = {};
195-
primitiveType: {[name: string]: string} = {};
195+
primitiveIndexType: {[name: string]: string} = {};
196196
anyValue: any;
197197
myClick(event: any) {}
198198
}

0 commit comments

Comments
 (0)