Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: handle tokens for invalid template element #14055

Merged
merged 1 commit into from Dec 14, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: we can use value?.slice

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think so. We need returning null when value is null.

// $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`;