|
| 1 | +/** |
| 2 | + * TypeScript removes normal comments when it creates declaration files. |
| 3 | + * |
| 4 | + * That means that `// @ts-ignore` comments are removed, which can lead to |
| 5 | + * unwanted errors in the generated declaration files, if those comments |
| 6 | + * are still necessary in the generated `.d.ts` files. |
| 7 | + * |
| 8 | + * As a workaround, it's possible to use `/** @ts-ignore ...` instead, |
| 9 | + * as these are preserved in the generated declaration files. |
| 10 | + * The downside to this is that these comments then also might end up in |
| 11 | + * IDE inline documentation, which we want to avoid. |
| 12 | + * |
| 13 | + * This build step post-processes all `.d.ts` files if a block-comment |
| 14 | + * starts with `* @ts-ignore`, it removes the leading `*`, effectively |
| 15 | + * turning them into non-docBlock comments for shipping. |
| 16 | + */ |
| 17 | + |
| 18 | +import { visit } from "recast"; |
| 19 | + |
| 20 | +import type { BuildStep } from "./build.ts"; |
| 21 | +import { applyRecast, frameComment } from "./helpers.ts"; |
| 22 | + |
| 23 | +export const preserveTsIgnore: BuildStep = async (options) => |
| 24 | + applyRecast({ |
| 25 | + glob: `**/*.{${options.jsExt},d.${options.tsExt}}`, |
| 26 | + cwd: options.targetDir, |
| 27 | + transformStep({ ast }) { |
| 28 | + return { |
| 29 | + ast: visit(ast, { |
| 30 | + visitNode(path) { |
| 31 | + this.traverse(path); |
| 32 | + const node = path.node; |
| 33 | + |
| 34 | + if (!node.comments) { |
| 35 | + return; |
| 36 | + } |
| 37 | + |
| 38 | + for (const comment of node.comments) { |
| 39 | + if ( |
| 40 | + comment.type === "CommentBlock" && |
| 41 | + comment.value.match(/^\*\s*@ts-ignore/) |
| 42 | + ) { |
| 43 | + comment.value = comment.value.substring(1); |
| 44 | + } |
| 45 | + } |
| 46 | + }, |
| 47 | + }), |
| 48 | + }; |
| 49 | + }, |
| 50 | + }); |
0 commit comments