Skip to content

Commit 643003f

Browse files
committed
Do not export types if their local name changed because of collisions (unless they were explicitly exported)
Fixes #286
1 parent 12550ce commit 643003f

File tree

4 files changed

+21
-18
lines changed

4 files changed

+21
-18
lines changed

src/bundle-generator.ts

+17-14
Original file line numberDiff line numberDiff line change
@@ -1092,6 +1092,15 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
10921092
// by default this option should be enabled
10931093
const exportReferencedTypes = outputOptions.exportReferencedTypes !== false;
10941094

1095+
function isExportedWithLocalName(namedDeclaration: ts.NamedDeclaration, exportedName: string): boolean {
1096+
const nodeName = getNodeName(namedDeclaration);
1097+
if (nodeName === undefined) {
1098+
throw new Error(`Cannot find node name ${namedDeclaration.getText()}`);
1099+
}
1100+
1101+
return collisionsResolver.resolveReferencedIdentifier(nodeName as ts.Identifier) === exportedName;
1102+
}
1103+
10951104
return generateOutput(
10961105
{
10971106
...collectionResult,
@@ -1102,14 +1111,18 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
11021111
return collisionsResolver.resolveReferencedQualifiedName(identifier);
11031112
}
11041113
},
1114+
// eslint-disable-next-line complexity
11051115
shouldStatementHasExportKeyword: (statement: ts.Statement) => {
1116+
if (isAmbientModule(statement) || ts.isExportDeclaration(statement)) {
1117+
return false;
1118+
}
1119+
11061120
const statementExports = getExportsForStatement(rootFileExports, typeChecker, statement);
11071121

11081122
// If true, then no direct export was found. That means that node might have
11091123
// an export keyword (like interface, type, etc) otherwise, if there are
11101124
// only re-exports with renaming (like export { foo as bar }) we don't need
11111125
// to put export keyword for this statement because we'll re-export it in the way
1112-
// const hasStatementDefaultKeyword = hasNodeModifier(statement, ts.SyntaxKind.DefaultKeyword);
11131126
let result = statementExports.length === 0 || statementExports.find((exp: SourceFileExport) => {
11141127
if (ts.isVariableStatement(statement)) {
11151128
for (const variableDeclaration of statement.declarationList.declarations) {
@@ -1128,17 +1141,7 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
11281141
return false;
11291142
}
11301143

1131-
if (isNodeNamedDeclaration(statement)) {
1132-
const nodeName = getNodeName(statement);
1133-
if (nodeName === undefined) {
1134-
throw new Error(`Cannot find node name ${statement.getText()}`);
1135-
}
1136-
1137-
const resolvedName = collisionsResolver.resolveReferencedIdentifier(nodeName as ts.Identifier);
1138-
return exp.exportedName === resolvedName;
1139-
}
1140-
1141-
return false;
1144+
return isNodeNamedDeclaration(statement) && isExportedWithLocalName(statement, exp.exportedName);
11421145
}) !== undefined;
11431146

11441147
// "direct export" means export from the root source file
@@ -1156,8 +1159,8 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
11561159
// "valuable" statements must be re-exported from root source file
11571160
// to having export keyword in declaration file
11581161
result = result && statementExports.length !== 0;
1159-
} else if (isAmbientModule(statement) || ts.isExportDeclaration(statement)) {
1160-
result = false;
1162+
} else if (isNodeNamedDeclaration(statement) && !isExportedWithLocalName(statement, getNodeName(statement)!.getText())) { // eslint-disable-line @typescript-eslint/no-non-null-assertion
1163+
return false;
11611164
}
11621165

11631166
return result;

tests/e2e/test-cases/merged-namespaces/output.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ declare namespace Ns2 {
2727
}
2828
}
2929
}
30-
export type FooBar$1 = number;
30+
type FooBar$1 = number;
3131
declare namespace Ns1$1 {
3232
namespace SubNs1 {
3333
interface Interface1 {

tests/e2e/test-cases/names-collision-with-globals/output.d.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
export interface Promise$3 {
1+
interface Promise$3 {
22
}
3-
export interface Date$2 {
3+
interface Date$2 {
44
}
55
export interface Int {
66
localD: Date$2;

tests/e2e/test-cases/re-export-star-with-selection/output.d.ts

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ export { Interface as Interface2, InterfaceWithFields } from 'fake-package';
22
import { NonDefaultInterface, NonDefaultInterface as Foo, NonDefaultInterface as Type, NonDefaultInterface as Type2 } from 'package-with-default-export';
33
export { ReExportedName as NewReExportedName } from 'package-with-re-exports';
44

5-
export type Foo$1 = string;
5+
type Foo$1 = string;
66
export type Bar = Foo$1;
77

88
export {

0 commit comments

Comments
 (0)