Skip to content

Commit 4e4df4a

Browse files
jhjh1214mattrbeck
authored andcommitted
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 8ffc232)
1 parent a02ad16 commit 4e4df4a

3 files changed

Lines changed: 102 additions & 2 deletions

File tree

‎packages/compiler-cli/test/ngtsc/ngtsc_spec.ts‎

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -10690,6 +10690,27 @@ runInEachFileSystem((os: string) => {
1069010690
'Parser Error: Bindings cannot contain assignments at column 5 in [x = 2]',
1069110691
);
1069210692
});
10693+
10694+
it('should report a parse error for unexpected tokens in a template literal interpolation', () => {
10695+
env.write(
10696+
'test.ts',
10697+
`
10698+
import {Component} from '@angular/core';
10699+
@Component({
10700+
template: '{{ \`Hello, $\{first last}!\` }}',
10701+
selector: 'test-cmp',
10702+
})
10703+
export class TestCmp {
10704+
first = 'Jane';
10705+
last = 'Doe';
10706+
}
10707+
`,
10708+
);
10709+
const diags = env.driveDiagnostics();
10710+
10711+
expect(diags.length).toBe(1);
10712+
expect(diags[0].messageText).toContain('Parser Error: Missing expected }');
10713+
});
1069310714
});
1069410715

1069510716
describe('shadow DOM selector diagnostics', () => {

‎packages/compiler/src/expression_parser/parser.ts‎

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1648,8 +1648,26 @@ class _ParseAST {
16481648
this.error('Template literal interpolation cannot be empty');
16491649
}
16501650
expressions.push(expression);
1651+
// Expect the closing brace before decrementing `rbracesExpected` so that, if the brace is
1652+
// missing, error recovery stops at this interpolation's brace instead of skipping the rest
1653+
// of the literal.
1654+
this.expectCharacter(chars.$RBRACE);
16511655
this.rbracesExpected--;
1656+
} else if (token.isError()) {
1657+
// The lexer produces an error token if the template literal isn't terminated.
1658+
// Report it directly and advance. `this.error` would skip over the same token again and
1659+
// report it twice.
1660+
this.errors.push(
1661+
getParseError(
1662+
token.toString()!,
1663+
this.input,
1664+
this.getErrorLocationText(this.index),
1665+
this.parseSourceSpan,
1666+
),
1667+
);
1668+
this.advance();
16521669
} else {
1670+
// Skip over any leftover tokens of an interpolation that was already reported as invalid.
16531671
this.advance();
16541672
}
16551673
}

‎packages/compiler/test/expression_parser/parser_spec.ts‎

Lines changed: 63 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ import {
2121
ParseSpan,
2222
PropertyRead,
2323
TemplateBinding,
24+
TemplateLiteral,
2425
VariableBinding,
2526
} from '../../src/expression_parser/ast';
2627
import {Lexer} from '../../src/expression_parser/lexer';
@@ -535,6 +536,66 @@ describe('parser', () => {
535536
expectBindingError('`hello ${}`', 'Template literal interpolation cannot be empty');
536537
});
537538

539+
it('should not report errors for valid interpolations', () => {
540+
for (const text of [
541+
'`${a}`',
542+
'`${a + b}`',
543+
'`${foo.bar}`',
544+
'`${condition ? a : b}`',
545+
'`${a?.b}`',
546+
'`${fn(a, [b, c])}`',
547+
'`${{a: 1}.a}`',
548+
'`${() => a}`',
549+
'`${a | pipe: b}`',
550+
'`before ${a} and ${b} after`',
551+
'`outer ${`inner ${a}`}`',
552+
'{"key": `${a}`}',
553+
'tag`${a}`',
554+
]) {
555+
expect(parseBinding(text).errors).withContext(text).toEqual([]);
556+
}
557+
});
558+
559+
it('should report an error if an interpolation has unexpected tokens after the expression', () => {
560+
expectBindingError('`hello ${a b} world`', 'Missing expected }');
561+
// Operators that are not supported in Angular expressions.
562+
expectBindingError('`hello ${a & b} world`', 'Missing expected }');
563+
expectBindingError('`hello ${a, b} world`', 'Missing expected }');
564+
// Interpolations that are nested inside of other constructs that expect a closing brace.
565+
expectBindingError('`hello ${`nested ${a b}`} world`', 'Missing expected }');
566+
expectBindingError('{"a": `hello ${b c}`}', 'Missing expected }');
567+
expectBindingError('tag`hello ${a b} world`', 'Missing expected }');
568+
expectBindingError('`hello ${a | pipe b} world`', 'Missing expected }');
569+
expectActionError('`hello ${a; b} world`', 'Missing expected }');
570+
});
571+
572+
it('should report an error if an interpolation is not closed', () => {
573+
expectBindingError('`hello ${a', 'Missing expected } at the end of the expression');
574+
expectBindingError('`hello ${a ', 'Missing expected } at the end of the expression');
575+
});
576+
577+
it('should report an error if a template literal with an interpolation is not closed', () => {
578+
expectBindingError('`hello ${a}', 'Unterminated template literal', 1);
579+
expectBindingError('`hello ${a} world', 'Unterminated template literal', 1);
580+
expectBindingError('`hello ${a} and ${b} world', 'Unterminated template literal', 1);
581+
});
582+
583+
it('should retain all the elements and expressions of a malformed template literal', () => {
584+
for (const text of [
585+
'`before ${a b} after`',
586+
'`before ${a b} ${c d} after`',
587+
'`before ${a & b} after`',
588+
'`before ${a; b} after`',
589+
]) {
590+
const result = validate(parseAction(text));
591+
const literal = result.ast as TemplateLiteral;
592+
expect(result.errors.length).withContext(text).toBeGreaterThan(0);
593+
expect(literal.elements.length)
594+
.withContext(text)
595+
.toBe(literal.expressions.length + 1);
596+
}
597+
});
598+
538599
it('should parse tagged template literals with no interpolations', () => {
539600
checkBinding('tag`hello!`');
540601
checkBinding('tags.first`hello!`');
@@ -1848,8 +1909,8 @@ function expectActionError(text: string, message: string, errorCount?: number) {
18481909
expectError(validate(parseAction(text)), message, errorCount);
18491910
}
18501911

1851-
function expectBindingError(text: string, message: string) {
1852-
expectError(validate(parseBinding(text)), message);
1912+
function expectBindingError(text: string, message: string, errorCount?: number) {
1913+
expectError(validate(parseBinding(text)), message, errorCount);
18531914
}
18541915

18551916
/**

0 commit comments

Comments
 (0)