mirror of
https://github.com/zebrajr/node.git
synced 2026-01-15 12:15:26 +00:00
tools: refactor prefer-primordials
Use optional chaining to improve code readability and remove use of `Array.prototype.reduce`. PR-URL: https://github.com/nodejs/node/pull/36018 Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
This commit is contained in:
@@ -66,7 +66,7 @@ new RuleTester({
|
||||
},
|
||||
{
|
||||
code: 'const { NumberParseInt } = primordials; NumberParseInt("xxx")',
|
||||
options: [{ name: 'parseInt' }]
|
||||
options: [{ name: 'parseInt', into: 'Number' }]
|
||||
},
|
||||
{
|
||||
code: `
|
||||
@@ -141,7 +141,7 @@ new RuleTester({
|
||||
const { Number } = primordials;
|
||||
Number.parseInt('10')
|
||||
`,
|
||||
options: [{ name: 'Number', into: Number }],
|
||||
options: [{ name: 'Number' }],
|
||||
errors: [{ message: /const { NumberParseInt } = primordials/ }]
|
||||
},
|
||||
{
|
||||
|
||||
@@ -23,10 +23,7 @@ function isTarget(map, varName) {
|
||||
}
|
||||
|
||||
function isIgnored(map, varName, propName) {
|
||||
if (!map.has(varName) || !map.get(varName).has(propName)) {
|
||||
return false;
|
||||
}
|
||||
return map.get(varName).get(propName).ignored;
|
||||
return map.get(varName)?.get(propName)?.ignored ?? false;
|
||||
}
|
||||
|
||||
function getReportName({ name, parentName, into }) {
|
||||
@@ -71,25 +68,26 @@ module.exports = {
|
||||
},
|
||||
create(context) {
|
||||
const globalScope = context.getSourceCode().scopeManager.globalScope;
|
||||
const nameMap = context.options.reduce((acc, option) =>
|
||||
acc.set(
|
||||
|
||||
const nameMap = new Map();
|
||||
const renameMap = new Map();
|
||||
|
||||
for (const option of context.options) {
|
||||
const names = option.ignore || [];
|
||||
nameMap.set(
|
||||
option.name,
|
||||
(option.ignore || [])
|
||||
.reduce((acc, name) => acc.set(name, {
|
||||
ignored: true
|
||||
}), new Map())
|
||||
)
|
||||
, new Map());
|
||||
const renameMap = context.options
|
||||
.filter((option) => option.into)
|
||||
.reduce((acc, option) =>
|
||||
acc.set(option.name, option.into)
|
||||
, new Map());
|
||||
new Map(names.map((name) => [name, { ignored: true }]))
|
||||
);
|
||||
if (option.into) {
|
||||
renameMap.set(option.name, option.into);
|
||||
}
|
||||
}
|
||||
|
||||
let reported;
|
||||
|
||||
return {
|
||||
Program() {
|
||||
reported = new Map();
|
||||
reported = new Set();
|
||||
},
|
||||
[identifierSelector](node) {
|
||||
if (reported.has(node.range[0])) {
|
||||
@@ -104,10 +102,10 @@ module.exports = {
|
||||
return;
|
||||
}
|
||||
|
||||
const defs = (globalScope.set.get(name) || {}).defs || null;
|
||||
const defs = globalScope.set.get(name)?.defs;
|
||||
if (parentName && isTarget(nameMap, parentName)) {
|
||||
if (!defs || defs[0].name.name !== 'primordials') {
|
||||
reported.set(node.range[0], true);
|
||||
reported.add(node.range[0]);
|
||||
const into = renameMap.get(name);
|
||||
context.report({
|
||||
node,
|
||||
@@ -120,7 +118,7 @@ module.exports = {
|
||||
return;
|
||||
}
|
||||
if (defs.length === 0 || defs[0].node.init.name !== 'primordials') {
|
||||
reported.set(node.range[0], true);
|
||||
reported.add(node.range[0]);
|
||||
const into = renameMap.get(name);
|
||||
context.report({
|
||||
node,
|
||||
|
||||
Reference in New Issue
Block a user