Browse Source

refactor(compiler-vapor): required returns

三咲智子 Kevin Deng 2 years ago
parent
commit
fa0ca8a5af

+ 3 - 3
packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

@@ -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
 }"
 `;
 

+ 3 - 3
packages/compiler-vapor/__tests__/transforms/__snapshots__/vFor.spec.ts.snap

@@ -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
 }"
 `;

+ 5 - 5
packages/compiler-vapor/__tests__/transforms/__snapshots__/vIf.spec.ts.snap

@@ -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
 }"
 `;

+ 23 - 23
packages/compiler-vapor/src/generators/block.ts

@@ -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
 }

+ 5 - 3
packages/compiler-vapor/src/generators/for.ts

@@ -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,
   )
 

+ 1 - 1
packages/compiler-vapor/src/ir.ts

@@ -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'> {

+ 2 - 0
packages/compiler-vapor/src/transform.ts

@@ -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)

+ 1 - 0
packages/compiler-vapor/src/transforms/vFor.ts

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

+ 1 - 0
packages/compiler-vapor/src/transforms/vIf.ts

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