Skip to content

Commit

Permalink
consistent-function-scoping: Do not fail on same-scoped function re…
Browse files Browse the repository at this point in the history
…ferences (#378)
  • Loading branch information
MrHen authored and sindresorhus committed Sep 18, 2019
1 parent 02ff741 commit b62d96e
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 0 deletions.
33 changes: 33 additions & 0 deletions rules/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,39 @@ function checkReferences(scope, parent, scopeManager) {
return true;
}

// This check looks for neighboring function definitions
const hitIdentifier = variable.identifiers.some(identifier => {
// Only look at identifiers that live in a FunctionDeclaration
if (!identifier.parent || identifier.parent.type !== 'FunctionDeclaration') {
return false;
}

const identifierScope = scopeManager.acquire(identifier);

// If we have a scope, the earlier checks should have worked so ignore them here
if (identifierScope) {
return false;
}

const identifierParentScope = scopeManager.acquire(identifier.parent);
if (!identifierParentScope) {
return false;
}

// Ignore identifiers from our own scope
if (scope === identifierParentScope) {
return false;
}

// Look at the scope above the function definition to see if lives
// next to the reference being checked
return parent === identifierParentScope.upper;
});

if (hitIdentifier) {
return true;
}

return false;
});

Expand Down
12 changes: 12 additions & 0 deletions test/consistent-function-scoping.js
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,18 @@ ruleTester.run('consistent-function-scoping', rule, {
}
return foo;
}
`,
outdent`
function doFoo(foo) {
function doBar(bar) {
foo.bar = bar;
}
function doZaz(zaz) {
doBar(zaz);
}
doZaz('zaz');
};
`
],
invalid: [
Expand Down

0 comments on commit b62d96e

Please sign in to comment.