From 9ebac71521b4aa39461cb90bea029ed712318b6c Mon Sep 17 00:00:00 2001 From: Keen Yee Liau Date: Thu, 31 Oct 2019 16:15:11 -0700 Subject: [PATCH] fix(language-service): Should not crash if expr ends unexpectedly (#33524) If there is any parser errors when parsing template, we should stop immediately and not proceed with template expression diagnostics. This regression is caused by https://github.com/angular/angular/commit/6d111546527812ab48f48e156f09ef41cb9be1ca and affected v9.0.0-next.4 onwards. PR closes https://github.com/angular/vscode-ng-language-service/issues/436 PR Close #33524 --- packages/language-service/src/diagnostics.ts | 11 ++++------- packages/language-service/test/diagnostics_spec.ts | 13 +++++++++++++ 2 files changed, 17 insertions(+), 7 deletions(-) diff --git a/packages/language-service/src/diagnostics.ts b/packages/language-service/src/diagnostics.ts index 9e92a2dd32..6f613055db 100644 --- a/packages/language-service/src/diagnostics.ts +++ b/packages/language-service/src/diagnostics.ts @@ -21,26 +21,23 @@ import {findPropertyValueOfType, findTightestNode, offsetSpan, spanOf} from './u * @param ast contains HTML and template AST */ export function getTemplateDiagnostics(ast: AstResult): ng.Diagnostic[] { - const results: ng.Diagnostic[] = []; const {parseErrors, templateAst, htmlAst, template} = ast; - if (parseErrors) { - results.push(...parseErrors.map(e => { + if (parseErrors && parseErrors.length) { + return parseErrors.map(e => { return { kind: ng.DiagnosticKind.Error, span: offsetSpan(spanOf(e.span), template.span.start), message: e.msg, }; - })); + }); } - const expressionDiagnostics = getTemplateExpressionDiagnostics({ + return getTemplateExpressionDiagnostics({ templateAst: templateAst, htmlAst: htmlAst, offset: template.span.start, query: template.query, members: template.members, }); - results.push(...expressionDiagnostics); - return results; } /** diff --git a/packages/language-service/test/diagnostics_spec.ts b/packages/language-service/test/diagnostics_spec.ts index daaa615435..4e309e6655 100644 --- a/packages/language-service/test/diagnostics_spec.ts +++ b/packages/language-service/test/diagnostics_spec.ts @@ -56,6 +56,19 @@ describe('diagnostics', () => { } }); + it('should report error for unexpected end of expression', () => { + const content = mockHost.override(TEST_TEMPLATE, `{{ 5 / }}`); + const diags = ngLS.getDiagnostics(TEST_TEMPLATE); + expect(diags.length).toBe(1); + const {messageText, start, length} = diags[0]; + expect(messageText) + .toBe( + 'Parser Error: Unexpected end of expression: {{ 5 / }} ' + + 'at the end of the expression [{{ 5 / }}] in /app/test.ng@0:0'); + expect(start).toBe(0); + expect(length).toBe(content.length); + }); + // https://github.com/angular/vscode-ng-language-service/issues/242 it('should support $any() type cast function', () => { mockHost.override(TEST_TEMPLATE, `
{{$any(title).xyz}}
`);