/
extract-const-value.ts
197 lines (174 loc) · 4.67 KB
/
extract-const-value.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
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
import type {
ArrayExpression,
BooleanLiteral,
ExportDeclaration,
Identifier,
KeyValueProperty,
Module,
Node,
NullLiteral,
NumericLiteral,
ObjectExpression,
RegExpLiteral,
StringLiteral,
VariableDeclaration,
} from '@swc/core'
/**
* Extracts the value of an exported const variable named `exportedName`
* (e.g. "export const config = { runtime: 'experimental-edge' }") from swc's AST.
* The value must be one of (or throws UnsupportedValueError):
* - string
* - boolean
* - number
* - null
* - undefined
* - array containing values listed in this list
* - object containing values listed in this list
*
* Throws NoSuchDeclarationError if the declaration is not found.
*/
export function extractExportedConstValue(
module: Module,
exportedName: string
): any {
for (const moduleItem of module.body) {
if (!isExportDeclaration(moduleItem)) {
continue
}
const declaration = moduleItem.declaration
if (!isVariableDeclaration(declaration)) {
continue
}
if (declaration.kind !== 'const') {
continue
}
for (const decl of declaration.declarations) {
if (
isIdentifier(decl.id) &&
decl.id.value === exportedName &&
decl.init
) {
return extractValue(decl.init)
}
}
}
throw new NoSuchDeclarationError()
}
/**
* A wrapper on top of `extractExportedConstValue` that returns undefined
* instead of throwing when the thrown error is known.
*/
export function tryToExtractExportedConstValue(
module: Module,
exportedName: string
) {
try {
return extractExportedConstValue(module, exportedName)
} catch (error) {
if (
error instanceof UnsupportedValueError ||
error instanceof NoSuchDeclarationError
) {
return undefined
}
}
}
function isExportDeclaration(node: Node): node is ExportDeclaration {
return node.type === 'ExportDeclaration'
}
function isVariableDeclaration(node: Node): node is VariableDeclaration {
return node.type === 'VariableDeclaration'
}
function isIdentifier(node: Node): node is Identifier {
return node.type === 'Identifier'
}
function isBooleanLiteral(node: Node): node is BooleanLiteral {
return node.type === 'BooleanLiteral'
}
function isNullLiteral(node: Node): node is NullLiteral {
return node.type === 'NullLiteral'
}
function isStringLiteral(node: Node): node is StringLiteral {
return node.type === 'StringLiteral'
}
function isNumericLiteral(node: Node): node is NumericLiteral {
return node.type === 'NumericLiteral'
}
function isArrayExpression(node: Node): node is ArrayExpression {
return node.type === 'ArrayExpression'
}
function isObjectExpression(node: Node): node is ObjectExpression {
return node.type === 'ObjectExpression'
}
function isKeyValueProperty(node: Node): node is KeyValueProperty {
return node.type === 'KeyValueProperty'
}
function isRegExpLiteral(node: Node): node is RegExpLiteral {
return node.type === 'RegExpLiteral'
}
class UnsupportedValueError extends Error {}
class NoSuchDeclarationError extends Error {}
function extractValue(node: Node): any {
if (isNullLiteral(node)) {
return null
} else if (isBooleanLiteral(node)) {
// e.g. true / false
return node.value
} else if (isStringLiteral(node)) {
// e.g. "abc"
return node.value
} else if (isNumericLiteral(node)) {
// e.g. 123
return node.value
} else if (isRegExpLiteral(node)) {
// e.g. /abc/i
return new RegExp(node.pattern, node.flags)
} else if (isIdentifier(node)) {
switch (node.value) {
case 'undefined':
return undefined
default:
throw new UnsupportedValueError()
}
} else if (isArrayExpression(node)) {
// e.g. [1, 2, 3]
const arr = []
for (const elem of node.elements) {
if (elem) {
if (elem.spread) {
// e.g. [ ...a ]
throw new UnsupportedValueError()
}
arr.push(extractValue(elem.expression))
} else {
// e.g. [1, , 2]
// ^^
arr.push(undefined)
}
}
return arr
} else if (isObjectExpression(node)) {
// e.g. { a: 1, b: 2 }
const obj: any = {}
for (const prop of node.properties) {
if (!isKeyValueProperty(prop)) {
// e.g. { ...a }
throw new UnsupportedValueError()
}
let key
if (isIdentifier(prop.key)) {
// e.g. { a: 1, b: 2 }
key = prop.key.value
} else if (isStringLiteral(prop.key)) {
// e.g. { "a": 1, "b": 2 }
key = prop.key.value
} else {
throw new UnsupportedValueError()
}
obj[key] = extractValue(prop.value)
}
return obj
} else {
throw new UnsupportedValueError()
}
}