Files
sweet/tests/doc_examples_test.tsx
T
Christian Svensson b7f1abb0d0 test(docs): type check docs code examples (#3180)
This PR adds the initial seeds of type checking the code in all code
blocks for the Markdown Docs.

- Adds a unit test, disabled due to the many remaining errors, that can
help find incorrect code
- Fixes a bunch of syntax errors, type errors, and other code issues
found in the docs

## Future improvements

A lot of remaining issues is mostly related to missing imports, e.g.
`App`, `staticFiles`, `HttpError` etc. There's a couple of different
directions to solve this in future PR's.

- Add explicit imports to all docs examples (may become unnecessarily
verbose)
- Add code to docs code blocks, but with a hint to remove it when
rendering the markdown as HTML
   ```tsx
   import { App } from "fresh";
   // #docs-start

   // Everything above marker above is trimmed away
   const app = new App();
   ```
- Add a `types.d.ts` with "global" types used during type checking, so
globals, imports and so on can be handled globally

I do think that some examples probably should be more explicit with
imports, but it would be overkill for a lot of places such as `app.md`
and `context.md`.
2025-08-05 23:56:04 +02:00

110 lines
3.4 KiB
TypeScript

import twDenoJson from "../plugin-tailwindcss/deno.json" with { type: "json" };
import * as Marked from "marked";
import { ensureDir, walk } from "@std/fs";
import { dirname, join, relative } from "@std/path";
// import { expect } from "@std/expect/expect";
import { withTmpDir } from "../src/test_utils.ts";
import { FRESH_VERSION, PREACT_VERSION } from "../update/src/update.ts";
Deno.test("Docs Code example checks", async () => {
await using tmp = await withTmpDir();
for await (const { path, code } of docsMarkdownFiles()) {
const codePath = join(tmp.dir, path);
await ensureDir(dirname(codePath));
await Deno.writeTextFile(codePath, code);
}
const denoJson = {
lock: false,
imports: {
fresh: `jsr:@fresh/core@${FRESH_VERSION}`,
"@fresh/plugin-tailwind-v3":
`jsr:@fresh/plugin-tailwind@^${twDenoJson.version}`,
"@fresh/plugin-tailwind":
`jsr:@fresh/plugin-tailwind@^${twDenoJson.version}`,
preact: `npm:preact@^${PREACT_VERSION}`,
"@deno/gfm": "jsr:@deno/gfm@^0.11.0",
"@std/expect": "jsr:@std/expect@^1.0.16",
},
compilerOptions: {
lib: ["dom", "dom.asynciterable", "deno.ns", "deno.unstable"],
jsx: "precompile",
jsxImportSource: "preact",
jsxPrecompileSkipElements: ["a", "img", "source", "body", "html", "head"],
},
};
await Deno.writeTextFile(
join(tmp.dir, "deno.json"),
JSON.stringify(denoJson, undefined, 2),
);
// Download and cache all dependencies (reduces `stdout` noise)
await new Deno.Command(Deno.execPath(), {
args: ["cache", "**/*"],
cwd: tmp.dir,
}).output();
const { stdout, stderr } = await new Deno.Command(Deno.execPath(), {
args: ["check", "**/*"],
cwd: tmp.dir,
}).output();
const decoder = new TextDecoder();
const output = `${decoder.decode(stdout)}\n${decoder.decode(stderr)}`;
// Log `deno check` output (can be removed if expects below are enabled)
// deno-lint-ignore no-console
console.log(output);
// TODO: Enable after fixing docs check issues
// expect(code).toBe(0);
// expect(output).toBe("");
});
async function* docsMarkdownFiles() {
// Limit to checking Fresh v2 (canary) docs for now
const docsDir = join(import.meta.dirname!, "..", "docs", "canary");
const docsIter = walk(docsDir, { exts: [".md"], includeDirs: false });
for await (const entry of docsIter) {
for (const { file, code } of await extractTsCode(entry.path)) {
const path = join(relative(docsDir, entry.path), file);
yield { path, code };
}
}
}
async function extractTsCode(path: string) {
const code: { file: string; code: string }[] = [];
const input = await Deno.readTextFile(path);
let index = 0;
const tokens = await Marked.lexer(input, { gfm: true, async: true });
Marked.walkTokens(tokens, (token) => {
// Get rid of `Marked.Tokens.Generic`
const t = token as Marked.MarkedToken;
if (t.type !== "code") return;
const result = /^([tj]sx?)\s*/.exec(t.lang ?? "");
if (!result) return;
// Codeblock must be TS/JS
index += 1;
// Probably a filename, but perhaps a title,
// so get rid of non-file safe characters
const [match, ext] = result;
let file = t.lang!.slice(match.length)
.toLocaleLowerCase()
.replaceAll(/[^a-z0-9._\-\/\\]/g, "_")
.replaceAll(/_{2,}/g, "_") || String(index);
if (!file.endsWith(ext)) file += `.${ext}`;
code.push({ file, code: t.text });
});
return code;
}