Skip to content

Commit eb501fc

Browse files
authored
Fix: Rule can't find reference of create function (eslint-community#107)
* Add test * Fix reference searching * Style
1 parent 8eccc37 commit eb501fc

File tree

2 files changed

+40
-8
lines changed

2 files changed

+40
-8
lines changed

lib/utils.js

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -30,16 +30,23 @@ function isNormalFunctionExpressionReference (node, scopeManager) {
3030
}
3131

3232
const scope = scopeManager.acquire(node) || scopeManager.globalScope;
33-
if (!scope) {
34-
return false;
35-
}
33+
const scopes = [scope];
34+
let createReference;
35+
while (scopes.length > 0) {
36+
const currentScope = scopes.shift();
37+
const found = currentScope.references.find(reference => {
38+
return reference.resolved && reference.identifier === node;
39+
});
3640

37-
const references = scope.references;
38-
const createReference = references.find(reference => {
39-
return reference.identifier === node;
40-
});
41+
if (found) {
42+
createReference = found;
43+
break;
44+
}
45+
46+
scopes.push(...currentScope.childScopes);
47+
}
4148

42-
if (!createReference || !createReference.resolved) {
49+
if (!createReference) {
4350
return false;
4451
}
4552

tests/lib/utils.js

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,31 @@ describe('utils', () => {
122122
);
123123
});
124124
});
125+
126+
for (const scopeOptions of [
127+
{ ignoreEval: true, ecmaVersion: 6, sourceType: 'script', nodejsScope: true },
128+
{ ignoreEval: true, ecmaVersion: 6, sourceType: 'script' },
129+
{ ignoreEval: true, ecmaVersion: 6, sourceType: 'module' },
130+
]) {
131+
const ast = espree.parse(`
132+
const create = () => {};
133+
const meta = {};
134+
module.exports = { create, meta };
135+
`, { ecmaVersion: 6 });
136+
const expected = {
137+
create: { type: 'Identifier' },
138+
meta: { type: 'Identifier' },
139+
isNewStyle: true,
140+
};
141+
it(`ScopeOptions: ${JSON.stringify(scopeOptions)}`, () => {
142+
const scope = escope.analyze(ast, scopeOptions);
143+
const ruleInfo = utils.getRuleInfo(ast, scope);
144+
assert(
145+
lodash.isMatch(ruleInfo, expected),
146+
`Expected \n${util.inspect(ruleInfo)}\nto match\n${util.inspect(expected)}`
147+
);
148+
});
149+
}
125150
});
126151
});
127152

0 commit comments

Comments
 (0)