Skip to content

Commit

Permalink
Fix template literals token parsing by account for template context
Browse files Browse the repository at this point in the history
Fixes #1002
Closes gh-1104
  • Loading branch information
mikesherov committed Mar 17, 2015
1 parent 50e6d32 commit 5a99bb0
Show file tree
Hide file tree
Showing 2 changed files with 458 additions and 52 deletions.
34 changes: 28 additions & 6 deletions esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -738,6 +738,14 @@
extra.openCurlyToken = extra.tokens.length;
}
}

// 123 is {, 125 is }
if (code === 123) {
state.curlyStack.push('{');
} else if (code === 125) {
state.curlyStack.pop();
}

return {
type: Token.Punctuator,
value: String.fromCharCode(code),
Expand Down Expand Up @@ -1203,11 +1211,12 @@
}

function scanTemplate() {
var cooked = '', ch, start, terminated, tail, restore, unescaped, code, octal;
var cooked = '', ch, start, terminated, head, tail, restore, unescaped, code, octal;

terminated = false;
tail = false;
start = index;
head = (source[index] === '`');

++index;

Expand Down Expand Up @@ -1314,12 +1323,21 @@
throwError({}, Messages.UnexpectedToken, 'ILLEGAL');
}

if (!tail) {
state.curlyStack.push('template');
}

if (!head) {
state.curlyStack.pop();
}

return {
type: Token.Template,
value: {
cooked: cooked,
raw: source.slice(start + 1, index - ((tail) ? 1 : 2))
},
head: head,
tail: tail,
octal: octal,
lineNumber: lineNumber,
Expand Down Expand Up @@ -1608,7 +1626,9 @@
return scanStringLiteral();
}

if (ch === 96) {
// Template literals start with backtick (#96) for template head
// or close curly (#125) for template middle or template tail.
if (ch === 96 || (ch === 125 && state.curlyStack[state.curlyStack.length - 1] === 'template')) {
return scanTemplate();
}
if (isIdentifierStart(ch)) {
Expand Down Expand Up @@ -2958,7 +2978,7 @@

expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();

while (match('.') || match('[') || match('(') || lookahead.type === Token.Template) {
while (match('.') || match('[') || match('(') || (lookahead.type === Token.Template && lookahead.head)) {
if (match('(')) {
args = parseArguments();
expr = markerApply(marker, delegate.createCallExpression(expr, args));
Expand All @@ -2979,7 +2999,7 @@

expr = matchKeyword('new') ? parseNewExpression() : parsePrimaryExpression();

while (match('.') || match('[') || lookahead.type === Token.Template) {
while (match('.') || match('[') || (lookahead.type === Token.Template && lookahead.head)) {
if (match('[')) {
expr = markerApply(marker, delegate.createMemberExpression('[', expr, parseComputedMember()));
} else if (match('.')) {
Expand Down Expand Up @@ -5167,7 +5187,8 @@
inFunctionBody: false,
inIteration: false,
inSwitch: false,
lastCommentStart: -1
lastCommentStart: -1,
curlyStack: []
};

extra = {};
Expand Down Expand Up @@ -5259,7 +5280,8 @@
inIteration: false,
inSwitch: false,
lastCommentStart: -1,
yieldAllowed: false
yieldAllowed: false,
curlyStack: []
};

extra = {};
Expand Down
Loading

0 comments on commit 5a99bb0

Please sign in to comment.