Skip to content

Commit

Permalink
fix: better way of preventing unintended exports
Browse files Browse the repository at this point in the history
sveltejs/svelte#12202 revealed that TypeScript has a harder time tracing imports when they are indirectly exposed, e.g. `function foo(): void; ... export { foo }` instead of `export function foo(): void;`, which can lead to bugs down the line. Ultimately this is a TypeScript limitation but in the meantime adding a lone `export {}` along `export function/const/etc` statements has the same effect as #82
  • Loading branch information
dummdidumm committed Jul 2, 2024
1 parent 7bf2dcd commit 5c11cc7
Show file tree
Hide file tree
Showing 32 changed files with 85 additions and 34 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# dts-buddy changelog

## 0.5.1

- Use more TypeScript-friendly way of preventing unintended exports ([#85](https://github.com/Rich-Harris/dts-buddy/pull/85))

## 0.5.0

- Prevent unintended exports ([#82](https://github.com/Rich-Harris/dts-buddy/pull/82))
Expand Down
18 changes: 8 additions & 10 deletions src/create-module-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -398,24 +398,22 @@ export function create_module_declaration(id, entry, created, resolve) {
if (mod) content += '\n' + mod;
}

if (export_specifiers.length > 0) {
content += `\n\n\texport { ${export_specifiers.join(', ')} };`;
}

// finally, export any bindings that are exported from external modules

/** @type {string[]} */
const specifiers = [];

for (const name of exports) {
const declaration = trace_export(entry, name);
if (declaration?.external) {
specifiers.push(declaration.alias);
export_specifiers.push(declaration.alias);
}
}

if (specifiers.length > 0) {
content += `\n\texport { ${specifiers.join(', ')} };`;
// Always add an export { .. } statement, even if there are no exports. This ensures
// that only the public types are exposed to consumers of the declaration file. Due to some
// old TypeScript inconsistency, omitting the export statement would expose all types.
if (export_specifiers.length > 0) {
content += `\n\n\texport { ${export_specifiers.join(', ')} };`;
} else {
content += '\n\n\texport {};';
}

content += `\n}`;
Expand Down
2 changes: 2 additions & 0 deletions test/samples/ambient-imports/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

declare module 'ambient-imports' {
export interface Foo {}

export {};
}

//# sourceMappingURL=index.d.ts.map
12 changes: 6 additions & 6 deletions test/samples/basic/output 5.5/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
declare module 'basic' {
/** A vector with two components */
interface Vector2 {
export interface Vector2 {
/** The x component */
x: number;
/** The y component */
Expand All @@ -11,18 +11,18 @@ declare module 'basic' {
* @param a the first vector
* @param b the second vector
* */
function add(a: Vector2, b: Vector2): Vector2;
export function add(a: Vector2, b: Vector2): Vector2;

export { Vector2, add };
export {};
}

declare module 'basic/subpackage' {
/**
* Multiply two vectors
* */
function multiply(a: import("basic").Vector2, b: import("basic").Vector2): import("basic").Vector2;
export function multiply(a: import("basic").Vector2, b: import("basic").Vector2): import("basic").Vector2;

export { multiply };
export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
4 changes: 2 additions & 2 deletions test/samples/basic/output 5.5/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
null,
null
],
"mappings": ";;WACiBA,OAAOA;;;;;;;;;;;UCKRC,GAAGA;;;;;;;;;UCAHC,QAAQA"
}
"mappings": ";;kBACiBA,OAAOA;;;;;;;;;;;iBCKRC,GAAGA;;;;;;;;;iBCAHC,QAAQA"
}
4 changes: 3 additions & 1 deletion test/samples/const-namespace/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ declare module 'const-namespace' {
export namespace a {
let x: number;
}

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/declare-module/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
declare module 'declare-module' {
export interface Foo {}

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/deconflict-priority/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ declare module 'deconflict-priority' {
export function x(foo: Foo, bar: Bar): void;
interface Foo {}
interface Bar {}

export {};
}

//# sourceMappingURL=index.d.ts.map
4 changes: 3 additions & 1 deletion test/samples/enum/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ declare module 'enum' {
VEGETABLE = 1,
MINERAL = 2
}

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/export-default-as/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ declare module 'export-default-as' {
* Add two numbers
* */
export function add(a: number, b: number): number;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/export-default-function/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
declare module 'export-default-function' {
export default function foo(): void;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/export-default-value/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
declare module 'export-default-value' {
const _default: 42;
export default _default;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/export-from/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
declare module 'export-from' {
export const foo: "hello";

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/export-named-as/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ declare module 'export-named-as' {
* Add two numbers
* */
export function plus(a: number, b: number): number;

export {};
}

//# sourceMappingURL=index.d.ts.map
4 changes: 3 additions & 1 deletion test/samples/export-type/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
declare module 'export-type' {
export type D = {};
export class B<D> {}

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/generics/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ declare module 'generics' {
export interface Foo<T> {
bar: T;
}

export {};
}

//# sourceMappingURL=index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ declare module 'ignores-missing-sourcemap-segments' {
export namespace object {
let answer: number;
}

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
4 changes: 3 additions & 1 deletion test/samples/import-external-default/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
declare module 'import-external-default' {
import type { default as x } from 'external';
export function foo(input: x): x;

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
Original file line number Diff line number Diff line change
@@ -1,7 +1,8 @@
declare module 'import-external-named-as-conflict' {
import type { foo as f } from 'external';
export function foo(): void;

export { f };
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
3 changes: 2 additions & 1 deletion test/samples/import-external-named-as/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare module 'import-external-named-as' {
import type { foo as f } from 'external';

export { f };
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
3 changes: 2 additions & 1 deletion test/samples/import-external-named/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
declare module 'import-external-named' {
import type { foo } from 'external';

export { foo };
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
6 changes: 3 additions & 3 deletions test/samples/jsdoc-import/output 5.5/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ declare module 'jsdoc-import' {
* @param a the first vector
* @param b the second vector
* */
function add(a: Vector2, b: Vector2): Vector2;
export function add(a: Vector2, b: Vector2): Vector2;
/** A vector with two components */
interface Vector2 {
/** The x component */
Expand All @@ -13,7 +13,7 @@ declare module 'jsdoc-import' {
y: number;
}

export { add };
export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
4 changes: 2 additions & 2 deletions test/samples/jsdoc-import/output 5.5/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -13,5 +13,5 @@
null,
null
],
"mappings": ";;;;;;UAQgBA,GAAGA;;WCPFC,OAAOA"
}
"mappings": ";;;;;;iBAQgBA,GAAGA;;WCPFC,OAAOA"
}
4 changes: 3 additions & 1 deletion test/samples/namespace-exports/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,8 @@ declare module 'namespace-exports' {
y: string;
}
}

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
4 changes: 3 additions & 1 deletion test/samples/overloads/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ declare module 'overloads' {
}
export function baz(input: Foo): Foo;
export function baz(input: Bar): Bar;

export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/path-config/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ declare module 'path-config' {
type Input = number;
type Output = number;
export function foo_nested(input: Input): Output;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/preserve-comments/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ declare module 'preserve-comments' {
* @deprecated
*/
export function a(): number;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/preserved-jsdoc-tags/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@ declare module 'preserved-jsdoc-tags' {
* const three = add(1, 2);
* */
export function add(a: number, b: number): number;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/throws/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@ declare module 'throws' {
* @throws {Error} nope
* */
export function nope(): never;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 2 additions & 0 deletions test/samples/treeshaking-inline-import/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,8 @@ declare module 'treeshaking-inline-import' {
/** The y component */
y: number;
}

export {};
}

//# sourceMappingURL=index.d.ts.map
4 changes: 4 additions & 0 deletions test/samples/ts/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ declare module 'ts' {
* Add two vectors
*/
export function add(a: Vector2, b: Vector2): Vector2;

export {};
}

declare module 'ts/subpackage' {
Expand All @@ -23,6 +25,8 @@ declare module 'ts/subpackage' {
* Multiply two vectors
*/
export function multiply(a: Vector2, b: Vector2): Vector2;

export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/ts/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -15,5 +15,5 @@
null,
null
],
"mappings": ";;kBACiBA,OAAOA;;;;;;;kBAQPC,OAAOA;;;;;;;iBAQRC,GAAGA;;;;;;;;iBCZHC,QAAQA"
"mappings": ";;kBACiBA,OAAOA;;;;;;;kBAQPC,OAAOA;;;;;;;iBAQRC,GAAGA;;;;;;;;;;iBCZHC,QAAQA"
}

0 comments on commit 5c11cc7

Please sign in to comment.