-
Notifications
You must be signed in to change notification settings - Fork 1
/
macro.js
41 lines (38 loc) · 1.07 KB
/
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
/* global expr, stmt */ /* = require('babel-plugin-ast-literal') */
const t = require('babel-types')
const { createMacro, MacroError } = require('babel-macros')
function funcRetObj(props) {
return expr`
function (i) {
return Object.create( ${t.objectExpression(props)} )
}
`
}
function getter(name, retVal) {
return (expr`
{
get ${name}() {
const o = this;
const v = ${retVal};
Object.defineProperty(this, ${name}, { value: v, enumerable: true });
return v;
}
}
`).properties[0]
}
module.exports = createMacro(function traph({ references, state, babel }) {
references.default.forEach(ref => {
// const t = traph({
// asd: (i, o) => i.x
// })
const obj = ref.container.arguments[0]
const funcs = obj.properties.map(p => {
console.assert(p.value.params[0].name === 'i')
console.assert(p.value.params[1].name === 'o')
return [p.key.name, p.value.body]
})
ref.parentPath.replaceWithMultiple(funcRetObj(funcs.map(([name, body]) =>
getter(name, body)
)))
})
})