fix(compiler-sfc): fix 'export default' rewrite with extra whitespaces (#4375)

This commit is contained in:
lidlanca 2021-08-23 18:40:16 -04:00 committed by GitHub
parent 9043d0dca7
commit 4792ebd687
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 81 additions and 5 deletions

View File

@ -33,6 +33,48 @@ return { x }
export const n = 1" export const n = 1"
`; `;
exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with many spaces and newline 1`] = `
"import { x } from './x'
export const n = 1
const __default__ = {
some:'option'
}
function setup(__props, { expose }) {
x()
return { x }
}
export default /*#__PURE__*/ Object.assign(__default__, {
setup
})"
`;
exports[`SFC compile <script setup> <script> and <script setup> co-usage spaces in ExportDefaultDeclaration node with minimal spaces 1`] = `
"import { x } from './x'
export const n = 1
const __default__ = {
some:'option'
}
function setup(__props, { expose }) {
x()
return { x }
}
export default /*#__PURE__*/ Object.assign(__default__, {
setup
})"
`;
exports[`SFC compile <script setup> defineEmits() 1`] = ` exports[`SFC compile <script setup> defineEmits() 1`] = `
"export default { "export default {
emits: ['foo', 'bar'], emits: ['foo', 'bar'],

View File

@ -140,6 +140,43 @@ defineExpose({ foo: 123 })
}) })
describe('<script> and <script setup> co-usage', () => { describe('<script> and <script setup> co-usage', () => {
describe('spaces in ExportDefaultDeclaration node', () => {
// #4371
test('with many spaces and newline', () => {
// #4371
const { content } = compile(`
<script>
export const n = 1
export default
{
some:'option'
}
</script>
<script setup>
import { x } from './x'
x()
</script>
`)
assertCode(content)
})
test('with minimal spaces', () => {
const { content } = compile(`
<script>
export const n = 1
export default{
some:'option'
}
</script>
<script setup>
import { x } from './x'
x()
</script>
`)
assertCode(content)
})
})
test('script first', () => { test('script first', () => {
const { content } = compile(` const { content } = compile(`
<script> <script>

View File

@ -605,11 +605,8 @@ export function compileScript(
// export default // export default
defaultExport = node defaultExport = node
const start = node.start! + scriptStartOffset! const start = node.start! + scriptStartOffset!
s.overwrite( const end = node.declaration.start! + scriptStartOffset!
start, s.overwrite(start, end, `const ${defaultTempVar} = `)
start + `export default`.length,
`const ${defaultTempVar} =`
)
} else if (node.type === 'ExportNamedDeclaration' && node.specifiers) { } else if (node.type === 'ExportNamedDeclaration' && node.specifiers) {
const defaultSpecifier = node.specifiers.find( const defaultSpecifier = node.specifiers.find(
s => s.exported.type === 'Identifier' && s.exported.name === 'default' s => s.exported.type === 'Identifier' && s.exported.name === 'default'