Skip to content

Commit

Permalink
New: ecmaFeatures.impliedStrict (fixes: eslint#227)
Browse files Browse the repository at this point in the history
  • Loading branch information
nre committed Jan 3, 2016
1 parent 73a079c commit ce69b3a
Show file tree
Hide file tree
Showing 8 changed files with 497 additions and 5 deletions.
3 changes: 3 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ var ast = espree.parse(code, {
// enable return in global scope
globalReturn: true,

// enable implied strict mode (if ecmaVersion >= 5)
impliedStrict: true,

// allow experimental object rest/spread
experimentalObjectRestSpread: true
}
Expand Down
15 changes: 15 additions & 0 deletions espree.js
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,15 @@ pp.extend("checkLVal", function(checkLVal) {
};
});

pp.extend("parseTopLevel", function(parseTopLevel) {
return /** @this acorn.Parser */ function(node) {
if (extra.ecmaFeatures.impliedStrict) {
this.strict = this.options.ecmaVersion >= 5;
}
return parseTopLevel.call(this, node);
};
});

/**
* Method to parse an object rest or object spread.
* @returns {ASTNode} The node representing object rest or object spread.
Expand Down Expand Up @@ -422,6 +431,7 @@ pp.extend("jsx_readString", function(jsxReadString) {
function tokenize(code, options) {
var toString,
tokens,
impliedStrict,
translator = new TokenTranslator(tt, code);

toString = String;
Expand Down Expand Up @@ -478,6 +488,8 @@ function tokenize(code, options) {
// apply parsing flags
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = options.ecmaFeatures;
impliedStrict = extra.ecmaFeatures.impliedStrict;
extra.ecmaFeatures.impliedStrict = typeof impliedStrict === "boolean" && impliedStrict;
}

try {
Expand Down Expand Up @@ -554,6 +566,7 @@ function parse(code, options) {
var program,
toString = String,
translator,
impliedStrict,
acornOptions = {
ecmaVersion: 5
};
Expand Down Expand Up @@ -620,6 +633,8 @@ function parse(code, options) {
// apply parsing flags after sourceType to allow overriding
if (options.ecmaFeatures && typeof options.ecmaFeatures === "object") {
extra.ecmaFeatures = options.ecmaFeatures;
impliedStrict = extra.ecmaFeatures.impliedStrict;
extra.ecmaFeatures.impliedStrict = typeof impliedStrict === "boolean" && impliedStrict;
if (options.ecmaFeatures.globalReturn) {
acornOptions.allowReturnOutsideFunction = true;
}
Expand Down
3 changes: 3 additions & 0 deletions lib/features.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,9 @@ module.exports = {
// allow return statement in global scope
globalReturn: false,

// allow implied strict mode
impliedStrict: false,

// allow experimental object rest/spread
experimentalObjectRestSpread: false
};
Original file line number Diff line number Diff line change
@@ -0,0 +1,132 @@
module.exports = {
"type": "Program",
"sourceType": "script",
"body": [
{
"type": "VariableDeclaration",
"declarations": [
{
"type": "VariableDeclarator",
"id": {
"type": "Identifier",
"name": "let",
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
}
},
"init": null,
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
}
}
],
"kind": "var",
"range": [
0,
8
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
}
}
],
"range": [
0,
8
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 8
}
},
"tokens": [
{
"type": "Keyword",
"value": "var",
"range": [
0,
3
],
"loc": {
"start": {
"line": 1,
"column": 0
},
"end": {
"line": 1,
"column": 3
}
}
},
{
"type": "Identifier",
"value": "let",
"range": [
4,
7
],
"loc": {
"start": {
"line": 1,
"column": 4
},
"end": {
"line": 1,
"column": 7
}
}
},
{
"type": "Punctuator",
"value": ";",
"range": [
7,
8
],
"loc": {
"start": {
"line": 1,
"column": 7
},
"end": {
"line": 1,
"column": 8
}
}
}
]
};
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
var let;
Loading

0 comments on commit ce69b3a

Please sign in to comment.