Skip to content

Commit

Permalink
feat: ability to filter typescript diagnostics (#314)
Browse files Browse the repository at this point in the history
  • Loading branch information
dsherret committed Jun 1, 2023
1 parent fc8a606 commit 45d5131
Show file tree
Hide file tree
Showing 3 changed files with 58 additions and 4 deletions.
25 changes: 24 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -123,7 +123,7 @@ await build({
});
```

### Type checking both ESM and script output
### Type Checking Both ESM and Script Output

By default, only the ESM output will be type checked for performance reasons.
That said, it's recommended to type check both the ESM and the script (CJS/UMD)
Expand All @@ -136,6 +136,29 @@ await build({
});
```

### Ignoring Specific Type Checking Errors

Sometimes you may be getting a TypeScript error that is not helpful and you want
to ignore it. This is possible by using the `filterDiagnostic` option:

```ts
await build({
// ...etc...
filterDiagnostic(diagnostic) {
if (
diagnostic.file?.fileName.endsWith("fmt/colors.ts")
) {
return false; // ignore all diagnostics in this file
}
// etc... more checks here
return true;
},
});
```

This is especially useful for ignoring type checking errors in remote
dependencies.

### Top Level Await

Top level await doesn't work in CommonJS/UMD and dnt will error if a top level
Expand Down
11 changes: 8 additions & 3 deletions mod.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ export interface BuildOptions {
* @default "npm"
*/
packageManager?: "npm" | "yarn" | "pnpm" | string;
/** Optional compiler options. */
/** Optional TypeScript compiler options. */
compilerOptions?: {
/** Uses tslib to import helper functions once per project instead of including them per-file if necessary.
* @default false
Expand Down Expand Up @@ -163,6 +163,10 @@ export interface BuildOptions {
emitDecoratorMetadata?: boolean;
useUnknownInCatchVariables?: boolean;
};
/** Filter out diagnostics that you want to ignore during type checking and emitting.
* @returns `true` to surface the diagnostic or `false` to ignore it.
*/
filterDiagnostic?: (diagnostic: ts.Diagnostic) => boolean;
/** Action to do after emitting and before running tests. */
postBuild?: () => void | Promise<void>;
}
Expand Down Expand Up @@ -440,7 +444,7 @@ export async function build(options: BuildOptions): Promise<void> {
log(`Type checking ${current}...`);
const diagnostics = filterDiagnostics(
ts.getPreEmitDiagnostics(program),
);
).filter((d) => options.filterDiagnostic?.(d) ?? true);
if (diagnostics.length > 0) {
outputDiagnostics(diagnostics);
throw new Error(`Had ${diagnostics.length} diagnostics.`);
Expand All @@ -458,7 +462,8 @@ export async function build(options: BuildOptions): Promise<void> {
// 1343: The_import_meta_meta_property_is_only_allowed_when_the_module_option_is_es2020_es2022_esnext_system_node16_or_nodenext
d.code !== 1343 &&
// 1470: The_import_meta_meta_property_is_not_allowed_in_files_which_will_build_into_CommonJS_output
d.code !== 1470
d.code !== 1470 &&
(options.filterDiagnostic?.(d) ?? true)
);
}

Expand Down
26 changes: 26 additions & 0 deletions tests/integration.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -936,6 +936,32 @@ Deno.test("should build and type check node types project", async () => {
});
});

Deno.test("should have the ability to ignore type checking errors", async () => {
const foundDiagnostics: unknown[] = [];
await runTest("node_types_project", {
scriptModule: false,
test: false,
entryPoints: ["main.ts"],
outDir: "./npm",
shims: {
// see issue 185
custom: [{
globalNames: ["TextEncoder", "TextDecoder"],
module: "util",
}],
},
package: {
name: "node_types",
version: "0.0.0",
},
filterDiagnostic(diagnostic) {
foundDiagnostics.push(diagnostic);
return false;
},
});
assertEquals(foundDiagnostics.length, 5);
});

Deno.test("should build and type check declaration import project", async () => {
await runTest("declaration_import_project", {
test: false,
Expand Down

0 comments on commit 45d5131

Please sign in to comment.