-
Notifications
You must be signed in to change notification settings - Fork 15
/
transform.ts
87 lines (81 loc) 路 2.84 KB
/
transform.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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
import {
fromFileUrl,
toFileUrl,
} from "https://deno.land/std@0.217.0/path/mod.ts";
import { intersection } from "https://deno.land/x/set_operations@v1.1.1/mod.ts";
interface ModuleInformation {
sourcePath: string;
sourceText: string;
functions: Set<string>;
}
function extractExportModulePaths(
sourcePath: string,
sourceText: string,
): string[] {
const baseUrl = new URL("./", toFileUrl(sourcePath));
return [...sourceText.matchAll(/^export \* from "(?<url>.*)"/gm)].map((
[, url]: string[],
) => fromFileUrl(new URL(url, baseUrl)));
}
function extractExportFunctions(sourceText: string): Set<string> {
return new Set(
[...sourceText.matchAll(
/^export\s+(?:async\s+)?function\s+(?:\*\s*)?(?<name>\w+)/gms,
)].map(([, name]: string[]) => name),
);
}
async function findExportModules(
sourcePath: string,
): Promise<ModuleInformation[]> {
const sourceText = await Deno.readTextFile(sourcePath);
const modules = extractExportModulePaths(sourcePath, sourceText);
const descendants = await Promise.all(modules.map(findExportModules));
const functions = extractExportFunctions(sourceText);
return [
{ sourcePath, sourceText, functions },
...descendants.flat(),
].filter(({ functions }) => functions.size > 0);
}
function replaceFunctionDocs(
sourceText: string,
fnJSDocs: Map<string, string>,
): string {
const reLeading = /(?<leading>\s*)/.source;
const reJSDoc = /(?:\/\*\*[^/](?:[^*]|\*[^/])*\*\/)?/.source;
const reLineComments = /(?:\s|\/\/[^\n]*\n)*/.source;
const reFunction =
/^export\s+(?:async\s+)?function\s+(?:\*\s*)?(?<name>\w+)/.source;
const reDef = `(?<def>${reLineComments}${reFunction})`;
const reFunctions = new RegExp(`${reLeading}${reJSDoc}${reDef}`, "gms");
const fnReplaced = new Set<string>();
return sourceText.replaceAll(reFunctions, (prev, ...args) => {
const { leading, def, name } = args.at(-1) as Record<string, string>;
const jsdoc = fnJSDocs.get(name);
if (jsdoc && !fnReplaced.has(name)) {
fnReplaced.add(name);
return `${leading}${jsdoc}${def}`;
}
return prev;
});
}
/**
* Overwrite the source file by replacing the JSDoc in the export functions.
* Everything except JSDoc is kept.
* Files referenced by `export * from "..."` are replaced recursively.
*
* @param rootSourcePath - origin source file path
* @param fnJSDocs - Map of function names to JSDoc
*/
export async function transform(
rootSourcePath: string,
fnJSDocs: Map<string, string>,
): Promise<void> {
const fnNames = new Set(fnJSDocs.keys());
const informations = await findExportModules(rootSourcePath);
for (const { sourcePath, sourceText, functions } of informations) {
if (intersection(functions, fnNames).size > 0) {
const transformed = replaceFunctionDocs(sourceText, fnJSDocs);
await Deno.writeTextFile(sourcePath, transformed);
}
}
}