diff --git a/lib/utils.js b/lib/utils.js index 40d38b84..fd92b1b5 100644 --- a/lib/utils.js +++ b/lib/utils.js @@ -30,16 +30,23 @@ function isNormalFunctionExpressionReference (node, scopeManager) { } const scope = scopeManager.acquire(node) || scopeManager.globalScope; - if (!scope) { - return false; - } + const scopes = [scope]; + let createReference; + while (scopes.length > 0) { + const currentScope = scopes.shift(); + const found = currentScope.references.find(reference => { + return reference.resolved && reference.identifier === node; + }); - const references = scope.references; - const createReference = references.find(reference => { - return reference.identifier === node; - }); + if (found) { + createReference = found; + break; + } + + scopes.push(...currentScope.childScopes); + } - if (!createReference || !createReference.resolved) { + if (!createReference) { return false; } diff --git a/tests/lib/utils.js b/tests/lib/utils.js index c92d9f88..cd711378 100644 --- a/tests/lib/utils.js +++ b/tests/lib/utils.js @@ -122,6 +122,31 @@ describe('utils', () => { ); }); }); + + for (const scopeOptions of [ + { ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true }, + { ignoreEval: true, ecmaVersion: 6, sourceType: 'script' }, + { ignoreEval: true, ecmaVersion: 6, sourceType: 'module' }, + ]) { + const ast = espree.parse(` + const create = () => {}; + const meta = {}; + module.exports = { create, meta }; + `, { ecmaVersion: 6 }); + const expected = { + create: { type: 'Identifier' }, + meta: { type: 'Identifier' }, + isNewStyle: true, + }; + it(`ScopeOptions: ${JSON.stringify(scopeOptions)}`, () => { + const scope = escope.analyze(ast, scopeOptions); + const ruleInfo = utils.getRuleInfo(ast, scope); + assert( + lodash.isMatch(ruleInfo, expected), + `Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(expected)}` + ); + }); + } }); });