Skip to content

Commit 24db951

Browse files
authored
fix(compiler-sfc): don't hoist props and emit (#8535)
fix #7805 close #7812
1 parent 2a2810c commit 24db951

12 files changed

+260
-255
lines changed

Diff for: packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

+18-69
Original file line numberDiff line numberDiff line change
@@ -62,6 +62,24 @@ return { fn }
6262
})"
6363
`;
6464
65+
exports[`SFC compile <script setup> > <script> and <script setup> co-usage > keep original semi style 1`] = `
66+
"export default {
67+
props: ['item'],
68+
emits: ['change'],
69+
setup(__props, { expose: __expose, emit: __emit }) {
70+
__expose();
71+
72+
console.log('test')
73+
const props = __props;
74+
const emit = __emit;
75+
(function () {})()
76+
77+
return { props, emit }
78+
}
79+
80+
}"
81+
`;
82+
6583
exports[`SFC compile <script setup> > <script> and <script setup> co-usage > script first 1`] = `
6684
"import { x } from './x'
6785
@@ -612,75 +630,6 @@ return { foo, bar, baz, y, z }
612630
}"
613631
`;
614632
615-
exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable declaration (full removal) 1`] = `
616-
"export default {
617-
props: ['item'],
618-
emits: ['a'],
619-
setup(__props, { expose: __expose, emit }) {
620-
__expose();
621-
622-
const props = __props;
623-
624-
625-
626-
return { props, emit }
627-
}
628-
629-
}"
630-
`;
631-
632-
exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable declaration 1`] = `
633-
"export default {
634-
props: ['item'],
635-
emits: ['a'],
636-
setup(__props, { expose: __expose, emit }) {
637-
__expose();
638-
639-
const props = __props;
640-
641-
const a = 1;
642-
643-
return { props, a, emit }
644-
}
645-
646-
}"
647-
`;
648-
649-
exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable declaration fix #6757 1`] = `
650-
"export default {
651-
props: ['item'],
652-
emits: ['a'],
653-
setup(__props, { expose: __expose, emit }) {
654-
__expose();
655-
656-
const props = __props;
657-
658-
const a = 1;
659-
660-
return { a, props, emit }
661-
}
662-
663-
}"
664-
`;
665-
666-
exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable declaration fix #7422 1`] = `
667-
"export default {
668-
props: ['item'],
669-
emits: ['foo'],
670-
setup(__props, { expose: __expose, emit: emits }) {
671-
__expose();
672-
673-
const props = __props;
674-
675-
const a = 0,
676-
b = 0;
677-
678-
return { props, emits, a, b }
679-
}
680-
681-
}"
682-
`;
683-
684633
exports[`SFC compile <script setup> > dev mode import usage check > TS annotations 1`] = `
685634
"import { defineComponent as _defineComponent } from 'vue'
686635
import { Foo, Bar, Baz, Qux, Fred } from './x'

Diff for: packages/compiler-sfc/__tests__/compileScript.spec.ts

+18-58
Original file line numberDiff line numberDiff line change
@@ -68,64 +68,6 @@ describe('SFC compile <script setup>', () => {
6868
assertCode(content)
6969
})
7070

71-
test('defineProps/defineEmits in multi-variable declaration', () => {
72-
const { content } = compile(`
73-
<script setup>
74-
const props = defineProps(['item']),
75-
a = 1,
76-
emit = defineEmits(['a']);
77-
</script>
78-
`)
79-
assertCode(content)
80-
expect(content).toMatch(`const a = 1;`) // test correct removal
81-
expect(content).toMatch(`props: ['item'],`)
82-
expect(content).toMatch(`emits: ['a'],`)
83-
})
84-
85-
// #6757
86-
test('defineProps/defineEmits in multi-variable declaration fix #6757 ', () => {
87-
const { content } = compile(`
88-
<script setup>
89-
const a = 1,
90-
props = defineProps(['item']),
91-
emit = defineEmits(['a']);
92-
</script>
93-
`)
94-
assertCode(content)
95-
expect(content).toMatch(`const a = 1;`) // test correct removal
96-
expect(content).toMatch(`props: ['item'],`)
97-
expect(content).toMatch(`emits: ['a'],`)
98-
})
99-
100-
// #7422
101-
test('defineProps/defineEmits in multi-variable declaration fix #7422', () => {
102-
const { content } = compile(`
103-
<script setup>
104-
const props = defineProps(['item']),
105-
emits = defineEmits(['foo']),
106-
a = 0,
107-
b = 0;
108-
</script>
109-
`)
110-
assertCode(content)
111-
expect(content).toMatch(`props: ['item'],`)
112-
expect(content).toMatch(`emits: ['foo'],`)
113-
expect(content).toMatch(`const a = 0,`)
114-
expect(content).toMatch(`b = 0;`)
115-
})
116-
117-
test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
118-
const { content } = compile(`
119-
<script setup>
120-
const props = defineProps(['item']),
121-
emit = defineEmits(['a']);
122-
</script>
123-
`)
124-
assertCode(content)
125-
expect(content).toMatch(`props: ['item'],`)
126-
expect(content).toMatch(`emits: ['a'],`)
127-
})
128-
12971
describe('<script> and <script setup> co-usage', () => {
13072
test('script first', () => {
13173
const { content } = compile(`
@@ -156,6 +98,24 @@ describe('SFC compile <script setup>', () => {
15698
assertCode(content)
15799
})
158100

101+
// #7805
102+
test('keep original semi style', () => {
103+
const { content } = compile(`
104+
<script setup>
105+
console.log('test')
106+
const props = defineProps(['item']);
107+
const emit = defineEmits(['change']);
108+
(function () {})()
109+
</script>
110+
`)
111+
assertCode(content)
112+
113+
expect(content).toMatch(`console.log('test')`)
114+
expect(content).toMatch(`const props = __props;`)
115+
expect(content).toMatch(`const emit = __emit;`)
116+
expect(content).toMatch(`(function () {})()`)
117+
})
118+
159119
test('script setup first, named default export', () => {
160120
const { content } = compile(`
161121
<script setup>

Diff for: packages/compiler-sfc/__tests__/compileScript/__snapshots__/defineEmits.spec.ts.snap

+32-32
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
exports[`defineEmits > basic usage 1`] = `
44
"export default {
55
emits: ['foo', 'bar'],
6-
setup(__props, { expose: __expose, emit: myEmit }) {
6+
setup(__props, { expose: __expose, emit: __emit }) {
77
__expose();
88
9-
9+
const myEmit = __emit
1010
1111
return { myEmit }
1212
}
@@ -19,10 +19,10 @@ exports[`defineEmits > w/ runtime options 1`] = `
1919
2020
export default /*#__PURE__*/_defineComponent({
2121
emits: ['a', 'b'],
22-
setup(__props, { expose: __expose, emit }) {
22+
setup(__props, { expose: __expose, emit: __emit }) {
2323
__expose();
2424
25-
25+
const emit = __emit
2626
2727
return { emit }
2828
}
@@ -36,10 +36,10 @@ export interface Emits { (e: 'foo' | 'bar'): void }
3636
3737
export default /*#__PURE__*/_defineComponent({
3838
emits: [\\"foo\\", \\"bar\\"],
39-
setup(__props, { expose: __expose, emit }) {
39+
setup(__props, { expose: __expose, emit: __emit }) {
4040
__expose();
4141
42-
42+
const emit = __emit
4343
4444
return { emit }
4545
}
@@ -53,10 +53,10 @@ export type Emits = { (e: 'foo' | 'bar'): void }
5353
5454
export default /*#__PURE__*/_defineComponent({
5555
emits: [\\"foo\\", \\"bar\\"],
56-
setup(__props, { expose: __expose, emit }) {
56+
setup(__props, { expose: __expose, emit: __emit }) {
5757
__expose();
5858
59-
59+
const emit = __emit
6060
6161
return { emit }
6262
}
@@ -70,10 +70,10 @@ interface Emits { (e: 'foo'): void }
7070
7171
export default /*#__PURE__*/_defineComponent({
7272
emits: ['foo'],
73-
setup(__props, { expose: __expose, emit }) {
73+
setup(__props, { expose: __expose, emit: __emit }) {
7474
__expose();
7575
76-
76+
const emit: Emits = __emit
7777
7878
return { emit }
7979
}
@@ -87,10 +87,10 @@ interface Emits { (e: 'foo' | 'bar'): void }
8787
8888
export default /*#__PURE__*/_defineComponent({
8989
emits: [\\"foo\\", \\"bar\\"],
90-
setup(__props, { expose: __expose, emit }) {
90+
setup(__props, { expose: __expose, emit: __emit }) {
9191
__expose();
9292
93-
93+
const emit = __emit
9494
9595
return { emit }
9696
}
@@ -103,10 +103,10 @@ exports[`defineEmits > w/ type (property syntax string literal) 1`] = `
103103
104104
export default /*#__PURE__*/_defineComponent({
105105
emits: [\\"foo:bar\\"],
106-
setup(__props, { expose: __expose, emit }) {
106+
setup(__props, { expose: __expose, emit: __emit }) {
107107
__expose();
108108
109-
109+
const emit = __emit
110110
111111
return { emit }
112112
}
@@ -119,10 +119,10 @@ exports[`defineEmits > w/ type (property syntax) 1`] = `
119119
120120
export default /*#__PURE__*/_defineComponent({
121121
emits: [\\"foo\\", \\"bar\\"],
122-
setup(__props, { expose: __expose, emit }) {
122+
setup(__props, { expose: __expose, emit: __emit }) {
123123
__expose();
124124
125-
125+
const emit = __emit
126126
127127
return { emit }
128128
}
@@ -136,10 +136,10 @@ export type Emits = (e: 'foo' | 'bar') => void
136136
137137
export default /*#__PURE__*/_defineComponent({
138138
emits: [\\"foo\\", \\"bar\\"],
139-
setup(__props, { expose: __expose, emit }) {
139+
setup(__props, { expose: __expose, emit: __emit }) {
140140
__expose();
141141
142-
142+
const emit = __emit
143143
144144
return { emit }
145145
}
@@ -153,10 +153,10 @@ type Emits = (e: 'foo' | 'bar') => void
153153
154154
export default /*#__PURE__*/_defineComponent({
155155
emits: [\\"foo\\", \\"bar\\"],
156-
setup(__props, { expose: __expose, emit }) {
156+
setup(__props, { expose: __expose, emit: __emit }) {
157157
__expose();
158158
159-
159+
const emit = __emit
160160
161161
return { emit }
162162
}
@@ -170,10 +170,10 @@ type Emits = { (e: 'foo' | 'bar'): void }
170170
171171
export default /*#__PURE__*/_defineComponent({
172172
emits: [\\"foo\\", \\"bar\\"],
173-
setup(__props, { expose: __expose, emit }) {
173+
setup(__props, { expose: __expose, emit: __emit }) {
174174
__expose();
175175
176-
176+
const emit = __emit
177177
178178
return { emit }
179179
}
@@ -186,10 +186,10 @@ exports[`defineEmits > w/ type (type literal w/ call signatures) 1`] = `
186186
187187
export default /*#__PURE__*/_defineComponent({
188188
emits: [\\"foo\\", \\"bar\\", \\"baz\\"],
189-
setup(__props, { expose: __expose, emit }) {
189+
setup(__props, { expose: __expose, emit: __emit }) {
190190
__expose();
191191
192-
192+
const emit = __emit
193193
194194
return { emit }
195195
}
@@ -204,10 +204,10 @@ type BaseEmit = \\"change\\"
204204
205205
export default /*#__PURE__*/_defineComponent({
206206
emits: [\\"some\\", \\"emit\\", \\"change\\", \\"another\\"],
207-
setup(__props, { expose: __expose, emit }) {
207+
setup(__props, { expose: __expose, emit: __emit }) {
208208
__expose();
209209
210-
210+
const emit = __emit;
211211
212212
return { emit }
213213
}
@@ -220,10 +220,10 @@ exports[`defineEmits > w/ type (union) 1`] = `
220220
221221
export default /*#__PURE__*/_defineComponent({
222222
emits: [\\"foo\\", \\"bar\\", \\"baz\\"],
223-
setup(__props, { expose: __expose, emit }) {
223+
setup(__props, { expose: __expose, emit: __emit }) {
224224
__expose();
225225
226-
226+
const emit = __emit
227227
228228
return { emit }
229229
}
@@ -236,10 +236,10 @@ exports[`defineEmits > w/ type 1`] = `
236236
237237
export default /*#__PURE__*/_defineComponent({
238238
emits: [\\"foo\\", \\"bar\\"],
239-
setup(__props, { expose: __expose, emit }) {
239+
setup(__props, { expose: __expose, emit: __emit }) {
240240
__expose();
241241
242-
242+
const emit = __emit
243243
244244
return { emit }
245245
}
@@ -254,10 +254,10 @@ exports[`defineEmits > w/ type from normal script 1`] = `
254254
255255
export default /*#__PURE__*/_defineComponent({
256256
emits: [\\"foo\\", \\"bar\\"],
257-
setup(__props, { expose: __expose, emit }) {
257+
setup(__props, { expose: __expose, emit: __emit }) {
258258
__expose();
259259
260-
260+
const emit = __emit
261261
262262
return { emit }
263263
}

0 commit comments

Comments
 (0)