Skip to content

Commit

Permalink
Merge 1b2ca07 into e7e49ec
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Mar 11, 2020
2 parents e7e49ec + 1b2ca07 commit 6c9909e
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 @@ -75,9 +75,16 @@ function checkReferences(scope, parent, scopeManager) {
);
}

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();
};
`,
// #391
outdent`
const enrichErrors = (packageName, cliArgs, f) => async (...args) => {
Expand Down Expand Up @@ -311,6 +337,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 6c9909e

Please sign in to comment.