Skip to content

Commit

Permalink
feat(compiler-sfc): enable reactive props destructure by default
Browse files Browse the repository at this point in the history
Also allow prohibiting usage via config.
RFC: vuejs/rfcs#502
  • Loading branch information
yyx990803 committed Apr 29, 2024
1 parent 75c8cf6 commit d2dac0e
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 14 deletions.
18 changes: 18 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineProps.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -597,11 +597,29 @@ const props = defineProps({ foo: String })
foo: Foo
}>()
</script>`,
{
propsDestructure: false,
},
)
expect(content).toMatch(`const { foo } = __props`)
assertCode(content)
})

test('prohibiting reactive destructure', () => {
expect(() =>
compile(
`<script setup lang="ts">
const { foo } = defineProps<{
foo: Foo
}>()
</script>`,
{
propsDestructure: 'error',
},
),
).toThrow()
})

describe('errors', () => {
test('w/ both type and non-type args', () => {
expect(() => {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,6 @@ describe('sfc reactive props destructure', () => {
function compile(src: string, options?: Partial<SFCScriptCompileOptions>) {
return compileSFCScript(src, {
inlineTemplate: true,
propsDestructure: true,
...options,
})
}
Expand Down
7 changes: 4 additions & 3 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,10 +106,11 @@ export interface SFCScriptCompileOptions {
*/
hoistStatic?: boolean
/**
* (**Experimental**) Enable reactive destructure for `defineProps`
* @default false
* Set to `false` to disable reactive destructure for `defineProps` (pre-3.5
* behavior), or set to `'error'` to throw hard error on props destructures.
* @default true
*/
propsDestructure?: boolean
propsDestructure?: boolean | 'error'
/**
* File system access methods to be used when resolving types
* imported in SFC macros. Defaults to ts.sys in Node.js, can be overwritten
Expand Down
14 changes: 4 additions & 10 deletions packages/compiler-sfc/src/script/definePropsDestructure.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,23 +22,17 @@ import { genPropsAccessExp } from '@vue/shared'
import { isCallOf, resolveObjectKey } from './utils'
import type { ScriptCompileContext } from './context'
import { DEFINE_PROPS } from './defineProps'
import { warnOnce } from '../warn'

export function processPropsDestructure(
ctx: ScriptCompileContext,
declId: ObjectPattern,
) {
if (!ctx.options.propsDestructure) {
if (ctx.options.propsDestructure === 'error') {
ctx.error(`Props destructure is explicitly prohibited via config.`, declId)
} else if (ctx.options.propsDestructure === false) {
return
}

warnOnce(
`This project is using reactive props destructure, which is an experimental ` +
`feature. It may receive breaking changes or be removed in the future, so ` +
`use at your own risk.\n` +
`To stay updated, follow the RFC at https://github.com/vuejs/rfcs/discussions/502.`,
)

ctx.propsDestructureDecl = declId

const registerBinding = (
Expand Down Expand Up @@ -104,7 +98,7 @@ export function transformDestructuredProps(
ctx: ScriptCompileContext,
vueImportAliases: Record<string, string>,
) {
if (!ctx.options.propsDestructure) {
if (ctx.options.propsDestructure === false) {
return
}

Expand Down

0 comments on commit d2dac0e

Please sign in to comment.