Skip to content

Commit

Permalink
fix(api): some enum literals not working
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 27, 2022
1 parent 3b477b5 commit d507076
Show file tree
Hide file tree
Showing 5 changed files with 532 additions and 4 deletions.
1 change: 0 additions & 1 deletion packages/api/src/localizedTree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,6 @@ import {
import { SymbolFlags } from "./typescript"

// TODO: optional param booleans can sometimes become undefined|true|false (should just be boolean)
// TODO: enum value aliases sometimes aren't working (like in SymbolFlags up above)

type TypeInfoRetriever = (
location: SourceFileLocation
Expand Down
16 changes: 13 additions & 3 deletions packages/api/src/tree.ts
Original file line number Diff line number Diff line change
Expand Up @@ -269,13 +269,23 @@ function _generateTypeTree(
return { kind: "primitive", primitive: "number" }
} else if (flags & ts.TypeFlags.Void) {
return { kind: "primitive", primitive: "void" }
} else if (flags & ts.TypeFlags.EnumLiteral) {
} else if (
flags & ts.TypeFlags.EnumLiteral ||
(flags & ts.TypeFlags.EnumLike &&
symbol &&
symbol.flags & ts.SymbolFlags.EnumMember)
) {
const enumSymbol =
symbol && symbol.flags & ts.SymbolFlags.EnumMember
? symbol
: type.symbol

return {
kind: "enum_literal",
value: (type as ts.StringLiteralType).value,
literalSymbol: getSymbolInfo(type.symbol),
literalSymbol: getSymbolInfo(enumSymbol),
parentSymbol: wrapSafe(getSymbolInfo)(
(type.symbol as TSSymbol).parent
(enumSymbol as TSSymbol).parent
),
}
} else if (flags & ts.TypeFlags.Enum) {
Expand Down
210 changes: 210 additions & 0 deletions tests/baselines/reference/enumWithBitshift.localized.tree
Original file line number Diff line number Diff line change
@@ -0,0 +1,210 @@
=== enumWithBitshift.ts ===

enum SymbolFlags {
None = 0,
FunctionScopedVariable = 1 << 0, // Variable (var) or parameter
}
> SymbolFlags --- {
"kindText": "enum",
"kind": "enum",
"symbol": {
"name": "SymbolFlags",
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 0,
"character": 5
},
"end": {
"line": 0,
"character": 16
}
}
}
]
},
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 0,
"character": 5
},
"end": {
"line": 0,
"character": 16
}
}
}
],
"children": [
{
"kindText": "enum",
"kind": "enum_literal",
"alias": "SymbolFlags.None",
"symbol": {
"name": "None",
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 1,
"character": 4
},
"end": {
"line": 1,
"character": 8
}
}
}
]
},
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 1,
"character": 4
},
"end": {
"line": 1,
"character": 8
}
}
}
],
"children": [],
"_id": "1"
},
{
"kindText": "enum",
"kind": "enum_literal",
"alias": "SymbolFlags.FunctionScopedVariable",
"symbol": {
"name": "FunctionScopedVariable",
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 26
}
}
}
]
},
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 26
}
}
}
],
"children": [],
"_id": "2"
}
],
"_id": "0"
}
> None = 0,
FunctionScopedVariable = 1 << 0,
> None = 0
> None --- {
"kindText": "enum",
"kind": "enum_literal",
"alias": "SymbolFlags.None",
"symbol": {
"name": "None",
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 1,
"character": 4
},
"end": {
"line": 1,
"character": 8
}
}
}
]
},
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 1,
"character": 4
},
"end": {
"line": 1,
"character": 8
}
}
}
],
"children": [],
"_id": "0"
}
> FunctionScopedVariable = 1 << 0
> FunctionScopedVariable --- {
"kindText": "enum",
"kind": "enum_literal",
"alias": "SymbolFlags.FunctionScopedVariable",
"symbol": {
"name": "FunctionScopedVariable",
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 26
}
}
}
]
},
"locations": [
{
"fileName": "cases/enumWithBitshift.ts",
"range": {
"start": {
"line": 2,
"character": 4
},
"end": {
"line": 2,
"character": 26
}
}
}
],
"children": [],
"_id": "0"
}
Loading

0 comments on commit d507076

Please sign in to comment.