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
5 changes: 5 additions & 0 deletions .changeset/examples-check.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'zile': patch
---

Added `zile examples:check [--fix]` to typecheck and format JSDoc examples.
43 changes: 43 additions & 0 deletions bun.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -15,8 +15,8 @@
"@changesets/cli": "^2.27.7",
"@types/node": "^24.7.1",
"publint": "0.3.14",
"typescript": "^5.9.3",
"type-fest": "^5.0.1",
"typescript": "^5.9.3",
"vitest": "^3.2.4"
},
"[!start-pkg]": "",
Expand Down Expand Up @@ -46,6 +46,7 @@
"dependencies": {
"@clack/prompts": "^1.0.0-alpha.6",
"cac": "6.7.14",
"oxfmt": "^0.51.0",
"tsconfck": "3.1.6"
},
"peerDependencies": {
Expand Down
1 change: 1 addition & 0 deletions src/Cli.ts
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ export async function run(options: run.Options): Promise<void> {
link: true,
})
commands.createNew(cli.command('new', 'Create a new zile project'))
commands.examplesCheck(cli.command('examples:check', 'Typecheck and format JSDoc examples'))
commands.preparePublish(cli.command('publish:prepare', 'Prepare package for publishing'))
commands.postPublish(cli.command('publish:post', 'Post-process package after publishing'))

Expand Down
208 changes: 208 additions & 0 deletions src/Examples.test.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,208 @@
import * as fs from 'node:fs/promises'
import * as os from 'node:os'
import * as path from 'node:path'
import * as Examples from './Examples.js'

const zileRoot = path.resolve(import.meta.dirname, '..')

async function setupProject(files: Record<string, string>): Promise<string> {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'zile-examples-'))
// Reuse zile's own typescript install via a symlinked node_modules.
await fs.symlink(
path.join(zileRoot, 'node_modules'),
path.join(tmp, 'node_modules'),
'dir',
)
await fs.writeFile(
path.join(tmp, 'tsconfig.json'),
JSON.stringify({
compilerOptions: {
target: 'ES2022',
module: 'ESNext',
moduleResolution: 'Bundler',
strict: true,
skipLibCheck: true,
},
}),
)
for (const [rel, contents] of Object.entries(files)) {
const target = path.join(tmp, rel)
await fs.mkdir(path.dirname(target), { recursive: true })
await fs.writeFile(target, contents)
}
return tmp
}

describe('Examples.extract', () => {
it('pulls fenced ts blocks out of a JSDoc comment', () => {
const source = [
'/**',
' * Adds two numbers.',
' *',
' * @example',
' * ```ts',
" * import { add } from 'mod'",
' * add(1, 2)',
' * ```',
' */',
'export function add(a: number, b: number) { return a + b }',
'',
].join('\n')

const blocks = Examples.extract({ file: '/virtual/add.ts', source })

expect(blocks).toHaveLength(1)
expect(blocks[0]?.code).toMatchInlineSnapshot(`
"import { add } from 'mod'
add(1, 2)"
`)
expect(blocks[0]?.fence).toBe('ts')
expect(blocks[0]?.startLine).toBe(5)
})

it('recognizes `ts twoslash` and `typescript` fences', () => {
const source = [
'/**',
' * ```ts twoslash',
' * const x = 1',
' * ```',
' *',
' * ```typescript',
' * const y = 2',
' * ```',
' */',
'export const v = 0',
'',
].join('\n')

const blocks = Examples.extract({ file: '/virtual/two.ts', source })

expect(blocks.map((b) => b.fence)).toEqual(['ts twoslash', 'typescript'])
})

it('ignores fenced code inside string and template literals', () => {
const source = [
'export const code = `',
'/**',
' * ```ts',
' * not a real example',
' * ```',
' */',
'`',
'',
].join('\n')

const blocks = Examples.extract({ file: '/virtual/literal.ts', source })

expect(blocks).toHaveLength(0)
})
})

describe('Examples.applyFixes', () => {
it('rewrites a snippet back into the original JSDoc comment', async () => {
const tmp = await fs.mkdtemp(path.join(os.tmpdir(), 'zile-examples-'))
const file = path.join(tmp, 'add.ts')
const original = [
'/**',
' * @example',
' * ```ts',
' * const x=1',
' * ```',
' */',
'export const v = 0',
'',
].join('\n')
await fs.writeFile(file, original)

const [block] = Examples.extract({ file, source: original })
if (!block) throw new Error('expected a block')

await Examples.applyFixes({ fixes: new Map([[block, 'const x = 1\n']]) })

const next = await fs.readFile(file, 'utf8')
expect(next).toMatchInlineSnapshot(`
"/**
* @example
* \`\`\`ts
* const x = 1
* \`\`\`
*/
export const v = 0
"
`)

await fs.rm(tmp, { recursive: true })
})
})

describe('Examples.check directives', () => {
it('skips blocks marked with `// @noErrors`', async () => {
const cwd = await setupProject({
'src/mod.ts': [
'/**',
' * @example',
' * ```ts',
' * // @noErrors',
' * const x: number = nope',
' * ```',
' */',
'export const v = 0',
'',
].join('\n'),
})
try {
const result = await Examples.check({ cwd, format: false })
expect(result.typeErrors).toEqual([])
} finally {
await fs.rm(cwd, { recursive: true, force: true })
}
})

it('skips blocks marked with `// @errors:`', async () => {
const cwd = await setupProject({
'src/mod.ts': [
'/**',
' * @example',
' * ```ts',
' * // @errors: 2304',
' * const x: number = nope',
' * ```',
' */',
'export const v = 0',
'',
].join('\n'),
})
try {
const result = await Examples.check({ cwd, format: false })
expect(result.typeErrors).toEqual([])
} finally {
await fs.rm(cwd, { recursive: true, force: true })
}
})

it('strips lines annotated with `// [!code --]` before type-checking', async () => {
const cwd = await setupProject({
'src/mod.ts': [
'/**',
' * @example',
' * ```ts',
" * import { join } from 'node:path' // [!code --]",
" * import { resolve } from 'node:path'",
' * const broken: number = nope // [!code --]',
' * const value: number = 1',
' * void resolve',
' * void value',
' * ```',
' */',
'export const v = 0',
'',
].join('\n'),
})
try {
const result = await Examples.check({ cwd, format: false })
expect(result.typeErrors).toEqual([])
} finally {
await fs.rm(cwd, { recursive: true, force: true })
}
})
})
Loading
Loading