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): No access this through function declaration in <script setup> #6827

Open
wants to merge 8 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
Original file line number Diff line number Diff line change
Expand Up @@ -613,6 +613,7 @@ describe('compiler: expression transform', () => {
props: BindingTypes.PROPS,
setup: BindingTypes.SETUP_MAYBE_REF,
setupConst: BindingTypes.SETUP_CONST,
setupLet: BindingTypes.SETUP_LET,
data: BindingTypes.DATA,
options: BindingTypes.OPTIONS,
reactive: BindingTypes.SETUP_REACTIVE_CONST,
Expand Down Expand Up @@ -658,6 +659,16 @@ describe('compiler: expression transform', () => {
expect(code).toMatchSnapshot()
})

// #6822
test('no access this through function declaration', () => {
const { code } = compileWithBindingMetadata(
`<div @click="setupConst()" @keyup="setupLet()" @keydown="setup()"></div>`,
)
expect(code).toMatch(`$setup.setupConst.bind()`)
expect(code).toMatch(`$setup.setupLet.bind()`)
expect(code).toMatch(`$setup.setup.bind()`)
})

test('literal const handling', () => {
const { code } = compileWithBindingMetadata(`<div>{{ literal }}</div>`, {
inline: true,
Expand Down
26 changes: 26 additions & 0 deletions packages/compiler-core/__tests__/transforms/vOn.spec.ts
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
import {
BindingTypes,
type CompilerOptions,
type ElementNode,
ErrorCodes,
Expand Down Expand Up @@ -760,5 +761,30 @@ describe('compiler: transform v-on', () => {
},
})
})

test('variable function handler ', () => {
const { node } = parseWithVOn(`<div v-on:click="foo" />`, {
prefixIdentifiers: true,
cacheHandlers: true,
bindingMetadata: {
foo: BindingTypes.SETUP_LET,
},
})
const vnodeCall = node.codegenNode as VNodeCall
expect(
(vnodeCall.props as ObjectExpression).properties[0].value,
).toMatchObject({
type: NodeTypes.JS_CACHE_EXPRESSION,
index: 0,
value: {
type: NodeTypes.COMPOUND_EXPRESSION,
children: [
`(...args) => (`,
{ content: `$setup.foo && $setup.foo.call(undefined, ...args)` },
`)`,
],
},
})
})
})
})
5 changes: 4 additions & 1 deletion packages/compiler-core/src/transforms/transformExpression.ts
Original file line number Diff line number Diff line change
Expand Up @@ -212,7 +212,10 @@ export function processExpression(
type === BindingTypes.LITERAL_CONST
) {
// setup bindings in non-inline mode
return `$setup.${raw}`
// #6822 No access `this` through function declaration in <script setup>
return parent && parent.type === 'CallExpression'
? `$setup.${raw}.bind()`
: `$setup.${raw}`
} else if (type === BindingTypes.PROPS_ALIASED) {
return `$props['${bindingMetadata.__propsAliases![raw]}']`
} else if (type) {
Expand Down
6 changes: 5 additions & 1 deletion packages/compiler-core/src/transforms/vOn.ts
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,11 @@ export const transformOn: DirectiveTransform = (
// avoiding the need to be patched.
if (shouldCache && isMemberExp) {
if (exp.type === NodeTypes.SIMPLE_EXPRESSION) {
exp.content = `${exp.content} && ${exp.content}(...args)`
if (exp.content.startsWith('$setup')) {
exp.content = `${exp.content} && ${exp.content}.call(undefined, ...args)`
posva marked this conversation as resolved.
Show resolved Hide resolved
} else {
exp.content = `${exp.content} && ${exp.content}(...args)`
}
} else {
exp.children = [...exp.children, ` && `, ...exp.children, `(...args)`]
}
Expand Down