1 /* eslint-disable no-case-declarations */
2 import {
3 ArrayExpression,
4 Estree,
5 Expression,
6 ExpressionJsVar,
7 ExpressionJsVarLiteral,
8 JSXElementExpression,
9 JSXTextExpression,
10 LiteralExpression,
11 ObjectExpression,
12 TemplateLiteralExpression,
13 VariableDeclaration,
14 } from "types"
15
16 const ALLOWED_BODY_TYPES = ["ExpressionStatement", "ExportNamedDeclaration"]
17
18 export function estreeToJs(estree: Estree) {
19 // TODO improve on this utility. Currently it's implemented to work
20 // for specific use cases as we don't have a lot of info on other
21 // use cases.
22 if (
23 !estree.body?.length ||
24 !estree.body[0].type ||
25 !ALLOWED_BODY_TYPES.includes(estree.body[0].type)
26 ) {
27 return
28 }
29
30 switch (estree.body[0].type) {
31 case "ExpressionStatement":
32 if (!estree.body[0].expression) {
33 return
34 }
35 return expressionToJs(estree.body[0].expression)
36 case "ExportNamedDeclaration":
37 if (!estree.body[0].declaration) {
38 return
39 }
40 return declarationToJs(estree.body[0].declaration)
41 }
42 }
43
44 export function expressionToJs(
45 expression: Expression
46 ): ExpressionJsVar | ExpressionJsVar[] | undefined {
47 switch (expression.type) {
48 case "ArrayExpression":
49 const arrVar: ExpressionJsVar[] = []
50 ;(expression as ArrayExpression).elements.forEach((elm) => {
51 const elmJsVar = expressionToJs(elm)
52 if (!elmJsVar) {
53 return
54 }
55 if (Array.isArray(elmJsVar)) {
56 arrVar.push(...elmJsVar)
57 } else {
58 arrVar.push(elmJsVar)
59 }
60 })
61 return arrVar
62 case "ObjectExpression":
63 const objVar: ExpressionJsVar = {}
64 ;(expression as ObjectExpression).properties.forEach((property) => {
65 const keyName = property.key.name ?? property.key.value
66
67 if (!keyName) {
68 return
69 }
70 const jsVal = expressionToJs(property.value)
71 if (!jsVal) {
72 return
73 }
74
75 objVar[keyName] = jsVal
76 })
77 return objVar
78 case "Literal":
79 return {
80 original: expression,
81 data: (expression as LiteralExpression).value,
82 } as ExpressionJsVarLiteral
83 case "JSXElement":
84 case "JSXFragment":
85 // Only take text children
86 let text = ""
87 ;(expression as JSXElementExpression).children.forEach((child) => {
88 if (child.type !== "JSXText") {
89 return
90 }
91
92 text += (child as JSXTextExpression).value
93 })
94 return {
95 original: {
96 type: "Literal",
97 value: text,
98 raw: `"${text}"`,
99 },
100 data: text,
101 }
102 case "TemplateLiteral":
103 const templateExpression = expression as TemplateLiteralExpression
104 let value = ""
105 let raw = ""
106 templateExpression.quasis.forEach((quasi) => {
107 value += quasi.value.cooked
108 raw += quasi.value.raw
109 })
110 return {
111 original: {
112 type: "Literal",
113 value,
114 raw,
115 },
116 data: value,
117 }
118 }
119 }
120
121 function declarationToJs(declaration: VariableDeclaration): {
122 name: string
123 value: ExpressionJsVar | ExpressionJsVar[] | undefined
124 } {
125 if (!declaration.declarations.length) {
126 throw new Error("No declarations found")
127 }
128 const name = declaration.declarations[0].id.name
129 const value = expressionToJs(declaration.declarations[0].init)
130 return {
131 name,
132 value,
133 }
134 }