Skip to content
Permalink
Browse files
feat: false negative with onlyDeclarations + properties in id-mat…
…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 9d6fe5a6b65f397bafc5eb0a995e96717cdc9b53
Showing with 131 additions and 1 deletion.
  1. +11 −0 lib/rules/id-match.js
  2. +120 −1 tests/lib/rules/id-match.js
@@ -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:
@@ -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
{
@@ -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.