diff --git a/rules/consistent-function-scoping.js b/rules/consistent-function-scoping.js index 3cff85bbed..9e502ab5e5 100644 --- a/rules/consistent-function-scoping.js +++ b/rules/consistent-function-scoping.js @@ -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; @@ -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) { @@ -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) { @@ -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) { diff --git a/test/consistent-function-scoping.js b/test/consistent-function-scoping.js index e2eee2a121..49a00a557a 100644 --- a/test/consistent-function-scoping.js +++ b/test/consistent-function-scoping.js @@ -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: [