From 2e1f1c6f3c9ec8772e0cb0fb213a6592057b721b Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Sat, 7 Nov 2020 17:29:12 +0100 Subject: [PATCH] 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 --- .../test-eslint-prefer-primordials.js | 4 +- tools/eslint-rules/prefer-primordials.js | 40 +++++++++---------- 2 files changed, 21 insertions(+), 23 deletions(-) diff --git a/test/parallel/test-eslint-prefer-primordials.js b/test/parallel/test-eslint-prefer-primordials.js index 2d04f9a1081c56..143ddd2b0d118f 100644 --- a/test/parallel/test-eslint-prefer-primordials.js +++ b/test/parallel/test-eslint-prefer-primordials.js @@ -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/ }] }, { diff --git a/tools/eslint-rules/prefer-primordials.js b/tools/eslint-rules/prefer-primordials.js index 51fb6ab8c2ad44..02b00a7e3b1f12 100644 --- a/tools/eslint-rules/prefer-primordials.js +++ b/tools/eslint-rules/prefer-primordials.js @@ -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,