Skip to content

Commit 7794201

Browse files
authored
fix: handle tokens for invalid template element (#14055)
1 parent f32e3dd commit 7794201

File tree

10 files changed

+1162
-6
lines changed

10 files changed

+1162
-6
lines changed

packages/babel-parser/src/parser/statement.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,7 +65,7 @@ const keywordRelationalOperator = /in(?:stanceof)?/y;
6565
* @param {*} tokens
6666
* @returns
6767
*/
68-
function babel7CompatTokens(tokens) {
68+
function babel7CompatTokens(tokens, input) {
6969
for (let i = 0; i < tokens.length; i++) {
7070
const token = tokens[i];
7171
const { type } = token;
@@ -106,7 +106,7 @@ function babel7CompatTokens(tokens) {
106106
const backquoteEnd = start + 1;
107107
const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
108108
let startToken;
109-
if (value.charCodeAt(0) === charCodes.graveAccent) {
109+
if (input.charCodeAt(start) === charCodes.graveAccent) {
110110
// $FlowIgnore: hacky way to create token
111111
startToken = new Token({
112112
type: getExportedToken(tt.backQuote),
@@ -135,7 +135,7 @@ function babel7CompatTokens(tokens) {
135135
// ends with '`'
136136
templateElementEnd = end - 1;
137137
templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
138-
templateValue = value.slice(1, -1);
138+
templateValue = value === null ? null : value.slice(1, -1);
139139
// $FlowIgnore: hacky way to create token
140140
endToken = new Token({
141141
type: getExportedToken(tt.backQuote),
@@ -149,7 +149,7 @@ function babel7CompatTokens(tokens) {
149149
// ends with `${`
150150
templateElementEnd = end - 2;
151151
templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
152-
templateValue = value.slice(1, -2);
152+
templateValue = value === null ? null : value.slice(1, -2);
153153
// $FlowIgnore: hacky way to create token
154154
endToken = new Token({
155155
type: getExportedToken(tt.dollarBraceL),
@@ -197,7 +197,9 @@ export default class StatementParser extends ExpressionParser {
197197
file.program = this.parseProgram(program);
198198
file.comments = this.state.comments;
199199

200-
if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
200+
if (this.options.tokens) {
201+
file.tokens = babel7CompatTokens(this.tokens, this.input);
202+
}
201203

202204
return this.finishNode(file, "File");
203205
}

packages/babel-parser/src/tokenizer/index.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1382,7 +1382,7 @@ export default class Tokenizer extends ParserErrors {
13821382
// Reads template string tokens.
13831383
readTemplateToken(): void {
13841384
let out = "",
1385-
chunkStart = this.state.pos, // eat '`' or `}`
1385+
chunkStart = this.state.pos,
13861386
containsInvalid = false;
13871387
++this.state.pos; // eat '`' or `}`
13881388
for (;;) {
Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
`\1`;
2+
`\1${x}\2${y}\3`;

0 commit comments

Comments
 (0)