Skip to content

Commit 50d102e

Browse files
aduh95targos
authored andcommitted
tools: fix bug in prefer-primordials ESLint rule
Refs: #40622 PR-URL: #40628 Reviewed-By: Michaël Zasso <targos@protonmail.com> Reviewed-By: Voltrex <mohammadkeyvanzade94@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com>
1 parent 95e4d29 commit 50d102e

2 files changed

Lines changed: 78 additions & 4 deletions

File tree

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

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,23 @@ new RuleTester({
8181
code: 'const { Map } = primordials; new Map()',
8282
options: [{name: 'Map',into: 'Safe'}],
8383
},
84+
{
85+
code: `
86+
const { Function } = primordials;
87+
const rename = Function;
88+
const obj = { rename };
89+
`,
90+
options: [{name: 'Function'}],
91+
},
92+
{
93+
code: `
94+
const { Function } = primordials;
95+
let rename;
96+
rename = Function;
97+
const obj = { rename };
98+
`,
99+
options: [{name: 'Function'}],
100+
},
84101
],
85102
invalid: [
86103
{
@@ -158,6 +175,16 @@ new RuleTester({
158175
options: [{name: 'Reflect'}],
159176
errors: [{message: /const{ReflectOwnKeys}=primordials/}]
160177
},
178+
{
179+
code: `
180+
const { Reflect } = primordials;
181+
module.exports = function() {
182+
const { ownKeys } = Reflect;
183+
}
184+
`,
185+
options: [{name: 'Reflect'}],
186+
errors: [{message: /const{ReflectOwnKeys}=primordials/}]
187+
},
161188
{
162189
code: 'new Map()',
163190
options: [{name: 'Map',into: 'Safe'}],
@@ -171,5 +198,36 @@ new RuleTester({
171198
options: [{name: 'Function'}],
172199
errors: [{message: /const{FunctionPrototype}=primordials/}]
173200
},
201+
{
202+
code: `
203+
const obj = { Function };
204+
`,
205+
options: [{name: 'Function'}],
206+
errors: [{message: /const{Function}=primordials/}]
207+
},
208+
{
209+
code: `
210+
const rename = Function;
211+
`,
212+
options: [{name: 'Function'}],
213+
errors: [{message: /const{Function}=primordials/}]
214+
},
215+
{
216+
code: `
217+
const rename = Function;
218+
const obj = { rename };
219+
`,
220+
options: [{name: 'Function'}],
221+
errors: [{message: /const{Function}=primordials/}]
222+
},
223+
{
224+
code: `
225+
let rename;
226+
rename = Function;
227+
const obj = { rename };
228+
`,
229+
options: [{name: 'Function'}],
230+
errors: [{message: /const{Function}=primordials/}]
231+
},
174232
]
175233
});

‎tools/eslint-rules/prefer-primordials.js‎

Lines changed: 20 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ function getDestructuringAssignmentParent(scope, node) {
5454
){
5555
returnnull;
5656
}
57-
returndeclaration.defs[0].node.init.name;
57+
returndeclaration.defs[0].node.init;
5858
}
5959

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

105106
constdefs=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])&&
110+
parent.parent?.id?.type!=='Identifier'){
108111
reported.add(node.range[0]);
109112
constinto=renameMap.get(name);
110113
context.report({
@@ -147,7 +150,20 @@ module.exports = {
147150
}
148151
});
149152
}
150-
}
153+
},
154+
VariableDeclarator(node){
155+
constname=node.init?.name;
156+
if(name!==undefined&&isTarget(nameMap,name)&&
157+
node.id.type==='Identifier'&&
158+
!globalScope.set.get(name)?.defs.length){
159+
reported.add(node.init.range[0]);
160+
context.report({
161+
node,
162+
messageId: 'error',
163+
data: { name },
164+
});
165+
}
166+
},
151167
};
152168
}
153169
};

0 commit comments

Comments
 (0)