Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(language-service): Should not crash if expr ends unexpectedly #33524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
11 changes: 4 additions & 7 deletions packages/language-service/src/diagnostics.ts
Expand Up @@ -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;
}

/**
Expand Down
13 changes: 13 additions & 0 deletions packages/language-service/test/diagnostics_spec.ts
Expand Up @@ -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, `<div>{{$any(title).xyz}}</div>`);
Expand Down