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

This commit is contained in:
三咲智子 Kevin Deng 2024-02-22 11:46:40 +08:00
parent 0e0ee5b85e
commit 004edd3bac
No known key found for this signature in database
GPG Key ID: 69992F2250DFD93E
9 changed files with 42 additions and 28 deletions

View File

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

View File

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

View File

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

View File

@ -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} = `,

View File

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

View File

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

View File

@ -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: [],
})

View File

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

View File

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