Skip to content

Commit

Permalink
fix: ignore module-not-found errors in doc-check (#1701)
Browse files Browse the repository at this point in the history
Sometimes we want to show examples that use modules that aren't a
dependency of the module we are documenting.

In this case tsc throws `TS2307` when trying to compile the example
code block.

Ignore this error so we can just treat these deps as `any` while
still getting the benefit of compiling the rest of the code block.
  • Loading branch information
achingbrain authored Dec 5, 2024
1 parent 9bb06f9 commit 41726f8
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 2 deletions.
5 changes: 5 additions & 0 deletions src/build/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -93,6 +93,11 @@ const tasks = new Listr([
if (ctx.bundlesize) {
const gzip = await gzipSize(outfile)
const maxsize = bytes(ctx.bundlesizeMax)

if (maxsize == null) {
throw new Error(`Could not parse bytes from "${ctx.bundlesizeMax}"`)
}

const diff = gzip - maxsize

task.output = 'Use https://esbuild.github.io/analyze/ to load "./dist/stats.json".'
Expand Down
29 changes: 28 additions & 1 deletion src/document-check.js
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,16 @@ import { formatCode, formatError, fromRoot, hasTsconfig, readJson } from './util
* @typedef {import("./types").GlobalOptions} GlobalOptions
* @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions
* @typedef {import("listr").ListrTaskWrapper} Task
* @typedef {import("ts-node").TSError} TSError
*/

/**
* A list of tsc errors to ignore when compiling code snippets in documentation.
*/
const TS_ERRORS_TO_SUPRESS = [
2307 // Cannot find module '...' or its corresponding type declarations
]

const tasks = new Listr(
[
{
Expand Down Expand Up @@ -54,7 +62,8 @@ const tasks = new Listr(
target: 'esnext',
module: 'esnext',
noImplicitAny: true,
noEmit: true
noEmit: true,
skipLibCheck: true
}
}
])
Expand All @@ -64,6 +73,15 @@ const tasks = new Listr(

results.forEach((result) => {
if (result.error) {
// ignore some diagnostic codes
if (isTSError(result.error)) {
const diagnosticCodes = result.error?.diagnosticCodes?.filter(code => !TS_ERRORS_TO_SUPRESS.includes(code))

if (diagnosticCodes.length === 0) {
return
}
}

process.exitCode = 1
console.log(kleur.red().bold(`Error compiling example code block ${result.index} in file ${result.file}:`))
console.log(formatError(result.error))
Expand All @@ -89,3 +107,12 @@ const tasks = new Listr(
)

export default tasks

/**
*
* @param {*} err
* @returns {err is TSError}
*/
function isTSError (err) {
return Array.isArray(err.diagnosticCodes)
}
7 changes: 6 additions & 1 deletion test/fixtures/document-check/pass/GOODREADME.md
Original file line number Diff line number Diff line change
@@ -1,3 +1,8 @@
```ts
export const a = 1;
import { foo } from 'dep-we-do-not-have'

// should not cause an error because we ignore TS2307
foo()

export const a = 1
```

0 comments on commit 41726f8

Please sign in to comment.