Skip to content

Commit c873440

Browse files
petebacondarwindylhunn
authored andcommitted
fix(compiler): do not allow unterminated interpolation to leak into later tokens (angular#42605)
When consuming a text token, the lexer tracks whether it is reading characters from inside an interpolation so that it can identify invalid ICU expressions. Inside an interpolation there will be no ICU expression so it is safe to have unmatched `{` characters, but outside an interpolation this is an error. Previously, if an interpolation was started, by an opening marker (e.g. `{{`) in a text token but the text came to an end before the closing marker (e.g. `}}`) then the lexer was not clearing its internal state that tracked that it was inside an interpolation. When the next text token was being consumed, the lexer, incorrectly thought it was already within an interpolation. This resulted in invalid ICU expression errors not being reported. For example, in the following snippet, the first text block has a prematurely ended interpolation, and the second text block contains an invalid `{` character. ``` <div>{{</div> <div>{</div> ``` Previously, the lexer would not have identified this as an error. Now there will be an EOF error that looks like: ``` TS-995002: Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.) ``` PR Close angular#42605
1 parent 8a67770 commit c873440

File tree

2 files changed

+16
-0
lines changed

2 files changed

+16
-0
lines changed

packages/compiler/src/ml_parser/lexer.ts

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -713,6 +713,10 @@ class _Tokenizer {
713713
}
714714
} while (!this._isTextEnd());
715715

716+
// It is possible that an interpolation was started but not ended inside this text token.
717+
// Make sure that we reset the state of the lexer correctly.
718+
this._inInterpolation = false;
719+
716720
this._endToken([this._processCarriageReturns(parts.join(''))]);
717721
}
718722

packages/compiler/test/ml_parser/lexer_spec.ts

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1208,6 +1208,18 @@ import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '../../src/parse_u
12081208
]]);
12091209
});
12101210

1211+
it('should report unescaped "{" as an error, even after a prematurely terminated interpolation',
1212+
() => {
1213+
expect(tokenizeAndHumanizeErrors(
1214+
`<code>{{b}<!---->}</code><pre>import {a} from 'a';</pre>`,
1215+
{tokenizeExpansionForms: true}))
1216+
.toEqual([[
1217+
lex.TokenType.RAW_TEXT,
1218+
`Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`,
1219+
'0:56',
1220+
]]);
1221+
});
1222+
12111223
it('should include 2 lines of context in message', () => {
12121224
const src = '111\n222\n333\nE\n444\n555\n666\n';
12131225
const file = new ParseSourceFile(src, 'file://');

0 commit comments

Comments
 (0)