From 8fce09e2a53dae8bc8238f251b23b4b8e9f83564 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Thu, 4 Nov 2021 18:04:27 +0100 Subject: [PATCH] tools: fix bug in `prefer-primordials` ESLint rule MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Refs: https://github.com/nodejs/node/pull/40622 PR-URL: https://github.com/nodejs/node/pull/40628 Reviewed-By: Michaƫl Zasso Reviewed-By: Voltrex Reviewed-By: James M Snell --- .../test-eslint-prefer-primordials.js | 58 +++++++++++++++++++ tools/eslint-rules/prefer-primordials.js | 24 ++++++-- 2 files changed, 78 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-eslint-prefer-primordials.js b/test/parallel/test-eslint-prefer-primordials.js index 143ddd2b0d118f..aa7f1940675f77 100644 --- a/test/parallel/test-eslint-prefer-primordials.js +++ b/test/parallel/test-eslint-prefer-primordials.js @@ -81,6 +81,23 @@ new RuleTester({ code: 'const { Map } = primordials; new Map()', options: [{ name: 'Map', into: 'Safe' }], }, + { + code: ` + const { Function } = primordials; + const rename = Function; + const obj = { rename }; + `, + options: [{ name: 'Function' }], + }, + { + code: ` + const { Function } = primordials; + let rename; + rename = Function; + const obj = { rename }; + `, + options: [{ name: 'Function' }], + }, ], invalid: [ { @@ -158,6 +175,16 @@ new RuleTester({ options: [{ name: 'Reflect' }], errors: [{ message: /const { ReflectOwnKeys } = primordials/ }] }, + { + code: ` + const { Reflect } = primordials; + module.exports = function() { + const { ownKeys } = Reflect; + } + `, + options: [{ name: 'Reflect' }], + errors: [{ message: /const { ReflectOwnKeys } = primordials/ }] + }, { code: 'new Map()', options: [{ name: 'Map', into: 'Safe' }], @@ -171,5 +198,36 @@ new RuleTester({ options: [{ name: 'Function' }], errors: [{ message: /const { FunctionPrototype } = primordials/ }] }, + { + code: ` + const obj = { Function }; + `, + options: [{ name: 'Function' }], + errors: [{ message: /const { Function } = primordials/ }] + }, + { + code: ` + const rename = Function; + `, + options: [{ name: 'Function' }], + errors: [{ message: /const { Function } = primordials/ }] + }, + { + code: ` + const rename = Function; + const obj = { rename }; + `, + options: [{ name: 'Function' }], + errors: [{ message: /const { Function } = primordials/ }] + }, + { + code: ` + let rename; + rename = Function; + const obj = { rename }; + `, + options: [{ name: 'Function' }], + errors: [{ message: /const { Function } = primordials/ }] + }, ] }); diff --git a/tools/eslint-rules/prefer-primordials.js b/tools/eslint-rules/prefer-primordials.js index 02b00a7e3b1f12..b30527eaf423a4 100644 --- a/tools/eslint-rules/prefer-primordials.js +++ b/tools/eslint-rules/prefer-primordials.js @@ -54,7 +54,7 @@ function getDestructuringAssignmentParent(scope, node) { ) { return null; } - return declaration.defs[0].node.init.name; + return declaration.defs[0].node.init; } const identifierSelector = @@ -94,17 +94,20 @@ module.exports = { return; } const name = node.name; - const parentName = getDestructuringAssignmentParent( + const parent = getDestructuringAssignmentParent( context.getScope(), node ); + const parentName = parent?.name; if (!isTarget(nameMap, name) && !isTarget(nameMap, parentName)) { return; } const defs = globalScope.set.get(name)?.defs; if (parentName && isTarget(nameMap, parentName)) { - if (!defs || defs[0].name.name !== 'primordials') { + if (defs?.[0].name.name !== 'primordials' && + !reported.has(parent.range[0]) && + parent.parent?.id?.type !== 'Identifier') { reported.add(node.range[0]); const into = renameMap.get(name); context.report({ @@ -147,7 +150,20 @@ module.exports = { } }); } - } + }, + VariableDeclarator(node) { + const name = node.init?.name; + if (name !== undefined && isTarget(nameMap, name) && + node.id.type === 'Identifier' && + !globalScope.set.get(name)?.defs.length) { + reported.add(node.init.range[0]); + context.report({ + node, + messageId: 'error', + data: { name }, + }); + } + }, }; } };