From 567eb63e9af8ec9c02286ce8430a0cbe1ccfe4ee Mon Sep 17 00:00:00 2001 From: Josh Parnham Date: Wed, 31 Aug 2022 10:00:59 +1000 Subject: [PATCH 1/2] Add allowJs test case --- tests/e2e/test-cases/allow-js/config.ts | 5 +++++ tests/e2e/test-cases/allow-js/hello.js | 3 +++ tests/e2e/test-cases/allow-js/input.ts | 5 +++++ tests/e2e/test-cases/allow-js/output.d.ts | 5 +++++ tests/e2e/test-cases/allow-js/tsconfig.json | 6 ++++++ 5 files changed, 24 insertions(+) create mode 100644 tests/e2e/test-cases/allow-js/config.ts create mode 100644 tests/e2e/test-cases/allow-js/hello.js create mode 100644 tests/e2e/test-cases/allow-js/input.ts create mode 100644 tests/e2e/test-cases/allow-js/output.d.ts create mode 100644 tests/e2e/test-cases/allow-js/tsconfig.json diff --git a/tests/e2e/test-cases/allow-js/config.ts b/tests/e2e/test-cases/allow-js/config.ts new file mode 100644 index 0000000..f1fb1a8 --- /dev/null +++ b/tests/e2e/test-cases/allow-js/config.ts @@ -0,0 +1,5 @@ +import { TestCaseConfig } from '../../test-cases/test-case-config'; + +const config: TestCaseConfig = {}; + +export = config; diff --git a/tests/e2e/test-cases/allow-js/hello.js b/tests/e2e/test-cases/allow-js/hello.js new file mode 100644 index 0000000..4544e17 --- /dev/null +++ b/tests/e2e/test-cases/allow-js/hello.js @@ -0,0 +1,3 @@ +const hello = { test: 1 }; + +export { hello } diff --git a/tests/e2e/test-cases/allow-js/input.ts b/tests/e2e/test-cases/allow-js/input.ts new file mode 100644 index 0000000..9e91fc8 --- /dev/null +++ b/tests/e2e/test-cases/allow-js/input.ts @@ -0,0 +1,5 @@ +import { hello } from "./hello.js"; + +export function test() { + return hello +} diff --git a/tests/e2e/test-cases/allow-js/output.d.ts b/tests/e2e/test-cases/allow-js/output.d.ts new file mode 100644 index 0000000..57c4d37 --- /dev/null +++ b/tests/e2e/test-cases/allow-js/output.d.ts @@ -0,0 +1,5 @@ +export declare function test(): { + test: number; +}; + +export {}; diff --git a/tests/e2e/test-cases/allow-js/tsconfig.json b/tests/e2e/test-cases/allow-js/tsconfig.json new file mode 100644 index 0000000..bf3a541 --- /dev/null +++ b/tests/e2e/test-cases/allow-js/tsconfig.json @@ -0,0 +1,6 @@ +{ + "extends": "../tsconfig.json", + "compilerOptions": { + "allowJs": true + } +} From b6bd34d1a4c981e102c353487ed2b29052702d51 Mon Sep 17 00:00:00 2001 From: Evgeniy Timokhov Date: Sun, 4 Sep 2022 12:27:26 +0100 Subject: [PATCH 2/2] Add `emitDeclarationOnly: true` flag to the compiler options It allows to use `allowJs` flag enabled and let the compiler generate types from js files too Fixes #220 --- src/compile-dts.ts | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/compile-dts.ts b/src/compile-dts.ts index b84c42c..db094a5 100644 --- a/src/compile-dts.ts +++ b/src/compile-dts.ts @@ -96,10 +96,14 @@ function changeExtensionToDts(fileName: string): string { function getDeclarationFiles(rootFiles: readonly string[], compilerOptions: ts.CompilerOptions): Map { // we must pass `declaration: true` and `noEmit: false` if we want to generate declaration files // see https://github.com/microsoft/TypeScript/issues/24002#issuecomment-550549393 + // also, we don't want to generate anything apart from declarations so that's why `emitDeclarationOnly: true` is here + // it allows to run the tool for projects with allowJs flag enabled to avoid errors like: + // error TS5055: Cannot write file '' because it would overwrite input file. compilerOptions = { ...compilerOptions, noEmit: false, declaration: true, + emitDeclarationOnly: true, }; const program = ts.createProgram(rootFiles, compilerOptions);