Skip to content

Commit

Permalink
fix parsing of nested template strings. Closes #1204
Browse files Browse the repository at this point in the history
  • Loading branch information
fabiosantoscode committed Jun 4, 2022
1 parent 1707753 commit f745ac7
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 8 deletions.
9 changes: 9 additions & 0 deletions lib/ast.js
Expand Up @@ -93,6 +93,7 @@ const set_tok_flag = (tok, flag, truth) => {
const TOK_FLAG_NLB = 0b0001;
const TOK_FLAG_QUOTE_SINGLE = 0b0010;
const TOK_FLAG_QUOTE_EXISTS = 0b0100;
const TOK_FLAG_TEMPLATE_END = 0b1000;

class AST_Token {
constructor(type, value, line, col, pos, nlb, comments_before, comments_after, file) {
Expand Down Expand Up @@ -128,6 +129,14 @@ class AST_Token {
set_tok_flag(this, TOK_FLAG_QUOTE_SINGLE, quote_type === "'");
set_tok_flag(this, TOK_FLAG_QUOTE_EXISTS, !!quote_type);
}

get template_end() {
return has_tok_flag(this, TOK_FLAG_TEMPLATE_END);
}

set template_end(new_template_end) {
set_tok_flag(this, TOK_FLAG_TEMPLATE_END, new_template_end);
}
}

var AST_Node = DEFNODE("Node", "start end", function AST_Node(props) {
Expand Down
17 changes: 9 additions & 8 deletions lib/parse.js
Expand Up @@ -162,7 +162,7 @@ import {
} from "./ast.js";

var LATEST_RAW = ""; // Only used for numbers and template strings
var LATEST_TEMPLATE_END = true;
var TEMPLATE_RAWS = new Map(); // Raw template strings

var KEYWORDS = "break case catch class const continue debugger default delete do else export extends finally for function if in instanceof let new return switch throw try typeof var void while with";
var KEYWORDS_ATOM = "false null true";
Expand Down Expand Up @@ -693,8 +693,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
next(true, true);
S.brace_counter++;
tok = token(begin ? "template_head" : "template_substitution", content);
LATEST_RAW = raw;
LATEST_TEMPLATE_END = false;
TEMPLATE_RAWS.set(tok, raw);
tok.template_end = false;
return tok;
}

Expand All @@ -710,8 +710,8 @@ function tokenizer($TEXT, filename, html5_comments, shebang) {
}
S.template_braces.pop();
tok = token(begin ? "template_head" : "template_substitution", content);
LATEST_RAW = raw;
LATEST_TEMPLATE_END = true;
TEMPLATE_RAWS.set(tok, raw);
tok.template_end = true;
return tok;
});

Expand Down Expand Up @@ -2367,19 +2367,19 @@ function parse($TEXT, options) {

segments.push(new AST_TemplateSegment({
start: S.token,
raw: LATEST_RAW,
raw: TEMPLATE_RAWS.get(S.token),
value: S.token.value,
end: S.token
}));

while (!LATEST_TEMPLATE_END) {
while (!S.token.template_end) {
next();
handle_regexp();
segments.push(expression(true));

segments.push(new AST_TemplateSegment({
start: S.token,
raw: LATEST_RAW,
raw: TEMPLATE_RAWS.get(S.token),
value: S.token.value,
end: S.token
}));
Expand Down Expand Up @@ -3342,6 +3342,7 @@ function parse($TEXT, options) {
} else {
toplevel = new AST_Toplevel({ start: start, body: body, end: end });
}
TEMPLATE_RAWS = new Map();
return toplevel;
})();

Expand Down
11 changes: 11 additions & 0 deletions test/compress/template-string.js
Expand Up @@ -947,3 +947,14 @@ template_string_new_parens: {
}
expect_exact: "(new Thing)``;"
}

template_string_nested: {
input: {
console.log(`${`${2,0}`} ${1}`)
console.log(`${String.raw`${2,0}\n`} ${1}`)
}
expect_stdout: [
"0 1",
"0\\n 1",
]
}

0 comments on commit f745ac7

Please sign in to comment.