]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): skip empty `defineOptions` and support TypeScript type assertions...
author三咲智子 Kevin Deng <sxzz@sxzz.moe>
Wed, 5 Apr 2023 09:33:29 +0000 (17:33 +0800)
committerGitHub <noreply@github.com>
Wed, 5 Apr 2023 09:33:29 +0000 (17:33 +0800)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index ee00977c1312413f14de1491cfe95b6ccc63ea31..818ea02e30307474a981627fedaf95b454450f41 100644 (file)
@@ -658,12 +658,25 @@ exports[`SFC compile <script setup> > defineOptions() > basic usage 1`] = `
   setup(__props, { expose: __expose }) {
   __expose();
 
+        
+        
+return {  }
+}
+
+})"
+`;
 
+exports[`SFC compile <script setup> > defineOptions() > empty argument 1`] = `
+"export default {
+  setup(__props, { expose: __expose }) {
+  __expose();
 
+        
+        
 return {  }
 }
 
-})"
+}"
 `;
 
 exports[`SFC compile <script setup> > defineProps w/ external definition 1`] = `
index 25bfce72bd2b125dc1e836e560ac189a16a2d12e..7187c5d319115796a358c9ad35a1383f42d6a5cb 100644 (file)
@@ -133,7 +133,7 @@ const myEmit = defineEmits(['foo', 'bar'])
     expect(bindings).toStrictEqual({
       myEmit: BindingTypes.SETUP_CONST
     })
-    // should remove defineOptions import and call
+    // should remove defineEmits import and call
     expect(content).not.toMatch('defineEmits')
     // should generate correct setup signature
     expect(content).toMatch(
@@ -205,10 +205,10 @@ const myEmit = defineEmits(['foo', 'bar'])
   describe('defineOptions()', () => {
     test('basic usage', () => {
       const { content } = compile(`
-<script setup>
-defineOptions({ name: 'FooApp' })
-</script>
-  `)
+        <script setup>
+        defineOptions({ name: 'FooApp' })
+        </script>
+      `)
       assertCode(content)
       // should remove defineOptions import and call
       expect(content).not.toMatch('defineOptions')
@@ -218,6 +218,18 @@ defineOptions({ name: 'FooApp' })
       )
     })
 
+    test('empty argument', () => {
+      const { content } = compile(`
+        <script setup>
+        defineOptions()
+        </script>
+      `)
+      assertCode(content)
+      expect(content).toMatch(`export default {`)
+      // should remove defineOptions import and call
+      expect(content).not.toMatch('defineOptions')
+    })
+
     it('should emit an error with two defineProps', () => {
       expect(() =>
         compile(`
@@ -249,6 +261,26 @@ defineOptions({ name: 'FooApp' })
       ).toThrowError(
         '[@vue/compiler-sfc] defineOptions() cannot be used to declare emits. Use defineEmits() instead.'
       )
+
+      expect(() =>
+        compile(`
+        <script setup>
+        defineOptions({ expose: ['foo'] })
+        </script>
+      `)
+      ).toThrowError(
+        '[@vue/compiler-sfc] defineOptions() cannot be used to declare expose. Use defineExpose() instead.'
+      )
+
+      expect(() =>
+        compile(`
+        <script setup>
+        defineOptions({ slots: ['foo'] })
+        </script>
+      `)
+      ).toThrowError(
+        '[@vue/compiler-sfc] defineOptions() cannot be used to declare slots. Use defineSlots() instead.'
+      )
     })
 
     it('should emit an error with type generic', () => {
@@ -262,6 +294,18 @@ defineOptions({ name: 'FooApp' })
         '[@vue/compiler-sfc] defineOptions() cannot accept type arguments'
       )
     })
+
+    it('should emit an error with type assertion', () => {
+      expect(() =>
+        compile(`
+        <script setup lang="ts">
+        defineOptions({ props: [] } as any)
+        </script>
+        `)
+      ).toThrowError(
+        '[@vue/compiler-sfc] defineOptions() cannot be used to declare props. Use defineProps() instead.'
+      )
+    })
   })
 
   test('defineExpose()', () => {
index 4902bea7a2c27d0b3225a81daba67064ee92a600..8d22d7e1348e6727eb08300bd186f301ce3cef89 100644 (file)
@@ -702,9 +702,10 @@ export function compileScript(
     if (node.typeParameters) {
       error(`${DEFINE_OPTIONS}() cannot accept type arguments`, node)
     }
+    if (!node.arguments[0]) return true
 
     hasDefineOptionsCall = true
-    optionsRuntimeDecl = node.arguments[0]
+    optionsRuntimeDecl = unwrapTSNode(node.arguments[0])
 
     let propsOption = undefined
     let emitsOption = undefined