Something went wrong. Try again.
[READ-ONLY] Mirror of https://github.com/sxzz/ast-kit. A toolkit for easy Babel AST generation and manipulation. jsr.io/@sxzz/ast-kit
Something went wrong. Try again.
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647import { parseExpression } from '@babel/parser'import { isTypeOf } from './check.ts'import type * as t from '@babel/types'
export const TS_NODE_TYPES: readonly [ 'TSAsExpression', 'TSTypeAssertion', 'TSNonNullExpression', 'TSInstantiationExpression', 'TSSatisfiesExpression',] = [ 'TSAsExpression', // foo as number 'TSTypeAssertion', // (<number>foo) 'TSNonNullExpression', // foo! 'TSInstantiationExpression', // foo<string> 'TSSatisfiesExpression', // foo satisfies T] as const satisfies readonly t.Node['type'][]
/** * Unwraps a TypeScript node by recursively traversing the AST until a non-TypeScript node is found. * @param node - The TypeScript node to unwrap. * @returns The unwrapped node. */export function unwrapTSNode(node: t.Node): t.Node { if (isTypeOf(node, TS_NODE_TYPES)) { return unwrapTSNode(node.expression) } else { return node }}
/** * Escapes a raw key by checking if it needs to be wrapped with quotes or not. * * @param rawKey - The raw key to escape. * @returns The escaped key. */export function escapeKey(rawKey: string): string { if (String(+rawKey) === rawKey) return rawKey try { const node = parseExpression(`({${rawKey}: 1})`) as t.ObjectExpression if ((node.properties[0] as t.ObjectProperty).key.type === 'Identifier') return rawKey } catch {} return JSON.stringify(rawKey)}