Skip to content

Commit 9ca5662

Browse files
committed
👍 Improve formatDocs() of gen- scripts
1 parent e073705 commit 9ca5662

File tree

2 files changed

+14
-8
lines changed

2 files changed

+14
-8
lines changed

scripts/gen-function/format.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -8,10 +8,13 @@ const translate: Record<string, string> = {
88
};
99

1010
function formatDocs(docs: string): string[] {
11-
const lines = docs.replaceAll(/\*\//g, "* /").split("\n").map((v) =>
12-
` * ${v}`
13-
);
14-
return ["/**", ...lines, " */"];
11+
const lines = docs.replaceAll(/\*\//g, "* /").split("\n");
12+
const leading =
13+
lines.map((v) => v.match(/^\s*/)![0]).reduce((a, v) =>
14+
a.length < v.length ? a : v
15+
).length;
16+
const normalizedLines = lines.map((v) => ` * ${v.substring(leading)}`);
17+
return ["/**", ...normalizedLines, " */"];
1518
}
1619

1720
function formatVariants(fn: string, vars: Variant[]): string[] {

scripts/gen-option/format.ts

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,10 +21,13 @@ function defaultValue(type: string): string {
2121
}
2222

2323
function formatDocs(docs: string): string[] {
24-
const lines = docs.replaceAll(/\*\//g, "* /").split("\n").map((v) =>
25-
` * ${v}`
26-
);
27-
return ["/**", ...lines, " */"];
24+
const lines = docs.replaceAll(/\*\//g, "* /").split("\n");
25+
const leading =
26+
lines.map((v) => v.match(/^\s*/)![0]).reduce((a, v) =>
27+
a.length < v.length ? a : v
28+
).length;
29+
const normalizedLines = lines.map((v) => ` * ${v.substring(leading)}`);
30+
return ["/**", ...normalizedLines, " */"];
2831
}
2932

3033
function formatOption({ name, type, scope, docs }: Option): string[] {

0 commit comments

Comments
 (0)