Skip to content

Commit dfed58a

Browse files
committed
Support for "declare global" modules
CC: @Zamiel
1 parent 690543b commit dfed58a

File tree

4 files changed

+43
-1
lines changed

4 files changed

+43
-1
lines changed

CHANGELOG.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,9 @@
11
# Unreleased
22

3+
### Features
4+
5+
- Added support for documenting a module's global declarations as its exports if it contains no real exports.
6+
37
### Bug Fixes
48

59
- Restore support for TS 4.0 through 4.5, #1945.

src/lib/converter/converter.ts

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -469,7 +469,7 @@ function getSymbolForModuleLike(
469469

470470
function getExports(
471471
context: Context,
472-
node: ts.SourceFile | ts.ModuleBlock,
472+
node: ts.SourceFile,
473473
symbol: ts.Symbol | undefined
474474
): ts.Symbol[] {
475475
let result: ts.Symbol[];
@@ -496,6 +496,27 @@ function getExports(
496496
result = context.checker
497497
.getExportsOfModule(symbol)
498498
.filter((s) => !hasAllFlags(s.flags, ts.SymbolFlags.Prototype));
499+
500+
if (result.length === 0) {
501+
const globalDecl = node.statements.find(
502+
(s) =>
503+
ts.isModuleDeclaration(s) &&
504+
s.flags & ts.NodeFlags.GlobalAugmentation
505+
);
506+
507+
if (globalDecl) {
508+
const globalSymbol = context.getSymbolAtLocation(globalDecl);
509+
if (globalSymbol) {
510+
result = context.checker
511+
.getExportsOfModule(globalSymbol)
512+
.filter((exp) =>
513+
exp.declarations?.some(
514+
(d) => d.getSourceFile() === node
515+
)
516+
);
517+
}
518+
}
519+
}
499520
} else {
500521
// Global file with no inferred top level symbol, get all symbols declared in this file.
501522
const sourceFile = node.getSourceFile();

src/test/behaviorTests.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,14 @@ export const behaviorTests: Record<
8181
"WithoutReadonlyNumeric"
8282
);
8383
},
84+
85+
declareGlobal(project) {
86+
equal(
87+
project.children?.map((c) => c.name),
88+
["DeclareGlobal"]
89+
);
90+
},
91+
8492
duplicateHeritageClauses(project) {
8593
const b = query(project, "B");
8694
equal(b.extendedTypes?.map(String), ["A"]);
Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
import { SyntaxKind } from "typescript";
2+
3+
declare global {
4+
interface DeclareGlobal {
5+
method(kind: SyntaxKind): void;
6+
}
7+
}
8+
9+
namespace NotIncluded {}

0 commit comments

Comments
 (0)