-
Notifications
You must be signed in to change notification settings - Fork 135
/
eval.macro.js
54 lines (47 loc) · 1.63 KB
/
eval.macro.js
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
const {parse} = require('@babel/parser')
// const printAST = require('ast-pretty-print')
const {createMacro} = require('../../')
module.exports = createMacro(evalMacro)
function evalMacro({references, state}) {
references.default.forEach(referencePath => {
if (referencePath.parentPath.type === 'TaggedTemplateExpression') {
asTag(referencePath.parentPath.get('quasi'), state)
} else if (referencePath.parentPath.type === 'CallExpression') {
asFunction(referencePath.parentPath.get('arguments'), state)
} else if (referencePath.parentPath.type === 'JSXOpeningElement') {
asJSX(
{
attributes: referencePath.parentPath.get('attributes'),
children: referencePath.parentPath.parentPath.get('children'),
},
state,
)
} else {
// TODO: throw a helpful error message
}
})
}
function asTag(quasiPath) {
const value = quasiPath.parentPath.get('quasi').evaluate().value
quasiPath.parentPath.replaceWith(evalToAST(value))
}
function asFunction(argumentsPaths) {
const value = argumentsPaths[0].evaluate().value
argumentsPaths[0].parentPath.replaceWith(evalToAST(value))
}
// eslint-disable-next-line no-unused-vars
function asJSX({attributes, children}) {
// It's a shame you cannot use evaluate() with JSX
const value = children[0].node.value
children[0].parentPath.replaceWith(evalToAST(value))
}
function evalToAST(value) {
let x
// eslint-disable-next-line
eval(`x = ${value}`)
return thingToAST(x)
}
function thingToAST(object) {
const fileNode = parse(`var x = ${JSON.stringify(object)}`)
return fileNode.program.body[0].declarations[0].init
}