Skip to content

Commit 899670a

Browse files
committed
tools: fix bug in prefer-primordials ESLint rule
Refs: nodejs#40622
1 parent e55ab89 commit 899670a

File tree

2 files changed

+51
-4
lines changed

2 files changed

+51
-4
lines changed

test/parallel/test-eslint-prefer-primordials.js

+32
Original file line numberDiff line numberDiff line change
@@ -158,6 +158,16 @@ new RuleTester({
158158
options: [{ name: 'Reflect' }],
159159
errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
160160
},
161+
{
162+
code: `
163+
const { Reflect } = primordials;
164+
module.exports = function() {
165+
const { ownKeys } = Reflect;
166+
}
167+
`,
168+
options: [{ name: 'Reflect' }],
169+
errors: [{ message: /const { ReflectOwnKeys } = primordials/ }]
170+
},
161171
{
162172
code: 'new Map()',
163173
options: [{ name: 'Map', into: 'Safe' }],
@@ -171,5 +181,27 @@ new RuleTester({
171181
options: [{ name: 'Function' }],
172182
errors: [{ message: /const { FunctionPrototype } = primordials/ }]
173183
},
184+
{
185+
code: `
186+
const obj = { Function };
187+
`,
188+
options: [{ name: 'Function' }],
189+
errors: [{ message: /const { Function } = primordials/ }]
190+
},
191+
{
192+
code: `
193+
const rename = Function;
194+
`,
195+
options: [{ name: 'Function' }],
196+
errors: [{ message: /const { Function } = primordials/ }]
197+
},
198+
{
199+
code: `
200+
const rename = Function;
201+
const obj = { rename };
202+
`,
203+
options: [{ name: 'Function' }],
204+
errors: [{ message: /const { Function } = primordials/ }]
205+
},
174206
]
175207
});

tools/eslint-rules/prefer-primordials.js

+19-4
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function getDestructuringAssignmentParent(scope, node) {
5454
) {
5555
return null;
5656
}
57-
return declaration.defs[0].node.init.name;
57+
return declaration.defs[0].node.init;
5858
}
5959

6060
const identifierSelector =
@@ -94,17 +94,19 @@ module.exports = {
9494
return;
9595
}
9696
const name = node.name;
97-
const parentName = getDestructuringAssignmentParent(
97+
const parent = getDestructuringAssignmentParent(
9898
context.getScope(),
9999
node
100100
);
101+
const parentName = parent?.name;
101102
if (!isTarget(nameMap, name) && !isTarget(nameMap, parentName)) {
102103
return;
103104
}
104105

105106
const defs = globalScope.set.get(name)?.defs;
106107
if (parentName && isTarget(nameMap, parentName)) {
107-
if (!defs || defs[0].name.name !== 'primordials') {
108+
if (defs?.[0].name.name !== 'primordials' &&
109+
!reported.has(parent.range[0])) {
108110
reported.add(node.range[0]);
109111
const into = renameMap.get(name);
110112
context.report({
@@ -147,7 +149,20 @@ module.exports = {
147149
}
148150
});
149151
}
150-
}
152+
},
153+
VariableDeclarator(node) {
154+
const name = node.init?.name;
155+
if (name !== undefined && isTarget(nameMap, name) &&
156+
node.id.type === 'Identifier' &&
157+
!globalScope.set.get(name)?.defs.length) {
158+
reported.add(node.init.range[0]);
159+
context.report({
160+
node,
161+
messageId: 'error',
162+
data: { name },
163+
});
164+
}
165+
},
151166
};
152167
}
153168
};

0 commit comments

Comments
 (0)