fix(language-service): remove completion for string (#37983)

If the user inputs a string(e.g. `<div [ngClass]="'str~{cursor}'"></div>`), the completion is useless.

PR Close #37983
This commit is contained in:
ivanwonder 2020-07-08 16:29:55 +08:00 committed by Andrew Scott
parent 36dd817913
commit 10aba15154
2 changed files with 17 additions and 1 deletions

View File

@ -75,7 +75,16 @@ export function getExpressionCompletions(
visitKeyedWrite(_ast) {},
visitLiteralArray(_ast) {},
visitLiteralMap(_ast) {},
visitLiteralPrimitive(_ast) {},
visitLiteralPrimitive(ast) {
// The type `LiteralPrimitive` include the `ERROR`, and it's wrapped as `string`.
// packages/compiler/src/template_parser/binding_parser.ts#L308
// So exclude the `ERROR` here.
if (typeof ast.value === 'string' &&
ast.value ===
templateInfo.source.slice(ast.sourceSpan.start + 1, ast.sourceSpan.end - 1)) {
result = undefined;
}
},
visitMethodCall(_ast) {},
visitPipe(ast) {
if (position >= ast.exp.span.end &&

View File

@ -848,6 +848,13 @@ describe('completions', () => {
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
expect(completions).toBeUndefined();
});
it('should not provide completions for string', () => {
mockHost.override(TEST_TEMPLATE, `<div [ngClass]="'str~{cursor}'"></div>`);
const marker = mockHost.getLocationMarkerFor(TEST_TEMPLATE, 'cursor');
const completions = ngLS.getCompletionsAtPosition(TEST_TEMPLATE, marker.start);
expect(completions).toBeUndefined();
});
});
function expectContain(