♻️ Drop assert imports from doc fences
`assert` is the sole injected exception: the generated test always binds it, so a fence must not import it. Revert the README imports and the generator's node:assert merging; the fences keep the assigned variables and `assert.equal` calls, and the generator again rejects a fence that imports node:assert.
This commit is contained in:
1 parent
d0dd8641f2
commit
a9b588e1c3
3 files changed
+11
-22
No files matched your search
@@ -46,14 +46,18 @@ const OUTPUT_DIR = "src/doc-test/__generated__";
|
||||
const TYPESCRIPT_LANGS: ReadonlySet<string> = new Set(["ts", "typescript"]);
|
||||
|
||||
/**
|
||||
* Modules the generated file already binds. An example that imports `node:test`
|
||||
* would collide with the prelude `test` binding, so it is surfaced as a fatal
|
||||
* error. `node:assert` is allowed: it is merged into the prelude assert import.
|
||||
* Modules the generated file already imports. An example that imports one of
|
||||
* these would collide with the prelude binding (duplicate `test` / `assert`), so
|
||||
* it is surfaced as a fatal error and the example is rewritten.
|
||||
*/
|
||||
const PRELUDE_MODULES: ReadonlySet<string> = new Set(["node:test"]);
|
||||
const PRELUDE_MODULES: ReadonlySet<string> = new Set([
|
||||
"node:assert",
|
||||
"node:test",
|
||||
]);
|
||||
|
||||
/** One line of the prelude every generated file starts with. */
|
||||
const PRELUDE_TEST = 'import { test } from "node:test";';
|
||||
const PRELUDE_ASSERT = 'import { strict as assert } from "node:assert";';
|
||||
|
||||
/** Indentation applied to every fence body line inside the `test` callback. */
|
||||
const INDENT = " ";
|
||||
@@ -321,17 +325,6 @@ const handleNode = (state: BuilderState, name: string, node: Block): void => {
|
||||
}
|
||||
};
|
||||
|
||||
/**
|
||||
* Guarantee `assert` is in scope in every generated file. The import is merged
|
||||
* into any `node:assert` import an example already declares, so an example can
|
||||
* stay self-contained without colliding with the harness.
|
||||
*/
|
||||
const ensurePreludeAssert = (named: Map<string, NamedImportGroup>): void => {
|
||||
addNamedImport(named, { source: "node:assert", isType: false }, [
|
||||
"strict as assert",
|
||||
]);
|
||||
};
|
||||
|
||||
/** Walk one Markdown file and collect its cases and hoisted imports. */
|
||||
const parseDoc = (
|
||||
name: string,
|
||||
@@ -346,7 +339,6 @@ const parseDoc = (
|
||||
for (const node of fromMarkdown(markdown).children) {
|
||||
handleNode(state, name, node);
|
||||
}
|
||||
ensurePreludeAssert(state.named);
|
||||
return {
|
||||
named: state.named,
|
||||
passthrough: state.passthrough,
|
||||
@@ -376,6 +368,7 @@ const buildHeader = (name: string, imports: readonly string[]): string[] => {
|
||||
`// Source: ${name}`,
|
||||
EMPTY,
|
||||
PRELUDE_TEST,
|
||||
PRELUDE_ASSERT,
|
||||
];
|
||||
if (imports.length > INITIAL_COUNT) {
|
||||
header.push(EMPTY, ...imports);
|
||||
|
||||
Reference in new issue
Block a user