diff --git a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts index 4bef1c129c7..c028fee324a 100644 --- a/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts +++ b/packages/compiler-cli/test/ngtsc/ngtsc_spec.ts @@ -10690,6 +10690,27 @@ runInEachFileSystem((os: string) => { 'Parser Error: Bindings cannot contain assignments at column 5 in [x = 2]', ); }); + + it('should report a parse error for unexpected tokens in a template literal interpolation', () => { + env.write( + 'test.ts', + ` + import {Component} from '@angular/core'; + @Component({ + template: '{{ \`Hello, $\{first last}!\` }}', + selector: 'test-cmp', + }) + export class TestCmp { + first = 'Jane'; + last = 'Doe'; + } + `, + ); + const diags = env.driveDiagnostics(); + + expect(diags.length).toBe(1); + expect(diags[0].messageText).toContain('Parser Error: Missing expected }'); + }); }); describe('shadow DOM selector diagnostics', () => { diff --git a/packages/compiler/src/expression_parser/parser.ts b/packages/compiler/src/expression_parser/parser.ts index 8c2d3eb2311..1b43fd28221 100644 --- a/packages/compiler/src/expression_parser/parser.ts +++ b/packages/compiler/src/expression_parser/parser.ts @@ -1648,8 +1648,26 @@ class _ParseAST { this.error('Template literal interpolation cannot be empty'); } expressions.push(expression); + // Expect the closing brace before decrementing `rbracesExpected` so that, if the brace is + // missing, error recovery stops at this interpolation's brace instead of skipping the rest + // of the literal. + this.expectCharacter(chars.$RBRACE); this.rbracesExpected--; + } else if (token.isError()) { + // The lexer produces an error token if the template literal isn't terminated. + // Report it directly and advance. `this.error` would skip over the same token again and + // report it twice. + this.errors.push( + getParseError( + token.toString()!, + this.input, + this.getErrorLocationText(this.index), + this.parseSourceSpan, + ), + ); + this.advance(); } else { + // Skip over any leftover tokens of an interpolation that was already reported as invalid. this.advance(); } } diff --git a/packages/compiler/test/expression_parser/parser_spec.ts b/packages/compiler/test/expression_parser/parser_spec.ts index 831c90cad32..3981fb82f31 100644 --- a/packages/compiler/test/expression_parser/parser_spec.ts +++ b/packages/compiler/test/expression_parser/parser_spec.ts @@ -21,6 +21,7 @@ import { ParseSpan, PropertyRead, TemplateBinding, + TemplateLiteral, VariableBinding, } from '../../src/expression_parser/ast'; import {Lexer} from '../../src/expression_parser/lexer'; @@ -535,6 +536,66 @@ describe('parser', () => { expectBindingError('`hello ${}`', 'Template literal interpolation cannot be empty'); }); + it('should not report errors for valid interpolations', () => { + for (const text of [ + '`${a}`', + '`${a + b}`', + '`${foo.bar}`', + '`${condition ? a : b}`', + '`${a?.b}`', + '`${fn(a, [b, c])}`', + '`${{a: 1}.a}`', + '`${() => a}`', + '`${a | pipe: b}`', + '`before ${a} and ${b} after`', + '`outer ${`inner ${a}`}`', + '{"key": `${a}`}', + 'tag`${a}`', + ]) { + expect(parseBinding(text).errors).withContext(text).toEqual([]); + } + }); + + it('should report an error if an interpolation has unexpected tokens after the expression', () => { + expectBindingError('`hello ${a b} world`', 'Missing expected }'); + // Operators that are not supported in Angular expressions. + expectBindingError('`hello ${a & b} world`', 'Missing expected }'); + expectBindingError('`hello ${a, b} world`', 'Missing expected }'); + // Interpolations that are nested inside of other constructs that expect a closing brace. + expectBindingError('`hello ${`nested ${a b}`} world`', 'Missing expected }'); + expectBindingError('{"a": `hello ${b c}`}', 'Missing expected }'); + expectBindingError('tag`hello ${a b} world`', 'Missing expected }'); + expectBindingError('`hello ${a | pipe b} world`', 'Missing expected }'); + expectActionError('`hello ${a; b} world`', 'Missing expected }'); + }); + + it('should report an error if an interpolation is not closed', () => { + expectBindingError('`hello ${a', 'Missing expected } at the end of the expression'); + expectBindingError('`hello ${a ', 'Missing expected } at the end of the expression'); + }); + + it('should report an error if a template literal with an interpolation is not closed', () => { + expectBindingError('`hello ${a}', 'Unterminated template literal', 1); + expectBindingError('`hello ${a} world', 'Unterminated template literal', 1); + expectBindingError('`hello ${a} and ${b} world', 'Unterminated template literal', 1); + }); + + it('should retain all the elements and expressions of a malformed template literal', () => { + for (const text of [ + '`before ${a b} after`', + '`before ${a b} ${c d} after`', + '`before ${a & b} after`', + '`before ${a; b} after`', + ]) { + const result = validate(parseAction(text)); + const literal = result.ast as TemplateLiteral; + expect(result.errors.length).withContext(text).toBeGreaterThan(0); + expect(literal.elements.length) + .withContext(text) + .toBe(literal.expressions.length + 1); + } + }); + it('should parse tagged template literals with no interpolations', () => { checkBinding('tag`hello!`'); checkBinding('tags.first`hello!`'); @@ -1848,8 +1909,8 @@ function expectActionError(text: string, message: string, errorCount?: number) { expectError(validate(parseAction(text)), message, errorCount); } -function expectBindingError(text: string, message: string) { - expectError(validate(parseBinding(text)), message); +function expectBindingError(text: string, message: string, errorCount?: number) { + expectError(validate(parseBinding(text)), message, errorCount); } /**