Skip to content

Commit

Permalink
Support for "declare global" modules
Browse files Browse the repository at this point in the history
  • Loading branch information
Gerrit0 committed Jun 1, 2022
1 parent 690543b commit dfed58a
Show file tree
Hide file tree
Showing 4 changed files with 43 additions and 1 deletion.
4 changes: 4 additions & 0 deletions CHANGELOG.md
@@ -1,5 +1,9 @@
# Unreleased

### Features

- Added support for documenting a module's global declarations as its exports if it contains no real exports.

### Bug Fixes

- Restore support for TS 4.0 through 4.5, #1945.
Expand Down
23 changes: 22 additions & 1 deletion src/lib/converter/converter.ts
Expand Up @@ -469,7 +469,7 @@ function getSymbolForModuleLike(

function getExports(
context: Context,
node: ts.SourceFile | ts.ModuleBlock,
node: ts.SourceFile,
symbol: ts.Symbol | undefined
): ts.Symbol[] {
let result: ts.Symbol[];
Expand All @@ -496,6 +496,27 @@ function getExports(
result = context.checker
.getExportsOfModule(symbol)
.filter((s) => !hasAllFlags(s.flags, ts.SymbolFlags.Prototype));

if (result.length === 0) {
const globalDecl = node.statements.find(
(s) =>
ts.isModuleDeclaration(s) &&
s.flags & ts.NodeFlags.GlobalAugmentation
);

if (globalDecl) {
const globalSymbol = context.getSymbolAtLocation(globalDecl);
if (globalSymbol) {
result = context.checker
.getExportsOfModule(globalSymbol)
.filter((exp) =>
exp.declarations?.some(
(d) => d.getSourceFile() === node
)
);
}
}
}
} else {
// Global file with no inferred top level symbol, get all symbols declared in this file.
const sourceFile = node.getSourceFile();
Expand Down
8 changes: 8 additions & 0 deletions src/test/behaviorTests.ts
Expand Up @@ -81,6 +81,14 @@ export const behaviorTests: Record<
"WithoutReadonlyNumeric"
);
},

declareGlobal(project) {
equal(
project.children?.map((c) => c.name),
["DeclareGlobal"]
);
},

duplicateHeritageClauses(project) {
const b = query(project, "B");
equal(b.extendedTypes?.map(String), ["A"]);
Expand Down
9 changes: 9 additions & 0 deletions src/test/converter2/behavior/declareGlobal.ts
@@ -0,0 +1,9 @@
import { SyntaxKind } from "typescript";

declare global {
interface DeclareGlobal {
method(kind: SyntaxKind): void;
}
}

namespace NotIncluded {}

0 comments on commit dfed58a

Please sign in to comment.