Navigation Menu

Skip to content

Commit

Permalink
feat: false negative with onlyDeclarations + properties in id-mat…
Browse files Browse the repository at this point in the history
…ch (#15431)

* fix: false negative with `onlyDeclarations` in `id-match`

Fixes #15123

* test: add more cases

* fix: false positive with computed properties

* refactor: code

* test: remove invalid test case
  • Loading branch information
snitin315 committed Dec 21, 2021
1 parent 775d181 commit 9d6fe5a
Show file tree
Hide file tree
Showing 2 changed files with 131 additions and 1 deletion.
11 changes: 11 additions & 0 deletions lib/rules/id-match.js
Expand Up @@ -211,6 +211,17 @@ module.exports = {
}
}

// For https://github.com/eslint/eslint/issues/15123
} else if (
parent.type === "Property" &&
parent.parent.type === "ObjectExpression" &&
parent.key === node &&
!parent.computed
) {
if (checkProperties && isInvalid(name)) {
report(node);
}

/*
* Properties have their own rules, and
* AssignmentPattern nodes can be treated like Properties:
Expand Down
121 changes: 120 additions & 1 deletion tests/lib/rules/id-match.js
Expand Up @@ -197,6 +197,58 @@ ruleTester.run("id-match", rule, {
}],
parserOptions: { ecmaVersion: 2022 }
},
{
code: `
const foo = {
foo_one: 1,
bar_one: 2,
fooBar: 3
};
`,
options: ["^[^_]+$", {
properties: false
}],
parserOptions: { ecmaVersion: 2022 }
},
{
code: `
const foo = {
foo_one: 1,
bar_one: 2,
fooBar: 3
};
`,
options: ["^[^_]+$", {
onlyDeclarations: true
}],
parserOptions: { ecmaVersion: 2022 }
},
{
code: `
const foo = {
foo_one: 1,
bar_one: 2,
fooBar: 3
};
`,
options: ["^[^_]+$", {
properties: false,
onlyDeclarations: false
}],
parserOptions: { ecmaVersion: 2022 }
},
{
code: `
const foo = {
[a]: 1,
};
`,
options: ["^[^a]", {
properties: true,
onlyDeclarations: true
}],
parserOptions: { ecmaVersion: 2022 }
},

// Class Methods
{
Expand Down Expand Up @@ -774,7 +826,74 @@ ruleTester.run("id-match", rule, {
type: "PrivateIdentifier"
}
]
}
},

// https://github.com/eslint/eslint/issues/15123
{
code: `
const foo = {
foo_one: 1,
bar_one: 2,
fooBar: 3
};
`,
options: ["^[^_]+$", {
properties: true,
onlyDeclarations: true
}],
parserOptions: { ecmaVersion: 2022 },
errors: [
{
message: "Identifier 'foo_one' does not match the pattern '^[^_]+$'.",
type: "Identifier"
},
{
message: "Identifier 'bar_one' does not match the pattern '^[^_]+$'.",
type: "Identifier"
}
]
},
{
code: `
const foo = {
foo_one: 1,
bar_one: 2,
fooBar: 3
};
`,
options: ["^[^_]+$", {
properties: true,
onlyDeclarations: false
}],
parserOptions: { ecmaVersion: 2022 },
errors: [
{
message: "Identifier 'foo_one' does not match the pattern '^[^_]+$'.",
type: "Identifier"
},
{
message: "Identifier 'bar_one' does not match the pattern '^[^_]+$'.",
type: "Identifier"
}
]
},
{
code: `
const foo = {
[a]: 1,
};
`,
options: ["^[^a]", {
properties: true,
onlyDeclarations: false
}],
parserOptions: { ecmaVersion: 2022 },
errors: [
{
message: "Identifier 'a' does not match the pattern '^[^a]'.",
type: "Identifier"
}
]
}
]
});

0 comments on commit 9d6fe5a

Please sign in to comment.