Skip to content

Commit 9ebac71

Browse files
Keen Yee Liauatscott
authored andcommitted
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 6d11154 and affected v9.0.0-next.4 onwards. PR closes angular/vscode-ng-language-service#436 PR Close #33524
1 parent ce30888 commit 9ebac71

File tree

2 files changed

+17
-7
lines changed

2 files changed

+17
-7
lines changed

packages/language-service/src/diagnostics.ts

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,26 +21,23 @@ import {findPropertyValueOfType, findTightestNode, offsetSpan, spanOf} from './u
2121
* @param ast contains HTML and template AST
2222
*/
2323
export function getTemplateDiagnostics(ast: AstResult): ng.Diagnostic[] {
24-
const results: ng.Diagnostic[] = [];
2524
const {parseErrors, templateAst, htmlAst, template} = ast;
26-
if (parseErrors) {
27-
results.push(...parseErrors.map(e => {
25+
if (parseErrors && parseErrors.length) {
26+
return parseErrors.map(e => {
2827
return {
2928
kind: ng.DiagnosticKind.Error,
3029
span: offsetSpan(spanOf(e.span), template.span.start),
3130
message: e.msg,
3231
};
33-
}));
32+
});
3433
}
35-
const expressionDiagnostics = getTemplateExpressionDiagnostics({
34+
return getTemplateExpressionDiagnostics({
3635
templateAst: templateAst,
3736
htmlAst: htmlAst,
3837
offset: template.span.start,
3938
query: template.query,
4039
members: template.members,
4140
});
42-
results.push(...expressionDiagnostics);
43-
return results;
4441
}
4542

4643
/**

packages/language-service/test/diagnostics_spec.ts

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,6 +56,19 @@ describe('diagnostics', () => {
5656
}
5757
});
5858

59+
it('should report error for unexpected end of expression', () => {
60+
const content = mockHost.override(TEST_TEMPLATE, `{{ 5 / }}`);
61+
const diags = ngLS.getDiagnostics(TEST_TEMPLATE);
62+
expect(diags.length).toBe(1);
63+
const {messageText, start, length} = diags[0];
64+
expect(messageText)
65+
.toBe(
66+
'Parser Error: Unexpected end of expression: {{ 5 / }} ' +
67+
'at the end of the expression [{{ 5 / }}] in /app/test.ng@0:0');
68+
expect(start).toBe(0);
69+
expect(length).toBe(content.length);
70+
});
71+
5972
// https://github.com/angular/vscode-ng-language-service/issues/242
6073
it('should support $any() type cast function', () => {
6174
mockHost.override(TEST_TEMPLATE, `<div>{{$any(title).xyz}}</div>`);

0 commit comments

Comments
 (0)