Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(compiler-sfc): fix that semi may be required when removing macro … #7812

Closed
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -1261,6 +1261,24 @@ return () => {}
}"
`;

exports[`SFC compile <script setup> > preserve semi when completely removing macro definitions 1`] = `
"export default {
props: ['item'],
setup(__props, { expose }) {
expose();

const props = __props;

console.log('test')

;(function () {})()

return { props }
}

}"
`;

exports[`SFC compile <script setup> > should expose top level declarations 1`] = `
"import { x } from './x'

Expand Down
15 changes: 15 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,21 @@ const myEmit = defineEmits(['foo', 'bar'])
expect(content).toMatch(`emits: ['a'],`)
})

// #7805
test('preserve semi when completely removing macro definitions', () => {
const { content } = compile(`
<script setup>
console.log('test')
const props = defineProps(['item']);
(function () {})()
</script>
`)
assertCode(content)
expect(content).toMatch(`console.log('test')`)
expect(content).toMatch(`props: ['item'],`)
expect(content).toMatch(`;(function () {})()`)
})

test('defineProps/defineEmits in multi-variable declaration (full removal)', () => {
const { content } = compile(`
<script setup>
Expand Down
27 changes: 27 additions & 0 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1189,6 +1189,25 @@ export function compileScript(
)
}

function needSaveSemi() {
const reg = /^[\[\{\(\+\-\/]/g
const index = scriptSetupAst.body.indexOf(node)
const nextNode = scriptSetupAst.body[index + 1]
if (
reg.test(
s.slice(nextNode?.start! + startOffset, nextNode?.end! + startOffset)
)
) {
return {
need: true,
start: nextNode?.start! + startOffset
}
}
return {
need: false
}
}

if (node.type === 'ExpressionStatement') {
// process `defineProps` and `defineEmit(s)` calls
if (
Expand All @@ -1197,6 +1216,10 @@ export function compileScript(
processWithDefaults(node.expression)
) {
s.remove(node.start! + startOffset, node.end! + startOffset)
const { need, start } = needSaveSemi()
if (need) {
s.appendLeft(start!, ';')
}
} else if (processDefineExpose(node.expression)) {
// defineExpose({}) -> expose({})
const callee = (node.expression as CallExpression).callee
Expand All @@ -1222,6 +1245,10 @@ export function compileScript(
if (isDefineProps || isDefineEmits) {
if (left === 1) {
s.remove(node.start! + startOffset, node.end! + startOffset)
const { need, start } = needSaveSemi()
if (need) {
s.appendLeft(start!, ';')
}
} else {
let start = decl.start! + startOffset
let end = decl.end! + startOffset
Expand Down