Skip to content

Commit

Permalink
feat(vscode): support bigint literals
Browse files Browse the repository at this point in the history
  • Loading branch information
mxsdev committed Oct 12, 2022
1 parent 190a996 commit 8353913
Show file tree
Hide file tree
Showing 6 changed files with 29 additions and 14 deletions.
2 changes: 1 addition & 1 deletion packages/api/src/index.ts
@@ -1,5 +1,5 @@
export { APIConfig } from './config'
export { getSymbolType, multilineTypeToString } from "./util"
export { getSymbolType, multilineTypeToString, pseudoBigIntToString } from "./util"
export { recursivelyExpandType } from "./merge"
export { generateTypeTree, getTypeInfoChildren } from "./tree"
export { TypeInfo, SymbolInfo, SignatureInfo, TypeId, IndexInfo, TypeInfoKind, TypeParameterInfo } from "./types"
5 changes: 2 additions & 3 deletions packages/api/src/tree.ts
Expand Up @@ -93,6 +93,8 @@ function _generateTypeTree({ symbol, type }: SymbolOrType, ctx: TypeTreeContext,
else if(flags & ts.TypeFlags.String) { return { kind: 'primitive', primitive: 'string' }}
else if(flags & ts.TypeFlags.Number) { return { kind: 'primitive', primitive: 'number' }}
else if(flags & ts.TypeFlags.Void) { return { kind: 'primitive', primitive: 'void' }}
// TODO: enum literal
else if(flags & ts.TypeFlags.EnumLiteral) { return { kind: 'enum_literal', value: (type as ts.StringLiteralType).value }}
// TODO: add enum info ?
else if(flags & ts.TypeFlags.Enum) { return { kind: 'primitive', primitive: 'enum' }}
else if(flags & ts.TypeFlags.BigInt) { return { kind: 'primitive', primitive: 'bigint' }}
Expand All @@ -102,9 +104,6 @@ function _generateTypeTree({ symbol, type }: SymbolOrType, ctx: TypeTreeContext,
else if(flags & ts.TypeFlags.StringLiteral) { return { kind: 'string_literal', value: (type as ts.StringLiteralType).value }}
else if(flags & ts.TypeFlags.NumberLiteral) { return { kind: 'number_literal', value: (type as ts.NumberLiteralType).value }}
else if(flags & ts.TypeFlags.BooleanLiteral) { return { kind: 'boolean_literal', value: (type as IntrinsicTypeInternal).intrinsicName === "true" }}
// TODO: enum literal
// else if(flags & ts.TypeFlags.EnumLiteral) { return { kind: 'enum_literal', value: (type as ts.StringLiteralType).value }}
// TODO: add enum info???
else if(flags & ts.TypeFlags.BigIntLiteral) { return { kind: 'bigint_literal', value: (type as ts.BigIntLiteralType).value }}
// TODO: add type param info
else if(flags & ts.TypeFlags.Object) {
Expand Down
4 changes: 4 additions & 0 deletions packages/api/src/util.ts
Expand Up @@ -225,6 +225,10 @@ export function getCheckFlags(symbol: ts.Symbol): CheckFlags {
return symbol.flags & ts.SymbolFlags.Transient ? (symbol as TransientSymbol).checkFlags : 0;
}

export function pseudoBigIntToString(value: ts.PseudoBigInt) {
return (value.negative ? "-" : "") + value.base10Value
}

export const enum CheckFlags {
Instantiated = 1 << 0, // Instantiated symbol
SyntheticProperty = 1 << 1, // Property in union or intersection type
Expand Down
6 changes: 3 additions & 3 deletions packages/typescript-explorer/src/localization.ts
Expand Up @@ -20,7 +20,7 @@ export const PrimitiveKindText: Record<PrimitiveKind, string> = {

type Kind = Exclude<TypeInfo['kind'], 'reference'>
export const KindText: Record<Kind, string> = {
"bigint_literal": "$1L",
"bigint_literal": "$1n",
"boolean_literal": "$1",
"enum_literal": "$1",
"number_literal": "$1",
Expand All @@ -44,9 +44,9 @@ export const KindText: Record<Kind, string> = {
"intrinsic": "intrinsic",
}

export function getKindText(kind: Kind, ...args: {toString(): string}[]) {
export function getKindText<K extends Kind>(kind: K, ...args: string[]) {
return args.reduce<string>((prev, curr, i) => {
return prev.replace(new RegExp(`\\\$${i+1}`, "g"), curr.toString())
return prev.replace(new RegExp(`\\\$${i+1}`, "g"), curr)
}, KindText[kind])
}

Expand Down
24 changes: 18 additions & 6 deletions packages/typescript-explorer/src/view/typeTreeView.ts
@@ -1,4 +1,4 @@
import { TypeInfo, TypeId, getTypeInfoChildren, SymbolInfo, SignatureInfo, IndexInfo } from '@ts-expand-type/api'
import { TypeInfo, TypeId, getTypeInfoChildren, SymbolInfo, SignatureInfo, IndexInfo, pseudoBigIntToString } from '@ts-expand-type/api'
import assert = require('assert');
import * as vscode from 'vscode'
import * as ts from 'typescript'
Expand Down Expand Up @@ -360,14 +360,26 @@ function generateTypeNodeMeta(info: ResolvedTypeInfo, dimension: number, {purpos
return getPrimitiveKindText(info.primitive)
}

case "bigint_literal":
case "boolean_literal":
case "enum_literal":
case "string_literal":
case "number_literal": {
case "bigint_literal": {
return getKindText(info.kind, pseudoBigIntToString(info.value))
}

case "boolean_literal": {
return getKindText(info.kind, info.value.toString())
}

case "enum_literal": {
return getKindText(info.kind, info.value)
}

case "string_literal": {
return getKindText(info.kind, info.value)
}

case "number_literal": {
return getKindText(info.kind, info.value.toString())
}

default: {
return getKindText(info.kind)
}
Expand Down
2 changes: 1 addition & 1 deletion tests/baselines/reference/consoleLog.tree

Large diffs are not rendered by default.

0 comments on commit 8353913

Please sign in to comment.