babel-eslint
Advanced tools
Comparing version 3.1.1 to 3.1.3
@@ -16,3 +16,12 @@ var traverse = require("babel-core").traverse; | ||
type === tt.ellipsis || type === tt.arrow || | ||
type === tt.star || | ||
type === tt.star || type === tt.incDec || | ||
type === tt.colon || type === tt.question || | ||
type === tt.template || type === tt.backQuote || | ||
type === tt.dollarBraceL || type === tt.at || | ||
type === tt.logicalOR || type === tt.logicalAND || | ||
type === tt.bitwiseOR || type === tt.bitwiseXOR || | ||
type === tt.bitwiseAND || type === tt.equality || | ||
type === tt.relational || type === tt.bitShift || | ||
type === tt.plusMin || type === tt.modulo || | ||
type === tt.exponent || type === tt.prefix || | ||
type.isAssign) { | ||
@@ -29,2 +38,6 @@ token.type = "Punctuator"; | ||
token.type = "JSXIdentifier"; | ||
} else if (type.keyword === "null") { | ||
token.type = "Null"; | ||
} else if (type.keyword === "false" || type.keyword === "true") { | ||
token.type = "Boolean"; | ||
} else if (type.keyword) { | ||
@@ -38,2 +51,9 @@ token.type = "Keyword"; | ||
token.value = JSON.stringify(token.value); | ||
} else if (type === tt.regexp) { | ||
token.type = "RegularExpression"; | ||
token.regex = { | ||
pattern: token.value.pattern, | ||
flags: token.value.flags | ||
}; | ||
token.value = String(token.value.value); | ||
} | ||
@@ -49,2 +69,9 @@ | ||
exports.toTokens = function (tokens) { | ||
// transform tokens to type "Template" | ||
convertTemplateType(tokens); | ||
return tokens.map(exports.toToken); | ||
}; | ||
function isCompatTag(tagName) { | ||
@@ -54,2 +81,88 @@ return tagName && /^[a-z]|\-/.test(tagName); | ||
function convertTemplateType(tokens) { | ||
var startingToken = 0; | ||
var currentToken = 0; | ||
var numBraces = 0; | ||
var hasTemplateEnded = true; | ||
function isBackQuote(token) { | ||
return tokens[token].type === tt.backQuote; | ||
} | ||
function isTemplateStarter(token) { | ||
return isBackQuote(token) || | ||
tokens[token].type === tt.braceR; | ||
} | ||
function isTemplateEnder(token) { | ||
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', | ||
value: createTemplateValue(start, end), | ||
range: [tokens[start].start, tokens[end].end], | ||
loc: { | ||
start: tokens[start].loc.start, | ||
end: tokens[end].loc.end | ||
} | ||
} | ||
// put new token in place of old tokens | ||
tokens.splice(start, end - start + 1, templateToken); | ||
} | ||
function trackNumBraces(token) { | ||
if (tokens[token].type === tt.braceL) { | ||
numBraces++; | ||
} else if (tokens[token].type === tt.braceR) { | ||
numBraces--; | ||
} | ||
} | ||
while (startingToken < tokens.length) { | ||
// template start: check if ` or } | ||
if (isTemplateStarter(startingToken) && numBraces === 0) { | ||
currentToken = startingToken + 1; | ||
// 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; | ||
} | ||
currentToken++; | ||
} | ||
hasTemplateEnded = isBackQuote(currentToken); | ||
// template start and end found: create new token | ||
replaceWithTemplateType(startingToken, currentToken); | ||
} else if (!hasTemplateEnded) { | ||
trackNumBraces(startingToken); | ||
} | ||
startingToken++; | ||
} | ||
} | ||
var astTransformVisitor = { | ||
@@ -110,3 +223,22 @@ noScope: true, | ||
} | ||
// template strings | ||
if (t.isTemplateLiteral(node)) { | ||
node.quasis.forEach(function (q) { | ||
q.range[0] -= 1; | ||
if (q.tail) { | ||
q.range[1] += 1; | ||
} else { | ||
q.range[1] += 2; | ||
} | ||
q.loc.start.column -= 1; | ||
if (q.tail) { | ||
q.loc.end.column += 1; | ||
} else { | ||
q.loc.end.column += 2; | ||
} | ||
}); | ||
} | ||
} | ||
}; |
@@ -59,3 +59,3 @@ var acornToEsprima = require("./acorn-to-esprima"); | ||
exports.attachComments = function(ast, comments, tokens) { | ||
exports.attachComments = function (ast, comments, tokens) { | ||
estraverse.attachComments(ast, comments, tokens); | ||
@@ -86,3 +86,3 @@ | ||
var len = comments.length; | ||
for(var i = 0; i < len && comments[i].start < firstTokenStart; i++ ) { | ||
for (var i = 0; i < len && comments[i].start < firstTokenStart; i++) { | ||
node.leadingComments.push(comments[i]); | ||
@@ -143,3 +143,3 @@ } | ||
// convert tokens | ||
ast.tokens = tokens.map(acornToEsprima.toToken); | ||
ast.tokens = acornToEsprima.toTokens(tokens); | ||
@@ -146,0 +146,0 @@ // add comments |
{ | ||
"name": "babel-eslint", | ||
"version": "3.1.1", | ||
"version": "3.1.3", | ||
"description": "", | ||
@@ -5,0 +5,0 @@ "main": "index.js", |
@@ -23,3 +23,3 @@ var babelEslint = require(".."); | ||
var keysTarget = Object.keys(target); | ||
for(var i in keysTarget) { | ||
for (var i in keysTarget) { | ||
var key = keysTarget[i]; | ||
@@ -37,3 +37,9 @@ path.push(key); | ||
var esAST = espree.parse(code, { | ||
env: { | ||
"es6": true, | ||
"node": true | ||
}, | ||
ecmaFeatures: { | ||
blockBindings: true, | ||
templateStrings: true, | ||
modules: true, | ||
@@ -55,5 +61,5 @@ classes: true, | ||
"\nespree:\n" + | ||
util.inspect(esAST, {depth: err.depth}) + | ||
util.inspect(esAST, {depth: err.depth, colors: true}) + | ||
"\nbabel-eslint:\n" + | ||
util.inspect(acornAST, {depth: err.depth}); | ||
util.inspect(acornAST, {depth: err.depth, colors: true}); | ||
throw err; | ||
@@ -64,2 +70,64 @@ } | ||
describe("acorn-to-esprima", function () { | ||
describe("templates", function () { | ||
it("empty template string", function () { | ||
parseAndAssertSame("``"); | ||
}); | ||
it("template string", function () { | ||
parseAndAssertSame("`test`"); | ||
}); | ||
it("template string using $", function () { | ||
parseAndAssertSame("`$`"); | ||
}); | ||
it("template string with expression", function () { | ||
parseAndAssertSame("`${a}`"); | ||
}); | ||
it("template string with multiple expressions", function () { | ||
parseAndAssertSame("`${a}${b}${c}`"); | ||
}); | ||
it("template string with expression and strings", function () { | ||
parseAndAssertSame("`a${a}a`"); | ||
}); | ||
it("template string with binary expression", function () { | ||
parseAndAssertSame("`a${a + b}a`"); | ||
}); | ||
it("tagged template", function () { | ||
parseAndAssertSame("jsx`<Button>Click</Button>`"); | ||
}); | ||
it("tagged template with expression", function () { | ||
parseAndAssertSame("jsx`<Button>Hi ${name}</Button>`"); | ||
}); | ||
it("tagged template with new operator", function () { | ||
parseAndAssertSame("new raw`42`"); | ||
}); | ||
it("template with nested function/object", function () { | ||
parseAndAssertSame("`outer${{x: {y: 10}}}bar${`nested${function(){return 1;}}endnest`}end`"); | ||
}); | ||
it("template with braces inside and outside of template string #96", function () { | ||
parseAndAssertSame("if (a) { var target = `{}a:${webpackPort}{}}}}`; } else { app.use(); }"); | ||
}); | ||
it("template also with braces #96", function () { | ||
parseAndAssertSame( | ||
"export default function f1() {" + | ||
"function f2(foo) {" + | ||
"const bar = 3;" + | ||
"return `${foo} ${bar}`;" + | ||
"}" + | ||
"return f2;" + | ||
"}" | ||
); | ||
}); | ||
}); | ||
it("simple expression", function () { | ||
@@ -163,2 +231,18 @@ parseAndAssertSame("a = 1"); | ||
}); | ||
it("null", function () { | ||
parseAndAssertSame("null"); | ||
}); | ||
it("boolean", function () { | ||
parseAndAssertSame("if (true) {} else if (false) {}"); | ||
}); | ||
it("regexp", function () { | ||
parseAndAssertSame("/affix-top|affix-bottom|affix|[a-z]/"); | ||
}); | ||
it("regexp in a template string", function () { | ||
parseAndAssertSame("`${/\\d/.exec(\"1\")[0]}`"); | ||
}); | ||
}); |
@@ -142,2 +142,10 @@ /*eslint-env mocha*/ | ||
}); | ||
it("template strings #31", function () { | ||
verifyAndAssertMessages( | ||
"console.log(`${a}, b`);", | ||
{ "comma-spacing": 1 }, | ||
[] | ||
); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
24170
668