-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
Copy pathoperation.ts
171 lines (160 loc) · 4.75 KB
/
operation.ts
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
import {
type IREffect,
IRNodeTypes,
type InsertionStateTypes,
type OperationNode,
isBlockOperation,
} from '../ir'
import type { CodegenContext } from '../generate'
import { genInsertNode, genPrependNode } from './dom'
import { genSetDynamicEvents, genSetEvent } from './event'
import { genFor } from './for'
import { genSetHtml } from './html'
import { genIf } from './if'
import { genDynamicProps, genSetProp } from './prop'
import { genDeclareOldRef, genSetTemplateRef } from './templateRef'
import { genGetTextChild, genSetText } from './text'
import {
type CodeFragment,
INDENT_END,
INDENT_START,
NEWLINE,
buildCodeFragment,
genCall,
} from './utils'
import { genCreateComponent } from './component'
import { genSlotOutlet } from './slotOutlet'
import { processExpressions } from './expression'
import { genBuiltinDirective } from './directive'
export function genOperations(
opers: OperationNode[],
context: CodegenContext,
): CodeFragment[] {
const [frag, push] = buildCodeFragment()
for (const operation of opers) {
push(...genOperationWithInsertionState(operation, context))
}
return frag
}
export function genOperationWithInsertionState(
oper: OperationNode,
context: CodegenContext,
): CodeFragment[] {
const [frag, push] = buildCodeFragment()
if (isBlockOperation(oper) && oper.parent) {
push(...genInsertionState(oper, context))
}
push(...genOperation(oper, context))
return frag
}
export function genOperation(
oper: OperationNode,
context: CodegenContext,
): CodeFragment[] {
switch (oper.type) {
case IRNodeTypes.SET_PROP:
return genSetProp(oper, context)
case IRNodeTypes.SET_DYNAMIC_PROPS:
return genDynamicProps(oper, context)
case IRNodeTypes.SET_TEXT:
return genSetText(oper, context)
case IRNodeTypes.SET_EVENT:
return genSetEvent(oper, context)
case IRNodeTypes.SET_DYNAMIC_EVENTS:
return genSetDynamicEvents(oper, context)
case IRNodeTypes.SET_HTML:
return genSetHtml(oper, context)
case IRNodeTypes.SET_TEMPLATE_REF:
return genSetTemplateRef(oper, context)
case IRNodeTypes.INSERT_NODE:
return genInsertNode(oper, context)
case IRNodeTypes.PREPEND_NODE:
return genPrependNode(oper, context)
case IRNodeTypes.IF:
return genIf(oper, context)
case IRNodeTypes.FOR:
return genFor(oper, context)
case IRNodeTypes.CREATE_COMPONENT_NODE:
return genCreateComponent(oper, context)
case IRNodeTypes.DECLARE_OLD_REF:
return genDeclareOldRef(oper)
case IRNodeTypes.SLOT_OUTLET_NODE:
return genSlotOutlet(oper, context)
case IRNodeTypes.DIRECTIVE:
return genBuiltinDirective(oper, context)
case IRNodeTypes.GET_TEXT_CHILD:
return genGetTextChild(oper, context)
default:
const exhaustiveCheck: never = oper
throw new Error(
`Unhandled operation type in genOperation: ${exhaustiveCheck}`,
)
}
}
export function genEffects(
effects: IREffect[],
context: CodegenContext,
): CodeFragment[] {
const {
helper,
block: { expressions },
} = context
const [frag, push, unshift] = buildCodeFragment()
let operationsCount = 0
const { ids, frag: declarationFrags } = processExpressions(
context,
expressions,
)
push(...declarationFrags)
for (let i = 0; i < effects.length; i++) {
const effect = effects[i]
operationsCount += effect.operations.length
const frags = context.withId(() => genEffect(effect, context), ids)
i > 0 && push(NEWLINE)
if (frag[frag.length - 1] === ')' && frags[0] === '(') {
push(';')
}
push(...frags)
}
const newLineCount = frag.filter(frag => frag === NEWLINE).length
if (newLineCount > 1 || operationsCount > 1 || declarationFrags.length > 0) {
unshift(`{`, INDENT_START, NEWLINE)
push(INDENT_END, NEWLINE, '}')
}
if (effects.length) {
unshift(NEWLINE, `${helper('renderEffect')}(() => `)
push(`)`)
}
return frag
}
export function genEffect(
{ operations }: IREffect,
context: CodegenContext,
): CodeFragment[] {
const [frag, push] = buildCodeFragment()
const operationsExps = genOperations(operations, context)
const newlineCount = operationsExps.filter(frag => frag === NEWLINE).length
if (newlineCount > 1) {
push(...operationsExps)
} else {
push(...operationsExps.filter(frag => frag !== NEWLINE))
}
return frag
}
function genInsertionState(
operation: InsertionStateTypes,
context: CodegenContext,
): CodeFragment[] {
return [
NEWLINE,
...genCall(
context.helper('setInsertionState'),
`n${operation.parent}`,
operation.anchor == null
? undefined
: operation.anchor === -1 // -1 indicates prepend
? `0` // runtime anchor value for prepend
: `n${operation.anchor}`,
),
]
}