Skip to content

Commit

Permalink
tools: fix bug in prefer-primordials ESLint rule
Browse files Browse the repository at this point in the history
Refs: nodejs#40622

PR-URL: nodejs#40628
Reviewed-By: Michaël Zasso <[email protected]>
Reviewed-By: Voltrex <[email protected]>
Reviewed-By: James M Snell <[email protected]>
  • Loading branch information
aduh95 authored Nov 4, 2021
1 parent 2cc7a91 commit 8fce09e
Show file tree
Hide file tree
Showing 2 changed files with 78 additions and 4 deletions.
58 changes: 58 additions & 0 deletions test/parallel/test-eslint-prefer-primordials.js
Original file line number Diff line number Diff line change
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
Original file line number Diff line number Diff line change
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 },
});
}
},
};
}
};

0 comments on commit 8fce09e

Please sign in to comment.