]> git.ipfire.org Git - thirdparty/vuejs/core.git/commitdiff
fix(compiler-sfc): should extract comment for import or type declarations (#2107)
authorunderfin <likui6666666@gmail.com>
Tue, 15 Sep 2020 14:39:27 +0000 (22:39 +0800)
committerGitHub <noreply@github.com>
Tue, 15 Sep 2020 14:39:27 +0000 (10:39 -0400)
fix #2102

packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap
packages/compiler-sfc/__tests__/compileScript.spec.ts
packages/compiler-sfc/src/compileScript.ts

index 59428dea71e2878410fd57cbbb8820d753279e7c..a6bb93e7405d362f80e0dd22b7e4bf0191741b86 100644 (file)
@@ -373,6 +373,19 @@ export function setup() {
 
       x()
       
+return {  }
+}
+
+export default { setup }"
+`;
+
+exports[`SFC compile <script setup> should extract comment for import or type declarations 1`] = `
+"import a from 'a' // comment
+import b from 'b'
+
+export function setup() {
+
+
 return {  }
 }
 
index 0a6b9d0042d79eb667d0279f57f3737c29447565..38a385acfcd0c3507538ad6278a37835505184b6 100644 (file)
@@ -28,6 +28,15 @@ describe('SFC compile <script setup>', () => {
     )
   })
 
+  test('should extract comment for import or type declarations', () => {
+    assertCode(
+      compile(`<script setup>
+import a from 'a' // comment
+import b from 'b'
+</script>`).content
+    )
+  })
+
   test('explicit setup signature', () => {
     assertCode(
       compile(`<script setup="props, { emit }">emit('foo')</script>`).content
index ed0911014ecf72ce30893f9a25d6e8f6338f7a21..a17f3babb305d9425af599170e048eed3ad44045 100644 (file)
@@ -266,6 +266,12 @@ export function compileScript(
     const start = node.start! + startOffset
     let end = node.end! + startOffset
     // import or type declarations: move to top
+    // locate comment
+    if (node.trailingComments && node.trailingComments.length > 0) {
+      const lastCommentNode =
+        node.trailingComments[node.trailingComments.length - 1]
+      end = lastCommentNode.end + startOffset
+    }
     // locate the end of whitespace between this statement and the next
     while (end <= source.length) {
       if (!/\s/.test(source.charAt(end))) {