Просмотр исходного кода

refactor(compiler-vapor): extract new block

三咲智子 Kevin Deng 2 лет назад
Родитель
Сommit
e42fecb992

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

@@ -25,7 +25,7 @@ import {
   type VaporDirectiveNode,
   type VaporDirectiveNode,
 } from './ir'
 } from './ir'
 import { isConstantExpression } from './utils'
 import { isConstantExpression } from './utils'
-import { newDynamic } from './transforms/utils'
+import { newBlock, newDynamic } from './transforms/utils'
 
 
 export type NodeTransform = (
 export type NodeTransform = (
   node: RootNode | TemplateChildNode,
   node: RootNode | TemplateChildNode,
@@ -211,14 +211,7 @@ export function transform(
     source: node.source,
     source: node.source,
     template: [],
     template: [],
     component: new Set(),
     component: new Set(),
-    block: {
-      type: IRNodeTypes.BLOCK,
-      node,
-      dynamic: newDynamic(),
-      effect: [],
-      operation: [],
-      returns: [],
-    },
+    block: newBlock(node),
   }
   }
 
 
   const context = new TransformContext(ir, node, options)
   const context = new TransformContext(ir, node, options)

+ 15 - 1
packages/compiler-vapor/src/transforms/utils.ts

@@ -9,13 +9,27 @@ import {
   createSimpleExpression,
   createSimpleExpression,
 } from '@vue/compiler-dom'
 } from '@vue/compiler-dom'
 import { extend } from '@vue/shared'
 import { extend } from '@vue/shared'
-import { DynamicFlag, type IRDynamicInfo } from '../ir'
+import {
+  type BlockIRNode,
+  DynamicFlag,
+  type IRDynamicInfo,
+  IRNodeTypes,
+} from '../ir'
 
 
 export const newDynamic = (): IRDynamicInfo => ({
 export const newDynamic = (): IRDynamicInfo => ({
   flags: DynamicFlag.REFERENCED,
   flags: DynamicFlag.REFERENCED,
   children: [],
   children: [],
 })
 })
 
 
+export const newBlock = (node: BlockIRNode['node']): BlockIRNode => ({
+  type: IRNodeTypes.BLOCK,
+  node,
+  dynamic: newDynamic(),
+  effect: [],
+  operation: [],
+  returns: [],
+})
+
 export function wrapTemplate(node: ElementNode, dirs: string[]): TemplateNode {
 export function wrapTemplate(node: ElementNode, dirs: string[]): TemplateNode {
   if (node.tagType === ElementTypes.TEMPLATE) {
   if (node.tagType === ElementTypes.TEMPLATE) {
     return node
     return node

+ 2 - 9
packages/compiler-vapor/src/transforms/vFor.ts

@@ -15,7 +15,7 @@ import {
   type VaporDirectiveNode,
   type VaporDirectiveNode,
 } from '../ir'
 } from '../ir'
 import { findProp, propToExpression } from '../utils'
 import { findProp, propToExpression } from '../utils'
-import { newDynamic, wrapTemplate } from './utils'
+import { newBlock, wrapTemplate } from './utils'
 
 
 export const transformVFor = createStructuralDirectiveTransform(
 export const transformVFor = createStructuralDirectiveTransform(
   'for',
   'for',
@@ -48,14 +48,7 @@ export function processFor(
   context.node = node = wrapTemplate(node, ['for'])
   context.node = node = wrapTemplate(node, ['for'])
   context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
   context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
   const id = context.reference()
   const id = context.reference()
-  const render: BlockIRNode = {
-    type: IRNodeTypes.BLOCK,
-    node,
-    dynamic: newDynamic(),
-    effect: [],
-    operation: [],
-    returns: [],
-  }
+  const render: BlockIRNode = newBlock(node)
   const exitBlock = context.enterBlock(render, true)
   const exitBlock = context.enterBlock(render, true)
   context.reference()
   context.reference()
 
 

+ 2 - 10
packages/compiler-vapor/src/transforms/vIf.ts

@@ -15,7 +15,7 @@ import {
   type VaporDirectiveNode,
   type VaporDirectiveNode,
 } from '../ir'
 } from '../ir'
 import { extend } from '@vue/shared'
 import { extend } from '@vue/shared'
-import { newDynamic, wrapTemplate } from './utils'
+import { newBlock, wrapTemplate } from './utils'
 import { getSiblingIf } from './transformComment'
 import { getSiblingIf } from './transformComment'
 
 
 export const transformVIf = createStructuralDirectiveTransform(
 export const transformVIf = createStructuralDirectiveTransform(
@@ -114,15 +114,7 @@ export function createIfBranch(
 ): [BlockIRNode, () => void] {
 ): [BlockIRNode, () => void] {
   context.node = node = wrapTemplate(node, ['if', 'else-if', 'else'])
   context.node = node = wrapTemplate(node, ['if', 'else-if', 'else'])
 
 
-  const branch: BlockIRNode = {
-    type: IRNodeTypes.BLOCK,
-    node,
-    dynamic: newDynamic(),
-    effect: [],
-    operation: [],
-    returns: [],
-  }
-
+  const branch: BlockIRNode = newBlock(node)
   const exitBlock = context.enterBlock(branch)
   const exitBlock = context.enterBlock(branch)
   context.reference()
   context.reference()
   return [branch, exitBlock]
   return [branch, exitBlock]