Skip to content

Commit

Permalink
New: Support ecmaVersion 7 (fixes #246)
Browse files Browse the repository at this point in the history
  • Loading branch information
nzakas committed Feb 25, 2016
1 parent c59de3a commit c02edd1
Show file tree
Hide file tree
Showing 14 changed files with 689 additions and 18 deletions.
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 c02edd1

Please sign in to comment.