Skip to content

Commit bbc5fe6

Browse files
authored
fix(compiler-sfc): should not rewrite scope variable (#3449)
fix #3445
1 parent ca6aa01 commit bbc5fe6

File tree

3 files changed

+90
-10
lines changed

3 files changed

+90
-10
lines changed

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

+29
Original file line numberDiff line numberDiff line change
@@ -626,6 +626,35 @@ return { }
626626
}"
627627
`;
628628
629+
exports[`SFC compile <script setup> ref: syntax sugar should not rewrite scope variable 1`] = `
630+
"import { ref as _ref } from 'vue'
631+
632+
export default {
633+
expose: [],
634+
setup(__props) {
635+
636+
const a = _ref(1)
637+
const b = _ref(1)
638+
const d = _ref(1)
639+
const e = 1
640+
function test() {
641+
const a = 2
642+
console.log(a)
643+
console.log(b.value)
644+
let c = { c: 3 }
645+
console.log(c)
646+
let $d
647+
console.log($d)
648+
console.log(d.value)
649+
console.log(e)
650+
}
651+
652+
return { a, b, d, e, test }
653+
}
654+
655+
}"
656+
`;
657+
629658
exports[`SFC compile <script setup> ref: syntax sugar using ref binding in property shorthand 1`] = `
630659
"import { ref as _ref } from 'vue'
631660

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

+28
Original file line numberDiff line numberDiff line change
@@ -708,6 +708,34 @@ const emit = defineEmit(['a', 'b'])
708708
assertCode(content)
709709
})
710710

711+
test('should not rewrite scope variable', () => {
712+
const { content } = compile(`
713+
<script setup>
714+
ref: a = 1
715+
ref: b = 1
716+
ref: d = 1
717+
const e = 1
718+
function test() {
719+
const a = 2
720+
console.log(a)
721+
console.log(b)
722+
let c = { c: 3 }
723+
console.log(c)
724+
let $d
725+
console.log($d)
726+
console.log(d)
727+
console.log(e)
728+
}
729+
</script>`)
730+
expect(content).toMatch('console.log(a)')
731+
expect(content).toMatch('console.log(b.value)')
732+
expect(content).toMatch('console.log(c)')
733+
expect(content).toMatch('console.log($d)')
734+
expect(content).toMatch('console.log(d.value)')
735+
expect(content).toMatch('console.log(e)')
736+
assertCode(content)
737+
})
738+
711739
test('object destructure', () => {
712740
const { content, bindings } = compile(`<script setup>
713741
ref: n = 1, ({ a, b: c, d = 1, e: f = 2, ...g } = useFoo())

packages/compiler-sfc/src/compileScript.ts

+33-10
Original file line numberDiff line numberDiff line change
@@ -1344,6 +1344,23 @@ function genRuntimeEmits(emits: Set<string>) {
13441344
: ``
13451345
}
13461346

1347+
function markScopeIdentifier(
1348+
node: Node & { scopeIds?: Set<string> },
1349+
child: Identifier,
1350+
knownIds: Record<string, number>
1351+
) {
1352+
const { name } = child
1353+
if (node.scopeIds && node.scopeIds.has(name)) {
1354+
return
1355+
}
1356+
if (name in knownIds) {
1357+
knownIds[name]++
1358+
} else {
1359+
knownIds[name] = 1
1360+
}
1361+
;(node.scopeIds || (node.scopeIds = new Set())).add(name)
1362+
}
1363+
13471364
/**
13481365
* Walk an AST and find identifiers that are variable references.
13491366
* This is largely the same logic with `transformExpressions` in compiler-core
@@ -1367,6 +1384,21 @@ function walkIdentifiers(
13671384
onIdentifier(node, parent!, parentStack)
13681385
}
13691386
} else if (isFunction(node)) {
1387+
// #3445
1388+
// should not rewrite local variables sharing a name with a top-level ref
1389+
if (node.body.type === 'BlockStatement') {
1390+
node.body.body.forEach(p => {
1391+
if (p.type === 'VariableDeclaration') {
1392+
;(walk as any)(p, {
1393+
enter(child: Node) {
1394+
if (child.type === 'Identifier') {
1395+
markScopeIdentifier(node, child, knownIds)
1396+
}
1397+
}
1398+
})
1399+
}
1400+
})
1401+
}
13701402
// walk function expressions and add its arguments to known identifiers
13711403
// so that we don't prefix them
13721404
node.params.forEach(p =>
@@ -1384,16 +1416,7 @@ function walkIdentifiers(
13841416
parent.right === child
13851417
)
13861418
) {
1387-
const { name } = child
1388-
if (node.scopeIds && node.scopeIds.has(name)) {
1389-
return
1390-
}
1391-
if (name in knownIds) {
1392-
knownIds[name]++
1393-
} else {
1394-
knownIds[name] = 1
1395-
}
1396-
;(node.scopeIds || (node.scopeIds = new Set())).add(name)
1419+
markScopeIdentifier(node, child, knownIds)
13971420
}
13981421
}
13991422
})

0 commit comments

Comments
 (0)