diff --git a/test/docs/library-examples.test.ts b/test/docs/library-examples.test.ts index b3a4c4832..2955d73aa 100644 --- a/test/docs/library-examples.test.ts +++ b/test/docs/library-examples.test.ts @@ -8,10 +8,10 @@ import { describe, expect, test } from "bun:test"; import { spawnSync } from "node:child_process"; -import { readFileSync, symlinkSync, writeFileSync } from "node:fs"; +import { existsSync, readFileSync, symlinkSync, writeFileSync } from "node:fs"; import { join, resolve } from "node:path"; import { ROOT } from "../root.js"; -import { SETTINGS_SCHEMA_PATH } from "../settings-schema.js"; +import { SETTINGS_SCHEMA_PATH, schemaNotBuilt } from "../settings-schema.js"; import { withTempDir } from "../temp-dir.js"; const PAGE = "docs/reference/library.md"; @@ -55,9 +55,13 @@ export function tsFences(markdown: string): Fence[] { * The fences as one module: only the two package paths are rewritten, on the * lines they already occupy, so a program line is a page line by offset alone * and nothing else about a fence is repaired before tsc sees it. The page - * imports each name once, in the first example that uses it. + * imports each name once, in the first example that uses it. `schema` is where + * the package's schema subpath lands. */ -export function examplesProgram(fences: readonly Fence[]): { +export function examplesProgram( + fences: readonly Fence[], + schema: string, +): { text: string; pageLine: (programLine: number) => number; } { @@ -67,7 +71,7 @@ export function examplesProgram(fences: readonly Fence[]): { for (const fence of fences) { starts.push(line); const body = fence.body - .replaceAll(`"${PACKAGE}/settings.schema.json"`, JSON.stringify(SETTINGS_SCHEMA_PATH)) + .replaceAll(`"${PACKAGE}/settings.schema.json"`, JSON.stringify(schema)) .replaceAll(`"${PACKAGE}"`, JSON.stringify(ENTRY)); parts.push(body); line += body.split("\n").length; @@ -92,15 +96,39 @@ export function examplesProgram(fences: readonly Fence[]): { /** `tsc --pretty false` prints `(,): error TS: `, continuation lines indented. */ const DIAGNOSTIC = /^(.+?)\((\d+),(\d+)\): error (TS\d+): (.*)$/; +/** + * Whether the program imports `module` (a static import, a re-export, a dynamic import, each spelled as the page + * spells it: `from`, or `import` and an optional parenthesis, then the quoted path); a string value naming the path + * is not a dependency. + */ +function importsModule(program: string, module: string): boolean { + const quoted = JSON.stringify(module); + for (let at = program.indexOf(quoted); at !== -1; at = program.indexOf(quoted, at + 1)) { + if (/\b(?:from|import)\s*\(?\s*$/.test(program.slice(0, at))) { + return true; + } + } + return false; +} + /** * Every diagnostic tsc reports for the page's fences, as `