fix(compiler-sfc): should extract comment for import or type declarations (#2107)

fix #2102
This commit is contained in:
underfin 2020-09-15 22:39:27 +08:00 committed by GitHub
parent 98cc1f9d84
commit 05df696a2b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 28 additions and 0 deletions

View File

@ -373,6 +373,19 @@ export function setup() {
x() 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 { } return { }
} }

View 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', () => { test('explicit setup signature', () => {
assertCode( assertCode(
compile(`<script setup="props, { emit }">emit('foo')</script>`).content compile(`<script setup="props, { emit }">emit('foo')</script>`).content

View File

@ -266,6 +266,12 @@ export function compileScript(
const start = node.start! + startOffset const start = node.start! + startOffset
let end = node.end! + startOffset let end = node.end! + startOffset
// import or type declarations: move to top // 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 // locate the end of whitespace between this statement and the next
while (end <= source.length) { while (end <= source.length) {
if (!/\s/.test(source.charAt(end))) { if (!/\s/.test(source.charAt(end))) {