1 | import * as t from 'babel-types'
|
2 | import generate from 'better-babel-generator'
|
3 |
|
4 | import babylonConfig from '../config/babylon'
|
5 |
|
6 | const template = require('babel-template')
|
7 |
|
8 | export function convertObjectToAstExpression (obj: Record<string, any>): t.ObjectProperty[] {
|
9 | const objArr = Object.keys(obj).map(key => {
|
10 | const value = obj[key]
|
11 | if (typeof value === 'string') {
|
12 | return t.objectProperty(t.stringLiteral(key), t.stringLiteral(value))
|
13 | }
|
14 | if (typeof value === 'number') {
|
15 | return t.objectProperty(t.stringLiteral(key), t.numericLiteral(value))
|
16 | }
|
17 | if (typeof value === 'boolean') {
|
18 | return t.objectProperty(t.stringLiteral(key), t.booleanLiteral(value))
|
19 | }
|
20 | if (Array.isArray(value)) {
|
21 | return t.objectProperty(t.stringLiteral(key), t.arrayExpression(convertArrayToAstExpression(value as [])))
|
22 | }
|
23 | if (value === null) {
|
24 | return t.objectProperty(t.stringLiteral(key), t.nullLiteral())
|
25 | }
|
26 | if (typeof value === 'object') {
|
27 | return t.objectProperty(t.stringLiteral(key), t.objectExpression(convertObjectToAstExpression(value)))
|
28 | }
|
29 | return t.objectProperty(t.stringLiteral(key), t.nullLiteral())
|
30 | })
|
31 | return objArr
|
32 | }
|
33 |
|
34 |
|
35 | export function generateMinimalEscapeCode (ast: t.File) {
|
36 | return generate(ast, {
|
37 | jsescOption: {
|
38 | minimal: true
|
39 | }
|
40 | }).code
|
41 | }
|
42 |
|
43 | export function convertArrayToAstExpression (arr: any[]): any[] {
|
44 | return arr.map(value => {
|
45 | if (typeof value === 'string') {
|
46 | return t.stringLiteral(value)
|
47 | }
|
48 | if (typeof value === 'number') {
|
49 | return t.numericLiteral(value)
|
50 | }
|
51 | if (typeof value === 'boolean') {
|
52 | return t.booleanLiteral(value)
|
53 | }
|
54 | if (Array.isArray(value)) {
|
55 | return convertArrayToAstExpression(value)
|
56 | }
|
57 | if (typeof value === 'object') {
|
58 | return t.objectExpression(convertObjectToAstExpression(value))
|
59 | }
|
60 | return t.nullLiteral()
|
61 | })
|
62 | }
|
63 |
|
64 | export function convertSourceStringToAstExpression (str: string, opts: object = {}) {
|
65 | return template(str, Object.assign({}, babylonConfig, opts))()
|
66 | }
|
67 |
|
68 | export function convertAstExpressionToVariable<T = any> (node): T {
|
69 | if (t.isObjectExpression(node)) {
|
70 | const obj = {}
|
71 | const properties = node.properties
|
72 | properties.forEach(property => {
|
73 | if (property.type === 'ObjectProperty' || property.type === 'ObjectMethod') {
|
74 | const key = convertAstExpressionToVariable(property.key)
|
75 | const value = convertAstExpressionToVariable(property.value)
|
76 | obj[key] = value
|
77 | }
|
78 | })
|
79 | return (obj as any) as T
|
80 | } else if (t.isArrayExpression(node)) {
|
81 | return (node.elements.map(convertAstExpressionToVariable) as any) as T
|
82 | } else if (t.isLiteral(node)) {
|
83 | return (node as any).value
|
84 | } else if (t.isIdentifier(node) || t.isJSXIdentifier(node)) {
|
85 | const name = node.name
|
86 | return name === 'undefined' ? ((undefined as any) as T) : ((name as any) as T)
|
87 | } else if (t.isJSXExpressionContainer(node)) {
|
88 | return convertAstExpressionToVariable<T>(node.expression)
|
89 | } else {
|
90 | return (undefined as any) as T
|
91 | }
|
92 | }
|
93 |
|
94 | export const getObjKey = node => {
|
95 | if (t.isIdentifier(node)) {
|
96 | return node.name
|
97 | } else {
|
98 | return node.value
|
99 | }
|
100 | }
|