fix(compiler-sfc): add error handling for defineModel() without variable assignment (#13352)
close #13280
This commit is contained in:
parent
89edc6cdcb
commit
00734afef5
|
@ -1007,7 +1007,7 @@ describe('SFC compile <script setup>', () => {
|
|||
expect(() =>
|
||||
compile(`<script setup>
|
||||
let bar = 1
|
||||
defineModel({
|
||||
const model = defineModel({
|
||||
default: () => bar
|
||||
})
|
||||
</script>`),
|
||||
|
@ -1017,7 +1017,7 @@ describe('SFC compile <script setup>', () => {
|
|||
expect(() =>
|
||||
compile(`<script setup>
|
||||
const bar = 1
|
||||
defineModel({
|
||||
const model = defineModel({
|
||||
default: () => bar
|
||||
})
|
||||
</script>`),
|
||||
|
@ -1027,7 +1027,7 @@ describe('SFC compile <script setup>', () => {
|
|||
expect(() =>
|
||||
compile(`<script setup>
|
||||
let bar = 1
|
||||
defineModel({
|
||||
const model = defineModel({
|
||||
get: () => bar,
|
||||
set: () => bar
|
||||
})
|
||||
|
|
|
@ -269,4 +269,16 @@ describe('defineModel()', () => {
|
|||
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()',
|
||||
)
|
||||
})
|
||||
})
|
||||
|
|
|
@ -22,6 +22,13 @@ export function processDefineModel(
|
|||
return false
|
||||
}
|
||||
|
||||
if (!declId) {
|
||||
ctx.error(
|
||||
'defineModel() must be assigned to a variable. For example: const model = defineModel()',
|
||||
node,
|
||||
)
|
||||
}
|
||||
|
||||
ctx.hasDefineModelCall = true
|
||||
|
||||
const type =
|
||||
|
|
Loading…
Reference in New Issue