-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
/
Copy pathtext.ts
46 lines (43 loc) · 1.27 KB
/
text.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
import type { SimpleExpressionNode } from '@vue/compiler-dom'
import type { CodegenContext } from '../generate'
import type { GetTextChildIRNode, SetTextIRNode } from '../ir'
import { getLiteralExpressionValue } from '../utils'
import { genExpression } from './expression'
import { type CodeFragment, NEWLINE, genCall } from './utils'
export function genSetText(
oper: SetTextIRNode,
context: CodegenContext,
): CodeFragment[] {
const { helper } = context
const { element, values, generated } = oper
const texts = combineValues(values, context)
return [
NEWLINE,
...genCall(helper('setText'), `${generated ? 'x' : 'n'}${element}`, texts),
]
}
function combineValues(
values: SimpleExpressionNode[],
context: CodegenContext,
): CodeFragment[] {
return values.flatMap((value, i) => {
let exp = genExpression(value, context)
if (getLiteralExpressionValue(value) == null) {
// dynamic, wrap with toDisplayString
exp = genCall(context.helper('toDisplayString'), exp)
}
if (i > 0) {
exp.unshift(' + ')
}
return exp
})
}
export function genGetTextChild(
oper: GetTextChildIRNode,
context: CodegenContext,
): CodeFragment[] {
return [
NEWLINE,
`const x${oper.parent} = ${context.helper('child')}(n${oper.parent})`,
]
}