Skip to content
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

tools: fix bug in prefer-primordials ESLint rule #40628

Merged
merged 1 commit into from Nov 4, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
58 changes: 58 additions & 0 deletions test/parallel/test-eslint-prefer-primordials.js
Expand Up @@ -81,6 +81,23 @@ new RuleTester({
code: 'const { Map } = primordials; new Map()',
options: [{ name: 'Map', into: 'Safe' }],
},
{
code: `
const { Function } = primordials;
const rename = Function;
const obj = { rename };
`,
options: [{ name: 'Function' }],
},
{
code: `
const { Function } = primordials;
let rename;
rename = Function;
const obj = { rename };
`,
options: [{ name: 'Function' }],
},
],
invalid: [
{
Expand Down Expand Up @@ -158,6 +175,16 @@ new RuleTester({
options: [{ name: 'Reflect' }],
errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
},
{
code: `
const { Reflect } = primordials;
module.exports = function() {
const { ownKeys } = Reflect;
}
`,
options: [{ name: 'Reflect' }],
errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
},
{
code: 'new Map()',
options: [{ name: 'Map', into: 'Safe' }],
Expand All @@ -171,5 +198,36 @@ new RuleTester({
options: [{ name: 'Function' }],
errors: [{ message: /const { FunctionPrototype } = primordials/ }]
},
{
code: `
const obj = { Function };
`,
options: [{ name: 'Function' }],
errors: [{ message: /const { Function } = primordials/ }]
},
{
code: `
const rename = Function;
`,
options: [{ name: 'Function' }],
errors: [{ message: /const { Function } = primordials/ }]
},
{
code: `
const rename = Function;
const obj = { rename };
`,
options: [{ name: 'Function' }],
errors: [{ message: /const { Function } = primordials/ }]
},
{
code: `
let rename;
rename = Function;
const obj = { rename };
`,
options: [{ name: 'Function' }],
errors: [{ message: /const { Function } = primordials/ }]
},
]
});
24 changes: 20 additions & 4 deletions tools/eslint-rules/prefer-primordials.js
Expand Up @@ -54,7 +54,7 @@ function getDestructuringAssignmentParent(scope, node) {
) {
return null;
}
return declaration.defs[0].node.init.name;
return declaration.defs[0].node.init;
}

const identifierSelector =
Expand Down Expand Up @@ -94,17 +94,20 @@ module.exports = {
return;
}
const name = node.name;
const parentName = getDestructuringAssignmentParent(
const parent = getDestructuringAssignmentParent(
context.getScope(),
node
);
const parentName = parent?.name;
if (!isTarget(nameMap, name) && !isTarget(nameMap, parentName)) {
return;
}

const defs = globalScope.set.get(name)?.defs;
if (parentName && isTarget(nameMap, parentName)) {
if (!defs || defs[0].name.name !== 'primordials') {
if (defs?.[0].name.name !== 'primordials' &&
!reported.has(parent.range[0]) &&
parent.parent?.id?.type !== 'Identifier') {
reported.add(node.range[0]);
const into = renameMap.get(name);
context.report({
Expand Down Expand Up @@ -147,7 +150,20 @@ module.exports = {
}
});
}
}
},
VariableDeclarator(node) {
const name = node.init?.name;
if (name !== undefined && isTarget(nameMap, name) &&
node.id.type === 'Identifier' &&
!globalScope.set.get(name)?.defs.length) {
reported.add(node.init.range[0]);
context.report({
node,
messageId: 'error',
data: { name },
});
}
},
};
}
};