refactor: pushMulti

This commit is contained in:
三咲智子 Kevin Deng 2023-12-10 01:01:57 +08:00
parent da8e196ca5
commit 4b4cb055a4
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
1 changed files with 62 additions and 47 deletions

View File

@ -58,6 +58,10 @@ export interface CodegenContext extends Required<CodegenOptions> {
loc?: SourceLocation, loc?: SourceLocation,
name?: string, name?: string,
): void ): void
pushMulti(
codes: [left: string, right: string, segment?: string],
...fn: Array<false | (() => void)>
): void
indent(): void indent(): void
deindent(): void deindent(): void
newline(): void newline(): void
@ -170,6 +174,15 @@ function createCodegenContext(
context.newline() context.newline()
context.push(code, newlineIndex, node) 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() { indent() {
++context.indentLevel ++context.indentLevel
}, },
@ -435,56 +448,58 @@ function genAppendNode(oper: AppendNodeIRNode, context: CodegenContext) {
} }
function genSetEvent(oper: SetEventIRNode, context: CodegenContext) { function genSetEvent(oper: SetEventIRNode, context: CodegenContext) {
const { vaporHelper, push, pushWithNewline } = context const { vaporHelper, push, pushWithNewline, pushMulti: pushMulti } = 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 { keys, nonKeys, options } = oper.modifiers const { keys, nonKeys, options } = oper.modifiers
// 3rd arg: event handler pushWithNewline(vaporHelper('on'))
if (oper.value && oper.value.content.trim()) { pushMulti(
if (keys.length) { ['(', ')', ', '],
push(`${vaporHelper('withKeys')}(`) // 1st arg: event name
} () => push(`n${oper.element}`),
if (nonKeys.length) { // 2nd arg: event name
push(`${vaporHelper('withModifiers')}(`) () => {
} if (oper.keyOverride) {
push('(...args) => (') const find = JSON.stringify(oper.keyOverride[0])
genExpression(oper.value, context) const replacement = JSON.stringify(oper.keyOverride[1])
push(' && ') pushMulti(['(', ')'], () => genExpression(oper.key, context))
genExpression(oper.value, context) push(` === ${find} ? ${replacement} : `)
push('(...args))') 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()
if (nonKeys.length) { ;(keys.length ? pushWithKeys : pushNoop)(() =>
push(`, ${genArrayExpression(nonKeys)})`) (nonKeys.length ? pushWithModifiers : pushNoop)(() => {
} push('(...args) => (')
if (keys.length) { genExpression(oper.value!, context)
push(`, ${genArrayExpression(keys)})`) push(' && ')
} genExpression(oper.value!, context)
} else { push('(...args))')
push('() => {}') }),
} )
} else {
// 4th arg, gen options push('() => {}')
if (options.length) { }
push(`, { ${options.map((v) => `${v}: true`).join(', ')} }`) },
} // 4th arg, gen options
!!options.length &&
push(')') (() => push(`{ ${options.map((v) => `${v}: true`).join(', ')} }`)),
)
} }
function genWithDirective(oper: WithDirectiveIRNode, context: CodegenContext) { function genWithDirective(oper: WithDirectiveIRNode, context: CodegenContext) {