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): should not rewrite scope variable #3449

Merged
merged 3 commits into from
Mar 25, 2021
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -508,6 +508,32 @@ return { n, a, b, c }
}"
`;

exports[`SFC compile <script setup> ref: syntax sugar avoid rewrite variable if has same name scoped variable declaration in parent block 1`] = `
"import { ref as _ref } from 'vue'

export default {
expose: [],
setup(__props) {

const a = _ref(1)
const b = _ref(1)
const d = _ref(1)
function test() {
const a = 2
console.log(a)
console.log(b.value)
let c = { c: 3 }
console.log(c)
let $d
console.log($d)
}

return { a, b, d, test }
}

}"
`;

exports[`SFC compile <script setup> ref: syntax sugar convert ref declarations 1`] = `
"import { ref as _ref } from 'vue'

Expand Down
24 changes: 24 additions & 0 deletions packages/compiler-sfc/__tests__/compileScript.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -708,6 +708,30 @@ const emit = defineEmit(['a', 'b'])
assertCode(content)
})

// #3445
test('avoid rewrite variable if has same name scoped variable declaration in parent block', () => {
const { content } = compile(`
<script setup>
ref: a = 1
ref: b = 1
ref: d = 1
function test() {
const a = 2
console.log(a)
console.log(b)
let c = { c: 3 }
console.log(c)
let $d
console.log($d)
}
</script>`)
expect(content).toMatch('console.log(a)')
expect(content).toMatch('console.log(b.value)')
expect(content).toMatch('console.log(c)')
expect(content).toMatch('console.log($d)')
assertCode(content)
})

test('object destructure', () => {
const { content, bindings } = compile(`<script setup>
ref: n = 1, ({ a, b: c, d = 1, e: f = 2, ...g } = useFoo())
Expand Down
40 changes: 30 additions & 10 deletions packages/compiler-sfc/src/compileScript.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1344,6 +1344,23 @@ function genRuntimeEmits(emits: Set<string>) {
: ``
}

function markScopeIdentifier(
node: Node & { scopeIds?: Set<string> },
child: Identifier,
knownIds: Record<string, number>
) {
const { name } = child
if (node.scopeIds && node.scopeIds.has(name)) {
return
}
if (name in knownIds) {
knownIds[name]++
} else {
knownIds[name] = 1
}
;(node.scopeIds || (node.scopeIds = new Set())).add(name)
}

/**
* Walk an AST and find identifiers that are variable references.
* This is largely the same logic with `transformExpressions` in compiler-core
Expand All @@ -1366,6 +1383,18 @@ function walkIdentifiers(
) {
onIdentifier(node, parent!, parentStack)
}
} else if (node.type === 'BlockStatement' && isFunction(parent!)) {
node.body.forEach(p => {
if (p.type === 'VariableDeclaration') {
;(walk as any)(p, {
enter(child: Node, parent: Node) {
if (child.type === 'Identifier') {
markScopeIdentifier(node, child, knownIds)
}
}
})
}
})
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
} else if (isFunction(node)) {
edison1105 marked this conversation as resolved.
Show resolved Hide resolved
// walk function expressions and add its arguments to known identifiers
// so that we don't prefix them
Expand All @@ -1384,16 +1413,7 @@ function walkIdentifiers(
parent.right === child
)
) {
const { name } = child
if (node.scopeIds && node.scopeIds.has(name)) {
return
}
if (name in knownIds) {
knownIds[name]++
} else {
knownIds[name] = 1
}
;(node.scopeIds || (node.scopeIds = new Set())).add(name)
markScopeIdentifier(node, child, knownIds)
}
}
})
Expand Down