Skip to content

Commit cfe6428

Browse files
committed
chore: gen unique handler name
1 parent 8192465 commit cfe6428

File tree

4 files changed

+37
-5
lines changed

4 files changed

+37
-5
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/transformElement.spec.ts.snap

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -100,9 +100,12 @@ exports[`compiler: element transform > component > should wrap as function if v-
100100
101101
export function render(_ctx) {
102102
const _component_Foo = _resolveComponent("Foo")
103+
const _component_Bar = _resolveComponent("Bar")
103104
const _on_bar = $event => (_ctx.handleBar($event))
104-
const n0 = _createComponentWithFallback(_component_Foo, { onBar: () => _on_bar }, null, true)
105-
return n0
105+
const n0 = _createComponentWithFallback(_component_Foo, { onBar: () => _on_bar })
106+
const _on_bar1 = () => _ctx.handler
107+
const n1 = _createComponentWithFallback(_component_Bar, { onBar: () => _on_bar1 })
108+
return [n0, n1]
106109
}"
107110
`;
108111

packages/compiler-vapor/__tests__/transforms/transformElement.spec.ts

Lines changed: 19 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -385,13 +385,17 @@ describe('compiler: element transform', () => {
385385

386386
test('should wrap as function if v-on expression is inline statement', () => {
387387
const { code, ir } = compileWithElementTransform(
388-
`<Foo v-on:bar="handleBar($event)" />`,
388+
`<Foo v-on:bar="handleBar($event)" /><Bar v-on:bar="() => handler" />`,
389389
)
390390
expect(code).toMatchSnapshot()
391+
expect(code).contains(`onBar: () => _on_bar`)
391392
expect(code).contains(
392393
`const _on_bar = $event => (_ctx.handleBar($event))`,
393394
)
394-
expect(code).contains(`onBar: () => _on_bar`)
395+
expect(code).contains(`onBar: () => _on_bar1`)
396+
expect(code).contains(
397+
`const _on_bar1 = () => _ctx.handler`,
398+
)
395399
expect(ir.block.operation).toMatchObject([
396400
{
397401
type: IRNodeTypes.CREATE_COMPONENT_NODE,
@@ -406,6 +410,19 @@ describe('compiler: element transform', () => {
406410
],
407411
],
408412
},
413+
{
414+
type: IRNodeTypes.CREATE_COMPONENT_NODE,
415+
tag: 'Bar',
416+
props: [
417+
[
418+
{
419+
key: { content: 'bar' },
420+
handler: true,
421+
values: [{ content: '_on_bar1' }],
422+
},
423+
],
424+
],
425+
},
409426
])
410427
})
411428
})

packages/compiler-vapor/src/generate.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,8 @@ export class CodegenContext {
3434

3535
identifiers: Record<string, string[]> = Object.create(null)
3636

37+
seemInlineHandlerNames: Record<string, number> = Object.create(null)
38+
3739
block: BlockIRNode
3840
withId<T>(fn: () => T, map: Record<string, string | null>): T {
3941
const { identifiers } = this

packages/compiler-vapor/src/generators/component.ts

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -93,6 +93,16 @@ export function genCreateComponent(
9393
}
9494
}
9595

96+
function getUniqueHandlerName(
97+
context: CodegenContext,
98+
name: string
99+
): string {
100+
const { seemInlineHandlerNames } = context
101+
const count = seemInlineHandlerNames[name] || 0
102+
seemInlineHandlerNames[name] = count + 1
103+
return count === 0 ? name : `${name}${count}`
104+
}
105+
96106
type InlineHandler = {
97107
name: string
98108
value: SimpleExpressionNode
@@ -113,7 +123,7 @@ function processInlineHandlers(
113123
const isMemberExp = isMemberExpression(value, context.options)
114124
// cache inline handlers (fn expression or inline statement)
115125
if (!isMemberExp) {
116-
const name = `_on_${prop.key.content}`
126+
const name = getUniqueHandlerName(context, `_on_${prop.key.content}`)
117127
handlers.push({ name, value })
118128
ids[name] = null
119129
// replace the original prop value with the handler name

0 commit comments

Comments
 (0)