Skip to content

Commit 8d3a335

Browse files
authored
feat: warn on likely failed d.ts file generation (#2428)
emitDts now warns when it detects diagnostics that likely mean the `d.ts` file was not generated
1 parent 1afe1d5 commit 8d3a335

File tree

1 file changed

+28
-1
lines changed

1 file changed

+28
-1
lines changed

packages/svelte2tsx/src/emitDts.ts

Lines changed: 28 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,34 @@ export async function emitDts(config: EmitDtsConfig) {
1313
const { options, filenames } = loadTsconfig(config, svelteMap);
1414
const host = await createTsCompilerHost(options, svelteMap);
1515
const program = ts.createProgram(filenames, options, host);
16-
program.emit();
16+
const result = program.emit();
17+
const likely_failed_files = result.diagnostics.filter((diagnostic) => {
18+
// List of errors which hint at a failed d.ts generation
19+
// https://github.com/microsoft/TypeScript/blob/main/src/compiler/diagnosticMessages.json
20+
return diagnostic.code === 2527 || (diagnostic.code >= 4000 && diagnostic.code <= 4108);
21+
});
22+
23+
if (likely_failed_files.length > 0) {
24+
const failed_by_file = new Map<string, string[]>();
25+
likely_failed_files.forEach((diagnostic) => {
26+
const file = diagnostic.file?.fileName;
27+
if (file) {
28+
const errors = failed_by_file.get(file) || [];
29+
errors.push(ts.flattenDiagnosticMessageText(diagnostic.messageText, '\n'));
30+
failed_by_file.set(file, errors);
31+
}
32+
});
33+
console.warn(
34+
'd.ts type declaration files for the following files were likely not generated due to the following errors:'
35+
);
36+
console.warn(
37+
[...failed_by_file.entries()]
38+
.map(([file, errors]) => {
39+
return `${file}\n${errors.map((error) => ` - ${error}`).join('\n')}`;
40+
})
41+
.join('\n')
42+
);
43+
}
1744
}
1845

1946
function loadTsconfig(config: EmitDtsConfig, svelteMap: SvelteMap) {

0 commit comments

Comments
 (0)