Skip to content

Commit

Permalink
Merge pull request #253 from eslint/issue246
Browse files Browse the repository at this point in the history
New: Support ecmaVersion 7 (fixes #246)
  • Loading branch information
nzakas committed Feb 25, 2016
2 parents c59de3a + da35d98 commit e770e84
Show file tree
Hide file tree
Showing 15 changed files with 694 additions and 19 deletions.
6 changes: 5 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ var ast = espree.parse(code, {
// create a top-level tokens array containing all tokens
tokens: true,

// specify the language version (3, 5, or 6, default is 5)
// specify the language version (3, 5, 6, or 7, default is 5)
ecmaVersion: 5,

// specify which type of script you're parsing (script or module, default is script)
Expand Down Expand Up @@ -130,6 +130,10 @@ We are building on top of Acorn, however, so that we can contribute back and hel

All of them.

### What ECMAScript 7 features do you support?

There is only one ECMAScript 7 syntax change: the exponentiation operator. Espree supports this.

### How do you determine which experimental features to support?

In general, we do not support experimental JavaScript features. We may make exceptions from time to time depending on the maturity of the features.
8 changes: 5 additions & 3 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -389,7 +389,7 @@ acorn.plugins.espree = function(instance) {
* @throws {SyntaxError} A syntax error.
* @returns {void}
*/
instance.raise = function(pos, message) {
instance.raise = instance.raiseRecoverable = function(pos, message) {
var loc = getLineInfo(this.input, pos);
var err = new SyntaxError(message);
err.index = pos;
Expand Down Expand Up @@ -509,12 +509,13 @@ function tokenize(code, options) {
case 3:
case 5:
case 6:
case 7:
acornOptions.ecmaVersion = options.ecmaVersion;
extra.ecmaVersion = options.ecmaVersion;
break;

default:
throw new Error("ecmaVersion must be 3, 5, or 6.");
throw new Error("ecmaVersion must be 3, 5, 6, or 7.");
}
}

Expand Down Expand Up @@ -645,12 +646,13 @@ function parse(code, options) {
case 3:
case 5:
case 6:
case 7:
acornOptions.ecmaVersion = options.ecmaVersion;
extra.ecmaVersion = options.ecmaVersion;
break;

default:
throw new Error("ecmaVersion must be 3, 5, or 6.");
throw new Error("ecmaVersion must be 3, 5, 6, or 7.");
}
}

Expand Down
4 changes: 4 additions & 0 deletions lib/token-translator.js
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,10 @@ TokenTranslator.prototype = {
token.type = Token.Keyword;
}

if (extra.ecmaVersion > 5 && (token.value === "yield" || token.value === "let")) {
token.type = Token.Keyword;
}

} else if (type === tt.semi || type === tt.comma ||
type === tt.parenL || type === tt.parenR ||
type === tt.braceL || type === tt.braceR ||
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
},
"license": "BSD-2-Clause",
"dependencies": {
"acorn": "^2.7.0",
"acorn": "^3.0.4",
"acorn-jsx": "^2.0.1"
},
"devDependencies": {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -329,4 +329,4 @@ module.exports = {
]
}
]
};
};
222 changes: 222 additions & 0 deletions tests/fixtures/ecma-version/7/exponential-plusplus.result.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
module.exports = {
"type": "Program",
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 9
}
},
"range": [
1,
10
],
"body": [
{
"type": "ExpressionStatement",
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 9
}
},
"range": [
1,
10
],
"expression": {
"type": "BinaryExpression",
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 8
}
},
"range": [
1,
9
],
"left": {
"type": "UpdateExpression",
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 3
}
},
"range": [
1,
4
],
"operator": "++",
"prefix": false,
"argument": {
"type": "Identifier",
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
},
"range": [
1,
2
],
"name": "a"
}
},
"operator": "**",
"right": {
"type": "Literal",
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 8
}
},
"range": [
8,
9
],
"value": 2,
"raw": "2"
}
}
}
],
"sourceType": "script",
"tokens": [
{
"type": "Identifier",
"value": "a",
"loc": {
"start": {
"line": 2,
"column": 0
},
"end": {
"line": 2,
"column": 1
}
},
"range": [
1,
2
]
},
{
"type": {
"label": "++/--",
"beforeExpr": false,
"startsExpr": true,
"isLoop": false,
"isAssign": false,
"prefix": true,
"postfix": true,
"binop": null
},
"value": "++",
"loc": {
"start": {
"line": 2,
"column": 1
},
"end": {
"line": 2,
"column": 3
}
},
"range": [
2,
4
]
},
{
"type": {
"label": "**",
"beforeExpr": true,
"startsExpr": false,
"isLoop": false,
"isAssign": false,
"prefix": false,
"postfix": false,
"binop": null,
"updateContext": null
},
"value": "**",
"loc": {
"start": {
"line": 2,
"column": 4
},
"end": {
"line": 2,
"column": 6
}
},
"range": [
5,
7
]
},
{
"type": "Numeric",
"value": "2",
"loc": {
"start": {
"line": 2,
"column": 7
},
"end": {
"line": 2,
"column": 8
}
},
"range": [
8,
9
]
},
{
"type": "Punctuator",
"value": ";",
"loc": {
"start": {
"line": 2,
"column": 8
},
"end": {
"line": 2,
"column": 9
}
},
"range": [
9,
10
]
}
]
};
2 changes: 2 additions & 0 deletions tests/fixtures/ecma-version/7/exponential-plusplus.src.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@

a++ ** 2;
Loading

0 comments on commit e770e84

Please sign in to comment.