Skip to content

Commit

Permalink
Fix: Arrow function scope strictness (fixes #49) (#50)
Browse files Browse the repository at this point in the history
  • Loading branch information
futpib authored and mysticatea committed Feb 22, 2019
1 parent 0cbeea5 commit 2533966
Show file tree
Hide file tree
Showing 2 changed files with 53 additions and 7 deletions.
5 changes: 0 additions & 5 deletions lib/scope.js
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,6 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) {
return true;
}

// ArrowFunctionExpression's scope is always strict scope.
if (block.type === Syntax.ArrowFunctionExpression) {
return true;
}

if (isMethodDefinition) {
return true;
}
Expand Down
55 changes: 53 additions & 2 deletions tests/es6-arrow-function-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,7 @@ describe("ES6 arrow function expression", () => {
scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("function");
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
expect(scope.isStrict).to.be.true;
expect(scope.isStrict).to.be.false;
expect(scope.variables).to.have.length(2);

// There's no "arguments"
Expand All @@ -77,7 +77,7 @@ describe("ES6 arrow function expression", () => {
scope = scopeManager.scopes[1];
expect(scope.type).to.be.equal("function");
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
expect(scope.isStrict).to.be.true;
expect(scope.isStrict).to.be.false;
expect(scope.variables).to.have.length(4);

// There's no "arguments"
Expand All @@ -86,6 +86,57 @@ describe("ES6 arrow function expression", () => {
expect(scope.variables[2].name).to.be.equal("c");
expect(scope.variables[3].name).to.be.equal("d");
});

it("inherits upper scope strictness", () => {
const ast = espree(`
"use strict";
var arrow = () => {};
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });

expect(scopeManager.scopes).to.have.length(2);

let scope = scopeManager.scopes[0];

expect(scope.type).to.be.equal("global");
expect(scope.block.type).to.be.equal("Program");
expect(scope.isStrict).to.be.true;
expect(scope.variables).to.have.length(1);

scope = scopeManager.scopes[1];

expect(scope.type).to.be.equal("function");
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
expect(scope.isStrict).to.be.true;
expect(scope.variables).to.have.length(0);
});

it("is strict when a strictness directive is used", () => {
const ast = espree(`
var arrow = () => {
"use strict";
};
`);

const scopeManager = analyze(ast, { ecmaVersion: 6 });

expect(scopeManager.scopes).to.have.length(2);

let scope = scopeManager.scopes[0];

expect(scope.type).to.be.equal("global");
expect(scope.block.type).to.be.equal("Program");
expect(scope.isStrict).to.be.false;
expect(scope.variables).to.have.length(1);

scope = scopeManager.scopes[1];

expect(scope.type).to.be.equal("function");
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
expect(scope.isStrict).to.be.true;
expect(scope.variables).to.have.length(0);
});
});

// vim: set sw=4 ts=4 et tw=80 :

0 comments on commit 2533966

Please sign in to comment.