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`.
  • Loading branch information
aduh95 committed Nov 14, 2020
1 parent d5e4e19 commit 599119c
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 599119c

Please sign in to comment.