Skip to content
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 24 additions & 9 deletions src/main/java/org/codelibs/sai/internal/parser/Parser.java
Original file line number Diff line number Diff line change
Expand Up @@ -204,6 +204,9 @@ public class Parser extends AbstractParser implements Loggable {

private RecompilableScriptFunctionData reparsedFunction;

/** Template substitutions currently being parsed, @see #skipFunctionBody. */
private int templateSubstitutions;

/**
* Constructor
*
Expand Down Expand Up @@ -3133,19 +3136,25 @@ private Expression templateLiteral() {
final long templateToken = token;
Expression concat = getLiteral();

while (true) {
concat = new BinaryNode(Token.recast(templateToken, TokenType.ADD), concat, expression());
templateSubstitutions++;

if (type != TEMPLATE_MIDDLE && type != TEMPLATE_TAIL) {
throw error(AbstractParser.message("expected.literal", "template"), token);
}
try {
while (true) {
concat = new BinaryNode(Token.recast(templateToken, TokenType.ADD), concat, expression());

if (type != TEMPLATE_MIDDLE && type != TEMPLATE_TAIL) {
throw error(AbstractParser.message("expected.literal", "template"), token);
}

final boolean last = type == TEMPLATE_TAIL;
concat = new BinaryNode(Token.recast(templateToken, TokenType.ADD), concat, getLiteral());
final boolean last = type == TEMPLATE_TAIL;
concat = new BinaryNode(Token.recast(templateToken, TokenType.ADD), concat, getLiteral());

if (last) {
return concat;
if (last) {
return concat;
}
}
} finally {
templateSubstitutions--;
}
}

Expand Down Expand Up @@ -3829,6 +3838,12 @@ private boolean skipFunctionBody(final FunctionNode functionNode) {
// Not reparsing, so don't skip any function body.
return false;
}
if (templateSubstitutions > 0) {
// Inside a template substitution. Skipping restarts the lexer at the closing
// brace of this body, and a lexer started there knows nothing of the template
// around it. Declining is a supported outcome - the caller drops the body.
return false;
}
// Skip to the RBRACE of this function, and continue parsing from there.
final RecompilableScriptFunctionData data = reparsedFunction.getScriptFunctionData(functionNode.getId());
if (data == null) {
Expand Down
33 changes: 33 additions & 0 deletions test/script/basic/es6/template-literals.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,3 +80,36 @@ print(`${ "}" }`);
var escaped = `a\
b`;
print(escaped.length);

// A function body inside a substitution, in a function that is only compiled when
// it is first called. The body cannot be skipped there: a lexer restarted at its
// closing brace would know nothing of the template around it.
function bodyInSubstitution() {
return `${ (function () { return 7; })() }`;
}
print(bodyInSubstitution());

function methodInSubstitution() {
return `${ ({ m() { return 8; } }).m() }`;
}
print(methodInSubstitution());

function arrowInSubstitution() {
return `${ (() => { return 9; })() }`;
}
print(arrowInSubstitution());

function twoBodiesInSubstitution() {
return `${ (function () { return 1; })() + (function () { return 2; })() }`;
}
print(twoBodiesInSubstitution());

function nestedTemplateWithBody() {
return `a${ `b${ (function () { return 1; })() }c` }d`;
}
print(nestedTemplateWithBody());

function bracesInsideTheBody() {
return `${ (function () { var o = { a: { b: 6 } }; return o.a.b; })() }`;
}
print(bracesInsideTheBody());
6 changes: 6 additions & 0 deletions test/script/basic/es6/template-literals.js.EXPECTED
Original file line number Diff line number Diff line change
Expand Up @@ -32,3 +32,9 @@ x1y
}
}
2
7
8
9
3
ab1cd
6