Skip to content
This repository has been archived by the owner on Aug 18, 2021. It is now read-only.

Commit

Permalink
add all espree ecmaFeatures, fixes #31 again
Browse files Browse the repository at this point in the history
  • Loading branch information
hzoo committed May 21, 2015
1 parent aa87a04 commit a98b1ba
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 10 deletions.
17 changes: 13 additions & 4 deletions acorn-to-esprima.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,16 +80,19 @@ function isCompatTag(tagName) {
function convertTemplateType(tokens) {
var startingToken = 0;
var currentToken = 0;
// track use of {}
var numBraces = 0;
var hasTemplateEnded = true;
// track number of nested templates
var numBackQuotes = 0;

function isBackQuote(token) {
return tokens[token].type === tt.backQuote;
}

function isTemplateStarter(token) {
return isBackQuote(token) ||
tokens[token].type === tt.braceR;
// only can be a template starter when in a template already
tokens[token].type === tt.braceR && numBackQuotes > 0;
}

function isTemplateEnder(token) {
Expand Down Expand Up @@ -138,6 +141,10 @@ function convertTemplateType(tokens) {
while (startingToken < tokens.length) {
// template start: check if ` or }
if (isTemplateStarter(startingToken) && numBraces === 0) {
if (isBackQuote(startingToken)) {
numBackQuotes++;
}

currentToken = startingToken + 1;

// check if token after template start is "template"
Expand All @@ -153,10 +160,12 @@ function convertTemplateType(tokens) {
currentToken++;
}

hasTemplateEnded = isBackQuote(currentToken);
if (isBackQuote(currentToken)) {
numBackQuotes--;
}
// template start and end found: create new token
replaceWithTemplateType(startingToken, currentToken);
} else if (!hasTemplateEnded) {
} else if (numBackQuotes > 0) {
trackNumBraces(startingToken);
}
startingToken++;
Expand Down
36 changes: 30 additions & 6 deletions test/babel-eslint.js
Original file line number Diff line number Diff line change
Expand Up @@ -33,16 +33,29 @@ function assertImplementsAST(target, source, path) {

function parseAndAssertSame(code) {
var esAST = espree.parse(code, {
env: {
"es6": true,
"node": true
},
ecmaFeatures: {
arrowFunctions: true,
blockBindings: true,
destructuring: true,
regexYFlag: true,
regexUFlag: true,
templateStrings: true,
modules: true,
binaryLiterals: true,
octalLiterals: true,
unicodeCodePointEscapes: true,
defaultParams: true,
restParams: true,
forOf: true,
objectLiteralComputedProperties: true,
objectLiteralShorthandMethods: true,
objectLiteralShorthandProperties: true,
objectLiteralDuplicateProperties: true,
generators: true,
spread: true,
classes: true,
jsx: true
modules: true,
jsx: true,
globalReturn: true
},
tokens: true,
loc: true,
Expand Down Expand Up @@ -124,6 +137,17 @@ describe("acorn-to-esprima", function () {
"}"
);
});

it("template with destructuring #31", function () {
parseAndAssertSame([
"module.exports = {",
"render() {",
"var {name} = this.props;",
"return Math.max(null, `Name: ${name}, Name: ${name}`);",
"}",
"};"
].join("\n"));
});
});

it("simple expression", function () {
Expand Down
13 changes: 13 additions & 0 deletions test/non-regression.js
Original file line number Diff line number Diff line change
Expand Up @@ -164,6 +164,19 @@ describe("verify", function () {
);
});

it("template with destructuring #31", function () {
verifyAndAssertMessages([
"module.exports = {",
"render() {",
"var {name} = this.props;",
"return Math.max(null, `Name: ${name}, Name: ${name}`);",
"}",
"};"].join("\n"),
{ "comma-spacing": 1 },
[]
);
});

describe("decorators #72", function () {
it("class declaration", function () {
verifyAndAssertMessages(
Expand Down

0 comments on commit a98b1ba

Please sign in to comment.