import type { Node, NodePath } from '@babel/core' import { types as t } from '@babel/core' import type { Ctx } from 'rntwsc/devtools/babel-plugin-tw/lib/context' import type { StrMap } from 'rntwsc/libs/utility-types' export type Literal = undefined | null | boolean | number | string export type WithPath = { path: NodePath keyPath?: NodePath value: T } export type MapWp = StrMap> export type ArrWp = WithPath[] export const pathToNode = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, ): WithPath => ({ path, keyPath, value: path.node, }) export const isUndefined = (node: t.Node) => t.isIdentifier(node, { name: 'undefined', }) export const pathToLiteral = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, ): WithPath => { let value const v = path.node if (isUndefined(v)) { value = undefined } else if (t.isNullLiteral(v)) { value = null } else if ( t.isBooleanLiteral(v) || t.isNumericLiteral(v) || t.isStringLiteral(v) ) { value = v.value } else { throw ctx.err(path, `expect a literal, found ${v.type}`) } return { path, value, keyPath, } } export const pathToString = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, ): WithPath => { const l = pathToLiteral(ctx, path, keyPath) if (typeof l.value !== 'string') { throw ctx.err(path, 'expect string literal') } return l as any } export const pathToPropertyName = (ctx: Ctx, path: NodePath) => { const k = path.node let name if (t.isIdentifier(k)) { name = k.name } else if (t.isStringLiteral(k)) { name = k.value } else { throw ctx.err(path, `can not get property name from type ${k.type}`) } if (!/^[a-z]/.test(name)) { // reserved some keys to use in runtime throw ctx.err(path, `expect property name "${name}" to start with a-z`) } return name } export const pathToObject = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, inner: (k: string, kPath: NodePath, innerPath: NodePath) => WithPath, ) => { if (!t.isObjectExpression(path.node)) { throw ctx.err(path, 'expect simple object literal') } const value: MapWp = {} for (const innerPath of path.get('properties')) { const p = innerPath.node if (!t.isObjectProperty(p) || p.computed) { throw ctx.err(innerPath, 'expect simple object properties') } const kPath = innerPath.get('key') const k = pathToPropertyName(ctx, kPath) value[k] = inner(k, kPath, innerPath.get('value')) } return { path, value, keyPath, } } export const pathToObjectLiteral = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, ) => pathToObject(ctx, path, keyPath, (k, kPath, innerPath) => pathToLiteral(ctx, innerPath, kPath), ) export const pathToObjectString = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, ) => pathToObject(ctx, path, keyPath, (k, kPath, innerPath) => pathToString(ctx, innerPath, kPath), ) export const pathToArray = ( ctx: Ctx, path: NodePath, keyPath: NodePath | undefined, inner: (i: number, innerPath: NodePath) => WithPath, ) => { if (!t.isArrayExpression(path.node)) { throw ctx.err(path, 'expect simple array literal') } const value: ArrWp = [] for (const [i, innerPath] of path.get('elements').entries()) { if (!innerPath?.node) { throw ctx.err(innerPath, `missing array element at index ${i}`) } value.push(inner(i, innerPath as any)) } return { path, value, keyPath, } }