Skip to content

Commit 004032f

Browse files
sxzzharshit-d3v
andcommitted
fix: prevent implicit exports of private declarations
Refs #310, #312 Co-authored-by: Harshit <[email protected]>
1 parent 18d7aa1 commit 004032f

50 files changed

Lines changed: 159 additions & 61 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

‎src/fake-js/index.ts‎

Lines changed: 35 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -571,7 +571,7 @@ export function createFakeJsPlugin({
571571
return { code: EMPTY_STUB, map: null }
572572
}
573573

574-
if (!program.body.some(isModuleStatement)) {
574+
if (needsScopeMarker(program.body)) {
575575
program.body.push(
576576
b.ExportNamedDeclaration({
577577
declaration: null,
@@ -641,16 +641,38 @@ export function createFakeJsPlugin({
641641
}
642642
}
643643

644-
function isModuleStatement(node: t.ProgramStatement): boolean {
645-
return (
646-
is.oneOf(node, [
647-
'ImportDeclaration',
648-
'ExportAllDeclaration',
649-
'ExportDefaultDeclaration',
650-
'ExportNamedDeclaration',
651-
'TSExportAssignment',
652-
]) ||
653-
(node.type === 'TSImportEqualsDeclaration' &&
654-
node.moduleReference.type === 'TSExternalModuleReference')
655-
)
644+
function needsScopeMarker(body: t.ProgramStatement[]): boolean {
645+
let hasModuleStatement = false
646+
let hasPrivateDeclaration = false
647+
648+
for (const node of body) {
649+
switch (node.type) {
650+
case 'ExportAllDeclaration':
651+
case 'TSExportAssignment':
652+
return false
653+
case 'ExportNamedDeclaration':
654+
if (!node.declaration) return false
655+
hasModuleStatement = true
656+
break
657+
case 'ExportDefaultDeclaration':
658+
if (!is.Declaration(node.declaration)) return false
659+
hasModuleStatement = true
660+
break
661+
case 'ImportDeclaration':
662+
hasModuleStatement = true
663+
break
664+
case 'TSImportEqualsDeclaration':
665+
hasModuleStatement ||=
666+
node.moduleReference.type === 'TSExternalModuleReference'
667+
break
668+
default:
669+
hasPrivateDeclaration ||=
670+
is.Declaration(node) &&
671+
(node.type !== 'TSModuleDeclaration' ||
672+
(node.kind !== 'global' && !is.StringLiteral(node.id)))
673+
}
674+
}
675+
676+
// Inline exports alone do not prevent private declarations from being exported.
677+
return !hasModuleStatement || hasPrivateDeclaration
656678
}

‎tests/__snapshots__/index.test.ts.snap‎

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ export declare class Cls {
1818
fn(e: Enum): void;
1919
}
2020
//#endregion
21+
export {};
2122
// basic.js
2223
//#region tests/fixtures/basic.ts
2324
const foo = 42;
@@ -333,7 +334,22 @@ exports[`infer type parameter 1`] = `
333334
type Mapped<T> = T extends Record<string, infer U> ? U : never;
334335
export type Fn1<U = unknown> = () => Mapped<U>;
335336
export type Fn2<V = unknown> = () => Mapped<V>;
336-
//#endregion"
337+
//#endregion
338+
export {};"
339+
`;
340+
341+
exports[`inline exports keep other declarations private 1`] = `
342+
"// export-decl-private.d.ts
343+
//#region tests/fixtures/export-decl-private.d.ts
344+
type Internal = {
345+
a: string;
346+
};
347+
export declare class Foo {
348+
readonly x: Internal;
349+
constructor(x: Internal);
350+
}
351+
//#endregion
352+
export {};"
337353
`;
338354
339355
exports[`input alias 1`] = `
@@ -414,7 +430,8 @@ export interface Test {
414430
[b](): string;
415431
get [c](): string;
416432
}
417-
//#endregion"
433+
//#endregion
434+
export {};"
418435
`;
419436
420437
exports[`re-export from lib > both 1`] = `
@@ -513,6 +530,7 @@ export declare class Cls {
513530
fn(e: Enum): void;
514531
}
515532
//#endregion
533+
export {};
516534
// basic.js
517535
//#region tests/fixtures/basic.ts
518536
const foo = 42;
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
type Internal = { a: string }
2+
export class Foo {
3+
constructor(public readonly x: Internal) {}
4+
}

‎tests/index.test.ts‎

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -766,3 +766,11 @@ test('declaration with export', async () => {
766766
)
767767
expect(snapshot).toMatchSnapshot()
768768
})
769+
770+
test('inline exports keep other declarations private', async () => {
771+
const { snapshot } = await rolldownBuild(
772+
path.resolve(dirname, 'fixtures/export-decl-private.ts'),
773+
[dts({ emitDtsOnly: true })],
774+
)
775+
expect(snapshot).toMatchSnapshot()
776+
})

‎tests/rollup-plugin-dts/computed-property/snapshot.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,4 +12,5 @@ export type Klass = {
1212
[0]: C;
1313
[Dprop]: D;
1414
};
15-
//#endregion
15+
//#endregion
16+
export {};

‎tests/rollup-plugin-dts/constructor-shorthands/snapshot.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,4 +7,5 @@ export declare class Foo {
77
protected b: B;
88
constructor(a: A, b: B);
99
}
10-
//#endregion
10+
//#endregion
11+
export {};

‎tests/rollup-plugin-dts/export-default-abstract/snapshot.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,4 +11,5 @@ export default abstract class MemberInfo {
1111
abstract readonly declaringType: TypeInfo;
1212
abstract readonly memberType: MemberTypes;
1313
}
14-
//#endregion
14+
//#endregion
15+
export {};

‎tests/rollup-plugin-dts/export-simple-alias/snapshot.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,4 +2,5 @@
22
//#region tests/rollup-plugin-dts/export-simple-alias/index.d.ts
33
interface Foo {}
44
export declare type Bar = Foo;
5-
//#endregion
5+
//#endregion
6+
export {};

‎tests/rollup-plugin-dts/generics/snapshot.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -32,4 +32,5 @@ export declare function fn<T = G>(g: T, h: Gen<H>): void;
3232
export declare type TyFn = <T = J>(j: T, k: Gen<K>) => L;
3333
export declare type TyCtor = new <T = M>(m: T, n: Gen<N>) => O;
3434
export interface I2 extends Gen<P> {}
35-
//#endregion
35+
//#endregion
36+
export {};

‎tests/rollup-plugin-dts/getters-setters/snapshot.d.ts‎

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,4 +9,5 @@ export declare class D {
99
set b(_: B);
1010
readonly c: C;
1111
}
12-
//#endregion
12+
//#endregion
13+
export {};

0 commit comments

Comments
 (0)