diff --git a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.ts b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.ts index 6c588c40e8..513a3727cc 100644 --- a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.ts +++ b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/component-name.ts @@ -5,7 +5,7 @@ import type { RuleFeature } from "@eslint-react/shared"; import { RE_CONSTANT_CASE, RE_PASCAL_CASE } from "@eslint-react/shared"; import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema"; -import { createRule } from "../utils"; +import { createRule, toRegExp } from "../utils"; type Case = "CONSTANT_CASE" | "PascalCase"; @@ -147,7 +147,7 @@ function normalizeOptions(options: Options) { ? { rule: opts } : { ...opts, - excepts: opts.excepts?.map((pattern) => new RegExp(pattern, "u")) ?? [], + excepts: opts.excepts?.map(toRegExp) ?? [], }, } as const; } diff --git a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.ts b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.ts index b9eb7f6699..2c7f5a5dd1 100644 --- a/packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.ts +++ b/packages/plugins/eslint-plugin-react-naming-convention/src/rules/filename.ts @@ -7,7 +7,7 @@ import type { JSONSchema4 } from "@typescript-eslint/utils/json-schema"; import { camelCase, kebabCase, pascalCase, snakeCase } from "string-ts"; import { match } from "ts-pattern"; -import { createRule } from "../utils"; +import { createRule, toRegExp } from "../utils"; export const RULE_NAME = "filename"; @@ -104,7 +104,7 @@ export default createRule({ function validate(name: string, casing: Case = rule, ignores: readonly string[] = excepts) { const shouldIgnore = ignores - .map((pattern) => new RegExp(pattern, "u")) + .map(toRegExp) .some((pattern) => pattern.test(name)); if (shouldIgnore) return true; diff --git a/packages/plugins/eslint-plugin-react-naming-convention/src/utils/index.ts b/packages/plugins/eslint-plugin-react-naming-convention/src/utils/index.ts index 69d92f43e3..f8934174c7 100644 --- a/packages/plugins/eslint-plugin-react-naming-convention/src/utils/index.ts +++ b/packages/plugins/eslint-plugin-react-naming-convention/src/utils/index.ts @@ -1 +1,2 @@ export * from "./create-rule"; +export * from "./regexp"; diff --git a/packages/plugins/eslint-plugin-react-naming-convention/src/utils/regexp.ts b/packages/plugins/eslint-plugin-react-naming-convention/src/utils/regexp.ts new file mode 100644 index 0000000000..86dfd0174a --- /dev/null +++ b/packages/plugins/eslint-plugin-react-naming-convention/src/utils/regexp.ts @@ -0,0 +1,26 @@ +// Ported from https://github.com/sveltejs/eslint-plugin-svelte/blob/main/packages/eslint-plugin-svelte/src/utils/regexp.ts +/* eslint-disable @typescript-eslint/strict-boolean-expressions */ +const RE_REGEXP_STR = /^\/(.+)\/([A-Za-z]*)$/u; + +/** + * Convert a string to the `RegExp`. + * Normal strings (e.g. `"foo"`) is converted to `/^foo$/` of `RegExp`. + * Strings like `"/^foo/i"` are converted to `/^foo/i` of `RegExp`. + * + * @param string The string to convert. + * @returns Returns the `RegExp`. + */ +export function toRegExp(string: string): { test(s: string): boolean } { + const [, pattern, flags = "u"] = RE_REGEXP_STR.exec(string) ?? []; + if (pattern) return new RegExp(pattern, flags); + return { test: (s) => s === string }; +} + +/** + * Checks whether given string is regexp string + * @param string The string to check + * @returns boolean + */ +export function isRegExp(string: string): boolean { + return Boolean(RE_REGEXP_STR.test(string)); +} diff --git a/scripts/update-website.ts b/scripts/update-website.ts index 595432ec1c..b4cf20389f 100644 --- a/scripts/update-website.ts +++ b/scripts/update-website.ts @@ -25,7 +25,7 @@ const [ [[], []], ); -await Promise.all(files.map(async ([src, dest]) => fs.copyFile(src, dest))); +await Promise.all(files.map(([src, dest]) => fs.copyFile(src, dest))); // fs.writeFileSync(path.join("apps", "website", "content", "docs", "rules", "data.json"), JSON.stringify(rules, null, 2));