-
-
Notifications
You must be signed in to change notification settings - Fork 105
/
Copy pathtransformTemplateRef.ts
43 lines (39 loc) · 1.16 KB
/
transformTemplateRef.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
import {
NodeTypes,
type SimpleExpressionNode,
createSimpleExpression,
} from '@vue/compiler-dom'
import type { NodeTransform } from '../transform'
import { IRNodeTypes } from '../ir'
import { normalizeBindShorthand } from './vBind'
import { findProp, isConstantExpression } from '../utils'
import { EMPTY_EXPRESSION } from './utils'
export const transformTemplateRef: NodeTransform = (node, context) => {
if (node.type !== NodeTypes.ELEMENT) return
const dir = findProp(node, 'ref', false, true)
if (!dir) return
let value: SimpleExpressionNode
if (dir.type === NodeTypes.DIRECTIVE) {
value = dir.exp || normalizeBindShorthand(dir.arg!, context)
} else {
value = dir.value
? createSimpleExpression(dir.value.content, true, dir.value.loc)
: EMPTY_EXPRESSION
}
return () => {
const id = context.reference()
const effect = !isConstantExpression(value)
effect &&
context.registerOperation({
type: IRNodeTypes.DECLARE_OLD_REF,
id,
})
context.registerEffect([value], {
type: IRNodeTypes.SET_TEMPLATE_REF,
element: id,
value,
refFor: !!context.inVFor,
effect,
})
}
}