Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

tools,lib: refactor prefer-primordials rule #36018

Merged
merged 1 commit into from
Feb 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
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
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;
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
}

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