Skip to content

Commit 0943580

Browse files
author
Nandiin
committed
feat: change all sugar plugins to work without pre-traversing the Program
1 parent 566cf2a commit 0943580

File tree

4 files changed

+73
-84
lines changed

4 files changed

+73
-84
lines changed

Diff for: packages/babel-sugar-functional-vue/src/index.js

+24-24
Original file line numberDiff line numberDiff line change
@@ -77,34 +77,34 @@ export default babel => {
7777
return {
7878
inherits: syntaxJsx,
7979
visitor: {
80-
Program(path) {
81-
path.traverse({
82-
ExportDefaultDeclaration(path) {
83-
if (!t.isArrowFunctionExpression(path.node.declaration) || !hasJSX(t, path)) {
84-
return
85-
}
80+
ExportDefaultDeclaration: {
81+
exit(path) {
82+
if (!t.isArrowFunctionExpression(path.node.declaration) || !hasJSX(t, path)) {
83+
return
84+
}
8685

87-
convertFunctionalComponent(t, path.get('declaration'))
88-
},
89-
VariableDeclaration(path) {
90-
if (
91-
path.node.declarations.length !== 1 ||
92-
!t.isVariableDeclarator(path.node.declarations[0]) ||
93-
!t.isArrowFunctionExpression(path.node.declarations[0].init)
94-
) {
95-
return
96-
}
86+
convertFunctionalComponent(t, path.get('declaration'))
87+
},
88+
},
89+
VariableDeclaration: {
90+
exit(path) {
91+
if (
92+
path.node.declarations.length !== 1 ||
93+
!t.isVariableDeclarator(path.node.declarations[0]) ||
94+
!t.isArrowFunctionExpression(path.node.declarations[0].init)
95+
) {
96+
return
97+
}
9798

98-
const declarator = path.get('declarations')[0]
99+
const declarator = path.get('declarations')[0]
99100

100-
if (!isFunctionalComponentDeclarator(t, declarator)) {
101-
return
102-
}
101+
if (!isFunctionalComponentDeclarator(t, declarator)) {
102+
return
103+
}
103104

104-
const name = path.node.declarations[0].id.name
105-
convertFunctionalComponent(t, path.get('declarations')[0].get('init'), name)
106-
},
107-
})
105+
const name = path.node.declarations[0].id.name
106+
convertFunctionalComponent(t, path.get('declarations')[0].get('init'), name)
107+
},
108108
},
109109
},
110110
}

Diff for: packages/babel-sugar-inject-h/src/index.js

+20-22
Original file line numberDiff line numberDiff line change
@@ -54,30 +54,28 @@ export default babel => {
5454
return {
5555
inherits: syntaxJsx,
5656
visitor: {
57-
Program(path) {
58-
path.traverse({
59-
'ObjectMethod|ClassMethod'(path) {
60-
if (firstParamIsH(t, path) || !hasJSX(t, path) || isInsideJSXExpression(t, path)) {
61-
return
62-
}
57+
'ObjectMethod|ClassMethod': {
58+
exit(path) {
59+
if (firstParamIsH(t, path) || !hasJSX(t, path) || isInsideJSXExpression(t, path)) {
60+
return
61+
}
6362

64-
const isRender = path.node.key.name === 'render'
63+
const isRender = path.node.key.name === 'render'
6564

66-
path
67-
.get('body')
68-
.unshiftContainer(
69-
'body',
70-
t.variableDeclaration('const', [
71-
t.variableDeclarator(
72-
t.identifier('h'),
73-
isRender
74-
? t.memberExpression(t.identifier('arguments'), t.numericLiteral(0), true)
75-
: t.memberExpression(t.thisExpression(), t.identifier('$createElement')),
76-
),
77-
]),
78-
)
79-
},
80-
})
65+
path
66+
.get('body')
67+
.unshiftContainer(
68+
'body',
69+
t.variableDeclaration('const', [
70+
t.variableDeclarator(
71+
t.identifier('h'),
72+
isRender
73+
? t.memberExpression(t.identifier('arguments'), t.numericLiteral(0), true)
74+
: t.memberExpression(t.thisExpression(), t.identifier('$createElement')),
75+
),
76+
]),
77+
)
78+
},
8179
},
8280
},
8381
}

Diff for: packages/babel-sugar-v-model/src/index.js

+19-26
Original file line numberDiff line numberDiff line change
@@ -24,21 +24,19 @@ export default function(babel) {
2424
return {
2525
inherits: syntaxJsx,
2626
visitor: {
27-
Program(path) {
28-
path.traverse({
29-
JSXAttribute(path) {
30-
const parsed = parseVModel(t, path)
31-
if (!parsed) {
32-
return
33-
}
34-
35-
const { modifiers, valuePath } = parsed
36-
37-
const parent = path.parentPath
38-
transformModel(t, parent, valuePath, modifiers)
39-
path.remove()
40-
},
41-
})
27+
JSXAttribute: {
28+
exit(path) {
29+
const parsed = parseVModel(t, path)
30+
if (!parsed) {
31+
return
32+
}
33+
34+
const { modifiers, valuePath } = parsed
35+
36+
const parent = path.parentPath
37+
transformModel(t, parent, valuePath, modifiers)
38+
path.remove()
39+
},
4240
},
4341
},
4442
}
@@ -182,17 +180,12 @@ const addProp = (t, path, propName, expression, unshift = false) => {
182180
*/
183181
const genAssignmentCode = (t, valuePath, valueExpression) => {
184182
let obj
185-
if (t.isMemberExpression(valuePath) && !t.isThisExpression(obj = valuePath.get('object').node)) {
186-
return t.callExpression(
187-
t.memberExpression(t.thisExpression(), t.identifier('$set')),
188-
[
189-
obj,
190-
valuePath.node.computed
191-
? valuePath.get('property').node
192-
: t.stringLiteral(valuePath.get('property.name').node),
193-
valueExpression
194-
]
195-
);
183+
if (t.isMemberExpression(valuePath) && !t.isThisExpression((obj = valuePath.get('object').node))) {
184+
return t.callExpression(t.memberExpression(t.thisExpression(), t.identifier('$set')), [
185+
obj,
186+
valuePath.node.computed ? valuePath.get('property').node : t.stringLiteral(valuePath.get('property.name').node),
187+
valueExpression,
188+
])
196189
} else {
197190
return t.assignmentExpression('=', valuePath.node, valueExpression)
198191
}

Diff for: packages/babel-sugar-v-on/src/index.js

+10-12
Original file line numberDiff line numberDiff line change
@@ -287,18 +287,16 @@ export default function(babel) {
287287
return {
288288
inherits: syntaxJsx,
289289
visitor: {
290-
Program(path) {
291-
path.traverse({
292-
JSXAttribute(path) {
293-
const { event, expression, isNative } = genHandler(path)
294-
295-
if (event) {
296-
path.remove()
297-
298-
addEvent(event, expression, isNative, path.parentPath.node.attributes)
299-
}
300-
},
301-
})
290+
JSXAttribute: {
291+
exit(path) {
292+
const { event, expression, isNative } = genHandler(path)
293+
294+
if (event) {
295+
path.remove()
296+
297+
addEvent(event, expression, isNative, path.parentPath.node.attributes)
298+
}
299+
},
302300
},
303301
},
304302
}

0 commit comments

Comments
 (0)