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: fix bugs in prefer-primordials linter rule #42010

Merged
merged 2 commits into from Feb 18, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 28 additions & 0 deletions test/parallel/test-eslint-prefer-primordials.js
Expand Up @@ -99,6 +99,34 @@ new RuleTester({
`,
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: [
{
Expand Down
19 changes: 17 additions & 2 deletions tools/eslint-rules/prefer-primordials.js
Expand Up @@ -57,8 +57,18 @@ function getDestructuringAssignmentParent(scope, node) {
return declaration.defs[0].node.init;
}

const identifierSelector =
'[type!=VariableDeclarator][type!=MemberExpression]>Identifier';
const parentSelectors = [
// We want to select identifier that refer to another reference, not the one
aduh95 marked this conversation as resolved.
Show resolved Hide resolved
// that create a new reference.
'ClassDeclaration',
'FunctionDeclaration',
'LabeledStatement',
'MemberExpression',
'MethodDefinition',
'SwitchCase',
'VariableDeclarator',
];
const identifierSelector = parentSelectors.map((selector) => `[type!=${selector}]`).join('') + '>Identifier';

module.exports = {
meta: {
Expand Down Expand Up @@ -90,6 +100,11 @@ module.exports = {
reported = new Set();
},
[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])) {
return;
}
Expand Down