Skip to content

Commit 874f801

Browse files
committed
De-merging declaration merging symbols while getting exports of a module
Fixes #301
1 parent 86fc2ff commit 874f801

File tree

7 files changed

+55
-1
lines changed

7 files changed

+55
-1
lines changed

src/helpers/typescript.ts

+25-1
Original file line numberDiff line numberDiff line change
@@ -194,11 +194,35 @@ export function getExportsForSourceFile(typeChecker: ts.TypeChecker, sourceFileS
194194
}
195195
}
196196

197+
const symbolsMergingResolvedExports: SourceFileExport[] = [];
198+
197199
result.forEach((exp: SourceFileExport) => {
198200
exp.symbol = getActualSymbol(exp.symbol, typeChecker);
201+
202+
const symbolsDeclarations = getDeclarationsForSymbol(exp.symbol);
203+
const importSpecifierDeclaration = symbolsDeclarations.find(ts.isImportSpecifier);
204+
if (symbolsDeclarations.length > 1 && importSpecifierDeclaration !== undefined) {
205+
// most likely this export is part of the symbol merging situation
206+
// where one of the declarations is the imported value but the other is declared locally
207+
// in this case we need to add an extra export to the exports list to make sure that it is marked as "exported"
208+
const referencedModule = resolveReferencedModule(importSpecifierDeclaration.parent.parent.parent, typeChecker);
209+
if (referencedModule !== null) {
210+
const referencedModuleSymbol = getNodeSymbol(referencedModule, typeChecker);
211+
if (referencedModuleSymbol !== null) {
212+
const importedName = (importSpecifierDeclaration.propertyName ?? importSpecifierDeclaration.name).getText();
213+
const exportedItemSymbol = typeChecker.getExportsOfModule(referencedModuleSymbol).find((exportSymbol: ts.Symbol) => exportSymbol.getName() === importedName);
214+
if (exportedItemSymbol !== undefined) {
215+
symbolsMergingResolvedExports.push({
216+
...exp,
217+
symbol: getActualSymbol(exportedItemSymbol, typeChecker),
218+
});
219+
}
220+
}
221+
}
222+
}
199223
});
200224

201-
return result;
225+
return [...result, ...symbolsMergingResolvedExports];
202226
}
203227

204228
export function resolveIdentifier(typeChecker: ts.TypeChecker, identifier: ts.Identifier): ts.NamedDeclaration | undefined {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
export const Bar = 12;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { TestCaseConfig } from '../test-case-config';
2+
3+
const config: TestCaseConfig = {
4+
output: {
5+
exportReferencedTypes: false,
6+
},
7+
};
8+
9+
export = config;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,7 @@
1+
import { Bar } from './bar';
2+
3+
export interface Foo {}
4+
5+
interface Bar {}
6+
7+
export { Bar };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('../run-test-case').runTestCase(__dirname);
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
import { Foo, Bar } from './foo';
2+
3+
const Foo = 2;
4+
export { Foo, Bar };
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
export declare const Bar = 12;
2+
export interface Foo {
3+
}
4+
export interface Bar {
5+
}
6+
export declare const Foo = 2;
7+
8+
export {};

0 commit comments

Comments
 (0)