Skip to content

Fix: Rule can't find reference of create function #107

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Apr 15, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -30,16 +30,22 @@ 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;
});
if (found) {
createReference = found;
break;
} else {
scopes.push(...currentScope.childScopes);
}
}

const references = scope.references;
const createReference = references.find(reference => {
return reference.identifier === node;
});

if (!createReference || !createReference.resolved) {
if (!createReference) {
return false;
}

Expand Down
25 changes: 25 additions & 0 deletions tests/lib/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -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' },
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should still break rules like this, It should return the reference. But I'm not going to work on it in this PR.

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)}`
);
});
}
});
});

Expand Down