This repository has been archived by the owner on Jan 1, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 12
/
utils.ts
145 lines (125 loc) · 3.42 KB
/
utils.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
import {
isArrayExpression,
isBooleanLiteral,
isObjectExpression,
isIdentifier,
isNullLiteral,
isNumericLiteral,
isUnaryExpression,
isStringLiteral,
ObjectExpression,
ObjectProperty,
isObjectProperty,
NumericLiteral,
StringLiteral,
BooleanLiteral,
ArrayExpression,
NullLiteral,
} from '@babel/types'
type ValidJsonValue =
| NumericLiteral
| StringLiteral
| BooleanLiteral
| NullLiteral
| ArrayExpression
| ObjectExpression
const isValidJsonValue = (
node: object | null | undefined
): node is ValidJsonValue => {
if (
isNumericLiteral(node) ||
isStringLiteral(node) ||
isBooleanLiteral(node) ||
isNullLiteral(node) ||
isArrayExpression(node) ||
isObjectExpression(node)
) {
return true
}
return false
}
type ObjectExpressionWithOnlyObjectProperties = Omit<
ObjectExpression,
'properties'
> & {
properties: ObjectProperty[]
}
/**
* Check whether given ObjectExpression consists of only `ObjectProperty`s as its properties.
*/
const isObjectExpressionWithOnlyObjectProperties = (
node: ObjectExpression
): node is ObjectExpressionWithOnlyObjectProperties => {
return node.properties.every((property) => isObjectProperty(property))
}
const isConvertibleObjectProperty = (properties: ObjectProperty[]) => {
return properties.every((node) => !node.computed)
}
const createSafeStringForJsonParse = (value: string) => {
if (/\\/.test(value)) {
value = value.replace(/\\/g, '\\\\')
}
if (/"/.test(value)) {
value = value.replace(/"/g, '\\"')
}
if (/[\t\f\r\n\b]/g.test(value)) {
const codes = ['\f', '\r', '\n', '\t', '\b']
const replaceCodes = ['\\f', '\\r', '\\n', '\\t', '\\b']
for (let i = 0; i < codes.length; i++) {
value = value.replace(new RegExp(codes[i], 'g'), replaceCodes[i])
}
}
return value
}
export function converter(node: object | null | undefined): unknown {
// for negative number, ex) -10
if (isUnaryExpression(node)) {
const { operator, argument } = node
if (operator === '-' && isNumericLiteral(argument)) {
return -argument.value
}
}
if (!isValidJsonValue(node)) {
throw new Error('Invalid value is included.')
}
if (isStringLiteral(node)) {
const { value } = node
const safeValue = createSafeStringForJsonParse(value)
return safeValue
}
if (isNullLiteral(node)) {
return null
}
if (isArrayExpression(node)) {
const { elements } = node
return elements.map((node) => converter(node))
}
if (isObjectExpression(node)) {
if (!isObjectExpressionWithOnlyObjectProperties(node)) {
throw new Error('Invalid syntax is included.')
}
const { properties } = node
if (!isConvertibleObjectProperty(properties)) {
throw new Error('Invalid syntax is included.')
}
return properties.reduce((acc, cur) => {
let key = ''
if (isIdentifier(cur.key)) {
key = createSafeStringForJsonParse(cur.key.name)
} else if (isStringLiteral(cur.key)) {
key = createSafeStringForJsonParse(cur.key.value)
} else if (isNumericLiteral(cur.key)) {
// see issues#10
if (!Number.isSafeInteger(cur.key.value)) {
throw new Error('Invalid syntax is included.')
}
key = cur.key.value.toString()
} else {
throw new Error('Invalid syntax is included.')
}
const value = converter(cur.value)
return { ...acc, [key]: value }
}, {})
}
return node.value
}