Skip to content

Commit a3525bf

Browse files
authored
fix(composition-api-render-instance): store currentInstance in variable for render instance (vuejs#168)
1 parent 3644a17 commit a3525bf

File tree

3 files changed

+58
-24
lines changed

3 files changed

+58
-24
lines changed

packages/babel-sugar-composition-api-render-instance/README.md

+21-13
Original file line numberDiff line numberDiff line change
@@ -37,20 +37,24 @@ Input:
3737

3838
```jsx
3939
defineComponent({
40-
setup: () => <MyComponent vModel={a.b} />
40+
setup() {
41+
return () => <MyComponent vModel={a.b} />
42+
}
4143
})
4244
```
4345

4446
Output (without @vue/babel-sugar-composition-api-render-instance):
4547

4648
```jsx
4749
defineComponent({
48-
setup: () => <MyComponent model={{
49-
value: a.b,
50-
callback: $$v => {
51-
this.$set(a, "b", $$v);
52-
}
53-
}} />;
50+
setup() {
51+
return () => <MyComponent model={{
52+
value: a.b,
53+
callback: $$v => {
54+
this.$set(a, "b", $$v);
55+
}
56+
}} />
57+
}
5458
})
5559
```
5660

@@ -60,11 +64,15 @@ Output (with @vue/babel-sugar-composition-api-render-instance):
6064
import { getCurrentInstance } from "@vue/composition-api";
6165

6266
defineComponent({
63-
setup: () => <MyComponent model={{
64-
value: a.b,
65-
callback: $$v => {
66-
getCurrentInstance().$set(a, "b", $$v);
67-
}
68-
}} />;
67+
setup() {
68+
const __currentInstance = getCurrentInstance();
69+
70+
return () => <MyComponent model={{
71+
value: a.b,
72+
callback: $$v => {
73+
__currentInstance.$set(a, "b", $$v);
74+
}
75+
}} />
76+
}
6977
})
7078
```

packages/babel-sugar-composition-api-render-instance/src/index.js

+19-1
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,8 @@ const autoImportGetCurrentInstance = (t, path) => {
2020
}
2121
}
2222

23+
const injectInstanceId = '__currentInstance'
24+
2325
export default ({ types: t }) => {
2426
return {
2527
inherits: syntaxJsx,
@@ -30,6 +32,11 @@ export default ({ types: t }) => {
3032
if (path1.node.key.name !== 'setup') {
3133
return
3234
}
35+
36+
let instanceInjected = false
37+
38+
39+
3340
path1.traverse({
3441
JSXAttribute(path2) {
3542
const n = path2.get('name')
@@ -41,7 +48,18 @@ export default ({ types: t }) => {
4148
const prop = path3.get('property')
4249
if (t.isThisExpression(obj) && t.isIdentifier(prop) && ['$', '_'].includes(prop.node.name[0])) {
4350
autoImportGetCurrentInstance(t, p)
44-
obj.replaceWith(t.callExpression(t.identifier('getCurrentInstance'), []))
51+
if (!instanceInjected) {
52+
path1.node.value.body.body.unshift(
53+
t.variableDeclaration('const', [
54+
t.variableDeclarator(
55+
t.identifier(injectInstanceId),
56+
t.callExpression(t.identifier('getCurrentInstance'), []),
57+
),
58+
]),
59+
)
60+
instanceInjected = true
61+
}
62+
obj.replaceWith(t.identifier(injectInstanceId))
4563
}
4664
},
4765
})

packages/babel-sugar-composition-api-render-instance/test/test.js

+18-10
Original file line numberDiff line numberDiff line change
@@ -34,51 +34,59 @@ const tests = [
3434
{
3535
name: 'Ignores non vModel arguments',
3636
from: `const a = { setup: () => { return () => <A x="y" a:b={c} /> } }`,
37-
to: `const a = {
37+
to: `
38+
const a = {
3839
setup: () => {
3940
return () => <A x="y" a:b={c} />;
4041
}
41-
};`,
42+
};`.trim(),
4243
},
4344
{
4445
name: 'Generic component vModel',
4546
from: `const a = { setup: () => { return () => <MyComponent vModel={a.b} /> } }`,
46-
to: `import { getCurrentInstance } from "@vue/composition-api";
47+
to: `
48+
import { getCurrentInstance } from "@vue/composition-api";
4749
const a = {
4850
setup: () => {
51+
const __currentInstance = getCurrentInstance();
52+
4953
return () => <MyComponent model={{
5054
value: a.b,
5155
callback: $$v => {
52-
getCurrentInstance().$set(a, "b", $$v);
56+
__currentInstance.$set(a, "b", $$v);
5357
}
5458
}} />;
5559
}
56-
};`,
60+
};`.trim(),
5761
},
5862
{
5963
name: 'Component vModel_number',
6064
from: `const a = { setup: () => { return () => <MyComponent vModel_number={a.b} /> } }`,
61-
to: `import { getCurrentInstance } from "@vue/composition-api";
65+
to: `
66+
import { getCurrentInstance } from "@vue/composition-api";
6267
const a = {
6368
setup: () => {
69+
const __currentInstance = getCurrentInstance();
70+
6471
return () => <MyComponent model={{
6572
value: a.b,
6673
callback: $$v => {
67-
getCurrentInstance().$set(a, "b", getCurrentInstance()._n($$v));
74+
__currentInstance.$set(a, "b", __currentInstance._n($$v));
6875
}
6976
}} />;
7077
}
71-
};`,
78+
};`.trim(),
7279
},
7380
{
7481
name: 'Ignore outside of setup()',
7582
from: `const A = <MyComponent vModel={a[b]} />`,
76-
to: `const A = <MyComponent model={{
83+
to: `
84+
const A = <MyComponent model={{
7785
value: a[b],
7886
callback: $$v => {
7987
this.$set(a, b, $$v);
8088
}
81-
}} />;`,
89+
}} />;`.trim(),
8290
},
8391
]
8492

0 commit comments

Comments
 (0)