]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): fix 'export default' rewrite with extra whitespaces (#4375)
authorlidlanca <8693091+lidlanca@users.noreply.github.com>
Mon, 23 Aug 2021 22:40:16 +0000 (18:40 -0400)
committerGitHub <noreply@github.com>
Mon, 23 Aug 2021 22:40:16 +0000 (18:40 -0400)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 7e3a35a7cf0ba6ca8b4baeade32c8d797ec08c60..32ce61b727ac8994d52b63507238717db3d48db5 100644 (file)
@@ -33,6 +33,48 @@ return { x }
       export const n = 1"
 `;
 
+exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with many spaces and newline 1`] = `
+"import { x } from './x'
+        
+        export const n = 1
+        const __default__ = {   
+          some:'option'
+        }
+        
+function setup(__props, { expose }) {
+
+        x()
+        
+return { x }
+}
+
+
+export default /*#__PURE__*/ Object.assign(__default__, {
+  setup
+})"
+`;
+
+exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with minimal spaces 1`] = `
+"import { x } from './x'
+        
+        export const n = 1
+        const __default__ = {   
+          some:'option'
+        }
+        
+function setup(__props, { expose }) {
+
+        x()
+        
+return { x }
+}
+
+
+export default /*#__PURE__*/ Object.assign(__default__, {
+  setup
+})"
+`;
+
 exports[`SFC compile <script setup> defineEmits() 1`] = `
 "export default {
   emits: ['foo', 'bar'],
index 5688805601264ba046304450fb1b1394500fa22f..e214160be3795fad37eb632c0229ff329a1a78a0 100644 (file)
@@ -140,6 +140,43 @@ defineExpose({ foo: 123 })
   })
 
   describe('<script> and <script setup> co-usage', () => {
+    describe('spaces in ExportDefaultDeclaration node', () => {
+      // #4371
+      test('with many spaces and newline', () => {
+        // #4371
+        const { content } = compile(`
+        <script>
+        export const n = 1
+        export        default     
+        {   
+          some:'option'
+        }
+        </script>
+        <script setup>
+        import { x } from './x'
+        x()
+        </script>
+        `)
+        assertCode(content)
+      })
+
+      test('with minimal spaces', () => {
+        const { content } = compile(`
+        <script>
+        export const n = 1
+        export default{   
+          some:'option'
+        }
+        </script>
+        <script setup>
+        import { x } from './x'
+        x()
+        </script>
+        `)
+        assertCode(content)
+      })
+    })
+
     test('script first', () => {
       const { content } = compile(`
       <script>
index 55e5e420511c43f1be5064164ca058ee2a25a161..eefddfd95ea11519f3fd6132b14ebf06c4d8b54b 100644 (file)
@@ -605,11 +605,8 @@ export function compileScript(
         // export default
         defaultExport = node
         const start = node.start! + scriptStartOffset!
-        s.overwrite(
-          start,
-          start + `export default`.length,
-          `const ${defaultTempVar} =`
-        )
+        const end = node.declaration.start! + scriptStartOffset!
+        s.overwrite(start, end, `const ${defaultTempVar} = `)
       } else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
         const defaultSpecifier = node.specifiers.find(
           s => s.exported.type === 'Identifier' && s.exported.name === 'default'