Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 6 additions & 3 deletions website/DOCS_AUTHORING.md
Original file line number Diff line number Diff line change
Expand Up @@ -24,9 +24,12 @@ and the docs say what is true today.
`src/content/docs/samples.test.ts` typechecks every `ts` and `tsx` code block and demo
source against that package, so a renamed prop or a changed return fails the test. A sample
with no import gets the adaptv names it uses; JSX or a `return` with no component around it
is wrapped in one; a name you never declare (`rows`, `Title`) is a placeholder. A block that
still cannot compile (a route file, a service worker, a snippet cut mid-tag) takes
`partial: true`. Fix the sample first; the test caps how many blocks may be partial.
is wrapped in one; a name you never declare (`rows`, `Title`) is a placeholder. A route file
is checked against the route ids listed in `samples.test.ts` (`ROUTE_IDS`): add the id your
sample uses. A module that imports `adaptv/sw` is checked against the worker lib, so it
declares `self` as `ServiceWorkerGlobalScope`. A block that still cannot compile (a snippet cut
mid-tag) takes `partial: true`. Fix the sample first; the test allows no partial block today,
so a new one means raising that cap with the reason in the PR.
- JSDoc in the source is the best starting text. Rewrite it for a reader who has never seen the
repo: drop decision-register ids (L20, O7, B9), internal file paths and history.
- The engines underneath (the router, the native shell) can be named when it helps someone debug,
Expand Down
3 changes: 2 additions & 1 deletion website/src/content/docs/pages/hooks-updates.ts
Original file line number Diff line number Diff line change
Expand Up @@ -133,9 +133,10 @@ export default defineApp({
type: "code",
label: "src/sw/push.ts",
lang: "ts",
partial: true,
code: `import { onAppMessage, sendToApp } from "adaptv/sw"

declare const self: ServiceWorkerGlobalScope

self.addEventListener("push", (event) => {
event.waitUntil(sendToApp({ type: "push", body: event.data?.text() }))
})
Expand Down
11 changes: 9 additions & 2 deletions website/src/content/docs/pages/list.ts
Original file line number Diff line number Diff line change
Expand Up @@ -134,8 +134,15 @@ const rows: Row[] = Array.from({ length: 10_000 }, (_, index) => ({
type: "code",
label: "feed.tsx",
lang: "tsx",
partial: true,
code: `const loadMore = useCallback(async () => {
code: `import { List } from "adaptv/components"
import { useCallback, useRef, useState } from "react"

type Post = { id: string; title: string }

const [posts, setPosts] = useState<Post[]>([])
const loading = useRef(false)

const loadMore = useCallback(async () => {
if (loading.current) return
loading.current = true
const next = await fetchPosts({ after: posts.at(-1)?.id })
Expand Down
5 changes: 3 additions & 2 deletions website/src/content/docs/pages/pull-to-refresh.ts
Original file line number Diff line number Diff line change
Expand Up @@ -104,12 +104,13 @@ const scrollRef = useRef<HTMLDivElement>(null)
type: "code",
label: "dark-indicator.tsx",
lang: "tsx",
partial: true,
code: `<PullToRefresh
onRefresh={refresh}
scrollContainerRef={scrollRef}
className="overflow-hidden [&_svg]:text-gray-950 dark:[&_svg]:text-white"
>`,
>
<ScrollView ref={scrollRef}>{rows}</ScrollView>
</PullToRefresh>`,
},
{
type: "p",
Expand Down
1 change: 0 additions & 1 deletion website/src/content/docs/pages/quick-start.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,6 @@ export default rootRoute([index("pages/home.page.tsx")])`,
type: "code",
label: "home.page.tsx",
lang: "tsx",
partial: true,
code: `import { ScrollView, View } from "adaptv/components"
import { createFileRoute } from "adaptv/router"

Expand Down
1 change: 0 additions & 1 deletion website/src/content/docs/pages/rendering.ts
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ export const page: DocPage = {
type: "code",
label: "src/routing/pages/orders.page.tsx",
lang: "tsx",
partial: true,
code: `import { createFileRoute } from "adaptv/router"

export const Route = createFileRoute("/orders")({
Expand Down
9 changes: 5 additions & 4 deletions website/src/content/docs/pages/routing.ts
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,6 @@ export default rootRoute([
type: "code",
label: "src/routing/pages/docs/doc.page.tsx",
lang: "tsx",
partial: true,
code: `import { createFileRoute, notFound } from "adaptv/router"

export const Route = createFileRoute("/_providers/docs/$slug")({
Expand Down Expand Up @@ -80,7 +79,6 @@ function DocRoute() {
type: "code",
label: "src/routing/layouts/providers.layout.tsx",
lang: "tsx",
partial: true,
code: `import { createFileRoute, Outlet } from "adaptv/router"
import AppProviders from "@/providers/app-providers"

Expand Down Expand Up @@ -137,8 +135,11 @@ export const Nav = () => (
type: "code",
label: "adaptv.config.ts",
lang: "ts",
partial: true,
code: `notFoundScreen: () => import("@/components/not-found-screen"),`,
code: `import { defineApp } from "adaptv/config"

export default defineApp({
notFoundScreen: () => import("@/components/not-found-screen"),
})`,
},
{ type: "h2", text: "Eject the router or the client entry" },
{
Expand Down
1 change: 0 additions & 1 deletion website/src/content/docs/pages/theming.ts
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,6 @@ function ThemeToggle() {
type: "code",
label: "src/routing/pages/settings.page.tsx",
lang: "tsx",
partial: true,
code: `export const Route = createFileRoute("/settings")({
chromeTint: "#1e0033",
component: Settings,
Expand Down
50 changes: 40 additions & 10 deletions website/src/content/docs/samples.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,10 @@ vi.mock("@/assets/captures/ios-list.png?adaptv-image", () => ({ default: {} }))
//small: a sample with no import at all gets the adaptv imports it uses, statements and
//JSX with no component around them are put in one, and a name the sample never declares
//and adaptv does not export is a placeholder (`rows`, `Title`). A fragment that still
//cannot compile sets `partial: true` on its block and is skipped: a route file (the ids
//`createFileRoute` accepts come from the app's generated route tree, which a docs page
//does not have), a service worker (it needs the worker's types) or a snippet cut mid-tag.
//cannot compile sets `partial: true` on its block and is skipped, such as a snippet cut
//mid-tag. A route file is checked against a short list of route ids (`ROUTE_IDS`, the
//generated route tree a docs page does not have), and a module that imports `adaptv/sw`
//against the worker lib instead of the DOM one.
//Needs `pnpm build` at the root first, like every website test.

type Sample = { id: string; ext: "ts" | "tsx"; code: string }
Expand Down Expand Up @@ -164,7 +165,15 @@ const options: ts.CompilerOptions = {
const VIRTUAL_DIR = resolve(".docs-samples")
const virtualPath = ({ id, ext }: Sample) => join(VIRTUAL_DIR, `${id}.${ext}`)

//What a real app has and a sample may lean on: the ambient `createFileRoute`.
//What a real app has and a sample may lean on: the ambient `createFileRoute`, and the
//route ids its generated route tree would hold (`createFileRoute` accepts only those).
const ROUTE_IDS = [
"/",
"/orders",
"/settings",
"/_providers",
"/_providers/docs/$slug",
]
const PRELUDE = join(VIRTUAL_DIR, "prelude.d.ts")
const files = new Map<string, string>([
[
Expand All @@ -175,6 +184,9 @@ declare module "adaptv/router" {
interface Register {
router: AdaptvRouter
}
interface FileRoutesByPath {
${ROUTE_IDS.map((id) => ` "${id}": { id: "${id}"; path: "${id}"; fullPath: "${id}"; preLoaderRoute: any; parentRoute: any }`).join("\n")}
}
}
`,
],
Expand All @@ -192,7 +204,24 @@ host.getSourceFile = (path, languageVersion, ...rest) => {
: ts.createSourceFile(path, text, languageVersion, true)
}

const program = ts.createProgram([...files.keys()], options, host)
//a service worker module runs where `self` is the worker scope: its own program, with
//the worker lib in place of the DOM one
const isWorker = (sample: Sample) => /from "adaptv\/sw"/.test(sample.code)
const workerOptions: ts.CompilerOptions = {
...options,
lib: ["lib.es2022.d.ts", "lib.webworker.d.ts"],
}
const workerSamples = SAMPLES.filter(isWorker).map(virtualPath)
const program = ts.createProgram(
[...files.keys()].filter((path) => !workerSamples.includes(path)),
options,
host,
)
const workerProgram = ts.createProgram(
[PRELUDE, ...workerSamples],
workerOptions,
host,
)

//Not mistakes in a fragment: a name it never declares (2304, 2552) when adaptv does not
//export it, what that makes implicit any (7005, 7006, 7031), and the app's own modules.
Expand All @@ -207,11 +236,12 @@ function isPlaceholder(diagnostic: ts.Diagnostic): boolean {

/** What the compiler says about one sample, one line each, with its position. */
function problems(sample: Sample): string[] {
const file = program.getSourceFile(virtualPath(sample))
const owner = isWorker(sample) ? workerProgram : program
const file = owner.getSourceFile(virtualPath(sample))
if (file === undefined) return ["the sample was not compiled"]
return [
...program.getSyntacticDiagnostics(file),
...program.getSemanticDiagnostics(file),
...owner.getSyntacticDiagnostics(file),
...owner.getSemanticDiagnostics(file),
]
.filter((diagnostic) => !isPlaceholder(diagnostic))
.map((diagnostic) => {
Expand Down Expand Up @@ -257,11 +287,11 @@ describe("samples", () => {
})

//`partial` is for what cannot compile, not for what is inconvenient to fix
it("has few blocks marked partial", () => {
it("has no block marked partial", () => {
const partial = ALL_DOCS.flatMap((page) => page.blocks).filter(
(block) => "partial" in block && block.partial === true,
)
expect(partial.length).toBeLessThanOrEqual(12)
expect(partial.length).toBe(0)
})

//a walker that stops finding blocks would pass every page below without checking anything
Expand Down
Loading