refactor(compiler-vapor): extract new block

This commit is contained in:
三咲智子 Kevin Deng 2024-04-28 23:05:20 +09:00
parent 05f4ade4d9
commit e42fecb992
No known key found for this signature in database
4 changed files with 21 additions and 29 deletions

View File

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

View File

@ -9,13 +9,27 @@ import {
createSimpleExpression,
} from '@vue/compiler-dom'
import { extend } from '@vue/shared'
import { DynamicFlag, type IRDynamicInfo } from '../ir'
import {
type BlockIRNode,
DynamicFlag,
type IRDynamicInfo,
IRNodeTypes,
} from '../ir'
export const newDynamic = (): IRDynamicInfo => ({
flags: DynamicFlag.REFERENCED,
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 {
if (node.tagType === ElementTypes.TEMPLATE) {
return node

View File

@ -15,7 +15,7 @@ import {
type VaporDirectiveNode,
} from '../ir'
import { findProp, propToExpression } from '../utils'
import { newDynamic, wrapTemplate } from './utils'
import { newBlock, wrapTemplate } from './utils'
export const transformVFor = createStructuralDirectiveTransform(
'for',
@ -48,14 +48,7 @@ export function processFor(
context.node = node = wrapTemplate(node, ['for'])
context.dynamic.flags |= DynamicFlag.NON_TEMPLATE | DynamicFlag.INSERT
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)
context.reference()

View File

@ -15,7 +15,7 @@ import {
type VaporDirectiveNode,
} from '../ir'
import { extend } from '@vue/shared'
import { newDynamic, wrapTemplate } from './utils'
import { newBlock, wrapTemplate } from './utils'
import { getSiblingIf } from './transformComment'
export const transformVIf = createStructuralDirectiveTransform(
@ -114,15 +114,7 @@ export function createIfBranch(
): [BlockIRNode, () => void] {
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)
context.reference()
return [branch, exitBlock]