Skip to content
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
tools: add polyfilled option to prefer-primordials rule
PR-URL: #55318
Reviewed-By: Michaël Zasso <targos@protonmail.com>
  • Loading branch information
aduh95 committed Nov 16, 2024
commit 8f188da3e3c0425be711b8a7d3af5f8a90dc55ae
3 changes: 3 additions & 0 deletions lib/.eslintrc.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,9 @@ rules:
into: Safe
- name: String
- name: Symbol
polyfilled:
- asyncDispose
- dispose
- name: SyntaxError
- name: TypeError
- name: Uint16Array
Expand Down
16 changes: 16 additions & 0 deletions test/parallel/test-eslint-prefer-primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,22 @@ new RuleTester({
options: [{ name: 'Symbol' }],
errors: [{ message: /const { SymbolIterator } = primordials/ }]
},
{
code: `
const { SymbolAsyncDispose } = primordials;
const a = { [SymbolAsyncDispose] () {} }
`,
options: [{ name: 'Symbol', polyfilled: ['asyncDispose', 'dispose'] }],
errors: [{ message: /const { SymbolAsyncDispose } = require\("internal\/util"\)/ }]
},
{
code: `
const { SymbolDispose } = primordials;
const a = { [SymbolDispose] () {} }
`,
options: [{ name: 'Symbol', polyfilled: ['asyncDispose', 'dispose'] }],
errors: [{ message: /const { SymbolDispose } = require\("internal\/util"\)/ }]
},
{
code: `
const { ObjectDefineProperty, Symbol } = primordials;
Expand Down
22 changes: 22 additions & 0 deletions tools/eslint-rules/prefer-primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ module.exports = {
meta: {
messages: {
error: 'Use `const { {{name}} } = primordials;` instead of the global.',
errorPolyfill: 'Use `const { {{name}} } = require("internal/util");` instead of the primordial.',
},
schema: {
type: 'array',
Expand All @@ -88,6 +89,10 @@ module.exports = {
items: { type: 'string' },
},
into: { type: 'string' },
polyfilled: {
type: 'array',
items: { type: 'string' },
},
},
additionalProperties: false,
},
Expand All @@ -99,6 +104,7 @@ module.exports = {

const nameMap = new Map();
const renameMap = new Map();
const polyfilledSet = new Set();

for (const option of context.options) {
const names = option.ignore || [];
Expand All @@ -109,6 +115,11 @@ module.exports = {
if (option.into) {
renameMap.set(option.name, option.into);
}
if (option.polyfilled) {
for (const propertyName of option.polyfilled) {
polyfilledSet.add(`${option.name}${propertyName[0].toUpperCase()}${propertyName.slice(1)}`);
}
}
}

let reported;
Expand Down Expand Up @@ -186,6 +197,17 @@ module.exports = {
},
VariableDeclarator(node) {
const name = node.init?.name;
if (name === 'primordials' && node.id.type === 'ObjectPattern') {
const name = node.id.properties.find(({ key }) => polyfilledSet.has(key.name))?.key.name;
if (name) {
context.report({
node,
messageId: 'errorPolyfill',
data: { name },
});
return;
}
}
if (name !== undefined && isTarget(nameMap, name) &&
node.id.type === 'Identifier' &&
!globalScope.set.get(name)?.defs.length) {
Expand Down