From d507076adcbcfe414818e8a46dbb736bbfe3907e Mon Sep 17 00:00:00 2001 From: charburgx Date: Thu, 27 Oct 2022 05:06:15 -0500 Subject: [PATCH] fix(api): some enum literals not working --- packages/api/src/localizedTree.ts | 1 - packages/api/src/tree.ts | 16 +- .../reference/enumWithBitshift.localized.tree | 210 ++++++++++++ .../baselines/reference/enumWithBitshift.tree | 305 ++++++++++++++++++ tests/cases/enumWithBitshift.ts | 4 + 5 files changed, 532 insertions(+), 4 deletions(-) create mode 100644 tests/baselines/reference/enumWithBitshift.localized.tree create mode 100644 tests/baselines/reference/enumWithBitshift.tree create mode 100644 tests/cases/enumWithBitshift.ts diff --git a/packages/api/src/localizedTree.ts b/packages/api/src/localizedTree.ts index b035be4..e8c12fb 100644 --- a/packages/api/src/localizedTree.ts +++ b/packages/api/src/localizedTree.ts @@ -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 diff --git a/packages/api/src/tree.ts b/packages/api/src/tree.ts index 3c06123..365130d 100644 --- a/packages/api/src/tree.ts +++ b/packages/api/src/tree.ts @@ -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) { diff --git a/tests/baselines/reference/enumWithBitshift.localized.tree b/tests/baselines/reference/enumWithBitshift.localized.tree new file mode 100644 index 0000000..8eace79 --- /dev/null +++ b/tests/baselines/reference/enumWithBitshift.localized.tree @@ -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" +} \ No newline at end of file diff --git a/tests/baselines/reference/enumWithBitshift.tree b/tests/baselines/reference/enumWithBitshift.tree new file mode 100644 index 0000000..f6dcfc3 --- /dev/null +++ b/tests/baselines/reference/enumWithBitshift.tree @@ -0,0 +1,305 @@ +=== enumWithBitshift.ts === + +enum SymbolFlags { + None = 0, + FunctionScopedVariable = 1 << 0, // Variable (var) or parameter +} +> SymbolFlags --- { + "kind": "enum", + "properties": [ + { + "kind": "enum_literal", + "literalSymbol": { + "name": "None", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 1, + "character": 4 + }, + "end": { + "line": 1, + "character": 8 + } + } + } + } + ] + }, + "parentSymbol": { + "name": "SymbolFlags", + "flags": 256, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 0, + "character": 5 + }, + "end": { + "line": 0, + "character": 16 + } + } + } + } + ] + }, + "symbolMeta": { + "name": "None", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 1, + "character": 4 + }, + "end": { + "line": 1, + "character": 8 + } + } + } + } + ] + }, + "id": "1" + }, + { + "kind": "enum_literal", + "literalSymbol": { + "name": "FunctionScopedVariable", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 2, + "character": 4 + }, + "end": { + "line": 2, + "character": 26 + } + } + } + } + ] + }, + "parentSymbol": { + "name": "SymbolFlags", + "flags": 256, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 0, + "character": 5 + }, + "end": { + "line": 0, + "character": 16 + } + } + } + } + ] + }, + "symbolMeta": { + "name": "FunctionScopedVariable", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 2, + "character": 4 + }, + "end": { + "line": 2, + "character": 26 + } + } + } + } + ] + }, + "id": "2" + } + ], + "symbolMeta": { + "name": "SymbolFlags", + "flags": 256, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 0, + "character": 5 + }, + "end": { + "line": 0, + "character": 16 + } + } + } + } + ] + }, + "id": "0" +} +> None = 0, + FunctionScopedVariable = 1 << 0, +> None = 0 +> None --- { + "kind": "enum_literal", + "literalSymbol": { + "name": "None", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 1, + "character": 4 + }, + "end": { + "line": 1, + "character": 8 + } + } + } + } + ] + }, + "parentSymbol": { + "name": "SymbolFlags", + "flags": 256, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 0, + "character": 5 + }, + "end": { + "line": 0, + "character": 16 + } + } + } + } + ] + }, + "symbolMeta": { + "name": "None", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 1, + "character": 4 + }, + "end": { + "line": 1, + "character": 8 + } + } + } + } + ] + }, + "id": "0" +} +> FunctionScopedVariable = 1 << 0 +> FunctionScopedVariable --- { + "kind": "enum_literal", + "literalSymbol": { + "name": "FunctionScopedVariable", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 2, + "character": 4 + }, + "end": { + "line": 2, + "character": 26 + } + } + } + } + ] + }, + "parentSymbol": { + "name": "SymbolFlags", + "flags": 256, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 0, + "character": 5 + }, + "end": { + "line": 0, + "character": 16 + } + } + } + } + ] + }, + "symbolMeta": { + "name": "FunctionScopedVariable", + "flags": 8, + "declarations": [ + { + "location": { + "fileName": "cases/enumWithBitshift.ts", + "range": { + "start": { + "line": 2, + "character": 4 + }, + "end": { + "line": 2, + "character": 26 + } + } + } + } + ] + }, + "id": "0" +} \ No newline at end of file diff --git a/tests/cases/enumWithBitshift.ts b/tests/cases/enumWithBitshift.ts new file mode 100644 index 0000000..292cc0b --- /dev/null +++ b/tests/cases/enumWithBitshift.ts @@ -0,0 +1,4 @@ +enum SymbolFlags { + None = 0, + FunctionScopedVariable = 1 << 0, // Variable (var) or parameter +} \ No newline at end of file