Skip to content

Commit

Permalink
Fix scope compare in consistent-function-scoping (#562)
Browse files Browse the repository at this point in the history
  • Loading branch information
fisker committed Feb 16, 2020
1 parent f489ce2 commit ec4387c
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
9 changes: 6 additions & 3 deletions rules/consistent-function-scoping.js
Expand Up @@ -8,6 +8,9 @@ const getReferences = scope => scope.references.concat(
...scope.childScopes.map(scope => getReferences(scope))
);

const isSameScope = (scope1, scope2) =>
scope1 && scope2 && (scope1 === scope2 || scope1.block === scope2.block);

function checkReferences(scope, parent, scopeManager) {
if (!scope) {
return false;
Expand All @@ -26,7 +29,7 @@ function checkReferences(scope, parent, scopeManager) {
}

const hitReference = variable.references.some(reference => {
return parent === reference.from;
return isSameScope(parent, reference.from);
});

if (hitReference) {
Expand All @@ -35,7 +38,7 @@ function checkReferences(scope, parent, scopeManager) {

const hitDefinitions = variable.defs.some(definition => {
const scope = scopeManager.acquire(definition.node);
return parent === scope;
return isSameScope(parent, scope);
});

if (hitDefinitions) {
Expand Down Expand Up @@ -71,7 +74,7 @@ function checkReferences(scope, parent, scopeManager) {

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

if (hitIdentifier) {
Expand Down
25 changes: 25 additions & 0 deletions test/consistent-function-scoping.js
Expand Up @@ -219,6 +219,31 @@ ruleTester.run('consistent-function-scoping', rule, {
throw new Error('unknown direction')
}
}
`,
// #374
outdent`
'use strict';
module.exports = function recordErrors(eventEmitter, stateArgument) {
const stateVariable = stateArgument;
function onError(error) {
stateVariable.inputError = error;
}
eventEmitter.once('error', onError);
};
`,
// #375
outdent`
module.exports = function recordErrors(eventEmitter, stateArgument) {
function onError(error) {
stateArgument.inputError = error;
}
function onError2(error) {
onError(error);
}
eventEmitter.once('error', onError2);
};
`
],
invalid: [
Expand Down

0 comments on commit ec4387c

Please sign in to comment.