From 8823770717bf451da2667b4d0d24b8915698856d Mon Sep 17 00:00:00 2001 From: Shinobu Hayashi Date: Mon, 20 Dec 2021 06:50:13 +0900 Subject: [PATCH] Fix: "Got unexpected undefined" error when using a re-exported type (#7424) --- .../re-exporting-aggregating/expected.d.ts | 4 +++ .../re-exporting-aggregating/hoge/bar.ts | 1 + .../re-exporting-aggregating/hoge/fuga.ts | 1 + .../re-exporting-aggregating/hoge/index.ts | 2 ++ .../re-exporting-aggregating/index.ts | 2 ++ .../re-exporting-aggregating/package.json | 6 +++++ .../re-exporting-aggregating/yarn.lock | 0 .../core/integration-tests/test/ts-types.js | 27 +++++++++++++++++++ .../typescript-types/src/TSModuleGraph.js | 5 +++- 9 files changed, 47 insertions(+), 1 deletion(-) create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/expected.d.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/bar.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/fuga.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/index.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/index.ts create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/package.json create mode 100644 packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/yarn.lock diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/expected.d.ts b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/expected.d.ts new file mode 100644 index 00000000000..f0d894a0e39 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/expected.d.ts @@ -0,0 +1,4 @@ +type fuga = "fuga"; +export type Baz = `${fuga}`; + +//# sourceMappingURL=types.d.ts.map diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/bar.ts b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/bar.ts new file mode 100644 index 00000000000..62796e1e537 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/bar.ts @@ -0,0 +1 @@ +export type bar = 'bar'; \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/fuga.ts b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/fuga.ts new file mode 100644 index 00000000000..abed84903a0 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/fuga.ts @@ -0,0 +1 @@ +export type fuga = "fuga"; \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/index.ts b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/index.ts new file mode 100644 index 00000000000..41ce94cd1fa --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/hoge/index.ts @@ -0,0 +1,2 @@ +export * from "./bar"; +export * from "./fuga"; \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/index.ts b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/index.ts new file mode 100644 index 00000000000..20c29f66631 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/index.ts @@ -0,0 +1,2 @@ +import { fuga } from './hoge'; +export type Baz = `${fuga}` \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/package.json b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/package.json new file mode 100644 index 00000000000..5714871f073 --- /dev/null +++ b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/package.json @@ -0,0 +1,6 @@ +{ + "name": "ts-types-re-exporting-aggregating", + "private": true, + "main": "dist/main.js", + "types": "dist/types.d.ts" +} \ No newline at end of file diff --git a/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/yarn.lock b/packages/core/integration-tests/test/integration/ts-types/re-exporting-aggregating/yarn.lock new file mode 100644 index 00000000000..e69de29bb2d diff --git a/packages/core/integration-tests/test/ts-types.js b/packages/core/integration-tests/test/ts-types.js index 1676a727c77..8a8e02324d0 100644 --- a/packages/core/integration-tests/test/ts-types.js +++ b/packages/core/integration-tests/test/ts-types.js @@ -361,4 +361,31 @@ describe('typescript types', function () { ); assert.equal(dist, expected); }); + + it('should handle re-exporting aggregating correctly', async function () { + await bundle( + path.join( + __dirname, + '/integration/ts-types/re-exporting-aggregating/index.ts', + ), + ); + + let dist = ( + await outputFS.readFile( + path.join( + __dirname, + '/integration/ts-types/re-exporting-aggregating/dist/types.d.ts', + ), + 'utf8', + ) + ).replace(/\r\n/g, '\n'); + let expected = await inputFS.readFile( + path.join( + __dirname, + '/integration/ts-types/re-exporting-aggregating/expected.d.ts', + ), + 'utf8', + ); + assert.equal(dist, expected); + }); }); diff --git a/packages/transformers/typescript-types/src/TSModuleGraph.js b/packages/transformers/typescript-types/src/TSModuleGraph.js index e522b19ec17..a7dc12600ed 100644 --- a/packages/transformers/typescript-types/src/TSModuleGraph.js +++ b/packages/transformers/typescript-types/src/TSModuleGraph.js @@ -143,10 +143,13 @@ export class TSModuleGraph { if (e.name === name) { return this.getExport(module, e); } else if (e.specifier) { - return this.resolveExport( + const m = this.resolveExport( nullthrows(this.getModule(e.specifier)), name, ); + if (m) { + return m; + } } } }