Skip to content

Commit be65b98

Browse files
committed
refactor: remove update function from compiler
1 parent 5630a58 commit be65b98

File tree

6 files changed

+81
-60
lines changed

6 files changed

+81
-60
lines changed

packages/compiler-vapor/__tests__/transforms/__snapshots__/vFor.spec.ts.snap

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
// Vitest Snapshot v1, https://vitest.dev/guide/snapshot.html
22

33
exports[`compiler: v-for > basic v-for 1`] = `
4-
"import { template as _template, fragment as _fragment, children as _children, on as _on, setText as _setText, renderEffect as _renderEffect, createFor as _createFor, append as _append } from 'vue/vapor';
4+
"import { template as _template, fragment as _fragment, children as _children, on as _on, renderEffect as _renderEffect, setText as _setText, createFor as _createFor, append as _append } from 'vue/vapor';
55
66
export function render(_ctx) {
77
const t0 = _template("<div></div>")
@@ -11,19 +11,43 @@ export function render(_ctx) {
1111
const n2 = t0()
1212
const { 0: [n3],} = _children(n2)
1313
_on(n3, "click", $event => (_ctx.remove(_block.s[0])))
14-
const _updateEffect = () => {
14+
_renderEffect(() => {
1515
const [item] = _block.s
1616
_setText(n3, item)
17-
}
18-
_renderEffect(_updateEffect)
19-
return [n2, _updateEffect]
17+
})
18+
return n2
2019
})
2120
_append(n0, n1)
2221
return n0
2322
}"
2423
`;
2524

26-
exports[`compiler: v-for > basic v-for 2`] = `
25+
exports[`compiler: v-for > multi effect 1`] = `
26+
"import { template as _template, fragment as _fragment, children as _children, renderEffect as _renderEffect, setDynamicProp as _setDynamicProp, createFor as _createFor, append as _append } from 'vue/vapor';
27+
28+
export function render(_ctx) {
29+
const t0 = _template("<div></div>")
30+
const t1 = _fragment()
31+
const n0 = t1()
32+
const n1 = _createFor(() => (_ctx.items), (_block) => {
33+
const n2 = t0()
34+
const { 0: [n3],} = _children(n2)
35+
_renderEffect(() => {
36+
const [item, index] = _block.s
37+
_setDynamicProp(n3, "item", item)
38+
})
39+
_renderEffect(() => {
40+
const [item, index] = _block.s
41+
_setDynamicProp(n3, "index", index)
42+
})
43+
return n2
44+
})
45+
_append(n0, n1)
46+
return n0
47+
}"
48+
`;
49+
50+
exports[`compiler: v-for > w/o value 1`] = `
2751
"import { template as _template, fragment as _fragment, createFor as _createFor, append as _append } from 'vue/vapor';
2852
2953
export function render(_ctx) {
@@ -32,7 +56,7 @@ export function render(_ctx) {
3256
const n0 = t1()
3357
const n1 = _createFor(() => (_ctx.items), (_block) => {
3458
const n2 = t0()
35-
return [n2, () => {}]
59+
return n2
3660
})
3761
_append(n0, n1)
3862
return n0

packages/compiler-vapor/__tests__/transforms/vFor.spec.ts

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,18 @@ import {
44
IRNodeTypes,
55
transformElement,
66
transformInterpolation,
7+
transformVBind,
78
transformVFor,
89
transformVOn,
910
} from '../../src'
1011
import { NodeTypes } from '@vue/compiler-dom'
1112

1213
const compileWithVFor = makeCompile({
1314
nodeTransforms: [transformInterpolation, transformVFor, transformElement],
14-
directiveTransforms: { on: transformVOn },
15+
directiveTransforms: {
16+
bind: transformVBind,
17+
on: transformVOn,
18+
},
1519
})
1620

1721
describe('compiler: v-for', () => {
@@ -66,8 +70,22 @@ describe('compiler: v-for', () => {
6670
expect((ir.operation[0] as ForIRNode).render.effect).lengthOf(1)
6771
})
6872

69-
test('basic v-for', () => {
73+
test('multi effect', () => {
74+
const { code } = compileWithVFor(
75+
`<div v-for="(item, index) of items" :item="item" :index="index" />`,
76+
)
77+
expect(code).matchSnapshot()
78+
})
79+
80+
test('w/o value', () => {
7081
const { code } = compileWithVFor(`<div v-for=" of items">item</div>`)
7182
expect(code).matchSnapshot()
7283
})
84+
85+
test.todo('object de-structured value', () => {
86+
const { code } = compileWithVFor(
87+
'<span v-for="({ id, value }) in items">{{ id }}{{ value }}</span>',
88+
)
89+
expect(code).matchSnapshot()
90+
})
7391
})

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

Lines changed: 3 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -21,14 +21,13 @@ export function genBlockFunction(
2121
oper: BlockFunctionIRNode,
2222
context: CodegenContext,
2323
args: CodeFragment[] = [],
24-
returnValue?: () => CodeFragment[],
2524
): CodeFragment[] {
2625
return [
2726
'(',
2827
...args,
2928
') => {',
3029
INDENT_START,
31-
...genBlockFunctionContent(oper, context, returnValue),
30+
...genBlockFunctionContent(oper, context),
3231
INDENT_END,
3332
NEWLINE,
3433
'}',
@@ -38,7 +37,6 @@ export function genBlockFunction(
3837
export function genBlockFunctionContent(
3938
ir: BlockFunctionIRNode | RootIRNode,
4039
context: CodegenContext,
41-
returnValue?: () => CodeFragment[],
4240
): CodeFragment[] {
4341
const { vaporHelper } = context
4442
const [frag, push] = buildCodeFragment(
@@ -63,13 +61,8 @@ export function genBlockFunctionContent(
6361
}
6462

6563
push(...genOperations(ir.operation, context))
66-
push(...genEffects(ir.effect, context))
67-
68-
push(
69-
NEWLINE,
70-
'return ',
71-
...(returnValue ? returnValue() : [`n${ir.dynamic.id}`]),
72-
)
64+
push(...(context.genEffect || genEffects)(ir.effect, context))
65+
push(NEWLINE, `return n${ir.dynamic.id}`)
7366

7467
return frag
7568
}

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

Lines changed: 22 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ import {
99
buildCodeFragment,
1010
} from '../generate'
1111
import type { ForIRNode, IREffect } from '../ir'
12-
import { genOperations } from './operation'
12+
import { genOperation } from './operation'
1313
import { NewlineType } from '@vue/compiler-dom'
1414

1515
export function genFor(
@@ -23,19 +23,14 @@ export function genFor(
2323
const rawKey = key && key.content
2424

2525
const sourceExpr = ['() => (', ...genExpression(source, context), ')']
26-
let updateFn = '_updateEffect'
2726
context.genEffect = genEffectInFor
2827

2928
const idMap: Record<string, string> = {}
3029
if (rawValue) idMap[rawValue] = `_block.s[0]`
3130
if (rawKey) idMap[rawKey] = `_block.s[1]`
3231

33-
const blockRet = (): CodeFragment[] => [
34-
`[n${render.dynamic.id!}, ${updateFn}]`,
35-
]
36-
3732
const blockFn = context.withId(
38-
() => genBlockFunction(render, context, ['_block'], blockRet),
33+
() => genBlockFunction(render, context, ['_block']),
3934
idMap,
4035
)
4136

@@ -48,44 +43,39 @@ export function genFor(
4843
]
4944

5045
function genEffectInFor(effects: IREffect[]): CodeFragment[] {
51-
if (!effects.length) {
52-
updateFn = '() => {}'
53-
return []
54-
}
46+
const [frag, push] = buildCodeFragment()
5547

56-
const [frag, push] = buildCodeFragment(INDENT_START)
57-
// const [value, key] = _block.s
48+
const idMap: Record<string, string | null> = {}
49+
if (value) idMap[value.content] = null
50+
if (key) idMap[key.content] = null
51+
52+
let statement: CodeFragment[] = []
5853
if (rawValue || rawKey) {
59-
push(
54+
// const [value, key] = _block.s
55+
statement = [
6056
NEWLINE,
6157
'const ',
6258
'[',
6359
rawValue && [rawValue, NewlineType.None, value.loc],
6460
rawKey && ', ',
6561
rawKey && [rawKey, NewlineType.None, key.loc],
6662
'] = _block.s',
67-
)
63+
]
6864
}
6965

70-
const idMap: Record<string, string | null> = {}
71-
if (value) idMap[value.content] = null
72-
if (key) idMap[key.content] = null
7366
context.withId(() => {
74-
effects.forEach(effect =>
75-
push(...genOperations(effect.operations, context)),
76-
)
67+
for (const { operations } of effects) {
68+
push(
69+
NEWLINE,
70+
`${vaporHelper('renderEffect')}(() => {`,
71+
INDENT_START,
72+
...statement,
73+
)
74+
operations.forEach(op => push(...genOperation(op, context)))
75+
push(INDENT_END, NEWLINE, '})')
76+
}
7777
}, idMap)
7878

79-
push(INDENT_END)
80-
81-
return [
82-
NEWLINE,
83-
`const ${updateFn} = () => {`,
84-
...frag,
85-
NEWLINE,
86-
'}',
87-
NEWLINE,
88-
`${vaporHelper('renderEffect')}(${updateFn})`,
89-
]
79+
return frag
9080
}
9181
}

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

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ export function genOperations(opers: OperationNode[], context: CodegenContext) {
2525
return frag
2626
}
2727

28-
function genOperation(
28+
export function genOperation(
2929
oper: OperationNode,
3030
context: CodegenContext,
3131
): CodeFragment[] {
@@ -60,17 +60,14 @@ function genOperation(
6060
}
6161

6262
export function genEffects(effects: IREffect[], context: CodegenContext) {
63-
if (context.genEffect) {
64-
return context.genEffect(effects)
65-
}
6663
const [frag, push] = buildCodeFragment()
6764
for (const effect of effects) {
6865
push(...genEffect(effect, context))
6966
}
7067
return frag
7168
}
7269

73-
function genEffect({ operations }: IREffect, context: CodegenContext) {
70+
export function genEffect({ operations }: IREffect, context: CodegenContext) {
7471
const { vaporHelper } = context
7572
const [frag, push] = buildCodeFragment(
7673
NEWLINE,

packages/runtime-vapor/src/for.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@ interface ForBlock extends Fragment {
1515

1616
export const createFor = (
1717
src: () => any[] | Record<string, string> | Set<any> | Map<any, any>,
18-
renderItem: (block: ForBlock) => [Block, () => void],
18+
renderItem: (block: ForBlock) => Block,
1919
getKey: ((item: any, index: number) => any) | null,
2020
getMemo?: (item: any) => any[],
2121
hydrationNode?: Node,
@@ -47,9 +47,8 @@ export const createFor = (
4747
memo: getMemo && getMemo(item),
4848
[fragmentKey]: true,
4949
})
50-
const res = scope.run(() => renderItem(block))!
51-
block.nodes = res[0]
52-
block.update = res[1]
50+
block.nodes = scope.run(() => renderItem(block))!
51+
block.update = () => scope.effects.forEach(effect => effect.run())
5352
if (getMemo) block.update()
5453
if (parent) insert(block.nodes, parent, anchor)
5554
return block

0 commit comments

Comments
 (0)