From: underfin Date: Tue, 15 Sep 2020 14:39:27 +0000 (+0800) Subject: fix(compiler-sfc): should extract comment for import or type declarations (#2107) X-Git-Tag: v3.0.0-rc.11~6 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=05df696a2b846a249f2569f4d6183c16e4be88e7;p=thirdparty%2Fvuejs%2Fcore.git fix(compiler-sfc): should extract comment for import or type declarations (#2107) fix #2102 --- diff --git a/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap b/packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap index 59428dea71..a6bb93e740 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 ed0911014e..a17f3babb3 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))) {