fix(compiler-sfc): add error handling for defineModel() without variable assignment (#13352)

close #13280
This commit is contained in:
Runyasak Chaengnaimuang 2025-05-21 08:06:05 +07:00 committed by GitHub
parent 89edc6cdcb
commit 00734afef5
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
3 changed files with 22 additions and 3 deletions

View File

@ -1007,7 +1007,7 @@ describe('SFC compile <script setup>', () => {
expect(() => expect(() =>
compile(`<script setup> compile(`<script setup>
let bar = 1 let bar = 1
defineModel({ const model = defineModel({
default: () => bar default: () => bar
}) })
</script>`), </script>`),
@ -1017,7 +1017,7 @@ describe('SFC compile <script setup>', () => {
expect(() => expect(() =>
compile(`<script setup> compile(`<script setup>
const bar = 1 const bar = 1
defineModel({ const model = defineModel({
default: () => bar default: () => bar
}) })
</script>`), </script>`),
@ -1027,7 +1027,7 @@ describe('SFC compile <script setup>', () => {
expect(() => expect(() =>
compile(`<script setup> compile(`<script setup>
let bar = 1 let bar = 1
defineModel({ const model = defineModel({
get: () => bar, get: () => bar,
set: () => bar set: () => bar
}) })

View File

@ -269,4 +269,16 @@ describe('defineModel()', () => {
modelValue: BindingTypes.SETUP_REF, modelValue: BindingTypes.SETUP_REF,
}) })
}) })
test('error when defineModel is not assigned to a variable', () => {
expect(() =>
compile(`
<script setup>
defineModel()
</script>
`),
).toThrow(
'defineModel() must be assigned to a variable. For example: const model = defineModel()',
)
})
}) })

View File

@ -22,6 +22,13 @@ export function processDefineModel(
return false return false
} }
if (!declId) {
ctx.error(
'defineModel() must be assigned to a variable. For example: const model = defineModel()',
node,
)
}
ctx.hasDefineModelCall = true ctx.hasDefineModelCall = true
const type = const type =