From d739298b144ee94a1f5ad56f120d286346d46bf2 Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Wed, 30 Dec 2020 10:35:56 -0700 Subject: [PATCH] fix: Functions might not have a parent in global files Closes #1436 --- src/lib/converter/symbols.ts | 6 +- src/test/converter/declaration/global-fn.d.ts | 1 + src/test/converter/declaration/specs.json | 71 ++++++++++++++----- 3 files changed, 57 insertions(+), 21 deletions(-) create mode 100644 src/test/converter/declaration/global-fn.d.ts diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index 0287795dc..7b193b733 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -243,14 +243,13 @@ function convertFunctionOrMethod( } const parentSymbol = context.project.getSymbolFromReflection(context.scope); - assert(parentSymbol, "Missing parent symbol when converting function"); const locationDeclaration = parentSymbol ?.getDeclarations() ?.find( (d) => ts.isClassDeclaration(d) || ts.isInterfaceDeclaration(d) - ) ?? parentSymbol.getDeclarations()?.[0]?.getSourceFile(); + ) ?? symbol.getDeclarations()?.[0]?.getSourceFile(); assert(locationDeclaration, "Missing declaration context"); const type = context.checker.getTypeOfSymbolAtLocation( @@ -269,9 +268,6 @@ function convertFunctionOrMethod( // All method signatures must have the same modifier flags. setModifiers(declarations[0], reflection); - const parentSymbol = context.project.getSymbolFromReflection( - context.scope - ); assert(parentSymbol, "Tried to convert a method without a parent."); if ( parentSymbol diff --git a/src/test/converter/declaration/global-fn.d.ts b/src/test/converter/declaration/global-fn.d.ts new file mode 100644 index 000000000..c4f0ff06f --- /dev/null +++ b/src/test/converter/declaration/global-fn.d.ts @@ -0,0 +1 @@ +declare function gh1436(): void; diff --git a/src/test/converter/declaration/specs.json b/src/test/converter/declaration/specs.json index 32c5b793d..dd6129f7b 100644 --- a/src/test/converter/declaration/specs.json +++ b/src/test/converter/declaration/specs.json @@ -198,20 +198,58 @@ }, { "id": 14, + "name": "global-fn", + "kind": 1, + "kindString": "Module", + "flags": {}, + "children": [ + { + "id": 15, + "name": "gh1436", + "kind": 64, + "kindString": "Function", + "flags": {}, + "signatures": [ + { + "id": 16, + "name": "gh1436", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "void" + } + } + ] + } + ], + "groups": [ + { + "title": "Functions", + "kind": 64, + "children": [ + 15 + ] + } + ] + }, + { + "id": 17, "name": "namespaces", "kind": 1, "kindString": "Module", "flags": {}, "children": [ { - "id": 19, + "id": 22, "name": "GH1124", "kind": 2, "kindString": "Namespace", "flags": {}, "children": [ { - "id": 20, + "id": 23, "name": "PrimitiveType", "kind": 4194304, "kindString": "Type alias", @@ -239,7 +277,7 @@ } }, { - "id": 21, + "id": 24, "name": "Value", "kind": 32, "kindString": "Variable", @@ -257,32 +295,32 @@ "title": "Type aliases", "kind": 4194304, "children": [ - 20 + 23 ] }, { "title": "Variables", "kind": 32, "children": [ - 21 + 24 ] } ] }, { - "id": 17, + "id": 20, "name": "GH1366", "kind": 2, "kindString": "Namespace", "flags": {}, "children": [ { - "id": 18, + "id": 21, "name": "Foo", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 15 + "target": 18 } ], "groups": [ @@ -290,20 +328,20 @@ "title": "References", "kind": 16777216, "children": [ - 18 + 21 ] } ] }, { - "id": 15, + "id": 18, "name": "Foo", "kind": 256, "kindString": "Interface", "flags": {}, "children": [ { - "id": 16, + "id": 19, "name": "prop", "kind": 1024, "kindString": "Property", @@ -319,7 +357,7 @@ "title": "Properties", "kind": 1024, "children": [ - 16 + 19 ] } ] @@ -330,15 +368,15 @@ "title": "Namespaces", "kind": 2, "children": [ - 19, - 17 + 22, + 20 ] }, { "title": "Interfaces", "kind": 256, "children": [ - 15 + 18 ] } ] @@ -351,7 +389,8 @@ "children": [ 1, 7, - 14 + 14, + 17 ] } ]