-
Notifications
You must be signed in to change notification settings - Fork 0
/
lib.ts
53 lines (47 loc) · 1.28 KB
/
lib.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { parse } from "https://deno.land/x/swc@0.2.1/mod.ts";
import { z } from "https://deno.land/x/zod@v3.16.1/mod.ts";
interface ASTRegex {
type: "RegExpLiteral";
pattern: string;
flag?: string;
}
function regexToString(regex: unknown): ASTRegex {
const schema = z.object({
type: z.literal("RegExpLiteral"),
pattern: z.string(),
flag: z.string().optional(),
});
return schema.parse(regex);
}
function filterType(module: unknown): ASTRegex[] {
const result: ASTRegex[] = [];
for (const value of Object.values(module)) {
if (value === null || value === undefined) {
continue;
} else if (
typeof value === "object" && "type" in value &&
value.type === "RegExpLiteral"
) {
result.push(regexToString(value));
} else if (typeof value === "object") {
result.push(...filterType(value as Record<string, unknown>));
} else if (Array.isArray(value)) {
result.push(...filterType(value));
} else {
continue;
}
}
return result;
}
export function getRegexes(source: string) {
const ast = parse(source, {
target: "es2019",
syntax: "typescript",
comments: false,
});
const regexes: ASTRegex[] = [];
for (const node of ast.body) {
regexes.push(...filterType(node));
}
return regexes;
}