-
Notifications
You must be signed in to change notification settings - Fork 364
Expand file tree
/
Copy pathapi_links_test.ts
More file actions
73 lines (63 loc) · 1.99 KB
/
api_links_test.ts
File metadata and controls
73 lines (63 loc) · 1.99 KB
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
import { walk } from "@std/fs";
import { assert } from "@std/assert";
const DIRS_TO_CHECK = ["./runtime"];
/**
* Find unlinked `Deno.*` API references in markdown prose.
*
* Matches: `Deno.serve` `Deno.readFile()` `Deno.FsFile`
* Ignores: [`Deno.serve`](/api/deno/~/Deno.serve) (already linked)
* Ignores: references inside fenced code blocks
*/
function findUnlinkedDenoApis(
content: string,
): { line: number; api: string }[] {
const lines = content.split("\n");
const results: { line: number; api: string }[] = [];
let inCodeBlock = false;
for (let i = 0; i < lines.length; i++) {
const line = lines[i];
if (/^```/.test(line)) {
inCodeBlock = !inCodeBlock;
continue;
}
if (inCodeBlock) continue;
// Match `Deno.something` or `Deno.something()` NOT preceded by [
const regex = /(?<!\[)`(Deno\.[a-zA-Z]\w+(?:\.\w+)*?)(?:\(\))?`/g;
let match;
while ((match = regex.exec(line)) !== null) {
results.push({ line: i + 1, api: match[1] });
}
}
return results;
}
Deno.test("`Deno.*` API references should be linked to /api/deno/", async () => {
const allUnlinked: { file: string; line: number; api: string }[] = [];
for (const dir of DIRS_TO_CHECK) {
for await (
const entry of walk(dir, {
exts: [".md", ".mdx"],
skip: [/migration_guide\.md$/],
})
) {
const content = await Deno.readTextFile(entry.path);
for (const { line, api } of findUnlinkedDenoApis(content)) {
allUnlinked.push({ file: entry.path, line, api });
}
}
}
if (allUnlinked.length > 0) {
const report = allUnlinked.map(
({ file, line, api }) =>
`${file}:${line} — \`${api}\` → [\`${api}\`](/api/deno/~/${api})`,
);
console.log(
`\nFound ${allUnlinked.length} unlinked Deno API references:\n`,
);
console.log(report.join("\n"));
console.log();
}
assert(
allUnlinked.length === 0,
`${allUnlinked.length} unlinked Deno API references found (see above)`,
);
});