Skip to content

Commit 0b3fb34

Browse files
aduh95marco-ippolito
authored andcommitted
tools: add polyfilled option to prefer-primordials rule
PR-URL: #55318 Backport-PR-URL: #55264 Reviewed-By: Michaël Zasso <[email protected]>
1 parent 00a2fc7 commit 0b3fb34

File tree

3 files changed

+41
-0
lines changed

3 files changed

+41
-0
lines changed

‎lib/.eslintrc.yaml‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -243,6 +243,9 @@ rules:
243243
into: Safe
244244
- name: String
245245
- name: Symbol
246+
polyfilled:
247+
- asyncDispose
248+
- dispose
246249
- name: SyntaxError
247250
- name: TypeError
248251
- name: Uint16Array

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

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -176,6 +176,22 @@ new RuleTester({
176176
options: [{name: 'Symbol'}],
177177
errors: [{message: /const{SymbolIterator}=primordials/}]
178178
},
179+
{
180+
code: `
181+
const{SymbolAsyncDispose } = primordials;
182+
const a ={[SymbolAsyncDispose] (){} }
183+
`,
184+
options: [{name: 'Symbol',polyfilled: ['asyncDispose','dispose']}],
185+
errors: [{message: /const{SymbolAsyncDispose}=require\("internal\/util"\)/}]
186+
},
187+
{
188+
code: `
189+
const{SymbolDispose } = primordials;
190+
const a ={[SymbolDispose] (){} }
191+
`,
192+
options: [{name: 'Symbol',polyfilled: ['asyncDispose','dispose']}],
193+
errors: [{message: /const{SymbolDispose}=require\("internal\/util"\)/}]
194+
},
179195
{
180196
code: `
181197
const{ObjectDefineProperty, Symbol } = primordials;

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

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@ module.exports ={
7474
meta: {
7575
messages: {
7676
error: 'Use `const{{{name}} } = primordials;` instead of the global.',
77+
errorPolyfill: 'Use `const{{{name}} } = require("internal/util");` instead of the primordial.',
7778
},
7879
schema: {
7980
type: 'array',
@@ -88,6 +89,10 @@ module.exports ={
8889
items: {type: 'string'},
8990
},
9091
into: {type: 'string'},
92+
polyfilled: {
93+
type: 'array',
94+
items: {type: 'string'},
95+
},
9196
},
9297
additionalProperties: false,
9398
},
@@ -99,6 +104,7 @@ module.exports ={
99104

100105
constnameMap=newMap();
101106
constrenameMap=newMap();
107+
constpolyfilledSet=newSet();
102108

103109
for(constoptionofcontext.options){
104110
constnames=option.ignore||[];
@@ -109,6 +115,11 @@ module.exports ={
109115
if(option.into){
110116
renameMap.set(option.name,option.into);
111117
}
118+
if(option.polyfilled){
119+
for(constpropertyNameofoption.polyfilled){
120+
polyfilledSet.add(`${option.name}${propertyName[0].toUpperCase()}${propertyName.slice(1)}`);
121+
}
122+
}
112123
}
113124

114125
letreported;
@@ -186,6 +197,17 @@ module.exports ={
186197
},
187198
VariableDeclarator(node){
188199
constname=node.init?.name;
200+
if(name==='primordials'&&node.id.type==='ObjectPattern'){
201+
constname=node.id.properties.find(({ key })=>polyfilledSet.has(key.name))?.key.name;
202+
if(name){
203+
context.report({
204+
node,
205+
messageId: 'errorPolyfill',
206+
data: { name },
207+
});
208+
return;
209+
}
210+
}
189211
if(name!==undefined&&isTarget(nameMap,name)&&
190212
node.id.type==='Identifier'&&
191213
!globalScope.set.get(name)?.defs.length){

0 commit comments

Comments
(0)