Skip to content
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,56 @@ return { count }
}"
`;

exports[`defineModel() > w/ props destructure, default factory using the props argument 1`] = `
"import { useModel as _useModel, mergeModels as _mergeModels, defineComponent as _defineComponent } from 'vue'

export default /*@__PURE__*/_defineComponent({
props: /*@__PURE__*/_mergeModels({
x: { type: Number, required: false }
}, {
"modelValue": {
default: props => props.x
},
"modelModifiers": {},
}),
emits: ["update:modelValue"],
setup(__props: any, { expose: __expose }) {
__expose();


const modelValue = _useModel(__props, "modelValue")

return { modelValue }
}

})"
`;

exports[`defineModel() > w/ props destructure, local shadowing a destructured prop 1`] = `
"import { useModel as _useModel, mergeModels as _mergeModels, defineComponent as _defineComponent } from 'vue'

export default /*@__PURE__*/_defineComponent({
props: /*@__PURE__*/_mergeModels({
x: { type: Number, required: false }
}, {
"modelValue": {
default: () => { const x = 1; return x }
},
"modelModifiers": {},
}),
emits: ["update:modelValue"],
setup(__props: any, { expose: __expose }) {
__expose();


const modelValue = _useModel(__props, "modelValue")

return { modelValue }
}

})"
`;

exports[`defineModel() > w/ template literal name 1`] = `
"import { useModel as _useModel } from 'vue'

Expand Down
49 changes: 49 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -287,6 +287,55 @@ describe('defineModel()', () => {
expect(content).toMatch(`set: (v) => { return v + __props.x }`)
})

// #15602
test('w/ props destructure, prop option referencing a destructured prop', () => {
expect(() =>
compile(
`
<script setup lang="ts">
const { x } = defineProps<{ x?: number }>()
const modelValue = defineModel({
default: () => x
})
</script>
`,
{ propsDestructure: true },
),
).toThrow(`prop options cannot reference destructured props`)
})

test('w/ props destructure, default factory using the props argument', () => {
const { content } = compile(
`
<script setup lang="ts">
const { x } = defineProps<{ x?: number }>()
const modelValue = defineModel({
default: props => props.x
})
</script>
`,
{ propsDestructure: true },
)
assertCode(content)
expect(content).toMatch(`default: props => props.x`)
})

test('w/ props destructure, local shadowing a destructured prop', () => {
const { content } = compile(
`
<script setup lang="ts">
const { x } = defineProps<{ x?: number }>()
const modelValue = defineModel({
default: () => { const x = 1; return x }
})
</script>
`,
{ propsDestructure: true },
)
assertCode(content)
expect(content).toMatch(`default: () => { const x = 1; return x }`)
})

test('w/ Boolean And Function types, production mode', () => {
const { content, bindings } = compile(
`
Expand Down
30 changes: 30 additions & 0 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ import {
isJS,
isLiteralNode,
isTS,
resolveObjectKey,
} from './script/utils'
import { analyzeScriptBindings } from './script/analyzeScriptBindings'
import {
Expand Down Expand Up @@ -296,6 +297,30 @@ export function compileScript(
})
}

function checkInvalidDestructuredPropsReference(
Comment thread
edison1105 marked this conversation as resolved.
Resolved
node: Node | undefined,
method: string,
) {
if (!node) return
const isDefault =
(node.type === 'ObjectProperty' || node.type === 'ObjectMethod') &&
resolveObjectKey(node.key, node.computed) === 'default'
walkIdentifiers(node, id => {
const key = propsLocalToPublicMap[id.name]
if (key) {
ctx.error(
`\`${method}()\` prop options cannot reference destructured props ` +
`because they are hoisted outside of the setup() function.` +
(isDefault
? ` Use the props argument of the default factory instead, ` +
`e.g. default: props => props.${key}`
Comment thread
coderabbitai[bot] marked this conversation as resolved.
Resolved
: ``),
id,
)
}
})
}

const scriptAst = ctx.scriptAst
const scriptSetupAst = ctx.scriptSetupAst!

Expand Down Expand Up @@ -719,9 +744,14 @@ export function compileScript(
checkInvalidScopeReference(ctx.propsDestructureDecl, DEFINE_PROPS)
checkInvalidScopeReference(ctx.emitsRuntimeDecl, DEFINE_EMITS)
checkInvalidScopeReference(ctx.optionsRuntimeDecl, DEFINE_OPTIONS)
const propsLocalToPublicMap: Record<string, string> = Object.create(null)
for (const key in ctx.propsDestructuredBindings) {
propsLocalToPublicMap[ctx.propsDestructuredBindings[key].local] = key
}
for (const { runtimeOptionNodes } of Object.values(ctx.modelDecls)) {
for (const node of runtimeOptionNodes) {
checkInvalidScopeReference(node, DEFINE_MODEL)
checkInvalidDestructuredPropsReference(node, DEFINE_MODEL)
}
}

Expand Down
Loading