| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532 |
- import { BindingTypes } from '@vue/compiler-core'
- import type { SFCScriptCompileOptions } from '../../src'
- import { assertCode, compileSFCScript } from '../utils'
- describe('sfc reactive props destructure', () => {
- function compile(src: string, options?: Partial<SFCScriptCompileOptions>) {
- return compileSFCScript(src, {
- inlineTemplate: true,
- ...options,
- })
- }
- test('basic usage', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { foo } = defineProps(['foo'])
- console.log(foo)
- </script>
- <template>{{ foo }}</template>
- `)
- expect(content).not.toMatch(`const { foo } =`)
- expect(content).toMatch(`console.log(__props.foo)`)
- expect(content).toMatch(`_toDisplayString(__props.foo)`)
- assertCode(content)
- expect(bindings).toStrictEqual({
- foo: BindingTypes.PROPS,
- })
- })
- test('multiple variable declarations', () => {
- const { content, bindings } = compile(`
- <script setup>
- const bar = 'fish', { foo } = defineProps(['foo']), hello = 'world'
- </script>
- <template><div>{{ foo }} {{ hello }} {{ bar }}</div></template>
- `)
- expect(content).not.toMatch(`const { foo } =`)
- expect(content).toMatch(`const bar = 'fish', hello = 'world'`)
- expect(content).toMatch(`_toDisplayString(hello)`)
- expect(content).toMatch(`_toDisplayString(bar)`)
- expect(content).toMatch(`_toDisplayString(__props.foo)`)
- assertCode(content)
- expect(bindings).toStrictEqual({
- foo: BindingTypes.PROPS,
- bar: BindingTypes.LITERAL_CONST,
- hello: BindingTypes.LITERAL_CONST,
- })
- })
- test('nested scope', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { foo, bar } = defineProps(['foo', 'bar'])
- function test(foo) {
- console.log(foo)
- console.log(bar)
- }
- </script>
- `)
- expect(content).not.toMatch(`const { foo, bar } =`)
- expect(content).toMatch(`console.log(foo)`)
- expect(content).toMatch(`console.log(__props.bar)`)
- assertCode(content)
- expect(bindings).toStrictEqual({
- foo: BindingTypes.PROPS,
- bar: BindingTypes.PROPS,
- test: BindingTypes.SETUP_CONST,
- })
- })
- test('default values w/ array runtime declaration', () => {
- const { content } = compile(`
- <script setup>
- const { foo = 1, bar = {}, func = () => {} } = defineProps(['foo', 'bar', 'baz'])
- </script>
- `)
- // literals can be used as-is, non-literals are always returned from a
- // function
- // functions need to be marked with a skip marker
- expect(content)
- .toMatch(`props: /*@__PURE__*/_mergeDefaults(['foo', 'bar', 'baz'], {
- foo: 1,
- bar: () => ({}),
- func: () => {}, __skip_func: true
- })`)
- assertCode(content)
- })
- test('default values w/ object runtime declaration', () => {
- const { content } = compile(`
- <script setup>
- const { foo = 1, bar = {}, func = () => {}, ext = x } = defineProps({ foo: Number, bar: Object, func: Function, ext: null })
- </script>
- `)
- // literals can be used as-is, non-literals are always returned from a
- // function
- // functions need to be marked with a skip marker since we cannot always
- // safely infer whether runtime type is Function (e.g. if the runtime decl
- // is imported, or spreads another object)
- expect(content)
- .toMatch(`props: /*@__PURE__*/_mergeDefaults({ foo: Number, bar: Object, func: Function, ext: null }, {
- foo: 1,
- bar: () => ({}),
- func: () => {}, __skip_func: true,
- ext: x, __skip_ext: true
- })`)
- assertCode(content)
- })
- test('default values w/ runtime declaration & key is string', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { foo = 1, 'foo:bar': fooBar = 'foo-bar' } = defineProps(['foo', 'foo:bar'])
- </script>
- `)
- expect(bindings).toStrictEqual({
- __propsAliases: {
- fooBar: 'foo:bar',
- },
- foo: BindingTypes.PROPS,
- 'foo:bar': BindingTypes.PROPS,
- fooBar: BindingTypes.PROPS_ALIASED,
- })
- expect(content).toMatch(`
- props: /*@__PURE__*/_mergeDefaults(['foo', 'foo:bar'], {
- foo: 1,
- "foo:bar": 'foo-bar'
- }),`)
- assertCode(content)
- })
- test('default values w/ type declaration', () => {
- const { content } = compile(`
- <script setup lang="ts">
- const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, func?: () => any }>()
- </script>
- `)
- // literals can be used as-is, non-literals are always returned from a
- // function
- expect(content).toMatch(`props: {
- foo: { type: Number, required: false, default: 1 },
- bar: { type: Object, required: false, default: () => ({}) },
- func: { type: Function, required: false, default: () => {} }
- }`)
- assertCode(content)
- })
- test('default values w/ type declaration & key is string', () => {
- const { content, bindings } = compile(`
- <script setup lang="ts">
- const { foo = 1, bar = 2, 'foo:bar': fooBar = 'foo-bar' } = defineProps<{
- "foo": number // double-quoted string
- 'bar': number // single-quoted string
- 'foo:bar': string // single-quoted string containing symbols
- "onUpdate:modelValue": (val: number) => void // double-quoted string containing symbols
- }>()
- </script>
- `)
- expect(bindings).toStrictEqual({
- __propsAliases: {
- fooBar: 'foo:bar',
- },
- foo: BindingTypes.PROPS,
- bar: BindingTypes.PROPS,
- 'foo:bar': BindingTypes.PROPS,
- fooBar: BindingTypes.PROPS_ALIASED,
- 'onUpdate:modelValue': BindingTypes.PROPS,
- })
- expect(content).toMatch(`
- props: {
- foo: { type: Number, required: true, default: 1 },
- bar: { type: Number, required: true, default: 2 },
- "foo:bar": { type: String, required: true, default: 'foo-bar' },
- "onUpdate:modelValue": { type: Function, required: true }
- },`)
- assertCode(content)
- })
- test('default values w/ type declaration, prod mode', () => {
- const { content } = compile(
- `
- <script setup lang="ts">
- const { foo = 1, bar = {}, func = () => {} } = defineProps<{ foo?: number, bar?: object, baz?: any, boola?: boolean, boolb?: boolean | number, func?: Function }>()
- </script>
- `,
- { isProd: true },
- )
- assertCode(content)
- // literals can be used as-is, non-literals are always returned from a
- // function
- expect(content).toMatch(`props: {
- foo: { default: 1 },
- bar: { default: () => ({}) },
- baz: {},
- boola: { type: Boolean },
- boolb: { type: [Boolean, Number] },
- func: { type: Function, default: () => {} }
- }`)
- })
- test('with TSInstantiationExpression', () => {
- const { content } = compile(
- `
- <script setup lang="ts">
- type Foo = <T extends string | number>(data: T) => void
- const { value } = defineProps<{ value: Foo }>()
- const foo = value<123>
- </script>
- `,
- { isProd: true },
- )
- assertCode(content)
- expect(content).toMatch(`const foo = __props.value<123>`)
- })
- test('aliasing', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { foo: bar } = defineProps(['foo'])
- let x = foo
- let y = bar
- </script>
- <template>{{ foo + bar }}</template>
- `)
- expect(content).not.toMatch(`const { foo: bar } =`)
- expect(content).toMatch(`let x = foo`) // should not process
- expect(content).toMatch(`let y = __props.foo`)
- // should convert bar to __props.foo in template expressions
- expect(content).toMatch(`_toDisplayString(__props.foo + __props.foo)`)
- assertCode(content)
- expect(bindings).toStrictEqual({
- x: BindingTypes.SETUP_LET,
- y: BindingTypes.SETUP_LET,
- foo: BindingTypes.PROPS,
- bar: BindingTypes.PROPS_ALIASED,
- __propsAliases: {
- bar: 'foo',
- },
- })
- })
- // #5425
- test('non-identifier prop names', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { 'foo.bar': fooBar } = defineProps({ 'foo.bar': Function })
- let x = fooBar
- </script>
- <template>{{ fooBar }}</template>
- `)
- expect(content).toMatch(`x = __props["foo.bar"]`)
- expect(content).toMatch(`toDisplayString(__props["foo.bar"])`)
- assertCode(content)
- expect(bindings).toStrictEqual({
- x: BindingTypes.SETUP_LET,
- 'foo.bar': BindingTypes.PROPS,
- fooBar: BindingTypes.PROPS_ALIASED,
- __propsAliases: {
- fooBar: 'foo.bar',
- },
- })
- })
- test('rest spread', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { foo, bar, ...rest } = defineProps(['foo', 'bar', 'baz'])
- </script>
- `)
- expect(content).toMatch(
- `const rest = _createPropsRestProxy(__props, ["foo","bar"])`,
- )
- assertCode(content)
- expect(bindings).toStrictEqual({
- foo: BindingTypes.PROPS,
- bar: BindingTypes.PROPS,
- baz: BindingTypes.PROPS,
- rest: BindingTypes.SETUP_REACTIVE_CONST,
- })
- })
- test('rest spread non-inline', () => {
- const { content, bindings } = compile(
- `
- <script setup>
- const { foo, ...rest } = defineProps(['foo', 'bar'])
- </script>
- <template>{{ rest.bar }}</template>
- `,
- { inlineTemplate: false },
- )
- expect(content).toMatch(
- `const rest = _createPropsRestProxy(__props, ["foo"])`,
- )
- assertCode(content)
- expect(bindings).toStrictEqual({
- foo: BindingTypes.PROPS,
- bar: BindingTypes.PROPS,
- rest: BindingTypes.SETUP_REACTIVE_CONST,
- })
- })
- // #6960
- test('computed static key', () => {
- const { content, bindings } = compile(`
- <script setup>
- const { ['foo']: foo } = defineProps(['foo'])
- console.log(foo)
- </script>
- <template>{{ foo }}</template>
- `)
- expect(content).not.toMatch(`const { foo } =`)
- expect(content).toMatch(`console.log(__props.foo)`)
- expect(content).toMatch(`_toDisplayString(__props.foo)`)
- assertCode(content)
- expect(bindings).toStrictEqual({
- foo: BindingTypes.PROPS,
- })
- })
- test('multi-variable declaration', () => {
- const { content } = compile(`
- <script setup>
- const { item } = defineProps(['item']),
- a = 1;
- </script>
- `)
- assertCode(content)
- expect(content).toMatch(`const a = 1;`)
- expect(content).toMatch(`props: ['item'],`)
- })
- // #6757
- test('multi-variable declaration fix #6757 ', () => {
- const { content } = compile(`
- <script setup>
- const a = 1,
- { item } = defineProps(['item']);
- </script>
- `)
- assertCode(content)
- expect(content).toMatch(`const a = 1;`)
- expect(content).toMatch(`props: ['item'],`)
- })
- // #7422
- test('multi-variable declaration fix #7422', () => {
- const { content } = compile(`
- <script setup>
- const { item } = defineProps(['item']),
- a = 0,
- b = 0;
- </script>
- `)
- assertCode(content)
- expect(content).toMatch(`const a = 0,`)
- expect(content).toMatch(`b = 0;`)
- expect(content).toMatch(`props: ['item'],`)
- })
- test('handle function parameters with same name as destructured props', () => {
- const { content } = compile(`
- <script setup>
- const { value } = defineProps()
- function test(value) {
- try {
- } catch {
- }
- }
- console.log(value)
- </script>
- `)
- assertCode(content)
- expect(content).toMatch(`console.log(__props.value)`)
- })
- test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
- const { content } = compile(`
- <script setup>
- const props = defineProps(['item']),
- emit = defineEmits(['a']);
- </script>
- `)
- assertCode(content)
- expect(content).toMatch(`props: ['item'],`)
- expect(content).toMatch(`emits: ['a'],`)
- })
- describe('errors', () => {
- test('should error on deep destructure', () => {
- expect(() =>
- compile(
- `<script setup>const { foo: [bar] } = defineProps(['foo'])</script>`,
- ),
- ).toThrow(`destructure does not support nested patterns`)
- expect(() =>
- compile(
- `<script setup>const { foo: { bar } } = defineProps(['foo'])</script>`,
- ),
- ).toThrow(`destructure does not support nested patterns`)
- })
- test('should error on computed key', () => {
- expect(() =>
- compile(
- `<script setup>const { [foo]: bar } = defineProps(['foo'])</script>`,
- ),
- ).toThrow(`destructure cannot use computed key`)
- })
- test('should warn when used with withDefaults', () => {
- compile(
- `<script setup lang="ts">
- const { foo } = withDefaults(defineProps<{ foo: string }>(), { foo: 'foo' })
- </script>`,
- )
- expect(
- `withDefaults() is unnecessary when using destructure`,
- ).toHaveBeenWarned()
- })
- test('should error if destructure reference local vars', () => {
- expect(() =>
- compile(
- `<script setup>
- let x = 1
- const {
- foo = () => x
- } = defineProps(['foo'])
- </script>`,
- ),
- ).toThrow(`cannot reference locally declared variables`)
- })
- test('should error if assignment to destructured prop binding', () => {
- expect(() =>
- compile(
- `<script setup>
- const { foo } = defineProps(['foo'])
- foo = 'bar'
- </script>`,
- ),
- ).toThrow(`Cannot assign to destructured props`)
- expect(() =>
- compile(
- `<script setup>
- let { foo } = defineProps(['foo'])
- foo = 'bar'
- </script>`,
- ),
- ).toThrow(`Cannot assign to destructured props`)
- })
- test('should error when passing destructured prop into certain methods', () => {
- expect(() =>
- compile(
- `<script setup>
- import { watch } from 'vue'
- const { foo } = defineProps(['foo'])
- watch(foo, () => {})
- </script>`,
- ),
- ).toThrow(
- `"foo" is a destructured prop and should not be passed directly to watch().`,
- )
- expect(() =>
- compile(
- `<script setup>
- import { watch as w } from 'vue'
- const { foo } = defineProps(['foo'])
- w(foo, () => {})
- </script>`,
- ),
- ).toThrow(
- `"foo" is a destructured prop and should not be passed directly to watch().`,
- )
- expect(() =>
- compile(
- `<script setup>
- import { toRef } from 'vue'
- const { foo } = defineProps(['foo'])
- toRef(foo)
- </script>`,
- ),
- ).toThrow(
- `"foo" is a destructured prop and should not be passed directly to toRef().`,
- )
- expect(() =>
- compile(
- `<script setup>
- import { toRef as r } from 'vue'
- const { foo } = defineProps(['foo'])
- r(foo)
- </script>`,
- ),
- ).toThrow(
- `"foo" is a destructured prop and should not be passed directly to toRef().`,
- )
- })
- // not comprehensive, but should help for most common cases
- test('should error if default value type does not match declared type', () => {
- expect(() =>
- compile(
- `<script setup lang="ts">
- const { foo = 'hello' } = defineProps<{ foo?: number }>()
- </script>`,
- ),
- ).toThrow(`Default value of prop "foo" does not match declared type.`)
- })
- // #8017
- test('should not throw an error if the variable is not a props', () => {
- expect(() =>
- compile(
- `<script setup lang='ts'>
- import { watch } from 'vue'
- const { userId } = defineProps({ userId: Number })
- const { error: e, info } = useRequest();
- watch(e, () => {});
- watch(info, () => {});
- </script>`,
- ),
- ).not.toThrowError()
- })
- })
- })
|