-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathindex.js
56 lines (48 loc) · 1.63 KB
/
index.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
55
56
let syncRe = /(.*)\$sync/
const camelizeRE = /-(\w)/g;
const camelize = (str) => {
return str.replace(camelizeRE, (_, c) => c ? c.toUpperCase() : '')
}
var genListener = function genListener(t, event, body) {
return t.jSXAttribute(
t.jSXIdentifier('on' + event),
t.jSXExpressionContainer(t.ArrowFunctionExpression([t.Identifier('$$val')], t.BlockStatement(body)))
)
}
var genAssignmentCode = function genAssignmentCode(t, model) {
return t.ExpressionStatement(t.AssignmentExpression('=', model, t.Identifier('$$val')))
}
module.exports = function(babel) {
const t = babel.types
return {
inherits: require('babel-plugin-syntax-jsx'),
visitor: {
JSXOpeningElement: function(path) {
path.get('attributes').forEach(function(attr) {
try {
let matched = attr.node.name.name.match(syncRe)
if (matched) {
let prop = matched[1]
attr.node.name.name = prop
let model
attr.traverse({
JSXExpressionContainer: function(path) {
model = path.node.expression
}
})
if (!t.isMemberExpression(model)) {
console.error('You should use MemberExpression with sync modifier, prop [' + prop + '] on node [' + path.node.name.name + ']')
return
}
let listener = genListener(t, 'Update:' + camelize(prop),
[genAssignmentCode(t, model)])
attr.insertAfter(listener)
}
} catch (e) {
// console.log(e) ignore these nodes
}
})
}
}
}
}