Skip to content

Commit aa5d87b

Browse files
committed
refactor(compiler-vapor): extract segments of genMulti
1 parent 17d598f commit aa5d87b

File tree

15 files changed

+128
-58
lines changed

15 files changed

+128
-58
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
2+
3+
exports[`compiler: template ref transform > dynamic ref 1`] = `
4+
"import { setRef as _setRef, template as _template } from 'vue/vapor';
5+
const t0 = _template("<div></div>")
6+
7+
export function render(_ctx) {
8+
const n0 = t0()
9+
_setRef(n0, _ctx.foo)
10+
return n0
11+
}"
12+
`;
13+
14+
exports[`compiler: template ref transform > ref + v-for 1`] = `
15+
"import { setRef as _setRef, createFor as _createFor, template as _template } from 'vue/vapor';
16+
const t0 = _template("<div></div>")
17+
18+
export function render(_ctx) {
19+
const n0 = _createFor(() => ([1,2,3]), (_block) => {
20+
const n2 = t0()
21+
_setRef(n2, "foo", true)
22+
return [n2, () => {}]
23+
})
24+
return n0
25+
}"
26+
`;
27+
28+
exports[`compiler: template ref transform > ref + v-if 1`] = `
29+
"import { setRef as _setRef, createIf as _createIf, template as _template } from 'vue/vapor';
30+
const t0 = _template("<div></div>")
31+
32+
export function render(_ctx) {
33+
const n0 = _createIf(() => (true), () => {
34+
const n2 = t0()
35+
_setRef(n2, "foo")
36+
return n2
37+
})
38+
return n0
39+
}"
40+
`;
41+
42+
exports[`compiler: template ref transform > static ref 1`] = `
43+
"import { setRef as _setRef, template as _template } from 'vue/vapor';
44+
const t0 = _template("<div></div>")
45+
46+
export function render(_ctx) {
47+
const n0 = t0()
48+
_setRef(n0, "foo")
49+
return n0
50+
}"
51+
`;

packages/compiler-vapor/__tests__/transforms/transformRef.spec.ts renamed to packages/compiler-vapor/__tests__/transforms/transformTemplateRef.spec.ts

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import {
55
type IfIRNode,
66
transformChildren,
77
transformElement,
8-
transformRef,
8+
transformTemplateRef,
99
transformVFor,
1010
transformVIf,
1111
} from '../../src'
@@ -15,7 +15,7 @@ const compileWithTransformRef = makeCompile({
1515
nodeTransforms: [
1616
transformVIf,
1717
transformVFor,
18-
transformRef,
18+
transformTemplateRef,
1919
transformElement,
2020
transformChildren,
2121
],
@@ -32,7 +32,7 @@ describe('compiler: template ref transform', () => {
3232
expect(ir.template).toEqual(['<div></div>'])
3333
expect(ir.block.operation).lengthOf(1)
3434
expect(ir.block.operation[0]).toMatchObject({
35-
type: IRNodeTypes.SET_REF,
35+
type: IRNodeTypes.SET_TEMPLATE_REF,
3636
element: 0,
3737
value: {
3838
content: 'foo',
@@ -58,7 +58,7 @@ describe('compiler: template ref transform', () => {
5858
expect(ir.template).toEqual(['<div></div>'])
5959
expect(ir.block.operation).lengthOf(1)
6060
expect(ir.block.operation[0]).toMatchObject({
61-
type: IRNodeTypes.SET_REF,
61+
type: IRNodeTypes.SET_TEMPLATE_REF,
6262
element: 0,
6363
value: {
6464
content: 'foo',
@@ -85,7 +85,7 @@ describe('compiler: template ref transform', () => {
8585

8686
expect(positive.operation).lengthOf(1)
8787
expect(positive.operation[0]).toMatchObject({
88-
type: IRNodeTypes.SET_REF,
88+
type: IRNodeTypes.SET_TEMPLATE_REF,
8989
element: 2,
9090
value: {
9191
content: 'foo',
@@ -109,7 +109,7 @@ describe('compiler: template ref transform', () => {
109109
const { render } = ir.block.operation[0] as ForIRNode
110110
expect(render.operation).lengthOf(1)
111111
expect(render.operation[0]).toMatchObject({
112-
type: IRNodeTypes.SET_REF,
112+
type: IRNodeTypes.SET_TEMPLATE_REF,
113113
element: 2,
114114
value: {
115115
content: 'foo',

packages/compiler-vapor/src/compile.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ import { transformVText } from './transforms/vText'
2121
import { transformVBind } from './transforms/vBind'
2222
import { transformVOn } from './transforms/vOn'
2323
import { transformVShow } from './transforms/vShow'
24-
import { transformRef } from './transforms/transformRef'
24+
import { transformTemplateRef } from './transforms/transformTemplateRef'
2525
import { transformText } from './transforms/transformText'
2626
import { transformVModel } from './transforms/vModel'
2727
import { transformVIf } from './transforms/vIf'
@@ -103,7 +103,7 @@ export function getBaseTransformPreset(
103103
transformOnce,
104104
transformVIf,
105105
transformVFor,
106-
transformRef,
106+
transformTemplateRef,
107107
transformText,
108108
transformElement,
109109
transformComment,

packages/compiler-vapor/src/generators/block.ts

+3-2
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,14 @@ import {
44
INDENT_END,
55
INDENT_START,
66
NEWLINE,
7+
SEGMENTS_ARRAY,
78
buildCodeFragment,
89
genCall,
10+
genMulti,
911
} from './utils'
1012
import type { CodegenContext } from '../generate'
1113
import { genEffects, genOperations } from './operation'
1214
import { genChildren } from './template'
13-
import { genMulti } from './utils'
1415

1516
export function genBlock(
1617
oper: BlockIRNode,
@@ -68,7 +69,7 @@ export function genBlockContent(
6869

6970
const returnsCode: CodeFragment[] =
7071
returns.length > 1
71-
? genMulti(['[', ']', ', '], ...returns.map(n => `n${n}`))
72+
? genMulti(SEGMENTS_ARRAY, ...returns.map(n => `n${n}`))
7273
: [`n${returns[0]}`]
7374
push(...(customReturns ? customReturns(returnsCode) : returnsCode))
7475

packages/compiler-vapor/src/generators/component.ts

+4-8
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,9 @@ import type { CodegenContext } from '../generate'
33
import type { CreateComponentIRNode, IRProp } from '../ir'
44
import {
55
type CodeFragment,
6-
INDENT_END,
7-
INDENT_START,
86
NEWLINE,
7+
SEGMENTS_ARRAY,
8+
SEGMENTS_OBJECT_NEWLINE,
99
genCall,
1010
genMulti,
1111
} from './utils'
@@ -64,17 +64,13 @@ export function genCreateComponent(
6464
})
6565
.filter(Boolean)
6666
if (props.length) {
67-
return genMulti(['[', ']', ', '], ...props)
67+
return genMulti(SEGMENTS_ARRAY, ...props)
6868
}
6969
}
7070

7171
function genStaticProps(props: IRProp[]) {
7272
return genMulti(
73-
[
74-
['{', INDENT_START, NEWLINE],
75-
[INDENT_END, NEWLINE, '}'],
76-
[', ', NEWLINE],
77-
],
73+
SEGMENTS_OBJECT_NEWLINE,
7874
...props.map(prop => {
7975
return [
8076
...genPropKey(prop, context),

packages/compiler-vapor/src/generators/directive.ts

+9-3
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,13 @@ import { createSimpleExpression, isSimpleIdentifier } from '@vue/compiler-dom'
22
import { camelize } from '@vue/shared'
33
import { genExpression } from './expression'
44
import type { CodegenContext } from '../generate'
5-
import { type CodeFragment, NEWLINE, genCall, genMulti } from './utils'
5+
import {
6+
type CodeFragment,
7+
NEWLINE,
8+
SEGMENTS_ARRAY,
9+
genCall,
10+
genMulti,
11+
} from './utils'
612
import {
713
IRNodeTypes,
814
type OperationNode,
@@ -22,7 +28,7 @@ export function genWithDirective(
2228

2329
const element = `n${opers[0].element}`
2430
const directiveItems = opers.map(genDirective)
25-
const directives = genMulti(['[', ']', ', '], ...directiveItems)
31+
const directives = genMulti(SEGMENTS_ARRAY, ...directiveItems)
2632

2733
return [
2834
NEWLINE,
@@ -47,7 +53,7 @@ export function genWithDirective(
4753
? ['{ ', genDirectiveModifiers(dir.modifiers), ' }']
4854
: false
4955

50-
return genMulti(['[', ']', ', '], directive, value, argument, modifiers)
56+
return genMulti(SEGMENTS_ARRAY, directive, value, argument, modifiers)
5157

5258
function genDirective() {
5359
const {

packages/compiler-vapor/src/generators/event.ts

+2-7
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,8 @@ import type { SetDynamicEventsIRNode, SetEventIRNode } from '../ir'
88
import { genExpression } from './expression'
99
import {
1010
type CodeFragment,
11-
INDENT_END,
12-
INDENT_START,
1311
NEWLINE,
12+
SEGMENTS_OBJECT_NEWLINE,
1413
genCall,
1514
genMulti,
1615
} from './utils'
@@ -60,11 +59,7 @@ export function genSetEvent(
6059
if (!options.length && !nonKeys.length && !keys.length && !effect) return
6160

6261
return genMulti(
63-
[
64-
['{', INDENT_START, NEWLINE],
65-
[INDENT_END, NEWLINE, '}'],
66-
[', ', NEWLINE],
67-
],
62+
SEGMENTS_OBJECT_NEWLINE,
6863
!!nonKeys.length && ['modifiers: ', genArrayExpression(nonKeys)],
6964
!!keys.length && ['keys: ', genArrayExpression(keys)],
7065
effect && ['effect: true'],

packages/compiler-vapor/src/generators/operation.ts

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import { genSetHtml } from './html'
77
import { genIf } from './if'
88
import { genSetModelValue } from './modelValue'
99
import { genDynamicProps, genSetProp } from './prop'
10-
import { genSetRef } from './ref'
10+
import { genSetTemplateRef } from './templateRef'
1111
import { genCreateTextNode, genSetText } from './text'
1212
import {
1313
type CodeFragment,
@@ -43,8 +43,8 @@ export function genOperation(
4343
return genSetDynamicEvents(oper, context)
4444
case IRNodeTypes.SET_HTML:
4545
return genSetHtml(oper, context)
46-
case IRNodeTypes.SET_REF:
47-
return genSetRef(oper, context)
46+
case IRNodeTypes.SET_TEMPLATE_REF:
47+
return genSetTemplateRef(oper, context)
4848
case IRNodeTypes.SET_MODEL_VALUE:
4949
return genSetModelValue(oper, context)
5050
case IRNodeTypes.CREATE_TEXT_NODE:

packages/compiler-vapor/src/generators/prop.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,14 @@ import type {
1111
VaporHelper,
1212
} from '../ir'
1313
import { genExpression } from './expression'
14-
import { type CodeFragment, NEWLINE, genCall, genMulti } from './utils'
14+
import {
15+
type CodeFragment,
16+
NEWLINE,
17+
SEGMENTS_ARRAY,
18+
SEGMENTS_OBJECT,
19+
genCall,
20+
genMulti,
21+
} from './utils'
1522
import { toHandlerKey } from '@vue/shared'
1623

1724
// only the static key prop will reach here
@@ -77,7 +84,7 @@ function genLiteralObjectProps(
7784
context: CodegenContext,
7885
): CodeFragment[] {
7986
return genMulti(
80-
['{ ', ' }', ', '],
87+
SEGMENTS_OBJECT,
8188
...props.map(prop => [
8289
...genPropKey(prop, context),
8390
`: `,
@@ -120,7 +127,7 @@ function genPropValue(values: SimpleExpressionNode[], context: CodegenContext) {
120127
return genExpression(values[0], context)
121128
}
122129
return genMulti(
123-
['[', ']', ', '],
130+
SEGMENTS_ARRAY,
124131
...values.map(expr => genExpression(expr, context)),
125132
)
126133
}

packages/compiler-vapor/src/generators/ref.ts renamed to packages/compiler-vapor/src/generators/templateRef.ts

+4-4
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
import { genExpression } from './expression'
22
import type { CodegenContext } from '../generate'
3-
import type { SetRefIRNode } from '../ir'
3+
import type { SetTemplateRefIRNode } from '../ir'
44
import { type CodeFragment, NEWLINE, genCall } from './utils'
55

6-
export function genSetRef(
7-
oper: SetRefIRNode,
6+
export function genSetTemplateRef(
7+
oper: SetTemplateRefIRNode,
88
context: CodegenContext,
99
): CodeFragment[] {
1010
const { vaporHelper } = context
1111
return [
1212
NEWLINE,
1313
...genCall(
1414
vaporHelper('setRef'),
15-
[`n${oper.element}`],
15+
`n${oper.element}`,
1616
genExpression(oper.value, context),
1717
oper.refFor && 'true',
1818
),

packages/compiler-vapor/src/generators/text.ts

+8-2
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,13 @@
11
import type { CodegenContext } from '../generate'
22
import type { CreateTextNodeIRNode, SetTextIRNode } from '../ir'
33
import { genExpression } from './expression'
4-
import { type CodeFragment, NEWLINE, genCall, genMulti } from './utils'
4+
import {
5+
type CodeFragment,
6+
NEWLINE,
7+
SEGMENTS_ARRAY,
8+
genCall,
9+
genMulti,
10+
} from './utils'
511

612
export function genSetText(
713
oper: SetTextIRNode,
@@ -31,7 +37,7 @@ export function genCreateTextNode(
3137
...genCall(vaporHelper('createTextNode'), [
3238
effect && '() => ',
3339
...genMulti(
34-
['[', ']', ', '],
40+
SEGMENTS_ARRAY,
3541
...values.map(value => genExpression(value, context)),
3642
),
3743
]),

packages/compiler-vapor/src/generators/utils.ts

+19-11
Original file line numberDiff line numberDiff line change
@@ -30,22 +30,23 @@ export function buildCodeFragment(...frag: CodeFragment[]) {
3030
return [frag, push] as const
3131
}
3232

33+
type Segments = [
34+
left: CodeFragments,
35+
right: CodeFragments,
36+
segment: CodeFragments,
37+
]
3338
export function genMulti(
34-
[left, right, seg]: [
35-
left: CodeFragments,
36-
right: CodeFragments,
37-
segment: CodeFragments,
38-
],
39-
...fns: CodeFragments[]
39+
[left, right, seg]: Segments,
40+
...frags: CodeFragments[]
4041
): CodeFragment[] {
4142
const frag: CodeFragment[] = []
42-
fns = fns.filter(Boolean)
43+
frags = frags.filter(Boolean)
4344
push(left)
4445
for (let [i, fn] of (
45-
fns as Array<Exclude<CodeFragments, FalsyValue>>
46+
frags as Array<Exclude<CodeFragments, FalsyValue>>
4647
).entries()) {
4748
push(fn)
48-
if (i < fns.length - 1) push(seg)
49+
if (i < frags.length - 1) push(seg)
4950
}
5051
push(right)
5152
return frag
@@ -55,12 +56,19 @@ export function genMulti(
5556
frag.push(...fn)
5657
}
5758
}
59+
export const SEGMENTS_ARRAY: Segments = ['[', ']', ', ']
60+
export const SEGMENTS_OBJECT: Segments = ['{ ', ' }', ', ']
61+
export const SEGMENTS_OBJECT_NEWLINE: Segments = [
62+
['{', INDENT_START, NEWLINE],
63+
[INDENT_END, NEWLINE, '}'],
64+
[', ', NEWLINE],
65+
]
5866

5967
export function genCall(
6068
name: string,
61-
...args: CodeFragments[]
69+
...frags: CodeFragments[]
6270
): CodeFragment[] {
63-
return [name, ...genMulti(['(', ')', ', '], ...args)]
71+
return [name, ...genMulti(['(', ')', ', '], ...frags)]
6472
}
6573

6674
export function genCodeFragment(context: CodegenContext) {

0 commit comments

Comments
 (0)