From: 三咲智子 Kevin Deng Date: Sat, 9 Dec 2023 17:01:57 +0000 (+0800) Subject: refactor: pushMulti X-Git-Tag: v3.6.0-alpha.1~16^2~722 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=4b4cb055a4044b362a91aa02edcc75eeb0445bfa;p=thirdparty%2Fvuejs%2Fcore.git refactor: pushMulti --- diff --git a/packages/compiler-vapor/src/generate.ts b/packages/compiler-vapor/src/generate.ts index e9427f4740..9dc5eb1adc 100644 --- a/packages/compiler-vapor/src/generate.ts +++ b/packages/compiler-vapor/src/generate.ts @@ -58,6 +58,10 @@ export interface CodegenContext extends Required { loc?: SourceLocation, name?: string, ): void + pushMulti( + codes: [left: string, right: string, segment?: string], + ...fn: Array void)> + ): void indent(): void deindent(): void newline(): void @@ -170,6 +174,15 @@ function createCodegenContext( context.newline() context.push(code, newlineIndex, node) }, + pushMulti([left, right, seg], ...fns) { + fns = fns.filter(Boolean) + context.push(left) + for (let i = 0; i < fns.length; i++) { + ;(fns[i] as () => void)() + if (seg && i < fns.length - 1) context.push(seg) + } + context.push(right) + }, indent() { ++context.indentLevel }, @@ -435,56 +448,58 @@ function genAppendNode(oper: AppendNodeIRNode, context: CodegenContext) { } function genSetEvent(oper: SetEventIRNode, context: CodegenContext) { - const { vaporHelper, push, pushWithNewline } = context - - pushWithNewline(`${vaporHelper('on')}(n${oper.element}, `) - - // 2nd arg: event name - if (oper.keyOverride) { - const find = JSON.stringify(oper.keyOverride[0]) - const replacement = JSON.stringify(oper.keyOverride[1]) - push('(') - genExpression(oper.key, context) - push(`) === ${find} ? ${replacement} : (`) - genExpression(oper.key, context) - push(')') - } else { - genExpression(oper.key, context) - } - push(', ') - + const { vaporHelper, push, pushWithNewline, pushMulti: pushMulti } = context const { keys, nonKeys, options } = oper.modifiers - // 3rd arg: event handler - if (oper.value && oper.value.content.trim()) { - if (keys.length) { - push(`${vaporHelper('withKeys')}(`) - } - if (nonKeys.length) { - push(`${vaporHelper('withModifiers')}(`) - } - push('(...args) => (') - genExpression(oper.value, context) - push(' && ') - genExpression(oper.value, context) - push('(...args))') - - if (nonKeys.length) { - push(`, ${genArrayExpression(nonKeys)})`) - } - if (keys.length) { - push(`, ${genArrayExpression(keys)})`) - } - } else { - push('() => {}') - } - - // 4th arg, gen options - if (options.length) { - push(`, { ${options.map((v) => `${v}: true`).join(', ')} }`) - } - - push(')') + pushWithNewline(vaporHelper('on')) + pushMulti( + ['(', ')', ', '], + // 1st arg: event name + () => push(`n${oper.element}`), + // 2nd arg: event name + () => { + if (oper.keyOverride) { + const find = JSON.stringify(oper.keyOverride[0]) + const replacement = JSON.stringify(oper.keyOverride[1]) + pushMulti(['(', ')'], () => genExpression(oper.key, context)) + push(` === ${find} ? ${replacement} : `) + pushMulti(['(', ')'], () => genExpression(oper.key, context)) + } else { + genExpression(oper.key, context) + } + }, + // 3rd arg: event handler + () => { + if (oper.value && oper.value.content.trim()) { + const pushWithKeys = (fn: () => void) => { + push(`${vaporHelper('withKeys')}(`) + fn() + push(`, ${genArrayExpression(keys)})`) + } + const pushWithModifiers = (fn: () => void) => { + push(`${vaporHelper('withModifiers')}(`) + fn() + push(`, ${genArrayExpression(nonKeys)})`) + } + const pushNoop = (fn: () => void) => fn() + + ;(keys.length ? pushWithKeys : pushNoop)(() => + (nonKeys.length ? pushWithModifiers : pushNoop)(() => { + push('(...args) => (') + genExpression(oper.value!, context) + push(' && ') + genExpression(oper.value!, context) + push('(...args))') + }), + ) + } else { + push('() => {}') + } + }, + // 4th arg, gen options + !!options.length && + (() => push(`{ ${options.map((v) => `${v}: true`).join(', ')} }`)), + ) } function genWithDirective(oper: WithDirectiveIRNode, context: CodegenContext) {