From 8848a4790dfb20bfc4fe5de300a611903b88fbdb Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 29 Aug 2023 11:24:05 +0800 Subject: [PATCH 1/4] fix: d.ts pre emit diagnostics lost after compilation --- src/builder/bundless/dts/index.ts | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/src/builder/bundless/dts/index.ts b/src/builder/bundless/dts/index.ts index df539d69..d5c5c039 100644 --- a/src/builder/bundless/dts/index.ts +++ b/src/builder/bundless/dts/index.ts @@ -188,9 +188,14 @@ export default async function getDeclarations( }); // check compile error + // 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 - if (result.diagnostics.length) { - result.diagnostics.forEach((d) => { + if (diagnostics.length) { + 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'); From 4e290d61abb4868ebc9ccf40bf3274c0a4928238 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 29 Aug 2023 12:20:52 +0800 Subject: [PATCH 2/4] refactor: handle no loc error from ts program --- src/builder/bundless/dts/index.ts | 33 +++++++++++++++++++++---------- 1 file changed, 23 insertions(+), 10 deletions(-) diff --git a/src/builder/bundless/dts/index.ts b/src/builder/bundless/dts/index.ts index d5c5c039..d047d60d 100644 --- a/src/builder/bundless/dts/index.ts +++ b/src/builder/bundless/dts/index.ts @@ -196,16 +196,29 @@ export default async function getDeclarations( // istanbul-ignore-if if (diagnostics.length) { 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}`, - ); + 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.'); } From 1b34db3878c5cab29f3ed47d03f8a9b4f8c61a98 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 29 Aug 2023 12:21:28 +0800 Subject: [PATCH 3/4] test: correct tsconfig for build cases --- tests/fixtures/build/bundle-targets/tsconfig.json | 1 - tests/fixtures/build/bundless-babel-config/tsconfig.json | 1 - tests/fixtures/build/bundless-babel-targets/tsconfig.json | 1 - tests/fixtures/build/bundless-define/tsconfig.json | 1 - .../fixtures/build/bundless-esbuild-targets/tsconfig.json | 1 - tests/fixtures/build/bundless-ignores/tsconfig.json | 1 - tests/fixtures/build/bundless-overrides/tsconfig.json | 1 - tests/fixtures/build/bundless-swc-targets/tsconfig.json | 1 - tests/fixtures/build/bundless-transformer/tsconfig.json | 1 - tests/fixtures/build/donocopy-public/tsconfig.json | 5 ----- tests/fixtures/build/tsconfig.json | 8 ++++++++ 11 files changed, 8 insertions(+), 14 deletions(-) delete mode 100644 tests/fixtures/build/bundle-targets/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-babel-config/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-babel-targets/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-define/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-esbuild-targets/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-ignores/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-overrides/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-swc-targets/tsconfig.json delete mode 100644 tests/fixtures/build/bundless-transformer/tsconfig.json delete mode 100644 tests/fixtures/build/donocopy-public/tsconfig.json create mode 100644 tests/fixtures/build/tsconfig.json diff --git a/tests/fixtures/build/bundle-targets/tsconfig.json b/tests/fixtures/build/bundle-targets/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundle-targets/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-babel-config/tsconfig.json b/tests/fixtures/build/bundless-babel-config/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-babel-config/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-babel-targets/tsconfig.json b/tests/fixtures/build/bundless-babel-targets/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-babel-targets/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-define/tsconfig.json b/tests/fixtures/build/bundless-define/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-define/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-esbuild-targets/tsconfig.json b/tests/fixtures/build/bundless-esbuild-targets/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-esbuild-targets/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-ignores/tsconfig.json b/tests/fixtures/build/bundless-ignores/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-ignores/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-overrides/tsconfig.json b/tests/fixtures/build/bundless-overrides/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-overrides/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-swc-targets/tsconfig.json b/tests/fixtures/build/bundless-swc-targets/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-swc-targets/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/bundless-transformer/tsconfig.json b/tests/fixtures/build/bundless-transformer/tsconfig.json deleted file mode 100644 index 0967ef42..00000000 --- a/tests/fixtures/build/bundless-transformer/tsconfig.json +++ /dev/null @@ -1 +0,0 @@ -{} diff --git a/tests/fixtures/build/donocopy-public/tsconfig.json b/tests/fixtures/build/donocopy-public/tsconfig.json deleted file mode 100644 index a224293f..00000000 --- a/tests/fixtures/build/donocopy-public/tsconfig.json +++ /dev/null @@ -1,5 +0,0 @@ -{ - "compilerOptions": { - "jsx": "react-jsx" - } -} diff --git a/tests/fixtures/build/tsconfig.json b/tests/fixtures/build/tsconfig.json new file mode 100644 index 00000000..57c4da7c --- /dev/null +++ b/tests/fixtures/build/tsconfig.json @@ -0,0 +1,8 @@ +{ + "extends": "../../../tsconfig.json", + "compilerOptions": { + "rootDir": ".", + "jsx": "react" + }, + "include": ["**/*.ts", "**/*.tsx"] +} From 46612ad769c1035be6706efb1cf8fc53b9275687 Mon Sep 17 00:00:00 2001 From: PeachScript Date: Tue, 29 Aug 2023 12:54:48 +0800 Subject: [PATCH 4/4] test: ignore coverage for dts error --- src/builder/bundless/dts/index.ts | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/builder/bundless/dts/index.ts b/src/builder/bundless/dts/index.ts index d047d60d..319d237f 100644 --- a/src/builder/bundless/dts/index.ts +++ b/src/builder/bundless/dts/index.ts @@ -193,7 +193,7 @@ export default async function getDeclarations( .getPreEmitDiagnostics(incrProgram.getProgram()) .concat(result.diagnostics); - // istanbul-ignore-if + /* istanbul ignore if -- @preserve */ if (diagnostics.length) { diagnostics.forEach((d) => { const fragments: string[] = [];