Skip to content

Commit

Permalink
fix: better way of preventing unintended exports (#85)
Browse files Browse the repository at this point in the history
* Revert "Fix exports (#82)"

This reverts commit b9435de.

* fix: better way of preventing unintended exports

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

* fix tests
  • Loading branch information
dummdidumm authored Jul 2, 2024
1 parent 079b5d1 commit 5867205
Show file tree
Hide file tree
Showing 61 changed files with 152 additions and 142 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
41 changes: 24 additions & 17 deletions src/create-module-declaration.js
Original file line number Diff line number Diff line change
Expand Up @@ -167,9 +167,11 @@ export function create_module_declaration(id, entry, created, resolve) {
declaration.alias = get_name(globals.has(name) ? declaration.name : name);
mark(declaration);

export_specifiers.push(
declaration.alias === name ? name : `${declaration.alias} as ${name}`
);
if (declaration.alias !== name) {
export_specifiers.push(`${declaration.alias} as ${name}`);
} else {
declaration.exported = true;
}
} else {
throw new Error('Something strange happened');
}
Expand Down Expand Up @@ -323,10 +325,15 @@ export function create_module_declaration(id, entry, created, resolve) {
}
}

let b = export_modifier.end;
const a = b - 6;
while (/\s/.test(module.dts[b])) b += 1;
result.remove(a, b);
if (!exports.has(declaration.alias)) {
// remove all export keywords in the initial pass; reinstate as necessary later
let b = export_modifier.end;
const a = b - 6;
while (/\s/.test(module.dts[b])) b += 1;
result.remove(a, b);
}
} else if (declaration.exported) {
export_specifiers.push(declaration.alias);
}
}

Expand Down Expand Up @@ -391,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 Expand Up @@ -500,6 +505,7 @@ export function create_module_declaration(id, entry, created, resolve) {
module: '<builtin>',
external: false,
included: true,
exported: false,
name,
alias: name,
dependencies: [],
Expand Down Expand Up @@ -527,6 +533,7 @@ function create_external_declaration(binding, alias) {
module: binding.id,
name: binding.name,
alias: '',
exported: false,
external: true,
included: false,
dependencies: [],
Expand Down
1 change: 1 addition & 0 deletions src/types.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ interface Declaration {
module: string;
name: string;
alias: string;
exported: boolean;
external: boolean;
included: boolean;
dependencies: Reference[];
Expand Down
1 change: 1 addition & 0 deletions src/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -342,6 +342,7 @@ export function get_dts(file, created, resolve) {
module: file,
name,
alias: '',
exported: false,
included: false,
external: false,
dependencies: [],
Expand Down
4 changes: 2 additions & 2 deletions test/samples/ambient-imports/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
/// <reference types="some-ambient-import" />

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

export { Foo };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/ambient-imports/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"sourcesContent": [
null
],
"mappings": ";;;WAEiBA,GAAGA"
"mappings": ";;;kBAEiBA,GAAGA"
}
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"
}
12 changes: 6 additions & 6 deletions test/samples/basic/output/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
2 changes: 1 addition & 1 deletion test/samples/basic/output/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"
}
6 changes: 3 additions & 3 deletions test/samples/const-namespace/output 5.0/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
declare module 'const-namespace' {
namespace a {
export namespace a {
const x: number;
}

export { a };
export {};
}

//# sourceMappingURL=index.d.ts.map
//# sourceMappingURL=index.d.ts.map
4 changes: 2 additions & 2 deletions test/samples/const-namespace/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
declare module 'const-namespace' {
namespace a {
export namespace a {
let x: number;
}

export { a };
export {};
}

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

export { Foo };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/declare-module/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"sourcesContent": [
null
],
"mappings": ";WAOiBA,GAAGA"
"mappings": ";kBAOiBA,GAAGA"
}
4 changes: 2 additions & 2 deletions test/samples/deconflict-priority/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
declare module 'deconflict-priority' {
function x(foo: Foo, bar: Bar): void;
export function x(foo: Foo, bar: Bar): void;
interface Foo {}
interface Bar {}

export { x };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/deconflict-priority/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,5 @@
null,
null
],
"mappings": ";UAIgBA,CAACA;WCJAC,GAAGA;WCCHC,GAAGA"
"mappings": ";iBAIgBA,CAACA;WCJAC,GAAGA;WCCHC,GAAGA"
}
4 changes: 2 additions & 2 deletions test/samples/enum/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,11 +1,11 @@
declare module 'enum' {
enum categories {
export enum categories {
ANIMAL = 0,
VEGETABLE = 1,
MINERAL = 2
}

export { categories };
export {};
}

//# sourceMappingURL=index.d.ts.map
4 changes: 2 additions & 2 deletions test/samples/enum/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"sourcesContent": [
null
],
"mappings": ";MAAYA,UAAUA"
}
"mappings": ";aAAYA,UAAUA"
}
4 changes: 2 additions & 2 deletions test/samples/export-default-as/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ declare module 'export-default-as' {
/**
* Add two numbers
* */
function add(a: number, b: number): number;
export function add(a: number, b: number): number;

export { add };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/export-default-as/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"sourcesContent": [
null
],
"mappings": ";;;;UAMwBA,GAAGA"
"mappings": ";;;;iBAMwBA,GAAGA"
}
2 changes: 1 addition & 1 deletion test/samples/export-default-function/output/index.d.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
declare module 'export-default-function' {
export default function foo(): void;

export { default };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/export-default-value/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ declare module 'export-default-value' {
const _default: 42;
export default _default;

export { default };
export {};
}

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

export { foo };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/export-from/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -10,5 +10,5 @@
"sourcesContent": [
null
],
"mappings": ";OAAaA,GAAGA"
"mappings": ";cAAaA,GAAGA"
}
4 changes: 2 additions & 2 deletions test/samples/export-named-as/output/index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,9 +2,9 @@ declare module 'export-named-as' {
/**
* Add two numbers
* */
function plus(a: number, b: number): number;
export function plus(a: number, b: number): number;

export { plus };
export {};
}

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

export { B, D };
export {};
}

//# sourceMappingURL=index.d.ts.map
2 changes: 1 addition & 1 deletion test/samples/export-type/output/index.d.ts.map
Original file line number Diff line number Diff line change
Expand Up @@ -5,4 +5,4 @@
"sources": [],
"sourcesContent": [],
"mappings": ""
}
}
Loading

0 comments on commit 5867205

Please sign in to comment.