From f373302738a995e6a34b8f5163fc4038cf22addd Mon Sep 17 00:00:00 2001 From: Gerrit Birkeland Date: Sat, 9 Jan 2021 13:14:14 -0700 Subject: [PATCH] fix: Crash with destructured export const Resolves #1462 --- src/lib/converter/symbols.ts | 23 +++- src/test/converter/exports/export.ts | 11 ++ src/test/converter/exports/specs.json | 142 +++++++++++++++++------- src/test/converter/variables/specs.json | 44 ++------ 4 files changed, 140 insertions(+), 80 deletions(-) diff --git a/src/lib/converter/symbols.ts b/src/lib/converter/symbols.ts index eb04fe7bc..70f0d4999 100644 --- a/src/lib/converter/symbols.ts +++ b/src/lib/converter/symbols.ts @@ -731,7 +731,10 @@ function convertVariable( setModifiers(declaration, reflection); // Does anyone care about this? I doubt it... - if (hasFlag(symbol.flags, ts.SymbolFlags.BlockScopedVariable)) { + if ( + ts.isVariableDeclaration(declaration) && + hasFlag(symbol.flags, ts.SymbolFlags.BlockScopedVariable) + ) { reflection.setFlag( ReflectionFlag.Const, hasFlag(declaration.parent.flags, ts.NodeFlags.Const) @@ -749,21 +752,29 @@ function convertVariableAsFunction( const declaration = symbol .getDeclarations() ?.find(ts.isVariableDeclaration); - assert(declaration); - const type = context.checker.getTypeOfSymbolAtLocation(symbol, declaration); + const type = context.checker.getTypeOfSymbolAtLocation( + symbol, + declaration ?? symbol.valueDeclaration + ); const reflection = context.createDeclarationReflection( ReflectionKind.Function, symbol, nameOverride ); - setModifiers(declaration, reflection); + setModifiers(declaration ?? symbol.valueDeclaration, reflection); // Does anyone care about this? I doubt it... - if (hasFlag(symbol.flags, ts.SymbolFlags.BlockScopedVariable)) { + if ( + declaration && + hasFlag(symbol.flags, ts.SymbolFlags.BlockScopedVariable) + ) { reflection.setFlag( ReflectionFlag.Const, - hasFlag(declaration.parent.flags, ts.NodeFlags.Const) + hasFlag( + (declaration || symbol.valueDeclaration).parent.flags, + ts.NodeFlags.Const + ) ); } diff --git a/src/test/converter/exports/export.ts b/src/test/converter/exports/export.ts index 7f6723767..f64728e7b 100644 --- a/src/test/converter/exports/export.ts +++ b/src/test/converter/exports/export.ts @@ -25,3 +25,14 @@ export namespace GH1453 { export type Foo = import("./mod").GH1453Helper; } + +export namespace GH1462 { + const sideEffects = { + /** method docs */ + method() {}, + /** prop docs */ + prop: 1, + }; + + export const { method: METHOD, prop: PROP } = sideEffects; +} diff --git a/src/test/converter/exports/specs.json b/src/test/converter/exports/specs.json index a0e97b46f..0af1595b6 100644 --- a/src/test/converter/exports/specs.json +++ b/src/test/converter/exports/specs.json @@ -12,44 +12,44 @@ "flags": {}, "children": [ { - "id": 39, + "id": 43, "name": "Mod", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 29 + "target": 33 }, { - "id": 40, + "id": 44, "name": "ModDefault", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 30 + "target": 34 }, { - "id": 44, + "id": 48, "name": "ThisModule", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 29 + "target": 33 }, { - "id": 42, + "id": 46, "name": "b", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 32 + "target": 36 }, { - "id": 38, + "id": 42, "name": "c", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 32 + "target": 36 }, { "id": 23, @@ -66,7 +66,7 @@ "flags": {}, "type": { "type": "reference", - "id": 29, + "id": 33, "name": "GH1453Helper" } }, @@ -94,7 +94,7 @@ }, "type": { "type": "reference", - "id": 29, + "id": 33, "name": "__module" }, "defaultValue": "..." @@ -109,7 +109,7 @@ }, "type": { "type": "reference", - "id": 29, + "id": 33, "name": "a" }, "defaultValue": "..." @@ -124,7 +124,7 @@ }, "type": { "type": "reference", - "id": 29, + "id": 33, "name": "__module" }, "defaultValue": "..." @@ -151,7 +151,66 @@ ] }, { - "id": 45, + "id": 29, + "name": "GH1462", + "kind": 2, + "kindString": "Namespace", + "flags": {}, + "children": [ + { + "id": 32, + "name": "PROP", + "kind": 32, + "kindString": "Variable", + "flags": {}, + "type": { + "type": "intrinsic", + "name": "number" + } + }, + { + "id": 30, + "name": "METHOD", + "kind": 64, + "kindString": "Function", + "flags": {}, + "signatures": [ + { + "id": 31, + "name": "METHOD", + "kind": 4096, + "kindString": "Call signature", + "flags": {}, + "comment": { + "shortText": "method docs" + }, + "type": { + "type": "intrinsic", + "name": "void" + } + } + ] + } + ], + "groups": [ + { + "title": "Variables", + "kind": 32, + "children": [ + 32 + ] + }, + { + "title": "Functions", + "kind": 64, + "children": [ + 30 + ] + } + ] + }, + { + "id": 49, "name": "GH1453Helper", "kind": 4194304, "kindString": "Type alias", @@ -162,7 +221,7 @@ } }, { - "id": 41, + "id": 45, "name": "a", "kind": 32, "kindString": "Variable", @@ -261,32 +320,33 @@ "title": "References", "kind": 16777216, "children": [ - 39, - 40, + 43, 44, - 42, - 38 + 48, + 46, + 42 ] }, { "title": "Namespaces", "kind": 2, "children": [ - 23 + 23, + 29 ] }, { "title": "Type aliases", "kind": 4194304, "children": [ - 45 + 49 ] }, { "title": "Variables", "kind": 32, "children": [ - 41 + 45 ] }, { @@ -477,38 +537,38 @@ ] }, { - "id": 29, + "id": 33, "name": "mod", "kind": 1, "kindString": "Module", "flags": {}, "children": [ { - "id": 36, + "id": 40, "name": "ThisModule", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 29 + "target": 33 }, { - "id": 33, + "id": 37, "name": "b", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 32 + "target": 36 }, { - "id": 34, + "id": 38, "name": "c", "kind": 16777216, "kindString": "Reference", "flags": {}, - "target": 32 + "target": 36 }, { - "id": 37, + "id": 41, "name": "GH1453Helper", "kind": 4194304, "kindString": "Type alias", @@ -519,7 +579,7 @@ } }, { - "id": 32, + "id": 36, "name": "a", "kind": 32, "kindString": "Variable", @@ -536,14 +596,14 @@ "defaultValue": "1" }, { - "id": 30, + "id": 34, "name": "default", "kind": 64, "kindString": "Function", "flags": {}, "signatures": [ { - "id": 31, + "id": 35, "name": "default", "kind": 4096, "kindString": "Call signature", @@ -564,30 +624,30 @@ "title": "References", "kind": 16777216, "children": [ - 36, - 33, - 34 + 40, + 37, + 38 ] }, { "title": "Type aliases", "kind": 4194304, "children": [ - 37 + 41 ] }, { "title": "Variables", "kind": 32, "children": [ - 32 + 36 ] }, { "title": "Functions", "kind": 64, "children": [ - 30 + 34 ] } ] @@ -602,7 +662,7 @@ 1, 6, 8, - 29 + 33 ] } ] diff --git a/src/test/converter/variables/specs.json b/src/test/converter/variables/specs.json index 64cff679f..c0306a8e5 100644 --- a/src/test/converter/variables/specs.json +++ b/src/test/converter/variables/specs.json @@ -306,9 +306,7 @@ "name": "destructArrayA", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "number" @@ -319,9 +317,7 @@ "name": "destructArrayB", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "string" @@ -332,9 +328,7 @@ "name": "destructArrayC", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "union", "types": [ @@ -355,9 +349,7 @@ "name": "destructArrayWithIgnoresA", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "number" @@ -368,9 +360,7 @@ "name": "destructArrayWithIgnoresRest", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "tuple", "elements": [ @@ -390,9 +380,7 @@ "name": "destructArrayWithRest", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "tuple", "elements": [ @@ -412,9 +400,7 @@ "name": "destructArrayWithRestA", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "number" @@ -425,9 +411,7 @@ "name": "destructArrayWithRestB", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "number" @@ -438,9 +422,7 @@ "name": "destructObjectA", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "number" @@ -451,9 +433,7 @@ "name": "destructObjectB", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "string" @@ -464,9 +444,7 @@ "name": "destructObjectC", "kind": 32, "kindString": "Variable", - "flags": { - "isLet": true - }, + "flags": {}, "type": { "type": "intrinsic", "name": "number"