Skip to content

Commit

Permalink
feat: add regexp flags support for id-naming-convention #180 (#181)
Browse files Browse the repository at this point in the history
  • Loading branch information
jdussouillez committed Mar 10, 2024
1 parent 6290397 commit 11106a9
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 2 deletions.
2 changes: 1 addition & 1 deletion docs/rules/id-naming-convention.md
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ This rule supports 4 naming cases. `camelCase`, `snake_case`, `PascalCase`, `keb
- `"camelCase"`: Enforce camelCase format.
- `"PascalCase"`: Enforce PascalCase format.
- `"kebab-case"`: Enforce kebab-case format.
- `"regex", { "pattern": "^my-regex$" }`: Enforce a format defined by a custom regex.
- `"regex", { "pattern": "^my-regex$", "flags": "i" }`: Enforce a format defined by a custom regex (`flags` option is optional).

#### "snake_case" (default)

Expand Down
6 changes: 5 additions & 1 deletion packages/eslint-plugin/lib/rules/id-naming-convention.js
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ module.exports = {
type: "object",
properties: {
pattern: { type: "string" },
flags: { type: "string" },
},
additionalProperties: false,
},
Expand All @@ -74,7 +75,10 @@ module.exports = {
const checkNaming =
convention === CONVENTIONS.REGEX
? (/** @type string */ name) =>
new RegExp(context.options[1].pattern).test(name)
new RegExp(
context.options[1].pattern,
context.options[1].flags || ""
).test(name)
: CONVENTION_CHECKERS[convention];

return {
Expand Down
13 changes: 13 additions & 0 deletions packages/eslint-plugin/tests/rules/id-naming-convention.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,10 @@ ruleTester.run("id-naming-convention", rule, {
code: `<div id="CuStOmReGeX"> </div>`,
options: ["regex", { pattern: "^([A-Z][a-z])+[A-Z]?$" }],
},
{
code: `<div id="CuStOmReGeX"> </div>`,
options: ["regex", { pattern: "^[a-z]+$", flags: "i" }],
},
],
invalid: [
{
Expand Down Expand Up @@ -54,5 +58,14 @@ ruleTester.run("id-naming-convention", rule, {
},
],
},
{
code: `<div id="kebab-case"> </div>`,
options: ["regex", { pattern: "^([A-Z][a-z])+[A-Z]?$", flags: "i" }],
errors: [
{
message: "The id 'kebab-case' is not matched with the regex.",
},
],
},
],
});

0 comments on commit 11106a9

Please sign in to comment.