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(transformer-wx): 非实例方法 bind 应该转换为匿名回调 #7292

Open
wants to merge 1 commit into
base: 2.x
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
23 changes: 23 additions & 0 deletions packages/taro-transformer-wx/__tests__/event.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,29 @@ describe('event', () => {
// expect(template).toMatch(`data-e-handleClick-a-b="{{777}}`)
})

test('非 this bind 绑定转换为匿名函数', () => {
const { template, ast, code } = transform({
...baseOptions,
code: buildComponent(
`
const callback = function(params) {}
return (
<View onClick={callback.bind(null, 1)} />
)
`,
'',
`import { Custom } from './utils'`
)
})

const instance = evalClass(ast)
removeShadowData(instance.state)
expect(instance.$$events).toEqual(['anonymousFunc0'])
expect(template).toMatch(
`bindtap="anonymousFunc0"`
)
})

describe('this.props.func', () => {
test('简单情况', () => {
const { template, ast, code } = transform({
Expand Down
3 changes: 3 additions & 0 deletions packages/taro-transformer-wx/src/class.ts
Original file line number Diff line number Diff line change
Expand Up @@ -974,6 +974,9 @@ class Transformer {
} else if (t.isMemberExpression(expr) && !t.isThisExpression(expr.object)) {
// @TODO: 新旧 props 系统在事件处理上耦合太深,快应用应用新 props 把旧 props 系统逻辑全部清楚
this.buildAnonyMousFunc(path, attr, expr)
} else if (t.isCallExpression(expr) && !code.startsWith('this')) {
// 非类实例方法 bind 应该转换为 anonymous func
this.buildAnonyMousFunc(path, attr, expr)
}
}

Expand Down