Skip to content

Commit

Permalink
fix: handle tokens for invalid template element (#14055)
Browse files Browse the repository at this point in the history
  • Loading branch information
JLHwung committed Dec 14, 2021
1 parent f32e3dd commit 7794201
Show file tree
Hide file tree
Showing 10 changed files with 1,162 additions and 6 deletions.
12 changes: 7 additions & 5 deletions packages/babel-parser/src/parser/statement.js
Expand Up @@ -65,7 +65,7 @@ const keywordRelationalOperator = /in(?:stanceof)?/y;
* @param {*} tokens
* @returns
*/
function babel7CompatTokens(tokens) {
function babel7CompatTokens(tokens, input) {
for (let i = 0; i < tokens.length; i++) {
const token = tokens[i];
const { type } = token;
Expand Down Expand Up @@ -106,7 +106,7 @@ function babel7CompatTokens(tokens) {
const backquoteEnd = start + 1;
const backquoteEndLoc = createPositionWithColumnOffset(loc.start, 1);
let startToken;
if (value.charCodeAt(0) === charCodes.graveAccent) {
if (input.charCodeAt(start) === charCodes.graveAccent) {
// $FlowIgnore: hacky way to create token
startToken = new Token({
type: getExportedToken(tt.backQuote),
Expand Down Expand Up @@ -135,7 +135,7 @@ function babel7CompatTokens(tokens) {
// ends with '`'
templateElementEnd = end - 1;
templateElementEndLoc = createPositionWithColumnOffset(loc.end, -1);
templateValue = value.slice(1, -1);
templateValue = value === null ? null : value.slice(1, -1);
// $FlowIgnore: hacky way to create token
endToken = new Token({
type: getExportedToken(tt.backQuote),
Expand All @@ -149,7 +149,7 @@ function babel7CompatTokens(tokens) {
// ends with `${`
templateElementEnd = end - 2;
templateElementEndLoc = createPositionWithColumnOffset(loc.end, -2);
templateValue = value.slice(1, -2);
templateValue = value === null ? null : value.slice(1, -2);
// $FlowIgnore: hacky way to create token
endToken = new Token({
type: getExportedToken(tt.dollarBraceL),
Expand Down Expand Up @@ -197,7 +197,9 @@ export default class StatementParser extends ExpressionParser {
file.program = this.parseProgram(program);
file.comments = this.state.comments;

if (this.options.tokens) file.tokens = babel7CompatTokens(this.tokens);
if (this.options.tokens) {
file.tokens = babel7CompatTokens(this.tokens, this.input);
}

return this.finishNode(file, "File");
}
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-parser/src/tokenizer/index.js
Expand Up @@ -1382,7 +1382,7 @@ export default class Tokenizer extends ParserErrors {
// Reads template string tokens.
readTemplateToken(): void {
let out = "",
chunkStart = this.state.pos, // eat '`' or `}`
chunkStart = this.state.pos,
containsInvalid = false;
++this.state.pos; // eat '`' or `}`
for (;;) {
Expand Down
@@ -0,0 +1,2 @@
`\1`;
`\1${x}\2${y}\3`;

0 comments on commit 7794201

Please sign in to comment.