Skip to content

Commit d2dac0e

Browse files
committedApr 29, 2024··
feat(compiler-sfc): enable reactive props destructure by default
Also allow prohibiting usage via config. RFC: vuejs/rfcs#502
1 parent 75c8cf6 commit d2dac0e

File tree

4 files changed

+26
-14
lines changed

4 files changed

+26
-14
lines changed
 

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

+18
Original file line numberDiff line numberDiff line change
@@ -597,11 +597,29 @@ const props = defineProps({ foo: String })
597597
foo: Foo
598598
}>()
599599
</script>`,
600+
{
601+
propsDestructure: false,
602+
},
600603
)
601604
expect(content).toMatch(`const { foo } = __props`)
602605
assertCode(content)
603606
})
604607

608+
test('prohibiting reactive destructure', () => {
609+
expect(() =>
610+
compile(
611+
`<script setup lang="ts">
612+
const { foo } = defineProps<{
613+
foo: Foo
614+
}>()
615+
</script>`,
616+
{
617+
propsDestructure: 'error',
618+
},
619+
),
620+
).toThrow()
621+
})
622+
605623
describe('errors', () => {
606624
test('w/ both type and non-type args', () => {
607625
expect(() => {

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

-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ describe('sfc reactive props destructure', () => {
66
function compile(src: string, options?: Partial<SFCScriptCompileOptions>) {
77
return compileSFCScript(src, {
88
inlineTemplate: true,
9-
propsDestructure: true,
109
...options,
1110
})
1211
}

‎packages/compiler-sfc/src/compileScript.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -106,10 +106,11 @@ export interface SFCScriptCompileOptions {
106106
*/
107107
hoistStatic?: boolean
108108
/**
109-
* (**Experimental**) Enable reactive destructure for `defineProps`
110-
* @default false
109+
* Set to `false` to disable reactive destructure for `defineProps` (pre-3.5
110+
* behavior), or set to `'error'` to throw hard error on props destructures.
111+
* @default true
111112
*/
112-
propsDestructure?: boolean
113+
propsDestructure?: boolean | 'error'
113114
/**
114115
* File system access methods to be used when resolving types
115116
* imported in SFC macros. Defaults to ts.sys in Node.js, can be overwritten

‎packages/compiler-sfc/src/script/definePropsDestructure.ts

+4-10
Original file line numberDiff line numberDiff line change
@@ -22,23 +22,17 @@ import { genPropsAccessExp } from '@vue/shared'
2222
import { isCallOf, resolveObjectKey } from './utils'
2323
import type { ScriptCompileContext } from './context'
2424
import { DEFINE_PROPS } from './defineProps'
25-
import { warnOnce } from '../warn'
2625

2726
export function processPropsDestructure(
2827
ctx: ScriptCompileContext,
2928
declId: ObjectPattern,
3029
) {
31-
if (!ctx.options.propsDestructure) {
30+
if (ctx.options.propsDestructure === 'error') {
31+
ctx.error(`Props destructure is explicitly prohibited via config.`, declId)
32+
} else if (ctx.options.propsDestructure === false) {
3233
return
3334
}
3435

35-
warnOnce(
36-
`This project is using reactive props destructure, which is an experimental ` +
37-
`feature. It may receive breaking changes or be removed in the future, so ` +
38-
`use at your own risk.\n` +
39-
`To stay updated, follow the RFC at https://github.com/vuejs/rfcs/discussions/502.`,
40-
)
41-
4236
ctx.propsDestructureDecl = declId
4337

4438
const registerBinding = (
@@ -104,7 +98,7 @@ export function transformDestructuredProps(
10498
ctx: ScriptCompileContext,
10599
vueImportAliases: Record<string, string>,
106100
) {
107-
if (!ctx.options.propsDestructure) {
101+
if (ctx.options.propsDestructure === false) {
108102
return
109103
}
110104

0 commit comments

Comments
 (0)
Please sign in to comment.