Skip to content

Commit

Permalink
fix: Crash with destructured export const
Browse files Browse the repository at this point in the history
Resolves #1462
  • Loading branch information
Gerrit0 committed Jan 9, 2021
1 parent 28c234e commit f373302
Show file tree
Hide file tree
Showing 4 changed files with 140 additions and 80 deletions.
23 changes: 17 additions & 6 deletions src/lib/converter/symbols.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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
)
);
}

Expand Down
11 changes: 11 additions & 0 deletions src/test/converter/exports/export.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
142 changes: 101 additions & 41 deletions src/test/converter/exports/specs.json
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -66,7 +66,7 @@
"flags": {},
"type": {
"type": "reference",
"id": 29,
"id": 33,
"name": "GH1453Helper"
}
},
Expand Down Expand Up @@ -94,7 +94,7 @@
},
"type": {
"type": "reference",
"id": 29,
"id": 33,
"name": "__module"
},
"defaultValue": "..."
Expand All @@ -109,7 +109,7 @@
},
"type": {
"type": "reference",
"id": 29,
"id": 33,
"name": "a"
},
"defaultValue": "..."
Expand All @@ -124,7 +124,7 @@
},
"type": {
"type": "reference",
"id": 29,
"id": 33,
"name": "__module"
},
"defaultValue": "..."
Expand All @@ -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",
Expand All @@ -162,7 +221,7 @@
}
},
{
"id": 41,
"id": 45,
"name": "a",
"kind": 32,
"kindString": "Variable",
Expand Down Expand Up @@ -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
]
},
{
Expand Down Expand Up @@ -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",
Expand All @@ -519,7 +579,7 @@
}
},
{
"id": 32,
"id": 36,
"name": "a",
"kind": 32,
"kindString": "Variable",
Expand All @@ -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",
Expand All @@ -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
]
}
]
Expand All @@ -602,7 +662,7 @@
1,
6,
8,
29
33
]
}
]
Expand Down
Loading

0 comments on commit f373302

Please sign in to comment.