Skip to content

Commit

Permalink
feat: change all sugar plugins to work without pre-traversing the Pro…
Browse files Browse the repository at this point in the history
…gram
  • Loading branch information
Nandiin committed Dec 3, 2019
1 parent 566cf2a commit 0943580
Show file tree
Hide file tree
Showing 4 changed files with 73 additions and 84 deletions.
48 changes: 24 additions & 24 deletions packages/babel-sugar-functional-vue/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -77,34 +77,34 @@ export default babel => {
return {
inherits: syntaxJsx,
visitor: {
Program(path) {
path.traverse({
ExportDefaultDeclaration(path) {
if (!t.isArrowFunctionExpression(path.node.declaration) || !hasJSX(t, path)) {
return
}
ExportDefaultDeclaration: {
exit(path) {
if (!t.isArrowFunctionExpression(path.node.declaration) || !hasJSX(t, path)) {
return
}

convertFunctionalComponent(t, path.get('declaration'))
},
VariableDeclaration(path) {
if (
path.node.declarations.length !== 1 ||
!t.isVariableDeclarator(path.node.declarations[0]) ||
!t.isArrowFunctionExpression(path.node.declarations[0].init)
) {
return
}
convertFunctionalComponent(t, path.get('declaration'))
},
},
VariableDeclaration: {
exit(path) {
if (
path.node.declarations.length !== 1 ||
!t.isVariableDeclarator(path.node.declarations[0]) ||
!t.isArrowFunctionExpression(path.node.declarations[0].init)
) {
return
}

const declarator = path.get('declarations')[0]
const declarator = path.get('declarations')[0]

if (!isFunctionalComponentDeclarator(t, declarator)) {
return
}
if (!isFunctionalComponentDeclarator(t, declarator)) {
return
}

const name = path.node.declarations[0].id.name
convertFunctionalComponent(t, path.get('declarations')[0].get('init'), name)
},
})
const name = path.node.declarations[0].id.name
convertFunctionalComponent(t, path.get('declarations')[0].get('init'), name)
},
},
},
}
Expand Down
42 changes: 20 additions & 22 deletions packages/babel-sugar-inject-h/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -54,30 +54,28 @@ export default babel => {
return {
inherits: syntaxJsx,
visitor: {
Program(path) {
path.traverse({
'ObjectMethod|ClassMethod'(path) {
if (firstParamIsH(t, path) || !hasJSX(t, path) || isInsideJSXExpression(t, path)) {
return
}
'ObjectMethod|ClassMethod': {
exit(path) {
if (firstParamIsH(t, path) || !hasJSX(t, path) || isInsideJSXExpression(t, path)) {
return
}

const isRender = path.node.key.name === 'render'
const isRender = path.node.key.name === 'render'

path
.get('body')
.unshiftContainer(
'body',
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('h'),
isRender
? t.memberExpression(t.identifier('arguments'), t.numericLiteral(0), true)
: t.memberExpression(t.thisExpression(), t.identifier('$createElement')),
),
]),
)
},
})
path
.get('body')
.unshiftContainer(
'body',
t.variableDeclaration('const', [
t.variableDeclarator(
t.identifier('h'),
isRender
? t.memberExpression(t.identifier('arguments'), t.numericLiteral(0), true)
: t.memberExpression(t.thisExpression(), t.identifier('$createElement')),
),
]),
)
},
},
},
}
Expand Down
45 changes: 19 additions & 26 deletions packages/babel-sugar-v-model/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -24,21 +24,19 @@ export default function(babel) {
return {
inherits: syntaxJsx,
visitor: {
Program(path) {
path.traverse({
JSXAttribute(path) {
const parsed = parseVModel(t, path)
if (!parsed) {
return
}

const { modifiers, valuePath } = parsed

const parent = path.parentPath
transformModel(t, parent, valuePath, modifiers)
path.remove()
},
})
JSXAttribute: {
exit(path) {
const parsed = parseVModel(t, path)
if (!parsed) {
return
}

const { modifiers, valuePath } = parsed

const parent = path.parentPath
transformModel(t, parent, valuePath, modifiers)
path.remove()
},
},
},
}
Expand Down Expand Up @@ -182,17 +180,12 @@ const addProp = (t, path, propName, expression, unshift = false) => {
*/
const genAssignmentCode = (t, valuePath, valueExpression) => {
let obj
if (t.isMemberExpression(valuePath) && !t.isThisExpression(obj = valuePath.get('object').node)) {
return t.callExpression(
t.memberExpression(t.thisExpression(), t.identifier('$set')),
[
obj,
valuePath.node.computed
? valuePath.get('property').node
: t.stringLiteral(valuePath.get('property.name').node),
valueExpression
]
);
if (t.isMemberExpression(valuePath) && !t.isThisExpression((obj = valuePath.get('object').node))) {
return t.callExpression(t.memberExpression(t.thisExpression(), t.identifier('$set')), [
obj,
valuePath.node.computed ? valuePath.get('property').node : t.stringLiteral(valuePath.get('property.name').node),
valueExpression,
])
} else {
return t.assignmentExpression('=', valuePath.node, valueExpression)
}
Expand Down
22 changes: 10 additions & 12 deletions packages/babel-sugar-v-on/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -287,18 +287,16 @@ export default function(babel) {
return {
inherits: syntaxJsx,
visitor: {
Program(path) {
path.traverse({
JSXAttribute(path) {
const { event, expression, isNative } = genHandler(path)

if (event) {
path.remove()

addEvent(event, expression, isNative, path.parentPath.node.attributes)
}
},
})
JSXAttribute: {
exit(path) {
const { event, expression, isNative } = genHandler(path)

if (event) {
path.remove()

addEvent(event, expression, isNative, path.parentPath.node.attributes)
}
},
},
},
}
Expand Down

0 comments on commit 0943580

Please sign in to comment.