Skip to content

Commit 8b417e6

Browse files
authored
consistent-function-scoping: Check use of this and arguments (#590)
1 parent 2e20294 commit 8b417e6

File tree

2 files changed

+85
-1
lines changed

2 files changed

+85
-1
lines changed

rules/consistent-function-scoping.js

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,16 @@ const isReactHook = scope =>
9595
scope.block.parent.callee.type === 'Identifier' &&
9696
reactHooks.has(scope.block.parent.callee.name);
9797

98+
const isArrowFunctionWithThis = scope =>
99+
scope.type === 'function' &&
100+
scope.block &&
101+
scope.block.type === 'ArrowFunctionExpression' &&
102+
(scope.thisFound || scope.childScopes.some(scope => isArrowFunctionWithThis(scope)));
103+
98104
function checkNode(node, scopeManager) {
99105
const scope = scopeManager.acquire(node);
100-
if (!scope) {
106+
107+
if (!scope || isArrowFunctionWithThis(scope)) {
101108
return true;
102109
}
103110

test/consistent-function-scoping.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -194,6 +194,32 @@ ruleTester.run('consistent-function-scoping', rule, {
194194
return Bar;
195195
};
196196
`,
197+
// `this`
198+
outdent`
199+
function doFoo(Foo) {
200+
const doBar = () => this;
201+
return doBar();
202+
};
203+
`,
204+
outdent`
205+
function doFoo(Foo) {
206+
const doBar = () => () => this;
207+
return doBar();
208+
};
209+
`,
210+
outdent`
211+
function doFoo(Foo) {
212+
const doBar = () => () => () => this;
213+
return doBar();
214+
};
215+
`,
216+
// `arguments`
217+
outdent`
218+
function doFoo(Foo) {
219+
const doBar = () => arguments;
220+
return doBar();
221+
};
222+
`,
197223
// React Hooks
198224
outdent`
199225
useEffect(() => {
@@ -317,6 +343,57 @@ ruleTester.run('consistent-function-scoping', rule, {
317343
`,
318344
errors: [createError({arrow: true})]
319345
},
346+
// `this`
347+
{
348+
code: outdent`
349+
function doFoo(Foo) {
350+
function doBar() {
351+
return this;
352+
}
353+
return doBar();
354+
};
355+
`,
356+
errors: [createError({name: 'doBar'})]
357+
},
358+
{
359+
code: outdent`
360+
function doFoo(Foo) {
361+
const doBar = () => (function() {return this})();
362+
return doBar();
363+
};
364+
`,
365+
errors: [createError({name: 'doBar', arrow: true})]
366+
},
367+
{
368+
code: outdent`
369+
function doFoo(Foo) {
370+
const doBar = () => (function() {return () => this})();
371+
return doBar();
372+
};
373+
`,
374+
errors: [createError({name: 'doBar', arrow: true})]
375+
},
376+
// `arguments`
377+
{
378+
code: outdent`
379+
function doFoo(Foo) {
380+
function doBar() {
381+
return arguments;
382+
}
383+
return doBar();
384+
};
385+
`,
386+
errors: [createError({name: 'doBar'})]
387+
},
388+
{
389+
code: outdent`
390+
function doFoo(Foo) {
391+
const doBar = () => (function() {return arguments})();
392+
return doBar();
393+
};
394+
`,
395+
errors: [createError({name: 'doBar', arrow: true})]
396+
},
320397
{
321398
code: outdent`
322399
function doFoo(foo) {

0 commit comments

Comments
 (0)