-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
Copy pathtransform.ts
310 lines (277 loc) · 7.61 KB
/
transform.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
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
import {
type AllNode,
type TransformOptions as BaseTransformOptions,
type CommentNode,
type CompilerCompatOptions,
type ElementNode,
ElementTypes,
NodeTypes,
type RootNode,
type SimpleExpressionNode,
type TemplateChildNode,
defaultOnError,
defaultOnWarn,
isVSlot,
} from '@vue/compiler-dom'
import { EMPTY_OBJ, NOOP, extend, isArray, isString } from '@vue/shared'
import {
type BlockIRNode,
DynamicFlag,
type HackOptions,
type IRDynamicInfo,
IRNodeTypes,
type IRSlots,
type OperationNode,
type RootIRNode,
type SetEventIRNode,
type VaporDirectiveNode,
} from './ir'
import { isConstantExpression, isStaticExpression } from './utils'
import { newBlock, newDynamic } from './transforms/utils'
export type NodeTransform = (
node: RootNode | TemplateChildNode,
context: TransformContext<RootNode | TemplateChildNode>,
) => void | (() => void) | (() => void)[]
export type DirectiveTransform = (
dir: VaporDirectiveNode,
node: ElementNode,
context: TransformContext<ElementNode>,
) => DirectiveTransformResult | void
export interface DirectiveTransformResult {
key: SimpleExpressionNode
value: SimpleExpressionNode
modifier?: '.' | '^'
runtimeCamelize?: boolean
handler?: boolean
handlerModifiers?: SetEventIRNode['modifiers']
model?: boolean
modelModifiers?: string[]
}
// A structural directive transform is technically also a NodeTransform;
// Only v-if and v-for fall into this category.
export type StructuralDirectiveTransform = (
node: ElementNode,
dir: VaporDirectiveNode,
context: TransformContext<ElementNode>,
) => void | (() => void)
export type TransformOptions = HackOptions<BaseTransformOptions>
export class TransformContext<T extends AllNode = AllNode> {
parent: TransformContext<RootNode | ElementNode> | null = null
root: TransformContext<RootNode>
index: number = 0
block: BlockIRNode = this.ir.block
options: Required<
Omit<TransformOptions, 'filename' | keyof CompilerCompatOptions>
>
template: string = ''
childrenTemplate: (string | null)[] = []
dynamic: IRDynamicInfo = this.ir.block.dynamic
inVOnce: boolean = false
inVFor: number = 0
comment: CommentNode[] = []
component: Set<string> = this.ir.component
directive: Set<string> = this.ir.directive
slots: IRSlots[] = []
private globalId = 0
constructor(
public ir: RootIRNode,
public node: T,
options: TransformOptions = {},
) {
this.options = extend({}, defaultOptions, options)
this.root = this as TransformContext<RootNode>
}
enterBlock(ir: BlockIRNode, isVFor: boolean = false): () => void {
const { block, template, dynamic, childrenTemplate, slots } = this
this.block = ir
this.dynamic = ir.dynamic
this.template = ''
this.childrenTemplate = []
this.slots = []
isVFor && this.inVFor++
return () => {
// exit
this.registerTemplate()
this.block = block
this.template = template
this.dynamic = dynamic
this.childrenTemplate = childrenTemplate
this.slots = slots
isVFor && this.inVFor--
}
}
increaseId = (): number => this.globalId++
reference(): number {
if (this.dynamic.id !== undefined) return this.dynamic.id
this.dynamic.flags |= DynamicFlag.REFERENCED
return (this.dynamic.id = this.increaseId())
}
pushTemplate(content: string): number {
const existing = this.ir.template.findIndex(
template => template === content,
)
if (existing !== -1) return existing
this.ir.template.push(content)
return this.ir.template.length - 1
}
registerTemplate(): number {
if (!this.template) return -1
const id = this.pushTemplate(this.template)
return (this.dynamic.template = id)
}
registerEffect(
expressions: SimpleExpressionNode[],
...operations: OperationNode[]
): void {
expressions = expressions.filter(exp => !isConstantExpression(exp))
if (
this.inVOnce ||
expressions.length === 0 ||
expressions.every(e =>
isStaticExpression(e, this.root.options.bindingMetadata),
)
) {
return this.registerOperation(...operations)
}
this.block.expressions.push(...expressions)
const existing = this.block.effect.find(e =>
isSameExpression(e.expressions, expressions),
)
if (existing) {
existing.operations.push(...operations)
} else {
this.block.effect.push({
expressions,
operations,
})
}
function isSameExpression(
a: SimpleExpressionNode[],
b: SimpleExpressionNode[],
) {
if (a.length !== b.length) return false
return a.every((exp, i) => exp.content === b[i].content)
}
}
registerOperation(...node: OperationNode[]): void {
this.block.operation.push(...node)
}
create<T extends TemplateChildNode>(
node: T,
index: number,
): TransformContext<T> {
return Object.assign(Object.create(TransformContext.prototype), this, {
node,
parent: this as any,
index,
template: '',
childrenTemplate: [],
dynamic: newDynamic(),
} satisfies Partial<TransformContext<T>>)
}
}
const defaultOptions = {
filename: '',
prefixIdentifiers: true,
hoistStatic: false,
hmr: false,
cacheHandlers: false,
nodeTransforms: [],
directiveTransforms: {},
transformHoist: null,
isBuiltInComponent: NOOP,
isCustomElement: NOOP,
expressionPlugins: [],
scopeId: null,
slotted: true,
ssr: false,
inSSR: false,
ssrCssVars: ``,
bindingMetadata: EMPTY_OBJ,
inline: false,
isTS: false,
onError: defaultOnError,
onWarn: defaultOnWarn,
}
// AST -> IR
export function transform(
node: RootNode,
options: TransformOptions = {},
): RootIRNode {
const ir: RootIRNode = {
type: IRNodeTypes.ROOT,
node,
source: node.source,
template: [],
component: new Set(),
directive: new Set(),
block: newBlock(node),
hasTemplateRef: false,
}
const context = new TransformContext(ir, node, options)
transformNode(context)
return ir
}
export function transformNode(
context: TransformContext<RootNode | TemplateChildNode>,
): void {
let { node } = context
// apply transform plugins
const { nodeTransforms } = context.options
const exitFns = []
for (const nodeTransform of nodeTransforms) {
const onExit = nodeTransform(node, context)
if (onExit) {
if (isArray(onExit)) {
exitFns.push(...onExit)
} else {
exitFns.push(onExit)
}
}
if (!context.node) {
// node was removed
return
} else {
// node may have been replaced
node = context.node
}
}
// exit transforms
context.node = node
let i = exitFns.length
while (i--) {
exitFns[i]()
}
if (context.node.type === NodeTypes.ROOT) {
context.registerTemplate()
}
}
export function createStructuralDirectiveTransform(
name: string | string[],
fn: StructuralDirectiveTransform,
): NodeTransform {
const matches = (n: string) =>
isString(name) ? n === name : name.includes(n)
return (node, context) => {
if (node.type === NodeTypes.ELEMENT) {
const { props } = node
// structural directive transforms are not concerned with slots
// as they are handled separately in vSlot.ts
if (node.tagType === ElementTypes.TEMPLATE && props.some(isVSlot)) {
return
}
const exitFns = []
for (const prop of props) {
if (prop.type === NodeTypes.DIRECTIVE && matches(prop.name)) {
const onExit = fn(
node,
prop as VaporDirectiveNode,
context as TransformContext<ElementNode>,
)
if (onExit) exitFns.push(onExit)
}
}
return exitFns
}
}
}