Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit d5e915b

Browse filesBrowse files
aduh95ruyadorno
authored andcommitted
tools: add polyfilled option to prefer-primordials rule
PR-URL: #55318 Backport-PR-URL: #55421 Reviewed-By: Michaël Zasso <targos@protonmail.com>
1 parent cb527a9 commit d5e915b
Copy full SHA for d5e915b

3 files changed

+39-1Lines changed: 39 additions & 1 deletion

File tree

Expand file treeCollapse file tree
Open diff view settings
Filter options
Expand file treeCollapse file tree
Open diff view settings
Collapse file

‎lib/eslint.config_partial.mjs‎

Copy file name to clipboardExpand all lines: lib/eslint.config_partial.mjs
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -440,7 +440,7 @@ export default [
440440
into: 'Safe',
441441
},
442442
{ name: 'String' },
443-
{ name: 'Symbol' },
443+
{ name: 'Symbol', polyfilled: ['asyncDispose', 'dispose'] },
444444
{ name: 'SyntaxError' },
445445
{ name: 'TypeError' },
446446
{ name: 'Uint16Array' },
Collapse file

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

Copy file name to clipboardExpand all lines: test/parallel/test-eslint-prefer-primordials.js
+16Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -177,6 +177,22 @@ new RuleTester({
177177
options: [{ name: 'Symbol' }],
178178
errors: [{ message: /const { SymbolIterator } = primordials/ }]
179179
},
180+
{
181+
code: `
182+
const { SymbolAsyncDispose } = primordials;
183+
const a = { [SymbolAsyncDispose] () {} }
184+
`,
185+
options: [{ name: 'Symbol', polyfilled: ['asyncDispose', 'dispose'] }],
186+
errors: [{ message: /const { SymbolAsyncDispose } = require\("internal\/util"\)/ }]
187+
},
188+
{
189+
code: `
190+
const { SymbolDispose } = primordials;
191+
const a = { [SymbolDispose] () {} }
192+
`,
193+
options: [{ name: 'Symbol', polyfilled: ['asyncDispose', 'dispose'] }],
194+
errors: [{ message: /const { SymbolDispose } = require\("internal\/util"\)/ }]
195+
},
180196
{
181197
code: `
182198
const { ObjectDefineProperty, Symbol } = primordials;
Collapse file

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

Copy file name to clipboardExpand all lines: tools/eslint-rules/prefer-primordials.js
+22Lines 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
const nameMap = new Map();
101106
const renameMap = new Map();
107+
const polyfilledSet = new Set();
102108

103109
for (const option of context.options) {
104110
const names = 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 (const propertyName of option.polyfilled) {
120+
polyfilledSet.add(`${option.name}${propertyName[0].toUpperCase()}${propertyName.slice(1)}`);
121+
}
122+
}
112123
}
113124

114125
let reported;
@@ -186,6 +197,17 @@ module.exports = {
186197
},
187198
VariableDeclarator(node) {
188199
const name = node.init?.name;
200+
if (name === 'primordials' && node.id.type === 'ObjectPattern') {
201+
const name = 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 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.