-
Notifications
You must be signed in to change notification settings - Fork 1
/
babel-plugin.js
113 lines (107 loc) · 4.76 KB
/
babel-plugin.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
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
//////////////////////////////////////////
//
// ONLY EDIT babel-plugin.js at ./src/babel-plugin.js, not ./babel-plugin.js!
//
//////////////////////////////////////////
var plugin = function(babel) {
return {
visitor: {
ObjectExpression(path) {
path.node.properties.forEach(function(prop){
if (prop.key.type === "Identifier") {
var keyLoc = prop.key.loc
prop.key = babel.types.stringLiteral(prop.key.name)
prop.key.loc = keyLoc
}
})
var call = babel.types.callExpression(
babel.types.identifier("__ohdMakeObject"), [babel.types.arrayExpression(
path.node.properties.map(function(prop) {
var type = babel.types.stringLiteral(prop.type)
type.ignore = true
if (prop.type === "ObjectMethod") {
// getter/setter
var kind = babel.types.stringLiteral(prop.kind);
kind.ignore = true;
var propArray = babel.types.arrayExpression([
type,
prop.key,
kind,
babel.types.functionExpression(
null,
prop.params,
prop.body
)
])
return propArray
} else {
var propArray = babel.types.arrayExpression([
type,
prop.key,
prop.value
])
return propArray
}
console.log("continue with type", prop.type)
})
)]
)
path.replaceWith(call)
},
AssignmentExpression(path) {
if (path.node.ignore) {
return
}
if (["+=", "-=", "/=", "*="].indexOf(path.node.operator) !== -1){
// I don't think this replacement is always going to be 100% equivalent
// to the +=/... operation, but it's close enough for now
// e.g. maybe there'd be props if path.node.left is sth like a.sth().aa would
// call sth twice
var operator = {"+=": "+", "-=": "-", "/=": "/", "*=": "*"}[path.node.operator]
var value = babel.types.binaryExpression(operator, path.node.left, path.node.right)
var replacement = babel.types.assignmentExpression("=", path.node.left, value)
path.replaceWith(replacement)
}
if (path.node.operator === "=" && path.node.left.type === "MemberExpression") {
var property;
if (path.node.left.computed === true) {
property = path.node.left.property
} else {
property = babel.types.stringLiteral(path.node.left.property.name)
property.loc = path.node.left.property.loc
}
var assignExpression = babel.types.callExpression(
babel.types.identifier("__ohdAssign"), [
path.node.left.object,
property,
path.node.right
]
)
assignExpression.loc = path.node.loc
path.replaceWith(assignExpression)
}
},
UnaryExpression(path) {
if (path.node.operator === "delete"){
var prop = path.node.argument.property
if (prop.type === "Identifier"){
prop = babel.types.stringLiteral(prop.name)
}
var call = babel.types.callExpression(
babel.types.identifier("__ohdDeleteProperty"),
[
path.node.argument.object,
prop
]
)
path.replaceWith(call)
}
}
}
}
}
if (typeof module === "undefined"){
window.babelPlugin = plugin;
} else {
module.exports = plugin
}