forked from lightscript/lightscript-eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Fix converting template types to handle nested templates (babel#610)
Fixes babel#603 (and the fixture from babel#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)
- Loading branch information
1 parent
74a3207
commit 873f02f
Showing
6 changed files
with
95 additions
and
95 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
*.json |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,99 +1,92 @@ | ||
"use strict"; | ||
|
||
module.exports = function(tokens, tt) { | ||
var startingToken = 0; | ||
var currentToken = 0; | ||
var numBraces = 0; // track use of {} | ||
var numBackQuotes = 0; // track number of nested templates | ||
let curlyBrace = null; | ||
let templateTokens = []; | ||
const result = []; | ||
|
||
function isBackQuote(token) { | ||
return tokens[token].type === tt.backQuote; | ||
} | ||
|
||
function isTemplateStarter(token) { | ||
return ( | ||
isBackQuote(token) || | ||
// only can be a template starter when in a template already | ||
(tokens[token].type === tt.braceR && numBackQuotes > 0) | ||
); | ||
} | ||
|
||
function isTemplateEnder(token) { | ||
return isBackQuote(token) || tokens[token].type === tt.dollarBraceL; | ||
} | ||
function addTemplateType() { | ||
const start = templateTokens[0]; | ||
const end = templateTokens[templateTokens.length - 1]; | ||
|
||
// 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; | ||
const value = templateTokens.reduce((result, token) => { | ||
if (token.value) { | ||
result += token.value; | ||
} else if (token.type !== tt.template) { | ||
result += token.type.label; | ||
} | ||
start++; | ||
} | ||
return value; | ||
} | ||
|
||
// create Template token | ||
function replaceWithTemplateType(start, end) { | ||
var templateToken = { | ||
return result; | ||
}, ""); | ||
|
||
result.push({ | ||
type: "Template", | ||
value: createTemplateValue(start, end), | ||
start: tokens[start].start, | ||
end: tokens[end].end, | ||
value: value, | ||
start: start.start, | ||
end: end.end, | ||
loc: { | ||
start: tokens[start].loc.start, | ||
end: tokens[end].loc.end, | ||
start: start.loc.start, | ||
end: end.loc.end, | ||
}, | ||
}; | ||
}); | ||
|
||
// put new token in place of old tokens | ||
tokens.splice(start, end - start + 1, templateToken); | ||
templateTokens = []; | ||
} | ||
|
||
function trackNumBraces(token) { | ||
if (tokens[token].type === tt.braceL) { | ||
numBraces++; | ||
} else if (tokens[token].type === tt.braceR) { | ||
numBraces--; | ||
} | ||
} | ||
tokens.forEach(token => { | ||
switch (token.type) { | ||
case tt.backQuote: | ||
if (curlyBrace) { | ||
result.push(curlyBrace); | ||
curlyBrace = null; | ||
} | ||
|
||
while (startingToken < tokens.length) { | ||
// template start: check if ` or } | ||
if (isTemplateStarter(startingToken) && numBraces === 0) { | ||
if (isBackQuote(startingToken)) { | ||
numBackQuotes++; | ||
} | ||
templateTokens.push(token); | ||
|
||
currentToken = startingToken + 1; | ||
if (templateTokens.length > 1) { | ||
addTemplateType(); | ||
} | ||
|
||
// check if token after template start is "template" | ||
if ( | ||
currentToken >= tokens.length - 1 || | ||
tokens[currentToken].type !== tt.template | ||
) { | ||
break; | ||
} | ||
|
||
// template end: find ` or ${ | ||
while (!isTemplateEnder(currentToken)) { | ||
if (currentToken >= tokens.length - 1) { | ||
break; | ||
case tt.dollarBraceL: | ||
templateTokens.push(token); | ||
addTemplateType(); | ||
break; | ||
|
||
case tt.braceR: | ||
if (curlyBrace) { | ||
result.push(curlyBrace); | ||
} | ||
currentToken++; | ||
} | ||
|
||
if (isBackQuote(currentToken)) { | ||
numBackQuotes--; | ||
} | ||
// template start and end found: create new token | ||
replaceWithTemplateType(startingToken, currentToken); | ||
} else if (numBackQuotes > 0) { | ||
trackNumBraces(startingToken); | ||
curlyBrace = token; | ||
break; | ||
|
||
case tt.template: | ||
if (curlyBrace) { | ||
templateTokens.push(curlyBrace); | ||
curlyBrace = null; | ||
} | ||
|
||
templateTokens.push(token); | ||
break; | ||
|
||
case tt.eof: | ||
if (curlyBrace) { | ||
result.push(curlyBrace); | ||
} | ||
|
||
break; | ||
|
||
default: | ||
if (curlyBrace) { | ||
result.push(curlyBrace); | ||
curlyBrace = null; | ||
} | ||
|
||
result.push(token); | ||
} | ||
startingToken++; | ||
} | ||
}); | ||
|
||
return result; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters