Skip to content

Commit 001f8ce

Browse files
committed
wip: defineContext -> useOptions
1 parent 292a657 commit 001f8ce

File tree

5 files changed

+131
-128
lines changed

5 files changed

+131
-128
lines changed

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

Lines changed: 39 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -56,25 +56,7 @@ return { color }
5656
}"
5757
`;
5858
59-
exports[`SFC compile <script setup> defineContext() 1`] = `
60-
"export default {
61-
props: {
62-
foo: String
63-
},
64-
emit: ['a', 'b'],
65-
setup(__props, { props, emit }) {
66-
67-
68-
69-
const bar = 1
70-
71-
return { props, emit, bar }
72-
}
73-
74-
}"
75-
`;
76-
77-
exports[`SFC compile <script setup> errors should allow defineContext() referencing imported binding 1`] = `
59+
exports[`SFC compile <script setup> errors should allow useOptions() referencing imported binding 1`] = `
7860
"import { bar } from './bar'
7961
8062
export default {
@@ -93,7 +75,7 @@ return { bar }
9375
}"
9476
`;
9577
96-
exports[`SFC compile <script setup> errors should allow defineContext() referencing scope var 1`] = `
78+
exports[`SFC compile <script setup> errors should allow useOptions() referencing scope var 1`] = `
9779
"export default {
9880
props: {
9981
foo: {
@@ -393,7 +375,40 @@ return { a, b, c, d, x }
393375
}"
394376
`;
395377
396-
exports[`SFC compile <script setup> with TypeScript defineContext w/ runtime options 1`] = `
378+
exports[`SFC compile <script setup> useOptions() 1`] = `
379+
"export default {
380+
props: {
381+
foo: String
382+
},
383+
emit: ['a', 'b'],
384+
setup(__props, { props, emit }) {
385+
386+
387+
388+
const bar = 1
389+
390+
return { props, emit, bar }
391+
}
392+
393+
}"
394+
`;
395+
396+
exports[`SFC compile <script setup> with TypeScript hoist type declarations 1`] = `
397+
"import { defineComponent } from 'vue'
398+
export interface Foo {}
399+
type Bar = {}
400+
401+
export default defineComponent({
402+
setup() {
403+
404+
405+
return { }
406+
}
407+
408+
})"
409+
`;
410+
411+
exports[`SFC compile <script setup> with TypeScript useOptions w/ runtime options 1`] = `
397412
"import { defineComponent } from 'vue'
398413
399414
@@ -410,7 +425,7 @@ return { props, emit }
410425
})"
411426
`;
412427
413-
exports[`SFC compile <script setup> with TypeScript defineContext w/ type / extract emits (union) 1`] = `
428+
exports[`SFC compile <script setup> with TypeScript useOptions w/ type / extract emits (union) 1`] = `
414429
"import { Slots, defineComponent } from 'vue'
415430
416431
@@ -431,7 +446,7 @@ return { emit }
431446
})"
432447
`;
433448
434-
exports[`SFC compile <script setup> with TypeScript defineContext w/ type / extract emits 1`] = `
449+
exports[`SFC compile <script setup> with TypeScript useOptions w/ type / extract emits 1`] = `
435450
"import { Slots, defineComponent } from 'vue'
436451
437452
@@ -452,7 +467,7 @@ return { emit }
452467
})"
453468
`;
454469
455-
exports[`SFC compile <script setup> with TypeScript defineContext w/ type / extract props 1`] = `
470+
exports[`SFC compile <script setup> with TypeScript useOptions w/ type / extract props 1`] = `
456471
"import { defineComponent } from 'vue'
457472
458473
interface Test {}
@@ -488,21 +503,6 @@ export default defineComponent({
488503
489504
490505
491-
return { }
492-
}
493-
494-
})"
495-
`;
496-
497-
exports[`SFC compile <script setup> with TypeScript hoist type declarations 1`] = `
498-
"import { defineComponent } from 'vue'
499-
export interface Foo {}
500-
type Bar = {}
501-
502-
export default defineComponent({
503-
setup() {
504-
505-
506506
return { }
507507
}
508508

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

Lines changed: 35 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,11 @@ describe('SFC compile <script setup>', () => {
3636
expect(content).toMatch('return { a, b, c, d, x }')
3737
})
3838

39-
test('defineContext()', () => {
39+
test('useOptions()', () => {
4040
const { content, bindings } = compile(`
4141
<script setup>
42-
import { defineContext } from 'vue'
43-
const { props, emit } = defineContext({
42+
import { useOptions } from 'vue'
43+
const { props, emit } = useOptions({
4444
props: {
4545
foo: String
4646
},
@@ -60,8 +60,8 @@ const bar = 1
6060
emit: 'const'
6161
})
6262

63-
// should remove defineContext import and call
64-
expect(content).not.toMatch('defineContext')
63+
// should remove useOptions import and call
64+
expect(content).not.toMatch('useOptions')
6565
// should generate correct setup signature
6666
expect(content).toMatch(`setup(__props, { props, emit }) {`)
6767
// should include context options in default export
@@ -143,7 +143,7 @@ const bar = 1
143143
const { content } = compile(
144144
`
145145
<script setup>
146-
import { ref, defineContext } from 'vue'
146+
import { ref, useOptions } from 'vue'
147147
import Foo from './Foo.vue'
148148
import other from './util'
149149
const count = ref(0)
@@ -183,11 +183,11 @@ const bar = 1
183183
assertCode(content)
184184
})
185185

186-
test('defineContext w/ runtime options', () => {
186+
test('useOptions w/ runtime options', () => {
187187
const { content } = compile(`
188188
<script setup lang="ts">
189-
import { defineContext } from 'vue'
190-
const { props, emit } = defineContext({
189+
import { useOptions } from 'vue'
190+
const { props, emit } = useOptions({
191191
props: { foo: String },
192192
emits: ['a', 'b']
193193
})
@@ -200,15 +200,15 @@ const { props, emit } = defineContext({
200200
setup(__props, { props, emit }) {`)
201201
})
202202

203-
test('defineContext w/ type / extract props', () => {
203+
test('useOptions w/ type / extract props', () => {
204204
const { content, bindings } = compile(`
205205
<script setup lang="ts">
206-
import { defineContext } from 'vue'
206+
import { useOptions } from 'vue'
207207
interface Test {}
208208
209209
type Alias = number[]
210210
211-
defineContext<{
211+
useOptions<{
212212
props: {
213213
string: string
214214
number: number
@@ -288,11 +288,11 @@ const { props, emit } = defineContext({
288288
})
289289
})
290290

291-
test('defineContext w/ type / extract emits', () => {
291+
test('useOptions w/ type / extract emits', () => {
292292
const { content } = compile(`
293293
<script setup lang="ts">
294-
import { defineContext } from 'vue'
295-
const { emit } = defineContext<{
294+
import { useOptions } from 'vue'
295+
const { emit } = useOptions<{
296296
emit: (e: 'foo' | 'bar') => void
297297
}>()
298298
</script>
@@ -302,11 +302,11 @@ const { props, emit } = defineContext({
302302
expect(content).toMatch(`emits: ["foo", "bar"] as unknown as undefined`)
303303
})
304304

305-
test('defineContext w/ type / extract emits (union)', () => {
305+
test('useOptions w/ type / extract emits (union)', () => {
306306
const { content } = compile(`
307307
<script setup lang="ts">
308-
import { defineContext } from 'vue'
309-
const { emit } = defineContext<{
308+
import { useOptions } from 'vue'
309+
const { emit } = useOptions<{
310310
emit: ((e: 'foo' | 'bar') => void) | ((e: 'baz', id: number) => void)
311311
}>()
312312
</script>
@@ -633,21 +633,21 @@ const { props, emit } = defineContext({
633633
).toThrow(`ref: statements can only contain assignment expressions`)
634634
})
635635

636-
test('defineContext() w/ both type and non-type args', () => {
636+
test('useOptions() w/ both type and non-type args', () => {
637637
expect(() => {
638638
compile(`<script setup lang="ts">
639-
import { defineContext } from 'vue'
640-
defineContext<{}>({})
639+
import { useOptions } from 'vue'
640+
useOptions<{}>({})
641641
</script>`)
642642
}).toThrow(`cannot accept both type and non-type arguments`)
643643
})
644644

645-
test('defineContext() referencing local var', () => {
645+
test('useOptions() referencing local var', () => {
646646
expect(() =>
647647
compile(`<script setup>
648-
import { defineContext } from 'vue'
648+
import { useOptions } from 'vue'
649649
const bar = 1
650-
defineContext({
650+
useOptions({
651651
props: {
652652
foo: {
653653
default: () => bar
@@ -658,24 +658,24 @@ const { props, emit } = defineContext({
658658
).toThrow(`cannot reference locally declared variables`)
659659
})
660660

661-
test('defineContext() referencing ref declarations', () => {
661+
test('useOptions() referencing ref declarations', () => {
662662
expect(() =>
663663
compile(`<script setup>
664-
import { defineContext } from 'vue'
664+
import { useOptions } from 'vue'
665665
ref: bar = 1
666-
defineContext({
666+
useOptions({
667667
props: { bar }
668668
})
669669
</script>`)
670670
).toThrow(`cannot reference locally declared variables`)
671671
})
672672

673-
test('should allow defineContext() referencing scope var', () => {
673+
test('should allow useOptions() referencing scope var', () => {
674674
assertCode(
675675
compile(`<script setup>
676-
import { defineContext } from 'vue'
676+
import { useOptions } from 'vue'
677677
const bar = 1
678-
defineContext({
678+
useOptions({
679679
props: {
680680
foo: {
681681
default: bar => bar + 1
@@ -686,12 +686,12 @@ const { props, emit } = defineContext({
686686
)
687687
})
688688

689-
test('should allow defineContext() referencing imported binding', () => {
689+
test('should allow useOptions() referencing imported binding', () => {
690690
assertCode(
691691
compile(`<script setup>
692-
import { defineContext } from 'vue'
692+
import { useOptions } from 'vue'
693693
import { bar } from './bar'
694-
defineContext({
694+
useOptions({
695695
props: {
696696
foo: {
697697
default: () => bar
@@ -901,8 +901,8 @@ describe('SFC analyze <script> bindings', () => {
901901
it('works for script setup', () => {
902902
const { bindings } = compile(`
903903
<script setup>
904-
import { defineContext } from 'vue'
905-
defineContext({
904+
import { useOptions } from 'vue'
905+
useOptions({
906906
props: {
907907
foo: String,
908908
}

0 commit comments

Comments
 (0)