Skip to content

Commit a412832

Browse files
committed
fix(compiler-sfc): don't hoist props and emit
closes #7812
1 parent 6277cb9 commit a412832

File tree

11 files changed

+142
-147
lines changed

11 files changed

+142
-147
lines changed

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

Lines changed: 15 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -616,12 +616,11 @@ exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable
616616
"export default {
617617
props: ['item'],
618618
emits: ['a'],
619-
setup(__props, { expose: __expose, emit }) {
619+
setup(__props, { expose: __expose, emit: __emit }) {
620620
__expose();
621621
622-
const props = __props;
623-
624-
622+
const props = __props,
623+
emit = __emit;
625624
626625
return { props, emit }
627626
}
@@ -633,12 +632,12 @@ exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable
633632
"export default {
634633
props: ['item'],
635634
emits: ['a'],
636-
setup(__props, { expose: __expose, emit }) {
635+
setup(__props, { expose: __expose, emit: __emit }) {
637636
__expose();
638637
639-
const props = __props;
640-
641-
const a = 1;
638+
const props = __props,
639+
a = 1,
640+
emit = __emit;
642641
643642
return { props, a, emit }
644643
}
@@ -650,12 +649,12 @@ exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable
650649
"export default {
651650
props: ['item'],
652651
emits: ['a'],
653-
setup(__props, { expose: __expose, emit }) {
652+
setup(__props, { expose: __expose, emit: __emit }) {
654653
__expose();
655654
656-
const props = __props;
657-
658-
const a = 1;
655+
const a = 1,
656+
props = __props,
657+
emit = __emit;
659658
660659
return { a, props, emit }
661660
}
@@ -667,12 +666,12 @@ exports[`SFC compile <script setup> > defineProps/defineEmits in multi-variable
667666
"export default {
668667
props: ['item'],
669668
emits: ['foo'],
670-
setup(__props, { expose: __expose, emit: emits }) {
669+
setup(__props, { expose: __expose, emit: __emit }) {
671670
__expose();
672671
673-
const props = __props;
674-
675-
const a = 0,
672+
const props = __props,
673+
emits = __emit,
674+
a = 0,
676675
b = 0;
677676
678677
return { props, emits, a, b }

packages/compiler-sfc/__tests__/compileScript.spec.ts

Lines changed: 8 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,9 @@ describe('SFC compile <script setup>', () => {
7777
</script>
7878
`)
7979
assertCode(content)
80-
expect(content).toMatch(`const a = 1;`) // test correct removal
80+
expect(content).toMatch(`const props = __props,`)
81+
expect(content).toMatch(`a = 1,`)
82+
expect(content).toMatch(`emit = __emit;`)
8183
expect(content).toMatch(`props: ['item'],`)
8284
expect(content).toMatch(`emits: ['a'],`)
8385
})
@@ -92,7 +94,9 @@ describe('SFC compile <script setup>', () => {
9294
</script>
9395
`)
9496
assertCode(content)
95-
expect(content).toMatch(`const a = 1;`) // test correct removal
97+
expect(content).toMatch(`const a = 1,`)
98+
expect(content).toMatch(`props = __props,`)
99+
expect(content).toMatch(`emit = __emit;`)
96100
expect(content).toMatch(`props: ['item'],`)
97101
expect(content).toMatch(`emits: ['a'],`)
98102
})
@@ -110,7 +114,8 @@ describe('SFC compile <script setup>', () => {
110114
assertCode(content)
111115
expect(content).toMatch(`props: ['item'],`)
112116
expect(content).toMatch(`emits: ['foo'],`)
113-
expect(content).toMatch(`const a = 0,`)
117+
expect(content).toMatch(`const props = __props,`)
118+
expect(content).toMatch(`a = 0,`)
114119
expect(content).toMatch(`b = 0;`)
115120
})
116121

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

Lines changed: 32 additions & 32 deletions
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)