Skip to content

Commit 6fad209

Browse files
committed
fix(compiler-sfc): should not rewrite ref sugar identifiers in types
fix #4062
1 parent 09f84e9 commit 6fad209

File tree

4 files changed

+248
-1
lines changed

4 files changed

+248
-1
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,228 @@
1+
// Jest Snapshot v1, https://goo.gl/fbAQLP
2+
3+
exports[`<script setup> ref sugar accessing ref binding 1`] = `
4+
"import { ref as _ref } from 'vue'
5+
6+
export default {
7+
setup(__props, { expose }) {
8+
expose()
9+
10+
const a = _ref(1)
11+
console.log(a.value)
12+
function get() {
13+
return a.value + 1
14+
}
15+
16+
return { a, get }
17+
}
18+
19+
}"
20+
`;
21+
22+
exports[`<script setup> ref sugar array destructure 1`] = `
23+
"import { ref as _ref } from 'vue'
24+
25+
export default {
26+
setup(__props, { expose }) {
27+
expose()
28+
29+
const n = _ref(1), [__a, __b = 1, ...__c] = useFoo()
30+
const a = _ref(__a);
31+
const b = _ref(__b);
32+
const c = _ref(__c);
33+
console.log(n.value, a.value, b.value, c.value)
34+
35+
return { n, a, b, c }
36+
}
37+
38+
}"
39+
`;
40+
41+
exports[`<script setup> ref sugar convert ref declarations 1`] = `
42+
"import { ref as _ref } from 'vue'
43+
44+
export default {
45+
setup(__props, { expose }) {
46+
expose()
47+
48+
const foo = _ref()
49+
const a = _ref(1)
50+
const b = _ref({
51+
count: 0
52+
})
53+
let c = () => {}
54+
let d
55+
56+
return { foo, a, b, c, d }
57+
}
58+
59+
}"
60+
`;
61+
62+
exports[`<script setup> ref sugar multi ref declarations 1`] = `
63+
"import { ref as _ref } from 'vue'
64+
65+
export default {
66+
setup(__props, { expose }) {
67+
expose()
68+
69+
const a = _ref(1), b = _ref(2), c = _ref({
70+
count: 0
71+
})
72+
73+
return { a, b, c }
74+
}
75+
76+
}"
77+
`;
78+
79+
exports[`<script setup> ref sugar mutating ref binding 1`] = `
80+
"import { ref as _ref } from 'vue'
81+
82+
export default {
83+
setup(__props, { expose }) {
84+
expose()
85+
86+
const a = _ref(1)
87+
const b = _ref({ count: 0 })
88+
function inc() {
89+
a.value++
90+
a.value = a.value + 1
91+
b.value.count++
92+
b.value.count = b.value.count + 1
93+
;({ a: a.value } = { a: 2 })
94+
;[a.value] = [1]
95+
}
96+
97+
return { a, b, inc }
98+
}
99+
100+
}"
101+
`;
102+
103+
exports[`<script setup> ref sugar nested destructure 1`] = `
104+
"import { ref as _ref } from 'vue'
105+
106+
export default {
107+
setup(__props, { expose }) {
108+
expose()
109+
110+
const [{ a: { b: __b }}] = useFoo()
111+
const b = _ref(__b);
112+
const { c: [__d, __e] } = useBar()
113+
const d = _ref(__d);
114+
const e = _ref(__e);
115+
console.log(b.value, d.value, e.value)
116+
117+
return { b, d, e }
118+
}
119+
120+
}"
121+
`;
122+
123+
exports[`<script setup> ref sugar object destructure 1`] = `
124+
"import { ref as _ref } from 'vue'
125+
126+
export default {
127+
setup(__props, { expose }) {
128+
expose()
129+
130+
const n = _ref(1), { a: __a, b: __c, d: __d = 1, e: __f = 2, ...__g } = useFoo()
131+
const a = _ref(__a);
132+
const c = _ref(__c);
133+
const d = _ref(__d);
134+
const f = _ref(__f);
135+
const g = _ref(__g);
136+
const { foo: __foo } = useSomthing(() => 1);
137+
const foo = _ref(__foo);
138+
console.log(n.value, a.value, c.value, d.value, f.value, g.value, foo.value)
139+
140+
return { n, a, c, d, f, g, foo }
141+
}
142+
143+
}"
144+
`;
145+
146+
exports[`<script setup> ref sugar should not convert non ref labels 1`] = `
147+
"export default {
148+
setup(__props, { expose }) {
149+
expose()
150+
151+
foo: a = 1, b = 2, c = {
152+
count: 0
153+
}
154+
155+
return { }
156+
}
157+
158+
}"
159+
`;
160+
161+
exports[`<script setup> ref sugar should not rewrite scope variable 1`] = `
162+
"import { ref as _ref } from 'vue'
163+
164+
export default {
165+
setup(__props, { expose }) {
166+
expose()
167+
168+
const a = _ref(1)
169+
const b = _ref(1)
170+
const d = _ref(1)
171+
const e = 1
172+
function test() {
173+
const a = 2
174+
console.log(a)
175+
console.log(b.value)
176+
let c = { c: 3 }
177+
console.log(c)
178+
let $d
179+
console.log($d)
180+
console.log(d.value)
181+
console.log(e)
182+
}
183+
184+
return { a, b, d, e, test }
185+
}
186+
187+
}"
188+
`;
189+
190+
exports[`<script setup> ref sugar should not rewrite type identifiers 1`] = `
191+
"import { ref as _ref, defineComponent as _defineComponent } from 'vue'
192+
193+
export default _defineComponent({
194+
props: {
195+
msg: { type: String, required: true },
196+
ids: { type: Array, required: false }
197+
} as unknown as undefined,
198+
setup(__props: {msg: string; ids?: string[]}, { expose }) {
199+
expose()
200+
201+
const props = __props
202+
203+
const ids = _ref([])
204+
205+
return { props, ids }
206+
}
207+
208+
})"
209+
`;
210+
211+
exports[`<script setup> ref sugar using ref binding in property shorthand 1`] = `
212+
"import { ref as _ref } from 'vue'
213+
214+
export default {
215+
setup(__props, { expose }) {
216+
expose()
217+
218+
const a = _ref(1)
219+
const b = { a: a.value }
220+
function test() {
221+
const { a } = b
222+
}
223+
224+
return { a, b, test }
225+
}
226+
227+
}"
228+
`;

packages/compiler-sfc/__tests__/compileScriptRefSugar.ts renamed to packages/compiler-sfc/__tests__/compileScriptRefSugar.spec.ts

+16
Original file line numberDiff line numberDiff line change
@@ -236,6 +236,22 @@ describe('<script setup> ref sugar', () => {
236236
assertCode(content)
237237
})
238238

239+
//#4062
240+
test('should not rewrite type identifiers', () => {
241+
const { content } = compile(
242+
`
243+
<script setup lang="ts">
244+
const props = defineProps<{msg: string; ids?: string[]}>()
245+
ref: ids = []
246+
</script>`,
247+
{
248+
refSugar: true
249+
}
250+
)
251+
assertCode(content)
252+
expect(content).not.toMatch('.value')
253+
})
254+
239255
describe('errors', () => {
240256
test('ref: non-assignment expressions', () => {
241257
expect(() =>

packages/compiler-sfc/src/compileScript.ts

+3
Original file line numberDiff line numberDiff line change
@@ -1708,6 +1708,9 @@ export function walkIdentifiers(
17081708
;(walk as any)(root, {
17091709
enter(node: Node & { scopeIds?: Set<string> }, parent: Node | undefined) {
17101710
parent && parentStack.push(parent)
1711+
if (node.type.startsWith('TS')) {
1712+
return this.skip()
1713+
}
17111714
if (node.type === 'Identifier') {
17121715
if (
17131716
!knownIds[node.name] &&

packages/sfc-playground/package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"@types/codemirror": "^0.0.108",
1919
"@vitejs/plugin-vue": "^1.2.0",
2020
"codemirror": "^5.60.0",
21-
"vite": "^2.1.5"
21+
"vite": "^2.4.0"
2222
},
2323
"dependencies": {
2424
"file-saver": "^2.0.5",

0 commit comments

Comments
 (0)