Skip to content

Commit 3097c5a

Browse files
committed
fix: function-namespaces were converted incorrectly
Resolves #1483
1 parent 7723ddf commit 3097c5a

File tree

6 files changed

+419
-189
lines changed

6 files changed

+419
-189
lines changed

src/lib/converter/symbols.ts

+31-15
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,12 @@ import {
99
TypeParameterReflection,
1010
} from "../models";
1111
import { flatMap, uniqueByEquals } from "../utils/array";
12-
import { getEnumFlags, hasAllFlags, removeFlag } from "../utils/enum";
12+
import {
13+
getEnumFlags,
14+
hasAllFlags,
15+
hasAnyFlag,
16+
removeFlag,
17+
} from "../utils/enum";
1318
import { Context } from "./context";
1419
import { convertDefaultValue } from "./convert-expression";
1520
import { ConverterEvents } from "./converter-events";
@@ -87,7 +92,10 @@ export function convertSymbol(
8792
// that TD supports is merging a class and interface. All others are
8893
// represented as multiple reflections
8994
if (hasAllFlags(symbol.flags, ts.SymbolFlags.Class)) {
90-
flags = removeFlag(flags, ts.SymbolFlags.Interface);
95+
flags = removeFlag(
96+
flags,
97+
ts.SymbolFlags.Interface | ts.SymbolFlags.Function
98+
);
9199
}
92100

93101
// Kind of declaration merging... we treat this as a property with get/set signatures.
@@ -166,15 +174,15 @@ function convertNamespace(
166174
symbol: ts.Symbol,
167175
nameOverride?: string
168176
) {
169-
// This can happen in JS land where a user defines a class using a mixture
170-
// of ES6 class syntax and adding properties to the class manually.
171-
if (
172-
symbol
173-
.getDeclarations()
174-
?.some((d) => ts.isModuleDeclaration(d) || ts.isSourceFile(d)) ===
175-
false
176-
) {
177-
return;
177+
let exportFlags = ts.SymbolFlags.ModuleMember;
178+
179+
// This can happen in JS land where "class" functions get tagged as a namespace too
180+
if (symbol.getDeclarations()?.some(ts.isModuleDeclaration) !== true) {
181+
exportFlags = ts.SymbolFlags.ClassMember;
182+
183+
if (hasAnyFlag(symbol.flags, ts.SymbolFlags.Class)) {
184+
return;
185+
}
178186
}
179187

180188
const reflection = context.createDeclarationReflection(
@@ -185,7 +193,7 @@ function convertNamespace(
185193

186194
convertSymbols(
187195
context.withScope(reflection),
188-
getSymbolExportsWithFlag(symbol, ts.SymbolFlags.ModuleMember)
196+
getSymbolExportsWithFlag(symbol, exportFlags)
189197
);
190198
}
191199

@@ -304,7 +312,13 @@ function convertFunctionOrMethod(
304312
const signatures = type.getCallSignatures();
305313

306314
const reflection = context.createDeclarationReflection(
307-
isMethod ? ReflectionKind.Method : ReflectionKind.Function,
315+
context.scope.kindOf(
316+
ReflectionKind.ClassOrInterface |
317+
ReflectionKind.VariableOrProperty |
318+
ReflectionKind.TypeLiteral
319+
)
320+
? ReflectionKind.Method
321+
: ReflectionKind.Function,
308322
symbol,
309323
nameOverride
310324
);
@@ -360,7 +374,7 @@ function convertClassOrInterface(
360374

361375
const classDeclaration = symbol
362376
.getDeclarations()
363-
?.find(ts.isClassDeclaration);
377+
?.find((d) => ts.isClassDeclaration(d) || ts.isFunctionDeclaration(d));
364378
if (classDeclaration) {
365379
setModifiers(classDeclaration, reflection);
366380

@@ -544,7 +558,9 @@ function convertProperty(
544558
}
545559

546560
const reflection = context.createDeclarationReflection(
547-
ReflectionKind.Property,
561+
context.scope.kindOf(ReflectionKind.Namespace)
562+
? ReflectionKind.Variable
563+
: ReflectionKind.Property,
548564
symbol,
549565
nameOverride
550566
);

src/test/converter/function/function.ts

+3
Original file line numberDiff line numberDiff line change
@@ -204,3 +204,6 @@ export function boolOrUndef(x: number) {
204204
if (x > 20) return false;
205205
return undefined;
206206
}
207+
208+
export function merged() {}
209+
merged.nsFn = function () {};

0 commit comments

Comments
 (0)