-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathindex.js
191 lines (166 loc) · 4.94 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
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
import syntaxJsx from '@babel/plugin-syntax-jsx'
const PLUGIN_DATA_PREFIX = `@vue/babel-sugar-inject-h_${Date.now()}`
// helpers for ast custom data get/set
const getv = (p, k) => p.getData(`${PLUGIN_DATA_PREFIX}_${k}`)
const setv = (p, k, v) => p.setData(`${PLUGIN_DATA_PREFIX}_${k}`, v)
/**
* Get the index of the parameter `h`
* @param t
* @param path ObjectMethod | ClassMethod | Function
* @returns number -1 if not found
*/
const indexOfParamH = (t, path) => {
const params = path.get('params')
return params.length ? params.findIndex(p => t.isIdentifier(p) && p.node.name === 'h') : -1
}
/**
* Check if expression is an object member function
*/
const isObjectMemberFunc = (t, path) => t.isFunction(path) && t.isObjectMember(path.parentPath)
/**
* Check if expression is an object function-typed member
*/
const isMemberFunction = (t, path) => t.isObjectMethod(path) || t.isClassMethod(path) || isObjectMemberFunc(t, path)
// find JSX, returns the first walked jsx expression
const findJSXElement = (t, path) => {
let elem = null
path.traverse({
JSXElement (p) {
elem = p
p.stop()
}
})
return elem
}
/**
* Get function-typed ancestry of the specific node range
* @param path JSXElement
* @param root The last boundary node
* @returns the ancestry paths
*/
const getFuncAncestry = (t, path, root) => {
const paths = []
if (path !== root) {
while (path = path.parentPath) {
if (t.isFunction(path)) {
paths.push(path)
}
if (path === root) break
}
}
return paths
}
/**
* Check if is inside a JSX expression
* @param t
* @param path ObjectMethod | ClassMethod
* @returns boolean
*/
const isInsideJSXExpression = (t, path) => path.findParent(p => p && t.isJSX(p)) !== null
/**
* Cleanup and reduce stack that `distance` gt than reference's
* @param stack
* @param reference The reference node to match
*/
const cleanupStack = (stack, path) => {
const ref = getv(path, 'distance')
stack.forEach(p => {
if (getv(p, 'distance') > ref) setv(p, 'hasJSX', false)
})
}
/**
* Prepend parameter `h` to function params (ensure as the first parameter)
*
* @param path {ArrowFunctionExpression|FunctionExpression}
*/
const addParamH = (t, path) => {
path.node.params = [t.identifier('h')].concat(path.node.params)
}
/**
* Prepend `h` variable to function body, i.e. const h = xxx;
*
* @param path {ObjectMethod|ClassMethod|FunctionExpression}
*/
const patchVariableH = (t, path) => {
const funcName = path.isFunctionExpression()
? path.parent.key.name
: path.node.key.name
const isRender = funcName === 'render'
if (isRender) {
addParamH(t, path)
return
}
path
.get('body')
.unshiftContainer(
'body',
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('h'),
t.memberExpression(t.thisExpression(), t.identifier('$createElement')),
),
]),
)
}
export default babel => {
const t = babel.types
return {
inherits: syntaxJsx,
visitor: {
Program (p) {
const stack = []
p.traverse({
'ObjectMethod|ClassMethod|FunctionDeclaration|FunctionExpression|ArrowFunctionExpression': {
enter (path) {
const jsxElem = findJSXElement(t, path)
if (!jsxElem || isInsideJSXExpression(t, path)) {
return
}
const ancestry = getFuncAncestry(t, jsxElem, path)
const isObjFn = isMemberFunction(t, path)
// check if JSX expression is in method
if (!isObjFn && ancestry.some(p => isMemberFunction(t, p))) {
return
}
// add to pending stack
stack.push(path)
setv(path, 'hasJSX', true)
setv(path, 'distance', ancestry.length)
// check params already has param `h`
if (indexOfParamH(t, path) !== -1) {
setv(path, 'fixed', true)
return
}
if (isObjFn) {
if (path.isArrowFunctionExpression()) {
addParamH(t, path)
} else {
patchVariableH(t, path)
}
setv(path, 'fixed', true)
}
},
exit (path) {
if (!getv(path, 'hasJSX')) {
return
}
stack.pop()
// skip and cancel remaining nodes if `h` has fixed
if (getv(path, 'fixed')) {
cleanupStack(stack, path)
return
}
// skip, functional JSX `h` should to be fixed in top of pending stack
if (!isObjectMemberFunc(t, path) && stack.some(p => getv(p, 'hasJSX'))) {
return
}
addParamH(t, path)
setv(path, 'fixed', true)
cleanupStack(stack, path)
}
}
})
}
},
}
}