Skip to content

Commit 05df696

Browse files
authored
fix(compiler-sfc): should extract comment for import or type declarations (#2107)
fix #2102
1 parent 98cc1f9 commit 05df696

File tree

3 files changed

+28
-0
lines changed

3 files changed

+28
-0
lines changed

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

+13
Original file line numberDiff line numberDiff line change
@@ -373,6 +373,19 @@ export function setup() {
373373
374374
x()
375375
376+
return { }
377+
}
378+
379+
export default { setup }"
380+
`;
381+
382+
exports[`SFC compile <script setup> should extract comment for import or type declarations 1`] = `
383+
"import a from 'a' // comment
384+
import b from 'b'
385+
386+
export function setup() {
387+
388+
376389
return { }
377390
}
378391

packages/compiler-sfc/__tests__/compileScript.spec.ts

+9
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,15 @@ describe('SFC compile <script setup>', () => {
2828
)
2929
})
3030

31+
test('should extract comment for import or type declarations', () => {
32+
assertCode(
33+
compile(`<script setup>
34+
import a from 'a' // comment
35+
import b from 'b'
36+
</script>`).content
37+
)
38+
})
39+
3140
test('explicit setup signature', () => {
3241
assertCode(
3342
compile(`<script setup="props, { emit }">emit('foo')</script>`).content

packages/compiler-sfc/src/compileScript.ts

+6
Original file line numberDiff line numberDiff line change
@@ -266,6 +266,12 @@ export function compileScript(
266266
const start = node.start! + startOffset
267267
let end = node.end! + startOffset
268268
// import or type declarations: move to top
269+
// locate comment
270+
if (node.trailingComments && node.trailingComments.length > 0) {
271+
const lastCommentNode =
272+
node.trailingComments[node.trailingComments.length - 1]
273+
end = lastCommentNode.end + startOffset
274+
}
269275
// locate the end of whitespace between this statement and the next
270276
while (end <= source.length) {
271277
if (!/\s/.test(source.charAt(end))) {

0 commit comments

Comments
 (0)