]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): add error handling for defineModel() without variable assignment...
authorRunyasak Chaengnaimuang <runyasak.c@gmail.com>
Wed, 21 May 2025 01:06:05 +0000 (08:06 +0700)
committerGitHub <noreply@github.com>
Wed, 21 May 2025 01:06:05 +0000 (09:06 +0800)
close #13280

packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/__tests__/compileScript/defineModel.spec.ts
packages/compiler-sfc/src/script/defineModel.ts

index 73c6d316a40a77e2cbc95008834fdc96a2fb69a4..b2a8036a0ee53d9c00a2cfe00da455f98181f2c1 100644 (file)
@@ -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
         })
index 5d696a95d8836058c64a8ee4367aaa1127fcfba1..104d3dc7ab7210d213418fdc3c6559f52ca0023f 100644 (file)
@@ -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()',
+    )
+  })
 })
index 05082800284667a63c61466fd83950eb898e8de2..746bbabe55210a2a0a9f3a1dabd1fb23b06b677b 100644 (file)
@@ -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 =