Skip to content

Commit

Permalink
Merge 5dd94c0 into 2e20294
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 11, 2020
2 parents 2e20294 + 5dd94c0 commit 0c759a9
Show file tree
Hide file tree
Showing 2 changed files with 85 additions and 1 deletion.
9 changes: 8 additions & 1 deletion rules/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -95,9 +95,16 @@ const isReactHook = scope =>
scope.block.parent.callee.type === 'Identifier' &&
reactHooks.has(scope.block.parent.callee.name);

const isArrowFunctionWithThis = scope =>
scope.type === 'function' &&
scope.block &&
scope.block.type === 'ArrowFunctionExpression' &&
(scope.thisFound || scope.childScopes.some(scope => isArrowFunctionWithThis(scope)));

function checkNode(node, scopeManager) {
const scope = scopeManager.acquire(node);
if (!scope) {

if (!scope || isArrowFunctionWithThis(scope)) {
return true;
}

Expand Down
77 changes: 77 additions & 0 deletions test/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,32 @@ ruleTester.run('consistent-function-scoping', rule, {
return Bar;
};
`,
// `this`
outdent`
function doFoo(Foo) {
const doBar = () => this;
return doBar();
};
`,
outdent`
function doFoo(Foo) {
const doBar = () => () => this;
return doBar();
};
`,
outdent`
function doFoo(Foo) {
const doBar = () => () => () => this;
return doBar();
};
`,
// `arguments`
outdent`
function doFoo(Foo) {
const doBar = () => arguments;
return doBar();
};
`,
// React Hooks
outdent`
useEffect(() => {
Expand Down Expand Up @@ -317,6 +343,57 @@ ruleTester.run('consistent-function-scoping', rule, {
`,
errors: [createError({arrow: true})]
},
// `this`
{
code: outdent`
function doFoo(Foo) {
function doBar() {
return this;
}
return doBar();
};
`,
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
function doFoo(Foo) {
const doBar = () => (function() {return this})();
return doBar();
};
`,
errors: [createError({name: 'doBar', arrow: true})]
},
{
code: outdent`
function doFoo(Foo) {
const doBar = () => (function() {return () => this})();
return doBar();
};
`,
errors: [createError({name: 'doBar', arrow: true})]
},
// `arguments`
{
code: outdent`
function doFoo(Foo) {
function doBar() {
return arguments;
}
return doBar();
};
`,
errors: [createError({name: 'doBar'})]
},
{
code: outdent`
function doFoo(Foo) {
const doBar = () => (function() {return arguments})();
return doBar();
};
`,
errors: [createError({name: 'doBar', arrow: true})]
},
{
code: outdent`
function doFoo(foo) {
Expand Down

0 comments on commit 0c759a9

Please sign in to comment.