Skip to content

Commit 41726f8

Browse files
authored
fix: ignore module-not-found errors in doc-check (#1701)
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.
1 parent 9bb06f9 commit 41726f8

3 files changed

Lines changed: 39 additions & 2 deletions

File tree

src/build/index.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,11 @@ const tasks = new Listr([
9393
if (ctx.bundlesize) {
9494
const gzip = await gzipSize(outfile)
9595
const maxsize = bytes(ctx.bundlesizeMax)
96+
97+
if (maxsize == null) {
98+
throw new Error(`Could not parse bytes from "${ctx.bundlesizeMax}"`)
99+
}
100+
96101
const diff = gzip - maxsize
97102

98103
task.output = 'Use https://esbuild.github.io/analyze/ to load "./dist/stats.json".'

src/document-check.js

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,16 @@ import { formatCode, formatError, fromRoot, hasTsconfig, readJson } from './util
1111
* @typedef {import("./types").GlobalOptions} GlobalOptions
1212
* @typedef {import("./types").DocsVerifierOptions} DocsVerifierOptions
1313
* @typedef {import("listr").ListrTaskWrapper} Task
14+
* @typedef {import("ts-node").TSError} TSError
1415
*/
1516

17+
/**
18+
* A list of tsc errors to ignore when compiling code snippets in documentation.
19+
*/
20+
const TS_ERRORS_TO_SUPRESS = [
21+
2307 // Cannot find module '...' or its corresponding type declarations
22+
]
23+
1624
const tasks = new Listr(
1725
[
1826
{
@@ -54,7 +62,8 @@ const tasks = new Listr(
5462
target: 'esnext',
5563
module: 'esnext',
5664
noImplicitAny: true,
57-
noEmit: true
65+
noEmit: true,
66+
skipLibCheck: true
5867
}
5968
}
6069
])
@@ -64,6 +73,15 @@ const tasks = new Listr(
6473

6574
results.forEach((result) => {
6675
if (result.error) {
76+
// ignore some diagnostic codes
77+
if (isTSError(result.error)) {
78+
const diagnosticCodes = result.error?.diagnosticCodes?.filter(code => !TS_ERRORS_TO_SUPRESS.includes(code))
79+
80+
if (diagnosticCodes.length === 0) {
81+
return
82+
}
83+
}
84+
6785
process.exitCode = 1
6886
console.log(kleur.red().bold(`Error compiling example code block ${result.index} in file ${result.file}:`))
6987
console.log(formatError(result.error))
@@ -89,3 +107,12 @@ const tasks = new Listr(
89107
)
90108

91109
export default tasks
110+
111+
/**
112+
*
113+
* @param {*} err
114+
* @returns {err is TSError}
115+
*/
116+
function isTSError (err) {
117+
return Array.isArray(err.diagnosticCodes)
118+
}
Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,8 @@
11
```ts
2-
export const a = 1;
2+
import { foo } from 'dep-we-do-not-have'
3+
4+
// should not cause an error because we ignore TS2307
5+
foo()
6+
7+
export const a = 1
38
```

0 commit comments

Comments
 (0)