Kaynağa Gözat

refactor(compiler-vapor): move `templateIndex` to dynamic

三咲智子 Kevin Deng 2 yıl önce
ebeveyn
işleme
004edd3bac

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

@@ -44,7 +44,9 @@ describe('compiler: v-for', () => {
         index: undefined,
         render: {
           type: IRNodeTypes.BLOCK,
-          templateIndex: 0,
+          dynamic: {
+            template: 0,
+          },
         },
         keyProperty: {
           type: NodeTypes.SIMPLE_EXPRESSION,

+ 24 - 8
packages/compiler-vapor/__tests__/transforms/vIf.spec.ts

@@ -43,7 +43,9 @@ describe('compiler: v-if', () => {
         },
         positive: {
           type: IRNodeTypes.BLOCK,
-          templateIndex: 0,
+          dynamic: {
+            template: 0,
+          },
         },
       },
     ])
@@ -125,11 +127,15 @@ describe('compiler: v-if', () => {
         },
         positive: {
           type: IRNodeTypes.BLOCK,
-          templateIndex: 0,
+          dynamic: {
+            template: 0,
+          },
         },
         negative: {
           type: IRNodeTypes.BLOCK,
-          templateIndex: 1,
+          dynamic: {
+            template: 1,
+          },
         },
       },
     ])
@@ -154,7 +160,9 @@ describe('compiler: v-if', () => {
         },
         positive: {
           type: IRNodeTypes.BLOCK,
-          templateIndex: 0,
+          dynamic: {
+            template: 0,
+          },
         },
         negative: {
           type: IRNodeTypes.IF,
@@ -165,7 +173,9 @@ describe('compiler: v-if', () => {
           },
           positive: {
             type: IRNodeTypes.BLOCK,
-            templateIndex: 1,
+            dynamic: {
+              template: 1,
+            },
           },
         },
       },
@@ -187,17 +197,23 @@ describe('compiler: v-if', () => {
         id: 1,
         positive: {
           type: IRNodeTypes.BLOCK,
-          templateIndex: 0,
+          dynamic: {
+            template: 0,
+          },
         },
         negative: {
           type: IRNodeTypes.IF,
           positive: {
             type: IRNodeTypes.BLOCK,
-            templateIndex: 1,
+            dynamic: {
+              template: 1,
+            },
           },
           negative: {
             type: IRNodeTypes.BLOCK,
-            templateIndex: 2,
+            dynamic: {
+              template: 2,
+            },
           },
         },
       },

+ 2 - 5
packages/compiler-vapor/src/generators/block.ts

@@ -31,16 +31,13 @@ export function genBlockFunction(
 }
 
 export function genBlockFunctionContent(
-  { dynamic, effect, operation, templateIndex, returns }: BlockIRNode,
+  { dynamic, effect, operation, returns }: BlockIRNode,
   context: CodegenContext,
   customReturns?: (returns: CodeFragment[]) => CodeFragment[],
 ): CodeFragment[] {
   const [frag, push] = buildCodeFragment()
 
-  if (templateIndex > -1) {
-    push(NEWLINE, `const n${dynamic.id} = t${templateIndex}()`)
-    push(...genChildren(dynamic, context, dynamic.id!))
-  }
+  push(...genChildren(dynamic, context, dynamic.id!))
 
   const directiveOps = operation.filter(
     (oper): oper is WithDirectiveIRNode =>

+ 7 - 3
packages/compiler-vapor/src/generators/template.ts

@@ -23,7 +23,11 @@ export function genChildren(
   const { vaporHelper } = context
   const [frag, push] = buildCodeFragment()
   let offset = 0
-  const { children } = dynamic
+  const { children, id, template } = dynamic
+
+  if (id !== undefined && template !== undefined) {
+    push(NEWLINE, `const n${id} = t${template}()`)
+  }
 
   for (const [index, child] of children.entries()) {
     if (child.flags & DynamicFlag.NON_TEMPLATE) {
@@ -36,11 +40,11 @@ export function genChildren(
         ? child.flags & DynamicFlag.INSERT
           ? child.anchor
           : child.id
-        : null
+        : undefined
 
     const newPaths = [...paths, elementIndex]
 
-    if (id !== null) {
+    if (id !== undefined) {
       push(
         NEWLINE,
         `const n${id} = `,

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

@@ -45,7 +45,6 @@ export type VaporHelper = keyof typeof import('@vue/runtime-vapor')
 export interface BlockIRNode extends BaseIRNode {
   type: IRNodeTypes.BLOCK
   node: RootNode | TemplateChildNode
-  templateIndex: number
   dynamic: IRDynamicInfo
   effect: IREffect[]
   operation: OperationNode[]
@@ -205,10 +204,11 @@ export enum DynamicFlag {
 }
 
 export interface IRDynamicInfo {
-  id: number | null
+  id?: number
   flags: DynamicFlag
-  anchor: number | null
+  anchor?: number
   children: IRDynamicInfo[]
+  template?: number
 }
 
 export interface IREffect {

+ 3 - 4
packages/compiler-vapor/src/transform.ts

@@ -139,7 +139,7 @@ function createRootContext(
 
     increaseId: () => globalId++,
     reference() {
-      if (this.dynamic.id !== null) return this.dynamic.id
+      if (this.dynamic.id !== undefined) return this.dynamic.id
       this.dynamic.flags |= DynamicFlag.REFERENCED
       return (this.dynamic.id = this.increaseId())
     },
@@ -181,11 +181,11 @@ function createRootContext(
         template => template === this.template,
       )
       if (existing !== -1) {
-        return (this.block.templateIndex = existing)
+        return (this.dynamic.template = existing)
       }
 
       root.template.push(this.template)
-      return (this.block.templateIndex = root.template.length - 1)
+      return (this.dynamic.template = root.template.length - 1)
     },
     registerOperation(...node) {
       this.block.operation.push(...node)
@@ -225,7 +225,6 @@ export function transform(
     block: {
       type: IRNodeTypes.BLOCK,
       node: root,
-      templateIndex: -1,
       dynamic: extend(genDefaultDynamic(), {
         flags: DynamicFlag.REFERENCED,
       } satisfies Partial<IRDynamicInfo>),

+ 0 - 2
packages/compiler-vapor/src/transforms/utils.ts

@@ -12,9 +12,7 @@ import { extend } from '@vue/shared'
 import { DynamicFlag, type IRDynamicInfo } from '../ir'
 
 export const genDefaultDynamic = (): IRDynamicInfo => ({
-  id: null,
   flags: DynamicFlag.NONE,
-  anchor: null,
   children: [],
 })
 

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

@@ -53,7 +53,6 @@ export function processFor(
   const render: BlockIRNode = {
     type: IRNodeTypes.BLOCK,
     node,
-    templateIndex: -1,
     dynamic: extend(genDefaultDynamic(), {
       flags: DynamicFlag.REFERENCED,
     } satisfies Partial<IRDynamicInfo>),

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

@@ -146,7 +146,6 @@ export function createIfBranch(
   const branch: BlockIRNode = {
     type: IRNodeTypes.BLOCK,
     node,
-    templateIndex: -1,
     dynamic: extend(genDefaultDynamic(), {
       flags: DynamicFlag.REFERENCED,
     } satisfies Partial<IRDynamicInfo>),