Skip to content

Commit a3fb7c8

Browse files
authored
refactor: add toRegExp utility for regex handling in naming conventions (#969)
1 parent cce1d38 commit a3fb7c8

File tree

5 files changed

+32
-5
lines changed

5 files changed

+32
-5
lines changed

packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import type { RuleFeature } from "@eslint-react/shared";
55
import { RE_CONSTANT_CASE, RE_PASCAL_CASE } from "@eslint-react/shared";
66
import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema";
77

8-
import { createRule } from "../utils";
8+
import { createRule, toRegExp } from "../utils";
99

1010
type Case = "CONSTANT_CASE" | "PascalCase";
1111

@@ -147,7 +147,7 @@ function normalizeOptions(options: Options) {
147147
? { rule: opts }
148148
: {
149149
...opts,
150-
excepts: opts.excepts?.map((pattern) => new RegExp(pattern, "u")) ?? [],
150+
excepts: opts.excepts?.map(toRegExp) ?? [],
151151
},
152152
} as const;
153153
}

packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema";
77
import { camelCase, kebabCase, pascalCase, snakeCase } from "string-ts";
88
import { match } from "ts-pattern";
99

10-
import { createRule } from "../utils";
10+
import { createRule, toRegExp } from "../utils";
1111

1212
export const RULE_NAME = "filename";
1313

@@ -104,7 +104,7 @@ export default createRule<Options, MessageID>({
104104

105105
function validate(name: string, casing: Case = rule, ignores: readonly string[] = excepts) {
106106
const shouldIgnore = ignores
107-
.map((pattern) => new RegExp(pattern, "u"))
107+
.map(toRegExp)
108108
.some((pattern) => pattern.test(name));
109109
if (shouldIgnore) return true;
110110

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,2 @@
11
export * from "./create-rule";
2+
export * from "./regexp";
Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
// Ported from https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/utils/regexp.ts
2+
/* eslint-disable @typescript-eslint/strict-boolean-expressions */
3+
const RE_REGEXP_STR = /^\/(.+)\/([A-Za-z]*)$/u;
4+
5+
/**
6+
* Convert a string to the `RegExp`.
7+
* Normal strings (e.g. `"foo"`) is converted to `/^foo$/` of `RegExp`.
8+
* Strings like `"/^foo/i"` are converted to `/^foo/i` of `RegExp`.
9+
*
10+
* @param string The string to convert.
11+
* @returns Returns the `RegExp`.
12+
*/
13+
export function toRegExp(string: string): { test(s: string): boolean } {
14+
const [, pattern, flags = "u"] = RE_REGEXP_STR.exec(string) ?? [];
15+
if (pattern) return new RegExp(pattern, flags);
16+
return { test: (s) => s === string };
17+
}
18+
19+
/**
20+
* Checks whether given string is regexp string
21+
* @param string The string to check
22+
* @returns boolean
23+
*/
24+
export function isRegExp(string: string): boolean {
25+
return Boolean(RE_REGEXP_STR.test(string));
26+
}

scripts/update-website.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ const [
2525
[[], []],
2626
);
2727

28-
await Promise.all(files.map(async ([src, dest]) => fs.copyFile(src, dest)));
28+
await Promise.all(files.map(([src, dest]) => fs.copyFile(src, dest)));
2929

3030
// fs.writeFileSync(path.join("apps", "website", "content", "docs", "rules", "data.json"), JSON.stringify(rules, null, 2));
3131

0 commit comments

Comments
 (0)