refactor(compiler-vapor): group directive

This commit is contained in:
三咲智子 Kevin Deng 2024-04-07 14:00:28 +08:00
parent 9a33d79963
commit 98bae0c4a9
No known key found for this signature in database
1 changed files with 14 additions and 8 deletions

View File

@ -1,4 +1,9 @@
import { type BlockIRNode, IRNodeTypes, type WithDirectiveIRNode } from '../ir'
import {
type BlockIRNode,
IRNodeTypes,
type OperationNode,
type WithDirectiveIRNode,
} from '../ir'
import {
type CodeFragment,
INDENT_END,
@ -41,11 +46,7 @@ export function genBlockContent(
push(...genChildren(child, context, child.id!))
}
const directiveOps = operation.filter(
(oper): oper is WithDirectiveIRNode =>
oper.type === IRNodeTypes.WITH_DIRECTIVE,
)
for (const directives of groupDirective(directiveOps)) {
for (const directives of groupDirective(operation)) {
push(...genWithDirective(directives, context))
}
@ -67,9 +68,14 @@ export function genBlockContent(
return frag
}
function groupDirective(ops: WithDirectiveIRNode[]): WithDirectiveIRNode[][] {
function groupDirective(operation: OperationNode[]): WithDirectiveIRNode[][] {
const directiveOps = operation.filter(
(oper): oper is WithDirectiveIRNode =>
oper.type === IRNodeTypes.WITH_DIRECTIVE,
)
const directiveMap: Record<number, WithDirectiveIRNode[]> = {}
for (const oper of ops) {
for (const oper of directiveOps) {
if (!directiveMap[oper.element]) directiveMap[oper.element] = []
directiveMap[oper.element].push(oper)
}