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 identifier prefixing edge case caused by reused AST #12239

Open
wants to merge 3 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all 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
50 changes: 29 additions & 21 deletions packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -61,30 +61,27 @@ export function walkIdentifiers(
) {
// mark property in destructure pattern
;(node as any).inPattern = true
} else if (isFunctionType(node)) {
if (node.scopeIds) {
node.scopeIds.forEach(id => markKnownIds(id, knownIds))
} else {
// walk function expressions and add its arguments to known identifiers
// so that we don't prefix them
walkFunctionParams(node, id =>
markScopeIdentifier(node, id, knownIds),
)
}
} else if (node.type === 'BlockStatement') {
if (node.scopeIds) {
node.scopeIds.forEach(id => markKnownIds(id, knownIds))
} else {
// #3445 record block-level local variables
walkBlockDeclarations(node, id =>
markScopeIdentifier(node, id, knownIds),
)
}
} else if (node.type === 'CatchClause' && node.param) {
} else if (isFunctionType(node) && !reuseScopeIds(node, knownIds)) {
// walk function expressions and add its arguments to known identifiers
// so that we don't prefix them
walkFunctionParams(node, id => markScopeIdentifier(node, id, knownIds))
} else if (
node.type === 'BlockStatement' &&
!reuseScopeIds(node, knownIds)
) {
// #3445 record block-level local variables
walkBlockDeclarations(node, id =>
markScopeIdentifier(node, id, knownIds),
)
} else if (
node.type === 'CatchClause' &&
node.param &&
!reuseScopeIds(node, knownIds)
) {
for (const id of extractIdentifiers(node.param)) {
markScopeIdentifier(node, id, knownIds)
}
} else if (isForStatement(node)) {
} else if (isForStatement(node) && !reuseScopeIds(node, knownIds)) {
walkForStatement(node, false, id =>
markScopeIdentifier(node, id, knownIds),
)
Expand All @@ -104,6 +101,17 @@ export function walkIdentifiers(
})
}

function reuseScopeIds(
node: Node & { scopeIds?: Set<string> },
knownIds: Record<string, number>,
): boolean {
if (node.scopeIds) {
node.scopeIds.forEach(id => markKnownIds(id, knownIds))
return true
}
return false
}

export function isReferencedIdentifier(
id: Identifier,
parent: Node | null,
Expand Down
48 changes: 48 additions & 0 deletions packages/compiler-sfc/__tests__/compileTemplate.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,54 @@ test('prefixing edge case for reused AST', () => {
expect(code).not.toMatch(`_ctx.t`)
})

test('for loop prefixing edge case for reused AST', () => {
const src = `
<script setup lang="ts">
import { Foo } from './foo'
</script>
<template>
<div @click="(event) => {
for (const item of event) {
console.log(item)
}
}"></div>
</template>
`
const { descriptor } = parse(src)
// compileScript triggers importUsageCheck
compileScript(descriptor, { id: 'xxx' })
const { code } = compileTemplate({
id: 'xxx',
filename: 'test.vue',
ast: descriptor.template!.ast,
source: descriptor.template!.content,
})
expect(code).not.toMatch(`_ctx.item`)
})

test('catch block param prefixing edge case for reused AST', () => {
const src = `
<script setup lang="ts">
import { Foo } from './foo'
</script>
<template>
<div @click="() => {
try {} catch (err) { console.error(err) }
}"/>
</template>
`
const { descriptor } = parse(src)
// compileScript triggers importUsageCheck
compileScript(descriptor, { id: 'xxx' })
const { code } = compileTemplate({
id: 'xxx',
filename: 'test.vue',
ast: descriptor.template!.ast,
source: descriptor.template!.content,
})
expect(code).not.toMatch(`_ctx.err`)
})

test('prefixing edge case for reused AST ssr mode', () => {
const src = `
<script setup lang="ts">
Expand Down