Skip to content
This repository was archived by the owner on Jul 19, 2025. It is now read-only.

Commit 4b4cb05

Browse files
committed
refactor: pushMulti
1 parent da8e196 commit 4b4cb05

File tree

1 file changed

+63
-48
lines changed

1 file changed

+63
-48
lines changed

packages/compiler-vapor/src/generate.ts

Lines changed: 63 additions & 48 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,10 @@ export interface CodegenContext extends Required<CodegenOptions> {
5858
loc?: SourceLocation,
5959
name?: string,
6060
): void
61+
pushMulti(
62+
codes: [left: string, right: string, segment?: string],
63+
...fn: Array<false | (() => void)>
64+
): void
6165
indent(): void
6266
deindent(): void
6367
newline(): void
@@ -170,6 +174,15 @@ function createCodegenContext(
170174
context.newline()
171175
context.push(code, newlineIndex, node)
172176
},
177+
pushMulti([left, right, seg], ...fns) {
178+
fns = fns.filter(Boolean)
179+
context.push(left)
180+
for (let i = 0; i < fns.length; i++) {
181+
;(fns[i] as () => void)()
182+
if (seg && i < fns.length - 1) context.push(seg)
183+
}
184+
context.push(right)
185+
},
173186
indent() {
174187
++context.indentLevel
175188
},
@@ -435,56 +448,58 @@ function genAppendNode(oper: AppendNodeIRNode, context: CodegenContext) {
435448
}
436449

437450
function genSetEvent(oper: SetEventIRNode, context: CodegenContext) {
438-
const { vaporHelper, push, pushWithNewline } = context
439-
440-
pushWithNewline(`${vaporHelper('on')}(n${oper.element}, `)
441-
442-
// 2nd arg: event name
443-
if (oper.keyOverride) {
444-
const find = JSON.stringify(oper.keyOverride[0])
445-
const replacement = JSON.stringify(oper.keyOverride[1])
446-
push('(')
447-
genExpression(oper.key, context)
448-
push(`) === ${find} ? ${replacement} : (`)
449-
genExpression(oper.key, context)
450-
push(')')
451-
} else {
452-
genExpression(oper.key, context)
453-
}
454-
push(', ')
455-
451+
const { vaporHelper, push, pushWithNewline, pushMulti: pushMulti } = context
456452
const { keys, nonKeys, options } = oper.modifiers
457453

458-
// 3rd arg: event handler
459-
if (oper.value && oper.value.content.trim()) {
460-
if (keys.length) {
461-
push(`${vaporHelper('withKeys')}(`)
462-
}
463-
if (nonKeys.length) {
464-
push(`${vaporHelper('withModifiers')}(`)
465-
}
466-
push('(...args) => (')
467-
genExpression(oper.value, context)
468-
push(' && ')
469-
genExpression(oper.value, context)
470-
push('(...args))')
471-
472-
if (nonKeys.length) {
473-
push(`, ${genArrayExpression(nonKeys)})`)
474-
}
475-
if (keys.length) {
476-
push(`, ${genArrayExpression(keys)})`)
477-
}
478-
} else {
479-
push('() => {}')
480-
}
481-
482-
// 4th arg, gen options
483-
if (options.length) {
484-
push(`, { ${options.map((v) => `${v}: true`).join(', ')} }`)
485-
}
486-
487-
push(')')
454+
pushWithNewline(vaporHelper('on'))
455+
pushMulti(
456+
['(', ')', ', '],
457+
// 1st arg: event name
458+
() => push(`n${oper.element}`),
459+
// 2nd arg: event name
460+
() => {
461+
if (oper.keyOverride) {
462+
const find = JSON.stringify(oper.keyOverride[0])
463+
const replacement = JSON.stringify(oper.keyOverride[1])
464+
pushMulti(['(', ')'], () => genExpression(oper.key, context))
465+
push(` === ${find} ? ${replacement} : `)
466+
pushMulti(['(', ')'], () => genExpression(oper.key, context))
467+
} else {
468+
genExpression(oper.key, context)
469+
}
470+
},
471+
// 3rd arg: event handler
472+
() => {
473+
if (oper.value && oper.value.content.trim()) {
474+
const pushWithKeys = (fn: () => void) => {
475+
push(`${vaporHelper('withKeys')}(`)
476+
fn()
477+
push(`, ${genArrayExpression(keys)})`)
478+
}
479+
const pushWithModifiers = (fn: () => void) => {
480+
push(`${vaporHelper('withModifiers')}(`)
481+
fn()
482+
push(`, ${genArrayExpression(nonKeys)})`)
483+
}
484+
const pushNoop = (fn: () => void) => fn()
485+
486+
;(keys.length ? pushWithKeys : pushNoop)(() =>
487+
(nonKeys.length ? pushWithModifiers : pushNoop)(() => {
488+
push('(...args) => (')
489+
genExpression(oper.value!, context)
490+
push(' && ')
491+
genExpression(oper.value!, context)
492+
push('(...args))')
493+
}),
494+
)
495+
} else {
496+
push('() => {}')
497+
}
498+
},
499+
// 4th arg, gen options
500+
!!options.length &&
501+
(() => push(`{ ${options.map((v) => `${v}: true`).join(', ')} }`)),
502+
)
488503
}
489504

490505
function genWithDirective(oper: WithDirectiveIRNode, context: CodegenContext) {

0 commit comments

Comments
 (0)