mirror of
https://github.com/nodejs/node.git
synced 2025-08-15 21:58:48 +02:00
tools: fix bugs in prefer-primordials linter rule
The ESLint rule would repport false positive if code is using an identifier that happens to have the same name as a primordials member. PR-URL: https://github.com/nodejs/node/pull/42010 Reviewed-By: Shingo Inoue <leko.noor@gmail.com> Reviewed-By: Rich Trott <rtrott@gmail.com>
This commit is contained in:
parent
68c4b8d56d
commit
6700b5c9e1
2 changed files with 45 additions and 2 deletions
|
@ -99,6 +99,34 @@ new RuleTester({
|
||||||
`,
|
`,
|
||||||
options: [{ name: 'Function' }],
|
options: [{ name: 'Function' }],
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
code: 'function identifier() {}',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'function* identifier() {}',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'class identifier {}',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'new class { identifier(){} }',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'const a = { identifier: \'4\' }',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'identifier:{const a = 4}',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
|
{
|
||||||
|
code: 'switch(0){case identifier:}',
|
||||||
|
options: [{ name: 'identifier' }]
|
||||||
|
},
|
||||||
],
|
],
|
||||||
invalid: [
|
invalid: [
|
||||||
{
|
{
|
||||||
|
|
|
@ -57,8 +57,18 @@ function getDestructuringAssignmentParent(scope, node) {
|
||||||
return declaration.defs[0].node.init;
|
return declaration.defs[0].node.init;
|
||||||
}
|
}
|
||||||
|
|
||||||
const identifierSelector =
|
const parentSelectors = [
|
||||||
'[type!=VariableDeclarator][type!=MemberExpression]>Identifier';
|
// We want to select identifiers that refer to other references, not the ones
|
||||||
|
// that create a new reference.
|
||||||
|
'ClassDeclaration',
|
||||||
|
'FunctionDeclaration',
|
||||||
|
'LabeledStatement',
|
||||||
|
'MemberExpression',
|
||||||
|
'MethodDefinition',
|
||||||
|
'SwitchCase',
|
||||||
|
'VariableDeclarator',
|
||||||
|
];
|
||||||
|
const identifierSelector = parentSelectors.map((selector) => `[type!=${selector}]`).join('') + '>Identifier';
|
||||||
|
|
||||||
module.exports = {
|
module.exports = {
|
||||||
meta: {
|
meta: {
|
||||||
|
@ -90,6 +100,11 @@ module.exports = {
|
||||||
reported = new Set();
|
reported = new Set();
|
||||||
},
|
},
|
||||||
[identifierSelector](node) {
|
[identifierSelector](node) {
|
||||||
|
if (node.parent.type === 'Property' && node.parent.key === node) {
|
||||||
|
// If the identifier is the key for this property declaration, it
|
||||||
|
// can't be referring to a primordials member.
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (reported.has(node.range[0])) {
|
if (reported.has(node.range[0])) {
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue