Skip to content

Commit

Permalink
fix: function-namespaces were converted incorrectly
Browse files Browse the repository at this point in the history
Resolves #1483
  • Loading branch information
Gerrit0 committed Jan 30, 2021
1 parent 7723ddf commit 3097c5a
Show file tree
Hide file tree
Showing 6 changed files with 419 additions and 189 deletions.
46 changes: 31 additions & 15 deletions src/lib/converter/symbols.ts
Expand Up @@ -9,7 +9,12 @@ import {
TypeParameterReflection,
} from "../models";
import { flatMap, uniqueByEquals } from "../utils/array";
import { getEnumFlags, hasAllFlags, removeFlag } from "../utils/enum";
import {
getEnumFlags,
hasAllFlags,
hasAnyFlag,
removeFlag,
} from "../utils/enum";
import { Context } from "./context";
import { convertDefaultValue } from "./convert-expression";
import { ConverterEvents } from "./converter-events";
Expand Down Expand Up @@ -87,7 +92,10 @@ export function convertSymbol(
// that TD supports is merging a class and interface. All others are
// represented as multiple reflections
if (hasAllFlags(symbol.flags, ts.SymbolFlags.Class)) {
flags = removeFlag(flags, ts.SymbolFlags.Interface);
flags = removeFlag(
flags,
ts.SymbolFlags.Interface | ts.SymbolFlags.Function
);
}

// Kind of declaration merging... we treat this as a property with get/set signatures.
Expand Down Expand Up @@ -166,15 +174,15 @@ function convertNamespace(
symbol: ts.Symbol,
nameOverride?: string
) {
// This can happen in JS land where a user defines a class using a mixture
// of ES6 class syntax and adding properties to the class manually.
if (
symbol
.getDeclarations()
?.some((d) => ts.isModuleDeclaration(d) || ts.isSourceFile(d)) ===
false
) {
return;
let exportFlags = ts.SymbolFlags.ModuleMember;

// This can happen in JS land where "class" functions get tagged as a namespace too
if (symbol.getDeclarations()?.some(ts.isModuleDeclaration) !== true) {
exportFlags = ts.SymbolFlags.ClassMember;

if (hasAnyFlag(symbol.flags, ts.SymbolFlags.Class)) {
return;
}
}

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

convertSymbols(
context.withScope(reflection),
getSymbolExportsWithFlag(symbol, ts.SymbolFlags.ModuleMember)
getSymbolExportsWithFlag(symbol, exportFlags)
);
}

Expand Down Expand Up @@ -304,7 +312,13 @@ function convertFunctionOrMethod(
const signatures = type.getCallSignatures();

const reflection = context.createDeclarationReflection(
isMethod ? ReflectionKind.Method : ReflectionKind.Function,
context.scope.kindOf(
ReflectionKind.ClassOrInterface |
ReflectionKind.VariableOrProperty |
ReflectionKind.TypeLiteral
)
? ReflectionKind.Method
: ReflectionKind.Function,
symbol,
nameOverride
);
Expand Down Expand Up @@ -360,7 +374,7 @@ function convertClassOrInterface(

const classDeclaration = symbol
.getDeclarations()
?.find(ts.isClassDeclaration);
?.find((d) => ts.isClassDeclaration(d) || ts.isFunctionDeclaration(d));
if (classDeclaration) {
setModifiers(classDeclaration, reflection);

Expand Down Expand Up @@ -544,7 +558,9 @@ function convertProperty(
}

const reflection = context.createDeclarationReflection(
ReflectionKind.Property,
context.scope.kindOf(ReflectionKind.Namespace)
? ReflectionKind.Variable
: ReflectionKind.Property,
symbol,
nameOverride
);
Expand Down
3 changes: 3 additions & 0 deletions src/test/converter/function/function.ts
Expand Up @@ -204,3 +204,6 @@ export function boolOrUndef(x: number) {
if (x > 20) return false;
return undefined;
}

export function merged() {}
merged.nsFn = function () {};

0 comments on commit 3097c5a

Please sign in to comment.