From 899670a6a0841fba755a2230d4589fd2edd11304 Mon Sep 17 00:00:00 2001 From: Antoine du Hamel Date: Wed, 27 Oct 2021 16:17:05 +0200 Subject: [PATCH] tools: fix bug in `prefer-primordials` ESLint rule Refs: https://github.com/nodejs/node/pull/40622 --- .../test-eslint-prefer-primordials.js | 32 +++++++++++++++++++ tools/eslint-rules/prefer-primordials.js | 23 ++++++++++--- 2 files changed, 51 insertions(+), 4 deletions(-) diff --git a/test/parallel/test-eslint-prefer-primordials.js b/test/parallel/test-eslint-prefer-primordials.js index 143ddd2b0d118f..c9f991e7d08187 100644 --- a/test/parallel/test-eslint-prefer-primordials.js +++ b/test/parallel/test-eslint-prefer-primordials.js @@ -158,6 +158,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 +181,27 @@ 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/ }] + }, ] }); diff --git a/tools/eslint-rules/prefer-primordials.js b/tools/eslint-rules/prefer-primordials.js index 02b00a7e3b1f12..1d09d3b33119d2 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,19 @@ 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])) { reported.add(node.range[0]); const into = renameMap.get(name); context.report({ @@ -147,7 +149,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 }, + }); + } + }, }; } };