Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: ability to filter typescript diagnostics #314

Merged
merged 3 commits into from
Jun 1, 2023
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
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