mirror of https://github.com/vuejs/core.git
refactor: pushMulti
This commit is contained in:
parent
da8e196ca5
commit
4b4cb055a4
|
@ -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
|
||||||
|
const { keys, nonKeys, options } = oper.modifiers
|
||||||
pushWithNewline(`${vaporHelper('on')}(n${oper.element}, `)
|
|
||||||
|
|
||||||
|
pushWithNewline(vaporHelper('on'))
|
||||||
|
pushMulti(
|
||||||
|
['(', ')', ', '],
|
||||||
|
// 1st arg: event name
|
||||||
|
() => push(`n${oper.element}`),
|
||||||
// 2nd arg: event name
|
// 2nd arg: event name
|
||||||
|
() => {
|
||||||
if (oper.keyOverride) {
|
if (oper.keyOverride) {
|
||||||
const find = JSON.stringify(oper.keyOverride[0])
|
const find = JSON.stringify(oper.keyOverride[0])
|
||||||
const replacement = JSON.stringify(oper.keyOverride[1])
|
const replacement = JSON.stringify(oper.keyOverride[1])
|
||||||
push('(')
|
pushMulti(['(', ')'], () => genExpression(oper.key, context))
|
||||||
genExpression(oper.key, context)
|
push(` === ${find} ? ${replacement} : `)
|
||||||
push(`) === ${find} ? ${replacement} : (`)
|
pushMulti(['(', ')'], () => genExpression(oper.key, context))
|
||||||
genExpression(oper.key, context)
|
|
||||||
push(')')
|
|
||||||
} else {
|
} else {
|
||||||
genExpression(oper.key, context)
|
genExpression(oper.key, context)
|
||||||
}
|
}
|
||||||
push(', ')
|
},
|
||||||
|
|
||||||
const { keys, nonKeys, options } = oper.modifiers
|
|
||||||
|
|
||||||
// 3rd arg: event handler
|
// 3rd arg: event handler
|
||||||
|
() => {
|
||||||
if (oper.value && oper.value.content.trim()) {
|
if (oper.value && oper.value.content.trim()) {
|
||||||
if (keys.length) {
|
const pushWithKeys = (fn: () => void) => {
|
||||||
push(`${vaporHelper('withKeys')}(`)
|
push(`${vaporHelper('withKeys')}(`)
|
||||||
}
|
fn()
|
||||||
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)})`)
|
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 {
|
} else {
|
||||||
push('() => {}')
|
push('() => {}')
|
||||||
}
|
}
|
||||||
|
},
|
||||||
// 4th arg, gen options
|
// 4th arg, gen options
|
||||||
if (options.length) {
|
!!options.length &&
|
||||||
push(`, { ${options.map((v) => `${v}: true`).join(', ')} }`)
|
(() => push(`{ ${options.map((v) => `${v}: true`).join(', ')} }`)),
|
||||||
}
|
)
|
||||||
|
|
||||||
push(')')
|
|
||||||
}
|
}
|
||||||
|
|
||||||
function genWithDirective(oper: WithDirectiveIRNode, context: CodegenContext) {
|
function genWithDirective(oper: WithDirectiveIRNode, context: CodegenContext) {
|
||||||
|
|
Loading…
Reference in New Issue