refactor(compiler-vapor): required returns

This commit is contained in:
三咲智子 Kevin Deng 2024-02-22 00:18:10 +08:00
parent 66c0e82d4b
commit fa0ca8a5af
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
9 changed files with 44 additions and 38 deletions

View File

@ -164,7 +164,7 @@ exports[`compile > dynamic root 1`] = `
export function render(_ctx) {
const n1 = _createTextNode()
_setText(n1, 1, 2)
return [n1]
return n1
}"
`;
@ -186,7 +186,7 @@ exports[`compile > expression parsing > interpolation 1`] = `
"(() => {
const n1 = _createTextNode()
_renderEffect(() => _setText(n1, a + b.value))
return [n1]
return n1
})()"
`;
@ -215,7 +215,7 @@ exports[`compile > static + dynamic root 1`] = `
export function render(_ctx) {
const n1 = _createTextNode()
_setText(n1, 1, 2, "3", 4, 5, "6", 7, 8, "9", 'A', 'B')
return [n1]
return n1
}"
`;

View File

@ -16,7 +16,7 @@ export function render(_ctx) {
_renderEffect(_updateEffect)
return [n2, _updateEffect]
}, (item) => (item.id))
return [n1]
return n1
}"
`;
@ -36,7 +36,7 @@ export function render(_ctx) {
_renderEffect(_updateEffect)
return [n2, _updateEffect]
})
return [n1]
return n1
}"
`;
@ -49,6 +49,6 @@ export function render(_ctx) {
const n2 = t0()
return [n2, () => {}]
})
return [n1]
return n1
}"
`;

View File

@ -11,7 +11,7 @@ export function render(_ctx) {
_renderEffect(() => _setText(n3, _ctx.msg))
return n2
})
return [n1]
return n1
}"
`;
@ -69,7 +69,7 @@ export function render(_ctx) {
_renderEffect(() => _setText(n3, _ctx.msg))
return n2
})
return [n1]
return n1
}"
`;
@ -86,7 +86,7 @@ export function render(_ctx) {
const n3 = t1()
return n3
})
return [n1]
return n1
}"
`;
@ -107,7 +107,7 @@ export function render(_ctx) {
const n4 = t2()
return n4
}))
return [n1]
return n1
}"
`;
@ -124,6 +124,6 @@ export function render(_ctx) {
const n3 = t1()
return n3
}))
return [n1]
return n1
}"
`;

View File

@ -21,14 +21,14 @@ export function genBlockFunction(
oper: BlockFunctionIRNode,
context: CodegenContext,
args: CodeFragment[] = [],
returnValue?: () => CodeFragment[],
customReturns?: (returns: CodeFragment[]) => CodeFragment[],
): CodeFragment[] {
return [
'(',
...args,
') => {',
INDENT_START,
...genBlockFunctionContent(oper, context, returnValue),
...genBlockFunctionContent(oper, context, customReturns),
INDENT_END,
NEWLINE,
'}',
@ -36,18 +36,24 @@ export function genBlockFunction(
}
export function genBlockFunctionContent(
ir: BlockFunctionIRNode | RootIRNode,
{
dynamic,
effect,
operation,
templateIndex,
returns,
}: BlockFunctionIRNode | RootIRNode,
context: CodegenContext,
returnValue?: () => CodeFragment[],
customReturns?: (returns: CodeFragment[]) => CodeFragment[],
): CodeFragment[] {
const [frag, push] = buildCodeFragment()
if (ir.templateIndex > -1) {
push(NEWLINE, `const n${ir.dynamic.id} = t${ir.templateIndex}()`)
push(...genChildren(ir.dynamic, context, ir.dynamic.id!))
if (templateIndex > -1) {
push(NEWLINE, `const n${dynamic.id} = t${templateIndex}()`)
push(...genChildren(dynamic, context, dynamic.id!))
}
const directiveOps = ir.operation.filter(
const directiveOps = operation.filter(
(oper): oper is WithDirectiveIRNode =>
oper.type === IRNodeTypes.WITH_DIRECTIVE,
)
@ -55,22 +61,16 @@ export function genBlockFunctionContent(
push(...genWithDirective(directives, context))
}
push(...genOperations(ir.operation, context))
push(...(context.genEffect || genEffects)(ir.effect, context))
push(...genOperations(operation, context))
push(...(context.genEffect || genEffects)(effect, context))
if (ir.returns) {
push(
NEWLINE,
`return `,
...genMulti(['[', ']', ', '], ...ir.returns.map(n => `n${n}`)),
)
} else {
push(
NEWLINE,
'return ',
...(returnValue ? returnValue() : [`n${ir.dynamic.id}`]),
)
}
push(NEWLINE, `return `)
const returnsCode: CodeFragment[] =
returns.length > 1
? genMulti(['[', ']', ', '], ...returns.map(n => `n${n}`))
: [`n${returns[0]}`]
push(...(customReturns ? customReturns(returnsCode) : returnsCode))
return frag
}

View File

@ -31,12 +31,14 @@ export function genFor(
if (rawValue) idMap[rawValue] = `_block.s[0]`
if (rawKey) idMap[rawKey] = `_block.s[1]`
const blockRet = (): CodeFragment[] => [
`[n${render.dynamic.id!}, ${updateFn}]`,
const blockReturns = (returns: CodeFragment[]): CodeFragment[] => [
'[',
...returns,
`, ${updateFn}]`,
]
const blockFn = context.withId(
() => genBlockFunction(render, context, ['_block'], blockRet),
() => genBlockFunction(render, context, ['_block'], blockReturns),
idMap,
)

View File

@ -49,7 +49,7 @@ export interface BlockFunctionIRNode extends BaseIRNode {
dynamic: IRDynamicInfo
effect: IREffect[]
operation: OperationNode[]
returns?: number[]
returns: number[]
}
export interface RootIRNode extends Omit<BlockFunctionIRNode, 'type'> {

View File

@ -228,6 +228,7 @@ export function transform(
} satisfies Partial<IRDynamicInfo>),
effect: [],
operation: [],
returns: [],
}
const context = createRootContext(ir, root, options)
@ -327,6 +328,7 @@ function processDynamicChildren(
}
// mixed: insert with anchor
context.block.returns = [context.dynamic.id!]
for (const [index, child] of children.entries()) {
if (child.flags & DynamicFlag.INSERT) {
prevDynamics.push(child)

View File

@ -59,6 +59,7 @@ export function processFor(
} satisfies Partial<IRDynamicInfo>),
effect: [],
operation: [],
returns: [],
}
const exitBlock = context.enterBlock(render)
context.reference()

View File

@ -152,6 +152,7 @@ export function createIfBranch(
} satisfies Partial<IRDynamicInfo>),
effect: [],
operation: [],
returns: [],
}
const exitBlock = context.enterBlock(branch)