Skip to content

Commit b49fa8a

Browse files
author
Nandiin
committed
feat: allow prior babel plugins to traverse JSX tree throughly, close #86
1 parent 67d6d39 commit b49fa8a

File tree

2 files changed

+32
-5
lines changed

2 files changed

+32
-5
lines changed

Diff for: packages/babel-plugin-transform-vue-jsx/src/index.js

+9-5
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,8 @@ const getChildren = (t, paths) =>
7575
if (path.isJSXSpreadChild()) {
7676
return transformJSXSpreadChild(t, path)
7777
}
78-
/* istanbul ignore else */
79-
if (path.isJSXElement()) {
80-
return transformJSXElement(t, path)
78+
if (path.isCallExpression()) {
79+
return path.node
8180
}
8281
/* istanbul ignore next */
8382
throw new Error(`getChildren: ${path.type} is not supported`)
@@ -339,6 +338,9 @@ const transformAttributes = (t, attributes) =>
339338
* @returns CallExpression
340339
*/
341340
const transformJSXElement = (t, path) => {
341+
if (t.isJSXAttribute(path.container)) {
342+
throw new Error(`getAttributes (attribute value): ${path.type} is not supported`)
343+
}
342344
const tag = getTag(t, path.get('openingElement'))
343345
const children = getChildren(t, path.get('children'))
344346
const openingElementPath = path.get('openingElement')
@@ -451,8 +453,10 @@ export default babel => {
451453
name: 'babel-plugin-transform-vue-jsx',
452454
inherits: syntaxJsx,
453455
visitor: {
454-
JSXElement(path) {
455-
path.replaceWith(transformJSXElement(t, path))
456+
JSXElement: {
457+
exit(path) {
458+
path.replaceWith(transformJSXElement(t, path))
459+
},
456460
},
457461
},
458462
}

Diff for: packages/babel-plugin-transform-vue-jsx/test/snapshot.js

+23
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import test from 'ava'
22
import { transform } from '@babel/core'
3+
import syntaxJsx from '@babel/plugin-syntax-jsx'
34
import plugin from '../dist/plugin.testing'
45

56
const transpile = src =>
@@ -356,3 +357,25 @@ test('JSXElement attribute value throws error', t =>
356357
resolve()
357358
})
358359
}))
360+
361+
362+
test('Allows prior plugins to traverse JSX tree throughly', t => {
363+
let visited = 0;
364+
const anotherPlugin = () => ({
365+
name: 'babel-plugin-another',
366+
inherits: syntaxJsx,
367+
visitor: {
368+
JSXElement(path) {
369+
visited++;
370+
371+
}
372+
}
373+
})
374+
return new Promise(resolve => {
375+
transform(`render(h => <a><b/></a>)`,{ plugins: [anotherPlugin, plugin] }, (err) => {
376+
if (err) t.fail()
377+
t.is(visited, 2)
378+
resolve();
379+
})
380+
})
381+
})

0 commit comments

Comments
 (0)