From 05df696a2b846a249f2569f4d6183c16e4be88e7 Mon Sep 17 00:00:00 2001 From: underfin Date: Tue, 15 Sep 2020 22:39:27 +0800 Subject: [PATCH] fix(compiler-sfc): should extract comment for import or type declarations (#2107) fix #2102 --- .../__snapshots__/compileScript.spec.ts.snap | 13 +++++++++++++ .../compiler-sfc/__tests__/compileScript.spec.ts | 9 +++++++++ packages/compiler-sfc/src/compileScript.ts | 6 ++++++ 3 files changed, 28 insertions(+) diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 59428dea71e..a6bb93e7405 100644 --- a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap +++ b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap @@ -373,6 +373,19 @@ export function setup() { x() +return { } +} + +export default { setup }" +`; + +exports[`SFC compile `).content + ) + }) + test('explicit setup signature', () => { assertCode( compile(``).content diff --git a/packages/compiler-sfc/src/compileScript.ts b/packages/compiler-sfc/src/compileScript.ts index ed0911014ec..a17f3babb30 100644 --- a/packages/compiler-sfc/src/compileScript.ts +++ b/packages/compiler-sfc/src/compileScript.ts @@ -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))) {