|
| 1 | +// This [Deno] program scans the compiled API documentation to check for errors. |
| 2 | +// |
| 3 | +// [Deno]: https://deno.land/ |
| 4 | +// |
| 5 | +// Usage: deno run --allow-read scripts/check-workspace.ts |
| 6 | +import { parse as parseFlags } from "https://deno.land/std@0.125.0/flags/mod.ts"; |
| 7 | +import { walk } from "https://deno.land/std@0.125.0/fs/mod.ts"; |
| 8 | +import * as path from "https://deno.land/std@0.125.0/path/mod.ts"; |
| 9 | +import * as log from "https://deno.land/std@0.125.0/log/mod.ts"; |
| 10 | + |
| 11 | +const parsedArgs = parseFlags(Deno.args, { |
| 12 | + "alias": { |
| 13 | + h: "help", |
| 14 | + d: "rustdoc-output", |
| 15 | + }, |
| 16 | + "string": [ |
| 17 | + "rustdoc-output", |
| 18 | + ], |
| 19 | +}); |
| 20 | + |
| 21 | +if (parsedArgs["help"]) { |
| 22 | + console.log("Arguments:"); |
| 23 | + console.log(" -h --help Displays this message"); |
| 24 | + console.log(" -d DIRECTORY --rustdoc-output=DIRECTORY"); |
| 25 | + console.log(" Specifies the rustdoc output directory to scan. " + |
| 26 | + "Defaults to `./target/doc` when unspecified."); |
| 27 | +} |
| 28 | + |
| 29 | +await log.setup({ |
| 30 | + handlers: { |
| 31 | + console: new log.handlers.ConsoleHandler("DEBUG"), |
| 32 | + }, |
| 33 | + |
| 34 | + loggers: { |
| 35 | + default: { |
| 36 | + level: "INFO", |
| 37 | + handlers: ["console"], |
| 38 | + }, |
| 39 | + }, |
| 40 | +}); |
| 41 | + |
| 42 | +const logger = log.getLogger(); |
| 43 | +let hasError = false; |
| 44 | +let expectedRepository: string | null = null; |
| 45 | + |
| 46 | +// A code fragment indicating the presence of `common.md` |
| 47 | +const COMMON_CSS_FRAGMENT = /\.toc-header \+ ul::before {/g; |
| 48 | +// Code fragments that require the presence of `common.md` |
| 49 | +const COMMON_CSS_USES = [ |
| 50 | + /class="toc-header"/, |
| 51 | + // The negative lookbehind is intended to avoid matching |
| 52 | + // the example code in `common.md` |
| 53 | + /(?<!\* *<div )class="admonition-follows"/, |
| 54 | + /class="disabled-feature-warning"/, |
| 55 | + // The negative lookbehind is intended to avoid matching |
| 56 | + // the example code in `common.md` |
| 57 | + /(?<!\* *<span )class="class"/, |
| 58 | + '<cneter>', |
| 59 | +]; |
| 60 | + |
| 61 | +await validateRustdocOutput(parsedArgs.d || "./target/doc"); |
| 62 | + |
| 63 | +if (hasError) { |
| 64 | + Deno.exit(1); |
| 65 | +} |
| 66 | + |
| 67 | +async function validateRustdocOutput(docPath: string): Promise<void> { |
| 68 | + let numFilesScanned = 0; |
| 69 | + |
| 70 | + logger.info(`Scanning ${docPath}`); |
| 71 | + for await (const { path } of walk(docPath, { includeDirs: false })) { |
| 72 | + if (!path.endsWith(".html") && !path.endsWith(".htm")) { |
| 73 | + continue; |
| 74 | + } |
| 75 | + |
| 76 | + logger.debug(`# ${path}`); |
| 77 | + |
| 78 | + const html = await Deno.readTextFile(path); |
| 79 | + numFilesScanned += 1; |
| 80 | + |
| 81 | + const numCommonCssInstances = |
| 82 | + Array.from(html.matchAll(COMMON_CSS_FRAGMENT)).length; |
| 83 | + if (numCommonCssInstances === 0) { |
| 84 | + // Maybe a redirect page? |
| 85 | + logger.debug(`${path}: Doesn't contain a fragment of 'common.md' - ignoring`); |
| 86 | + continue; |
| 87 | + } else if (numCommonCssInstances >= 2) { |
| 88 | + // `#[doc = ...]` (per-file) + `$RUSTDOCFLAGS` [ref:doc_global_styling] |
| 89 | + logger.debug(`${path}: There's a per-file inclusion of 'common.md' - ignoring`); |
| 90 | + |
| 91 | + if (numCommonCssInstances > 2) { |
| 92 | + logger.warning(`${path}: Includes too many instances of 'common.md'`); |
| 93 | + } |
| 94 | + |
| 95 | + continue; |
| 96 | + } else { |
| 97 | + // This file lacks a per-file incluson of `common.md`. |
| 98 | + } |
| 99 | + |
| 100 | + // To prevent the degradation of the appearance in the absence of the |
| 101 | + // appropriate `$RUSTDOCFLAGS`, this page must be devoid of constructs |
| 102 | + // that make use of the styling rules defined by `common.md`. |
| 103 | + for (const cssUsage of COMMON_CSS_USES) { |
| 104 | + if (html.match(cssUsage)) { |
| 105 | + logger.error(`${path}: This file lacks a per-file inclusion of 'common.md', ` + |
| 106 | + `but includes a code fragment '${cssUsage}'`); |
| 107 | + hasError = true; |
| 108 | + } |
| 109 | + } |
| 110 | + } |
| 111 | + |
| 112 | + logger.info(`${numFilesScanned} file(s) have been checked`); |
| 113 | +} // validateRustdocOutput |
0 commit comments