Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -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;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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";

Expand Down Expand Up @@ -104,7 +104,7 @@ export default createRule<Options, MessageID>({

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;

Expand Down
Original file line number Diff line number Diff line change
@@ -1 +1,2 @@
export * from "./create-rule";
export * from "./regexp";
Original file line number Diff line number Diff line change
@@ -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));
}
2 changes: 1 addition & 1 deletion scripts/update-website.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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));

Expand Down