fix(compiler): report errors for malformed template literal interpolations

The expression parser silently discarded any tokens that followed the
expression inside a template literal interpolation. For example,
`${a b}` was parsed as `${a}` and `${flags & 1}` as `${flags}`, without
a diagnostic. A template literal with an interpolation that was never
terminated was also accepted, because its lexer error was skipped in the
same way.

`parseTemplateLiteral` used a catch-all `advance()` to consume the `}`
that closes an interpolation, which also consumed anything else that was
left over. The closing brace is now expected explicitly, before
`rbracesExpected` is decremented so that error recovery still stops at
the brace of the interpolation, and lexer errors are reported.

Templates that were previously accepted with the extra tokens dropped
(or with an unterminated literal) now fail to compile with a parse
error. Valid expressions are not affected.

(cherry picked from commit 8ffc232bbf)
This commit is contained in:
jhjh1214
2026-10-24 13:54:54 -07:00
committed by Matt Berry
parent a02ad167dc
commit 4e4df4a696
3 changed files with 102 additions and 2 deletions
@@ -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', () => {
@@ -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();
}
}
@@ -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);
}
/**