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-core): should correctly handle the variable scoping in the inline expression #11513

Closed
wants to merge 1 commit into from
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.
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 @@ -29,6 +29,22 @@ return function render(_ctx, _cache) {
}"
`;

exports[`compiler: expression transform > should correctly handle the variable scoping 1`] = `
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue

return function render(_ctx, _cache) {
return (_openBlock(), _createElementBlock("div", {
onClick: () => {
for (const msg of _ctx.msgs) {
_ctx.log(msg)
}
_ctx.error(_ctx.msgs)
const msgs = 123
}
}, null, 8 /* PROPS */, ["onClick"]))
}"
`;

exports[`compiler: expression transform > should not prefix catch block param 1`] = `
"const { openBlock: _openBlock, createElementBlock: _createElementBlock } = Vue

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -518,6 +518,21 @@ describe('compiler: expression transform', () => {
expect(code).toMatchSnapshot()
})

test('should correctly handle the variable scoping', () => {
const { code } = compile(
`<div @click="() => {
for (const msg of msgs) {
log(msg)
}
error(msgs)
const msgs = 123
}"/>`,
)
expect(code).toMatch(`log(msg)`)
expect(code).toMatch(`error(_ctx.msgs)`)
expect(code).toMatchSnapshot()
})

test('should not prefix catch block param', () => {
const { code } = compile(
`<div @click="() => {
Expand Down
10 changes: 9 additions & 1 deletion packages/compiler-core/src/babelUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,14 @@ export function walkIdentifiers(
markScopeIdentifier(node, id, knownIds),
)
}
} else if (node.type === 'VariableDeclaration') {
if (parent && !node.declare) {
for (const decl of node.declarations) {
for (const id of extractIdentifiers(decl.id)) {
markScopeIdentifier(parent, id, knownIds)
}
}
}
} else if (node.type === 'CatchClause' && node.param) {
for (const id of extractIdentifiers(node.param)) {
markScopeIdentifier(node, id, knownIds)
Expand Down Expand Up @@ -190,7 +198,7 @@ export function walkBlockDeclarations(
onIdent: (node: Identifier) => void,
) {
for (const stmt of block.body) {
if (stmt.type === 'VariableDeclaration') {
if (stmt.type === 'VariableDeclaration' && stmt.kind === 'var') {
if (stmt.declare) continue
for (const decl of stmt.declarations) {
for (const id of extractIdentifiers(decl.id)) {
Expand Down