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(() =>
|
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
|
||||||
})
|
})
|
||||||
|
|
|
@ -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()',
|
||||||
|
)
|
||||||
|
})
|
||||||
})
|
})
|
||||||
|
|
|
@ -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 =
|
||||||
|
|
Loading…
Reference in New Issue