Skip to content

Commit

Permalink
fix: d.ts pre emit diagnostics lost after compilation (#701)
Browse files Browse the repository at this point in the history
* fix: d.ts pre emit diagnostics lost after compilation

* refactor: handle no loc error from ts program

* test: correct tsconfig for build cases

* test: ignore coverage for dts error
  • Loading branch information
PeachScript committed Aug 31, 2023
1 parent 9ab6ff3 commit 6388306
Show file tree
Hide file tree
Showing 12 changed files with 39 additions and 27 deletions.
44 changes: 31 additions & 13 deletions src/builder/bundless/dts/index.ts
Expand Up @@ -188,19 +188,37 @@ export default async function getDeclarations(
});

// check compile error
// istanbul-ignore-if
if (result.diagnostics.length) {
result.diagnostics.forEach((d) => {
const loc = ts.getLineAndCharacterOfPosition(d.file!, d.start!);
const rltPath = winPath(path.relative(opts.cwd, d.file!.fileName));
const errMsg = ts.flattenDiagnosticMessageText(d.messageText, '\n');

logger.error(
`${chalk.blueBright(rltPath)}:${
// correct line number & column number, ref: https://github.com/microsoft/TypeScript/blob/93f2d2b9a1b2f8861b49d76bb5e58f6e9f2b56ee/src/compiler/tracing.ts#L185
`${chalk.yellow(loc.line + 1)}:${chalk.yellow(loc.character + 1)}`
} - ${chalk.gray(`TS${d.code}:`)} ${errMsg}`,
);
// ref: https://github.com/microsoft/TypeScript/wiki/Using-the-Compiler-API#a-minimal-compiler
const diagnostics = ts
.getPreEmitDiagnostics(incrProgram.getProgram())
.concat(result.diagnostics);

/* istanbul ignore if -- @preserve */
if (diagnostics.length) {
diagnostics.forEach((d) => {
const fragments: string[] = [];

// show file path & line number
if (d.file && d.start) {
const rltPath = winPath(path.relative(opts.cwd, d.file!.fileName));
const loc = ts.getLineAndCharacterOfPosition(d.file!, d.start!);

fragments.push(
`${chalk.blueBright(rltPath)}:${
// correct line number & column number, ref: https://github.com/microsoft/TypeScript/blob/93f2d2b9a1b2f8861b49d76bb5e58f6e9f2b56ee/src/compiler/tracing.ts#L185
`${chalk.yellow(loc.line + 1)}:${chalk.yellow(
loc.character + 1,
)} -`
}`,
);
}

// show error code
fragments.push(chalk.gray(`TS${d.code}:`));
// show error message
fragments.push(ts.flattenDiagnosticMessageText(d.messageText, '\n'));

logger.error(fragments.join(' '));
});
throw new Error('Declaration generation failed.');
}
Expand Down
1 change: 0 additions & 1 deletion tests/fixtures/build/bundle-targets/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-babel-config/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-babel-targets/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-define/tsconfig.json

This file was deleted.

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-ignores/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-overrides/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-swc-targets/tsconfig.json

This file was deleted.

1 change: 0 additions & 1 deletion tests/fixtures/build/bundless-transformer/tsconfig.json

This file was deleted.

5 changes: 0 additions & 5 deletions tests/fixtures/build/donocopy-public/tsconfig.json

This file was deleted.

8 changes: 8 additions & 0 deletions tests/fixtures/build/tsconfig.json
@@ -0,0 +1,8 @@
{
"extends": "../../../tsconfig.json",
"compilerOptions": {
"rootDir": ".",
"jsx": "react"
},
"include": ["**/*.ts", "**/*.tsx"]
}

0 comments on commit 6388306

Please sign in to comment.