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

Commit

Permalink
Fix: arrow function scope strictness (take 2) (#52)
Browse files Browse the repository at this point in the history
* Fix: Arrow function scope strictness (fixes #49) (#50)

* Fix: Error on functions with non-block body

* Apply suggestion from review

Co-authored-by: Toru Nagashima <[email protected]>
  • Loading branch information
2 people authored and not-an-aardvark committed Mar 15, 2019
1 parent 14c092a commit 299df64
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
9 changes: 4 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 All @@ -67,6 +62,10 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) {
}

if (scope.type === "function") {
if (block.type === Syntax.ArrowFunctionExpression && block.body.type !== Syntax.BlockStatement) {
return false;
}

if (block.type === Syntax.Program) {
body = block;
} else {
Expand Down
70 changes: 68 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,72 @@ 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);
});

it("works with no body", () => {
const ast = espree("var arrow = a => a;");

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

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

const scope = scopeManager.scopes[1];

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

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

0 comments on commit 299df64

Please sign in to comment.