]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): handle type modifier in import specifiers (#5498)
author木杉 <zhmushan@qq.com>
Thu, 14 Apr 2022 02:10:26 +0000 (10:10 +0800)
committerGitHub <noreply@github.com>
Thu, 14 Apr 2022 02:10:26 +0000 (22:10 -0400)
packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 52bda6f48b69a53fc474a2129ffa1d4bec1a18ca..de223bf91d8df2e94af8acd48f7422ff1fc9b35b 100644 (file)
@@ -1351,6 +1351,22 @@ return {  }
 })"
 `;
 
+exports[`SFC compile <script setup> with TypeScript import type 1`] = `
+"import { defineComponent as _defineComponent } from 'vue'
+import type { Foo } from './main.ts'
+        import { type Bar, Baz } from './main.ts'
+        
+export default /*#__PURE__*/_defineComponent({
+  setup(__props, { expose }) {
+  expose();
+
+        
+return { Baz }
+}
+
+})"
+`;
+
 exports[`SFC compile <script setup> with TypeScript runtime Enum 1`] = `
 "import { defineComponent as _defineComponent } from 'vue'
 enum Foo { A = 123 }
index 500777ee5ca90fc606b8ed4322f562009f6d924c..17c97745e32df41506d732aa226a9dc875f71f4e 100644 (file)
@@ -1103,6 +1103,17 @@ const emit = defineEmits(['a', 'b'])
         Foo: BindingTypes.SETUP_CONST
       })
     })
+
+    test('import type', () => {
+      const { content } = compile(
+        `<script setup lang="ts">
+        import type { Foo } from './main.ts'
+        import { type Bar, Baz } from './main.ts'
+        </script>`
+      )
+      expect(content).toMatch(`return { Baz }`)
+      assertCode(content)
+    })
   })
 
   describe('async/await detection', () => {
index 05623326ca0c3b9b5f99727d26e7a9cc7f4c7a1b..f70faeaf02b2956be67d8ab58aa33007b2c0f92e 100644 (file)
@@ -802,7 +802,9 @@ export function compileScript(
             node.source.value,
             specifier.local.name,
             imported,
-            node.importKind === 'type',
+            node.importKind === 'type' ||
+              (specifier.type === 'ImportSpecifier' &&
+                specifier.importKind === 'type'),
             false
           )
         }
@@ -979,7 +981,9 @@ export function compileScript(
             source,
             local,
             imported,
-            node.importKind === 'type',
+            node.importKind === 'type' ||
+              (specifier.type === 'ImportSpecifier' &&
+                specifier.importKind === 'type'),
             true
           )
         }