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

Commit 299df64

Browse files
futpibmysticatea
authored andcommitted
Fix: arrow function scope strictness (take 2) (#52)
* 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]>
1 parent 14c092a commit 299df64

File tree

2 files changed

+72
-7
lines changed

2 files changed

+72
-7
lines changed

lib/scope.js

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -49,11 +49,6 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) {
4949
return true;
5050
}
5151

52-
// ArrowFunctionExpression's scope is always strict scope.
53-
if (block.type === Syntax.ArrowFunctionExpression) {
54-
return true;
55-
}
56-
5752
if (isMethodDefinition) {
5853
return true;
5954
}
@@ -67,6 +62,10 @@ function isStrictScope(scope, block, isMethodDefinition, useDirective) {
6762
}
6863

6964
if (scope.type === "function") {
65+
if (block.type === Syntax.ArrowFunctionExpression && block.body.type !== Syntax.BlockStatement) {
66+
return false;
67+
}
68+
7069
if (block.type === Syntax.Program) {
7170
body = block;
7271
} else {

tests/es6-arrow-function-expression.js

Lines changed: 68 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ describe("ES6 arrow function expression", () => {
5252
scope = scopeManager.scopes[1];
5353
expect(scope.type).to.be.equal("function");
5454
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
55-
expect(scope.isStrict).to.be.true;
55+
expect(scope.isStrict).to.be.false;
5656
expect(scope.variables).to.have.length(2);
5757

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

8383
// There's no "arguments"
@@ -86,6 +86,72 @@ describe("ES6 arrow function expression", () => {
8686
expect(scope.variables[2].name).to.be.equal("c");
8787
expect(scope.variables[3].name).to.be.equal("d");
8888
});
89+
90+
it("inherits upper scope strictness", () => {
91+
const ast = espree(`
92+
"use strict";
93+
var arrow = () => {};
94+
`);
95+
96+
const scopeManager = analyze(ast, { ecmaVersion: 6 });
97+
98+
expect(scopeManager.scopes).to.have.length(2);
99+
100+
let scope = scopeManager.scopes[0];
101+
102+
expect(scope.type).to.be.equal("global");
103+
expect(scope.block.type).to.be.equal("Program");
104+
expect(scope.isStrict).to.be.true;
105+
expect(scope.variables).to.have.length(1);
106+
107+
scope = scopeManager.scopes[1];
108+
109+
expect(scope.type).to.be.equal("function");
110+
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
111+
expect(scope.isStrict).to.be.true;
112+
expect(scope.variables).to.have.length(0);
113+
});
114+
115+
it("is strict when a strictness directive is used", () => {
116+
const ast = espree(`
117+
var arrow = () => {
118+
"use strict";
119+
};
120+
`);
121+
122+
const scopeManager = analyze(ast, { ecmaVersion: 6 });
123+
124+
expect(scopeManager.scopes).to.have.length(2);
125+
126+
let scope = scopeManager.scopes[0];
127+
128+
expect(scope.type).to.be.equal("global");
129+
expect(scope.block.type).to.be.equal("Program");
130+
expect(scope.isStrict).to.be.false;
131+
expect(scope.variables).to.have.length(1);
132+
133+
scope = scopeManager.scopes[1];
134+
135+
expect(scope.type).to.be.equal("function");
136+
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
137+
expect(scope.isStrict).to.be.true;
138+
expect(scope.variables).to.have.length(0);
139+
});
140+
141+
it("works with no body", () => {
142+
const ast = espree("var arrow = a => a;");
143+
144+
const scopeManager = analyze(ast, { ecmaVersion: 6 });
145+
146+
expect(scopeManager.scopes).to.have.length(2);
147+
148+
const scope = scopeManager.scopes[1];
149+
150+
expect(scope.type).to.be.equal("function");
151+
expect(scope.block.type).to.be.equal("ArrowFunctionExpression");
152+
expect(scope.isStrict).to.be.false;
153+
expect(scope.variables).to.have.length(1);
154+
});
89155
});
90156

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

0 commit comments

Comments
 (0)