Skip to content

Commit 11106a9

Browse files
authored
feat: add regexp flags support for id-naming-convention #180 (#181)
1 parent 6290397 commit 11106a9

File tree

3 files changed

+19
-2
lines changed

3 files changed

+19
-2
lines changed

docs/rules/id-naming-convention.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ This rule supports 4 naming cases. `camelCase`, `snake_case`, `PascalCase`, `keb
2222
- `"camelCase"`: Enforce camelCase format.
2323
- `"PascalCase"`: Enforce PascalCase format.
2424
- `"kebab-case"`: Enforce kebab-case format.
25-
- `"regex", { "pattern": "^my-regex$" }`: Enforce a format defined by a custom regex.
25+
- `"regex", { "pattern": "^my-regex$", "flags": "i" }`: Enforce a format defined by a custom regex (`flags` option is optional).
2626

2727
#### "snake_case" (default)
2828

packages/eslint-plugin/lib/rules/id-naming-convention.js

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,7 @@ module.exports = {
5555
type: "object",
5656
properties: {
5757
pattern: { type: "string" },
58+
flags: { type: "string" },
5859
},
5960
additionalProperties: false,
6061
},
@@ -74,7 +75,10 @@ module.exports = {
7475
const checkNaming =
7576
convention === CONVENTIONS.REGEX
7677
? (/** @type string */ name) =>
77-
new RegExp(context.options[1].pattern).test(name)
78+
new RegExp(
79+
context.options[1].pattern,
80+
context.options[1].flags || ""
81+
).test(name)
7882
: CONVENTION_CHECKERS[convention];
7983

8084
return {

packages/eslint-plugin/tests/rules/id-naming-convention.test.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,10 @@ ruleTester.run("id-naming-convention", rule, {
2525
code: `<div id="CuStOmReGeX"> </div>`,
2626
options: ["regex", { pattern: "^([A-Z][a-z])+[A-Z]?$" }],
2727
},
28+
{
29+
code: `<div id="CuStOmReGeX"> </div>`,
30+
options: ["regex", { pattern: "^[a-z]+$", flags: "i" }],
31+
},
2832
],
2933
invalid: [
3034
{
@@ -54,5 +58,14 @@ ruleTester.run("id-naming-convention", rule, {
5458
},
5559
],
5660
},
61+
{
62+
code: `<div id="kebab-case"> </div>`,
63+
options: ["regex", { pattern: "^([A-Z][a-z])+[A-Z]?$", flags: "i" }],
64+
errors: [
65+
{
66+
message: "The id 'kebab-case' is not matched with the regex.",
67+
},
68+
],
69+
},
5770
],
5871
});

0 commit comments

Comments
 (0)