fix(compiler-sfc): fix defineProps/defineEmits usage in multi-variable declarations

fix #3739
This commit is contained in:
Evan You
2021-06-28 16:27:30 -04:00
parent 2973b6c30a
commit 62c1b2f7dc
3 changed files with 74 additions and 3 deletions

View File

@@ -820,7 +820,10 @@ export function compileScript(
}
if (node.type === 'VariableDeclaration' && !node.declare) {
for (const decl of node.declarations) {
const total = node.declarations.length
let left = total
for (let i = 0; i < total; i++) {
const decl = node.declarations[i]
if (decl.init) {
const isDefineProps =
processDefineProps(decl.init) || processWithDefaults(decl.init)
@@ -838,10 +841,20 @@ export function compileScript(
)
}
if (isDefineProps || isDefineEmits)
if (node.declarations.length === 1) {
if (left === 1) {
s.remove(node.start! + startOffset, node.end! + startOffset)
} else {
s.remove(decl.start! + startOffset, decl.end! + startOffset)
let start = decl.start! + startOffset
let end = decl.end! + startOffset
if (i < total - 1) {
// not the last one, locate the start of the next
end = node.declarations[i + 1].start! + startOffset
} else {
// last one, locate the end of the prev
start = node.declarations[i - 1].end! + startOffset
}
s.remove(start, end)
left--
}
}
}