Skip to content

feat: allow prior babel plugins to traverse JSX tree thoroughly #87

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 3 commits into from
Dec 30, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions packages/babel-plugin-transform-vue-jsx/src/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -75,9 +75,8 @@ const getChildren = (t, paths) =>
if (path.isJSXSpreadChild()) {
return transformJSXSpreadChild(t, path)
}
/* istanbul ignore else */
if (path.isJSXElement()) {
return transformJSXElement(t, path)
if (path.isCallExpression()) {
return path.node
}
/* istanbul ignore next */
throw new Error(`getChildren: ${path.type} is not supported`)
Expand Down Expand Up @@ -339,6 +338,9 @@ const transformAttributes = (t, attributes) =>
* @returns CallExpression
*/
const transformJSXElement = (t, path) => {
if (t.isJSXAttribute(path.container)) {
throw new Error(`getAttributes (attribute value): ${path.type} is not supported`)
}
const tag = getTag(t, path.get('openingElement'))
const children = getChildren(t, path.get('children'))
const openingElementPath = path.get('openingElement')
Expand Down Expand Up @@ -451,8 +453,10 @@ export default babel => {
name: 'babel-plugin-transform-vue-jsx',
inherits: syntaxJsx,
visitor: {
JSXElement(path) {
path.replaceWith(transformJSXElement(t, path))
JSXElement: {
exit(path) {
path.replaceWith(transformJSXElement(t, path))
},
},
},
}
Expand Down
23 changes: 23 additions & 0 deletions packages/babel-plugin-transform-vue-jsx/test/snapshot.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import test from 'ava'
import { transform } from '@babel/core'
import syntaxJsx from '@babel/plugin-syntax-jsx'
import plugin from '../dist/plugin.testing'

const transpile = src =>
Expand Down Expand Up @@ -356,3 +357,25 @@ test('JSXElement attribute value throws error', t =>
resolve()
})
}))


test('Allows prior plugins to traverse JSX tree throughly', t => {
let visited = 0;
const anotherPlugin = () => ({
name: 'babel-plugin-another',
inherits: syntaxJsx,
visitor: {
JSXElement(path) {
visited++;

}
}
})
return new Promise(resolve => {
transform(`render(h => <a><b/></a>)`,{ plugins: [anotherPlugin, plugin] }, (err) => {
if (err) t.fail()
t.is(visited, 2)
resolve();
})
})
})
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