Skip to content

Commit

Permalink
Fix: } token followed by template had been lost (fixes #293) (#294)
Browse files Browse the repository at this point in the history
  • Loading branch information
mysticatea authored and ilyavolodin committed Sep 26, 2016
1 parent 9810bab commit 80abdce
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 1 deletion.
8 changes: 7 additions & 1 deletion lib/token-translator.js
Expand Up @@ -201,6 +201,13 @@ TokenTranslator.prototype = {
}

if (token.type === tt.backQuote) {

// if there's already a curly, it's not part of the template
if (this._curlyBrace) {
tokens.push(this.translate(this._curlyBrace, extra));
this._curlyBrace = null;
}

templateTokens.push(token);

// it's the end
Expand All @@ -217,7 +224,6 @@ TokenTranslator.prototype = {

// if there's already a curly, it's not part of the template
if (this._curlyBrace) {

tokens.push(this.translate(this._curlyBrace, extra));
}

Expand Down
33 changes: 33 additions & 0 deletions tests/lib/tokenize.js
Expand Up @@ -149,4 +149,37 @@ describe("tokenize()", function() {
assert.deepEqual(tester.getRaw(tokens), [ { type: "Identifier", value: "a"}]);
});

it("should not remove } token followed by a template literal.", function() {
var tokens = espree.tokenize("const obj = {}\n`template${{}}!`", {ecmaVersion: 6});
assert.deepEqual(
tester.getRaw(tokens),
[
{type: "Keyword", value: "const"},
{type: "Identifier", value: "obj"},
{type: "Punctuator", value: "="},
{type: "Punctuator", value: "{"},
{type: "Punctuator", value: "}"},
{type: "Template", value: "`template${"},
{type: "Punctuator", value: "{"},
{type: "Punctuator", value: "}"},
{type: "Template", value: "}!`"}
]
);

tokens = espree.tokenize("if (a) { b }\n`template`", {ecmaVersion: 6});
assert.deepEqual(
tester.getRaw(tokens),
[
{type: "Keyword", value: "if"},
{type: "Punctuator", value: "("},
{type: "Identifier", value: "a"},
{type: "Punctuator", value: ")"},
{type: "Punctuator", value: "{"},
{type: "Identifier", value: "b"},
{type: "Punctuator", value: "}"},
{type: "Template", value: "`template`"}
]
);
});

});

0 comments on commit 80abdce

Please sign in to comment.