Skip to content

Commit aa5a334

Browse files
committed
Added ignoring symbols from "fake" namespace generation if they aren't used by that namespace
Partially reverts eb862bf Fixes #319
1 parent 54f8817 commit aa5a334

File tree

8 files changed

+38
-24
lines changed

8 files changed

+38
-24
lines changed

src/bundle-generator.ts

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,6 +1002,12 @@ export function generateDtsBundle(entries: readonly EntryPointConfig[], options:
10021002

10031003
function createNamespaceForExports(exports: ts.SymbolTable, namespaceSymbol: ts.Symbol): string | null {
10041004
function addSymbolToNamespaceExports(namespaceExports: Map<string, string>, symbol: ts.Symbol): void {
1005+
// if a symbol isn't used by the namespace symbol it might mean that it shouldn't be included into the bundle because of tree-shaking
1006+
// in this case we shouldn't even try to add such symbol to the namespace
1007+
if (!typesUsageEvaluator.isSymbolUsedBySymbol(symbol, namespaceSymbol)) {
1008+
return;
1009+
}
1010+
10051011
const symbolKnownNames = collisionsResolver.namesForSymbol(symbol);
10061012
if (symbolKnownNames.size === 0) {
10071013
throw new Error(`Cannot get local names for symbol '${symbol.getName()}' while generating namespaced export`);

src/types-usage-evaluator.ts

Lines changed: 3 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -237,29 +237,12 @@ export class TypesUsageEvaluator {
237237
}
238238

239239
private computeUsagesRecursively(parent: ts.Node, parentSymbol: ts.Symbol): void {
240-
const processUsageForChild = (child: ts.Node) => {
240+
ts.forEachChild(parent, (child: ts.Node) => {
241241
if (child.kind === ts.SyntaxKind.JSDoc) {
242242
return;
243243
}
244244

245-
let recursionStartNode = child;
246-
if (ts.isQualifiedName(child) && !ts.isQualifiedName(child.parent)) {
247-
const leftmostSymbol = this.getNodeOwnSymbol(child.left);
248-
249-
// i.e. `import * as NS from './local-module'`
250-
const namespaceImport = getDeclarationsForSymbol(leftmostSymbol).find(ts.isNamespaceImport);
251-
if (namespaceImport !== undefined) {
252-
// if a node is a qualified name and its top-level part was created by a namespaced import
253-
// then we shouldn't add usages of that "namespaced import" to the parent symbol
254-
// because we can just import the referenced symbol directly, without wrapping with a namespace
255-
recursionStartNode = child.right;
256-
257-
// recursive processing doesn't process a node itself so we need to handle it separately
258-
processUsageForChild(recursionStartNode);
259-
}
260-
}
261-
262-
this.computeUsagesRecursively(recursionStartNode, parentSymbol);
245+
this.computeUsagesRecursively(child, parentSymbol);
263246

264247
if (ts.isIdentifier(child) || child.kind === ts.SyntaxKind.DefaultKeyword) {
265248
// identifiers in labelled tuples don't have symbols for their labels
@@ -289,9 +272,7 @@ export class TypesUsageEvaluator {
289272
}
290273
}
291274
}
292-
};
293-
294-
ts.forEachChild(parent, processUsageForChild);
275+
});
295276
}
296277

297278
private addUsages(childSymbol: ts.Symbol, parentSymbol: ts.Symbol): void {

tests/e2e/test-cases/names-collision-across-files/output.d.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
import * as fakePackage from 'fake-package';
12
import { Interface as FPI1, Interface as FPI2, Interface as Interface$2 } from 'fake-package';
23

34
export type ExportEqNs = string;
@@ -67,7 +68,7 @@ export interface Inter {
6768
field2: AnotherInterface$1;
6869
field3: TypeName;
6970
field4: AnotherInterface;
70-
field5: FPI1;
71+
field5: fakePackage.Interface;
7172
field6: FPI1;
7273
field7: Interface$2;
7374
}
@@ -79,7 +80,7 @@ export interface Inter2 {
7980
field2: AnotherInterface$1;
8081
field3: TypeName;
8182
field4: AnotherInterface;
82-
field5: FPI1;
83+
field5: fakePackage.Interface;
8384
field6: FPI2;
8485
field7: Interface$2;
8586
field8: DefaultInterface;
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
import { TestCaseConfig } from '../../test-cases/test-case-config';
2+
3+
const config: TestCaseConfig = {};
4+
5+
export = config;
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
require('../run-test-case').runTestCase(__dirname);
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
import * as fakePackage from 'fake-package';
2+
3+
export declare class MyClass {
4+
static func(): fakePackage.InterfaceWithFields;
5+
static field: fakePackage.Interface;
6+
}
Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
import * as fakePackage from 'fake-package';
2+
3+
export declare class MyClass {
4+
static func(): fakePackage.InterfaceWithFields;
5+
static field: fakePackage.Interface;
6+
}
7+
8+
export {};
Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
{
2+
"extends": "../tsconfig.json",
3+
"compilerOptions": {
4+
"types": ["node"]
5+
}
6+
}

0 commit comments

Comments
 (0)