Skip to content

Commit 8969609

Browse files
authored
fix: functional-vue & inject-h should traverse before JSX plugin (#166)
partially revert #87, fix functional-vue & inject-h in preset fixes #165
1 parent aa8419b commit 8969609

File tree

6 files changed

+120
-48
lines changed

6 files changed

+120
-48
lines changed

packages/babel-sugar-functional-vue/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prepublish": "yarn build",
1616
"build": "rollup -c",
1717
"build:test": "rollup -c rollup.config.testing.js",
18-
"pretest": "yarn build:test",
18+
"pretest": "yarn build:test && cd ../babel-plugin-transform-vue-jsx && yarn build:test",
1919
"test": "nyc --reporter=html --reporter=text-summary ava -v test/test.js"
2020
},
2121
"devDependencies": {

packages/babel-sugar-functional-vue/src/index.js

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

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-
}
88+
convertFunctionalComponent(t, path.get('declaration'))
89+
},
90+
},
91+
VariableDeclaration: {
92+
exit(path) {
93+
if (
94+
path.node.declarations.length !== 1 ||
95+
!t.isVariableDeclarator(path.node.declarations[0]) ||
96+
!t.isArrowFunctionExpression(path.node.declarations[0].init)
97+
) {
98+
return
99+
}
98100

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

101-
if (!isFunctionalComponentDeclarator(t, declarator)) {
102-
return
103-
}
103+
if (!isFunctionalComponentDeclarator(t, declarator)) {
104+
return
105+
}
104106

105-
const name = path.node.declarations[0].id.name
106-
convertFunctionalComponent(t, path.get('declarations')[0].get('init'), name)
107-
},
108-
},
107+
const name = path.node.declarations[0].id.name
108+
convertFunctionalComponent(t, path.get('declarations')[0].get('init'), name)
109+
},
110+
},
111+
})
112+
}
109113
},
110114
}
111115
}

packages/babel-sugar-functional-vue/test/test.js

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava'
22
import { transform } from '@babel/core'
33
import plugin from '../dist/plugin.testing'
4+
import jsxPlugin from '../../babel-plugin-transform-vue-jsx/dist/plugin.testing'
45

56
const transpile = src =>
67
new Promise((resolve, reject) => {
@@ -18,6 +19,22 @@ const transpile = src =>
1819
)
1920
})
2021

22+
const transpileWithJSXPlugin = src =>
23+
new Promise((resolve, reject) => {
24+
transform(
25+
src,
26+
{
27+
plugins: [plugin, jsxPlugin],
28+
},
29+
(err, result) => {
30+
if (err) {
31+
return reject(err)
32+
}
33+
resolve(result.code)
34+
},
35+
)
36+
})
37+
2138
const tests = [
2239
{
2340
name: 'Generic functional component',
@@ -107,3 +124,19 @@ tests.map(({ name, from, to, NODE_ENV }) => {
107124
}
108125
})
109126
})
127+
128+
test('Should work with JSX plugin enabled', async t => {
129+
const from = `export const A = ({ props, listeners }) => <div onClick={listeners.click}>{props.msg}</div>`
130+
const to = `export const A = {
131+
functional: true,
132+
render: (h, {
133+
props,
134+
listeners
135+
}) => h("div", {
136+
"on": {
137+
"click": listeners.click
138+
}
139+
}, [props.msg])
140+
};`
141+
t.is(await(transpileWithJSXPlugin(from)), to)
142+
})

packages/babel-sugar-inject-h/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
"prepublish": "yarn build",
1616
"build": "rollup -c",
1717
"build:test": "rollup -c rollup.config.testing.js",
18-
"pretest": "yarn build:test",
18+
"pretest": "yarn build:test && cd ../babel-plugin-transform-vue-jsx && yarn build:test",
1919
"test": "nyc --reporter=html --reporter=text-summary ava -v test/test.js"
2020
},
2121
"devDependencies": {

packages/babel-sugar-inject-h/src/index.js

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

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

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-
},
79-
},
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+
})
81+
}
8082
},
8183
}
8284
}

packages/babel-sugar-inject-h/test/test.js

+33
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import test from 'ava'
22
import { transform } from '@babel/core'
33
import plugin from '../dist/plugin.testing'
4+
import jsxPlugin from '../../babel-plugin-transform-vue-jsx/dist/plugin.testing'
45

56
const transpile = src =>
67
new Promise((resolve, reject) => {
@@ -18,6 +19,22 @@ const transpile = src =>
1819
)
1920
})
2021

22+
const transpileWithJSXPlugin = src =>
23+
new Promise((resolve, reject) => {
24+
transform(
25+
src,
26+
{
27+
plugins: [plugin, jsxPlugin],
28+
},
29+
(err, result) => {
30+
if (err) {
31+
return reject(err)
32+
}
33+
resolve(result.code)
34+
},
35+
)
36+
})
37+
2138
const tests = [
2239
{
2340
name: 'Simple injection in object methods',
@@ -106,3 +123,19 @@ const tests = [
106123
]
107124

108125
tests.forEach(({ name, from, to }) => test(name, async t => t.is(await transpile(from), to)))
126+
127+
test('Should work with JSX plugin enabled', async t => {
128+
const from = `const obj = {
129+
render () {
130+
return <div>test</div>
131+
}
132+
}`
133+
const to = `const obj = {
134+
render() {
135+
const h = arguments[0];
136+
return h("div", ["test"]);
137+
}
138+
139+
};`
140+
t.is(await(transpileWithJSXPlugin(from)), to)
141+
})

0 commit comments

Comments
 (0)