Browse Source

fix(compiler-sfc): fix edge case of default export call with no args (#7536)

closes #7534
三咲智子 Kevin Deng 3 năm trước cách đây
mục cha
commit
d60e58c9f6

+ 18 - 0
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap

@@ -59,6 +59,24 @@ return { n, get x() { return x } }
 }"
 `;
 
+exports[`SFC compile <script setup> > <script> and <script setup> co-usage > export call expression as default 1`] = `
+"function fn() {
+        return \\"hello, world\\";
+      }
+      const __default__ = fn();
+      
+export default /*#__PURE__*/Object.assign(__default__, {
+  setup(__props, { expose }) {
+  expose();
+
+      console.log('foo')
+      
+return { fn }
+}
+
+})"
+`;
+
 exports[`SFC compile <script setup> > <script> and <script setup> co-usage > script first 1`] = `
 "import { x } from './x'
       

+ 16 - 0
packages/compiler-sfc/__tests__/compileScript.spec.ts

@@ -386,6 +386,22 @@ defineExpose({ foo: 123 })
         assertCode(content)
       })
     })
+
+    test('export call expression as default', () => {
+      const { content } = compile(`
+      <script>
+      function fn() {
+        return "hello, world";
+      }
+      export default fn();
+      </script>
+
+      <script setup>
+      console.log('foo')
+      </script>
+      `)
+      assertCode(content)
+    })
   })
 
   describe('imports', () => {

+ 1 - 0
packages/compiler-sfc/src/compileScript.ts

@@ -1116,6 +1116,7 @@ export function compileScript(
           optionProperties = defaultExport.declaration.properties
         } else if (
           defaultExport.declaration.type === 'CallExpression' &&
+          defaultExport.declaration.arguments[0] &&
           defaultExport.declaration.arguments[0].type === 'ObjectExpression'
         ) {
           optionProperties = defaultExport.declaration.arguments[0].properties