Fix converting template types to handle nested templates (babel/babel-eslint#610)

Fixes https://github.com/babel/babel-eslint/issues/603 (and the fixture from https://github.com/babel/babel-eslint/issues/609 works).

Reworks our code that converts the format of Babylon template tokens to be a bit more robust, especially with things like nested templates with arrows.

(Adapted the logic from https://github.com/eslint/espree/blob/master/lib/token-translator.js)
This commit is contained in:
Brian Ng 2018-06-18 16:46:31 -05:00
parent 99968db2b1
commit 077bea0a45
6 changed files with 100 additions and 100 deletions

View File

@ -0,0 +1 @@
*.json

View File

@ -1,99 +1,92 @@
"use strict"; "use strict";
module.exports = function(tokens, tt) { module.exports = function(tokens, tt) {
var startingToken = 0; let curlyBrace = null;
var currentToken = 0; let templateTokens = [];
var numBraces = 0; // track use of {} const result = [];
var numBackQuotes = 0; // track number of nested templates
function isBackQuote(token) { function addTemplateType() {
return tokens[token].type === tt.backQuote; const start = templateTokens[0];
const end = templateTokens[templateTokens.length - 1];
const value = templateTokens.reduce((result, token) => {
if (token.value) {
result += token.value;
} else if (token.type !== tt.template) {
result += token.type.label;
} }
function isTemplateStarter(token) { return result;
return ( }, "");
isBackQuote(token) ||
// only can be a template starter when in a template already
(tokens[token].type === tt.braceR && numBackQuotes > 0)
);
}
function isTemplateEnder(token) { result.push({
return isBackQuote(token) || tokens[token].type === tt.dollarBraceL;
}
// append the values between start and end
function createTemplateValue(start, end) {
var value = "";
while (start <= end) {
if (tokens[start].value) {
value += tokens[start].value;
} else if (tokens[start].type !== tt.template) {
value += tokens[start].type.label;
}
start++;
}
return value;
}
// create Template token
function replaceWithTemplateType(start, end) {
var templateToken = {
type: "Template", type: "Template",
value: createTemplateValue(start, end), value: value,
start: tokens[start].start, start: start.start,
end: tokens[end].end, end: end.end,
loc: { loc: {
start: tokens[start].loc.start, start: start.loc.start,
end: tokens[end].loc.end, end: end.loc.end,
}, },
}; });
// put new token in place of old tokens templateTokens = [];
tokens.splice(start, end - start + 1, templateToken);
} }
function trackNumBraces(token) { tokens.forEach(token => {
if (tokens[token].type === tt.braceL) { switch (token.type) {
numBraces++; case tt.backQuote:
} else if (tokens[token].type === tt.braceR) { if (curlyBrace) {
numBraces--; result.push(curlyBrace);
} curlyBrace = null;
} }
while (startingToken < tokens.length) { templateTokens.push(token);
// template start: check if ` or }
if (isTemplateStarter(startingToken) && numBraces === 0) { if (templateTokens.length > 1) {
if (isBackQuote(startingToken)) { addTemplateType();
numBackQuotes++;
} }
currentToken = startingToken + 1;
// check if token after template start is "template"
if (
currentToken >= tokens.length - 1 ||
tokens[currentToken].type !== tt.template
) {
break; break;
}
// template end: find ` or ${ case tt.dollarBraceL:
while (!isTemplateEnder(currentToken)) { templateTokens.push(token);
if (currentToken >= tokens.length - 1) { addTemplateType();
break; break;
}
currentToken++; case tt.braceR:
if (curlyBrace) {
result.push(curlyBrace);
} }
if (isBackQuote(currentToken)) { curlyBrace = token;
numBackQuotes--; break;
case tt.template:
if (curlyBrace) {
templateTokens.push(curlyBrace);
curlyBrace = null;
} }
// template start and end found: create new token
replaceWithTemplateType(startingToken, currentToken); templateTokens.push(token);
} else if (numBackQuotes > 0) { break;
trackNumBraces(startingToken);
case tt.eof:
if (curlyBrace) {
result.push(curlyBrace);
} }
startingToken++;
break;
default:
if (curlyBrace) {
result.push(curlyBrace);
curlyBrace = null;
} }
result.push(token);
}
});
return result;
}; };

View File

@ -6,11 +6,6 @@ var toTokens = require("./toTokens");
var toAST = require("./toAST"); var toAST = require("./toAST");
module.exports = function(ast, traverse, tt, code) { module.exports = function(ast, traverse, tt, code) {
// remove EOF token, eslint doesn't use this for anything and it interferes
// with some rules see https://github.com/babel/babel-eslint/issues/2
// todo: find a more elegant way to do this
ast.tokens.pop();
// convert tokens // convert tokens
ast.tokens = toTokens(ast.tokens, tt, code); ast.tokens = toTokens(ast.tokens, tt, code);

View File

@ -4,16 +4,7 @@ var convertTemplateType = require("./convertTemplateType");
var toToken = require("./toToken"); var toToken = require("./toToken");
module.exports = function(tokens, tt, code) { module.exports = function(tokens, tt, code) {
// transform tokens to type "Template" return convertTemplateType(tokens, tt)
convertTemplateType(tokens, tt); .filter(t => t.type !== "CommentLine" && t.type !== "CommentBlock")
.map(t => toToken(t, tt, code));
var transformedTokens = [];
for (var i = 0; i < tokens.length; i++) {
var token = tokens[i];
if (token.type !== "CommentLine" && token.type !== "CommentBlock") {
transformedTokens.push(toToken(token, tt, code));
}
}
return transformedTokens;
}; };

View File

@ -158,6 +158,14 @@ describe("babylon-to-espree", () => {
}; };
`); `);
}); });
it("template with arrow returning template #603", () => {
parseAndAssertSame(`
var a = \`\${() => {
\`\${''}\`
}}\`;
`);
});
}); });
it("simple expression", () => { it("simple expression", () => {

View File

@ -1112,6 +1112,18 @@ describe("verify", () => {
); );
}); });
it("template with arrow returning template #603", () => {
verifyAndAssertMessages(
`
var a = \`\${() => {
\`\${''}\`
}}\`;
`,
{ indent: 1 },
[]
);
});
describe("decorators #72", () => { describe("decorators #72", () => {
it("class declaration", () => { it("class declaration", () => {
verifyAndAssertMessages( verifyAndAssertMessages(