Skip to content

Commit 0000fd1

Browse files
committed
fix: normalize all-type export statements
1 parent f18f3e7 commit 0000fd1

7 files changed

Lines changed: 30 additions & 9 deletions

File tree

src/fake-js.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1342,6 +1342,7 @@ function patchImportExport(
13421342
}
13431343
}
13441344
}
1345+
normalizeTypeOnlyExport(node)
13451346
}
13461347

13471348
if (node.source?.value && RE_DTS.test(node.source.value)) {
@@ -1366,6 +1367,26 @@ function patchImportExport(
13661367
}
13671368
}
13681369

1370+
function normalizeTypeOnlyExport(node: t.ExportNamedDeclaration): void {
1371+
if (node.declaration || !node.specifiers.length) return
1372+
1373+
for (const specifier of node.specifiers) {
1374+
if (
1375+
specifier.type !== 'ExportSpecifier' ||
1376+
specifier.exportKind !== 'type'
1377+
) {
1378+
return
1379+
}
1380+
}
1381+
1382+
node.exportKind = 'type'
1383+
for (const specifier of node.specifiers) {
1384+
if (specifier.type === 'ExportSpecifier') {
1385+
specifier.exportKind = 'value'
1386+
}
1387+
}
1388+
}
1389+
13691390
/**
13701391
* Handle `__exportAll` call
13711392
*/

tests/__snapshots__/index.test.ts.snap

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -429,7 +429,7 @@ export { import_stub_lib as t };
429429
// b.d.ts
430430
import { t as a_d_exports } from "./a.js";
431431
type LibType = a_d_exports.LibType;
432-
export { type LibType };"
432+
export type { LibType };"
433433
`;
434434
435435
exports[`re-export from lib > onlyA 1`] = `
@@ -441,7 +441,7 @@ exports[`re-export from lib > onlyB 1`] = `
441441
"// b.d.ts
442442
import * as import_stub_lib from "stub-lib";
443443
type LibType = import_stub_lib.LibType;
444-
export { type LibType };"
444+
export type { LibType };"
445445
`;
446446
447447
exports[`real css imports are externalized 1`] = `
@@ -457,7 +457,7 @@ exports[`resolve dts 1`] = `
457457
//#region tests/fixtures/resolve-dts/mod.d.ts
458458
type Foo = string;
459459
//#endregion
460-
export { type Foo };
460+
export type { Foo };
461461
// index.js
462462
"
463463
`;
@@ -495,7 +495,7 @@ exports[`tree-shaking 1`] = `
495495
//#region tests/fixtures/tree-shaking/mod.d.ts
496496
type A = string;
497497
//#endregion
498-
export { type A };
498+
export type { A };
499499
// index.js
500500
"
501501
`;

tests/__snapshots__/source-map-oxc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ export { mod_d_exports as Mod, Ns, Obj, a, b, fn };
3333
## index.d.ts.map
3434

3535
```map
36-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/mod.ts","../../fixtures/source-map/index.ts"],"mappings":";;;cAAa,GAAA;;;cCAA,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAG,KAAA,EAAO,GAAA;AAAA,UAIT,GAAA;EACf,MAAA;IACE,GAAA;EAAA;EAEF,MAAA;EACA,SAAA;AAAA;AAAA,kBAGe,EAAA;EAAA,KACH,GAAA;EAAA,KACA,GAAA,MAAS,CAAA;EAAA,KACT,GAAA;IACV,EAAA;EAAA;AAAA"}
36+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/mod.ts","../../fixtures/source-map/index.ts"],"mappings":";;;cAAa,GAAA;;;cCAA,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAG,KAAO,EAAA,GAAA;AAAA,UAIT,GAAA;EACf,MAAA;IACE,GAAA;EAAA;EAEF,MAAA;EACA,SAAA;AAAA;AAAA,kBAGe,EAAA;EAAA,KACH,GAAA;EAAA,KACA,GAAA,MAAS,CAAA;EAAA,KACT,GAAA;IACV,EAAA;EAAA;AAAA"}
3737
```

tests/__snapshots__/source-map-tsc.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ export { mod_d_exports as Mod, Ns, Obj, a, b, fn };
3333
## index.d.ts.map
3434

3535
```map
36-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/mod.ts","../../fixtures/source-map/index.ts"],"mappings":";;;cAAa,GAAA;;;cCAA,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAG,KAAA,EAAO,GAAA;AAAA,UAIT,GAAA;EACf,MAAA;IACE,GAAA;EAAA;EAEF,MAAA;EACA,SAAA;AAAA;AAAA,kBAGe,EAAA;EAAA,KACH,GAAA;EAAA,KACA,GAAA,MAAS,CAAA;EAAA,KACT,GAAA;IACV,EAAA;EAAA;AAAA"}
36+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/mod.ts","../../fixtures/source-map/index.ts"],"mappings":";;;cAAa,GAAA;;;cCAA,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAG,KAAU,EAAH,GAAG;AAAA,UAIZ,GAAA;EACf,MAAA;IACE,GAAA;EAAA;EAEF,MAAA;EACA,SAAA;AAAA;AAAA,kBAGe,EAAA;EAAA,KACH,GAAA;EAAA,KACA,GAAA,MAAS,CAAC;EAAA,KACV,GAAA;IACV,EAAA;EAAA;AAAA"}
3737
```

tests/__snapshots__/source-map-tsgo.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -33,5 +33,5 @@ export { mod_d_exports as Mod, Ns, Obj, a, b, fn };
3333
## index.d.ts.map
3434

3535
```map
36-
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/mod.ts","../../fixtures/source-map/index.ts"],"mappings":";;;cAAa,GAAA;;;cCAA,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAG,KAAA,EAAO,GAAA;AAAA,UAIT,GAAA;EACf,MAAA;IACE,GAAA;EAAA;EAEF,MAAA;EACA,SAAA;AAAA;AAAA,kBAGe,EAAA;EAAA,KACH,GAAA;EAAA,KACA,GAAA,MAAS,CAAA;EAAA,KACT,GAAA;IACV,EAAA;EAAA;AAAA"}
36+
{"version":3,"file":"index.d.ts","names":[],"sources":["../../fixtures/source-map/mod.ts","../../fixtures/source-map/index.ts"],"mappings":";;;cAAa,GAAA;;;cCAA,CAAA;AAAA,cAEA,CAAA;AAAA,KAIR,GAAA;AAAA,iBACW,EAAA,CAAG,KAAU,EAAH,GAAG;AAAA,UAIZ,GAAA;EACf,MAAA;IACE,GAAA;EAAA;EAEF,MAAA;EACA,SAAA;AAAA;AAAA,kBAGe,EAAA;EAAA,KACH,GAAA;EAAA,KACA,GAAA,MAAS,CAAC;EAAA,KACV,GAAA;IACV,EAAA;EAAA;AAAA"}
3737
```

tests/rollup-plugin-dts/export-all-as-type/known-diff.patch

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,4 +6,4 @@ Index: diff.patch
66
// index.d.ts
77
declare const foo: number
88
-export { foo }
9-
+export { type foo }
9+
+export type { foo }

tests/rollup-plugin-dts/export-all-as-type/snapshot.d.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,4 @@
22
//#region tests/rollup-plugin-dts/export-all-as-type/all.d.ts
33
declare const foo: number;
44
//#endregion
5-
export { type foo };
5+
export type { foo };

0 commit comments

Comments
 (0)