fix(compiler): do not allow unterminated interpolation to leak into later tokens (#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 #42605
This commit is contained in:
Pete Bacon Darwin 2021-06-19 11:25:53 +01:00 committed by Dylan Hunn
parent 8a67770687
commit c873440ad2
2 changed files with 16 additions and 0 deletions

View File

@ -713,6 +713,10 @@ class _Tokenizer {
}
} while (!this._isTextEnd());
// It is possible that an interpolation was started but not ended inside this text token.
// Make sure that we reset the state of the lexer correctly.
this._inInterpolation = false;
this._endToken([this._processCarriageReturns(parts.join(''))]);
}

View File

@ -1208,6 +1208,18 @@ import {ParseLocation, ParseSourceFile, ParseSourceSpan} from '../../src/parse_u
]]);
});
it('should report unescaped "{" as an error, even after a prematurely terminated interpolation',
() => {
expect(tokenizeAndHumanizeErrors(
`<code>{{b}<!---->}</code><pre>import {a} from 'a';</pre>`,
{tokenizeExpansionForms: true}))
.toEqual([[
lex.TokenType.RAW_TEXT,
`Unexpected character "EOF" (Do you have an unescaped "{" in your template? Use "{{ '{' }}") to escape it.)`,
'0:56',
]]);
});
it('should include 2 lines of context in message', () => {
const src = '111\n222\n333\nE\n444\n555\n666\n';
const file = new ParseSourceFile(src, 'file://');