Skip to content

Commit

Permalink
tools: refactor prefer-primordials
Browse files Browse the repository at this point in the history
Use optional chaining to improve code readability and remove use of
`Array.prototype.reduce`.

PR-URL: #36018
Reviewed-By: Shingo Inoue <leko.noor@gmail.com>
  • Loading branch information
aduh95 authored and targos committed Jun 11, 2021
1 parent 698bffa commit a4dd50f
Show file tree
Hide file tree
Showing 2 changed files with 21 additions and 23 deletions.
4 changes: 2 additions & 2 deletions test/parallel/test-eslint-prefer-primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ new RuleTester({
},
{
code: 'const { NumberParseInt } = primordials; NumberParseInt("xxx")',
options: [{ name: 'parseInt' }]
options: [{ name: 'parseInt', into: 'Number' }]
},
{
code: `
Expand Down Expand Up @@ -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/ }]
},
{
Expand Down
40 changes: 19 additions & 21 deletions tools/eslint-rules/prefer-primordials.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 }) {
Expand Down Expand Up @@ -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])) {
Expand All @@ -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,
Expand All @@ -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,
Expand Down

0 comments on commit a4dd50f

Please sign in to comment.