Skip to content

Commit 6b03b47

Browse files
committed
fix(compiler-vapor): call withDirectives after created
1 parent b7b652e commit 6b03b47

File tree

8 files changed

+76
-34
lines changed

8 files changed

+76
-34
lines changed

packages/compiler-vapor/__tests__/__snapshots__/compile.spec.ts.snap

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,27 @@ export function render(_ctx) {
2222
}"
2323
`;
2424

25+
exports[`compile > custom directive > component 1`] = `
26+
"import { resolveComponent as _resolveComponent, createComponent as _createComponent, resolveDirective as _resolveDirective, withDirectives as _withDirectives, insert as _insert, createIf as _createIf, template as _template } from 'vue/vapor';
27+
const t0 = _template("<div></div>")
28+
29+
export function render(_ctx) {
30+
const _component_Bar = _resolveComponent("Bar")
31+
const _component_Comp = _resolveComponent("Comp")
32+
const n0 = _createIf(() => (true), () => {
33+
const n3 = t0()
34+
const n2 = _createComponent(_component_Bar)
35+
_withDirectives(n2, [[_resolveDirective("vHello"), void 0, void 0, { world: true }]])
36+
_insert(n2, n3)
37+
return n3
38+
})
39+
_insert(n0, n4)
40+
const n4 = _createComponent(_component_Comp, null, true)
41+
_withDirectives(n4, [[_resolveDirective("vTest")]])
42+
return n4
43+
}"
44+
`;
45+
2546
exports[`compile > directives > custom directive > basic 1`] = `
2647
"import { withDirectives as _withDirectives, template as _template } from 'vue/vapor';
2748
const t0 = _template("<div></div>")

packages/compiler-vapor/__tests__/compile.spec.ts

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -201,5 +201,16 @@ describe('compile', () => {
201201
const code = compile(`<div v-test v-hello.world />`)
202202
expect(code).matchSnapshot()
203203
})
204+
205+
test('component', () => {
206+
const code = compile(`
207+
<Comp v-test>
208+
<div v-if="true">
209+
<Bar v-hello.world />
210+
</div>
211+
</Comp>
212+
`)
213+
expect(code).matchSnapshot()
214+
})
204215
})
205216
})

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

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -177,10 +177,10 @@ const t0 = _template("<input>")
177177
178178
export function render(_ctx) {
179179
const n0 = t0()
180-
const n1 = t0()
181-
const n2 = t0()
182180
_withDirectives(n0, [[_vModelText, () => _ctx.setupRef.child]])
181+
const n1 = t0()
183182
_withDirectives(n1, [[_vModelText, () => _ctx.setupLet.child]])
183+
const n2 = t0()
184184
_withDirectives(n2, [[_vModelText, () => _ctx.setupMaybeRef.child]])
185185
_delegate(n0, "update:modelValue", () => $event => (_ctx.setupRef.child = $event))
186186
_delegate(n1, "update:modelValue", () => $event => (_ctx.setupLet.child = $event))
@@ -192,10 +192,10 @@ export function render(_ctx) {
192192
exports[`compiler: vModel transform > should support member expression w/ inline 1`] = `
193193
"(() => {
194194
const n0 = t0()
195-
const n1 = t0()
196-
const n2 = t0()
197195
_withDirectives(n0, [[_vModelText, () => setupRef.value.child]])
196+
const n1 = t0()
198197
_withDirectives(n1, [[_vModelText, () => _unref(setupLet).child]])
198+
const n2 = t0()
199199
_withDirectives(n2, [[_vModelText, () => _unref(setupMaybeRef).child]])
200200
_delegate(n0, "update:modelValue", () => $event => (setupRef.value.child = $event))
201201
_delegate(n1, "update:modelValue", () => $event => (_unref(setupLet).child = $event))

packages/compiler-vapor/src/generate.ts

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ import type {
33
BaseCodegenResult,
44
CodegenSourceMapGenerator,
55
} from '@vue/compiler-dom'
6-
import type { IREffect, RootIRNode, VaporHelper } from './ir'
6+
import type { BlockIRNode, IREffect, RootIRNode, VaporHelper } from './ir'
77
import { SourceMapGenerator } from 'source-map-js'
88
import { extend, remove } from '@vue/shared'
99
import { genBlockContent } from './generators/block'
@@ -43,11 +43,12 @@ export class CodegenContext {
4343

4444
identifiers: Record<string, string[]> = Object.create(null)
4545

46+
block: BlockIRNode
4647
genEffects: Array<
4748
(effects: IREffect[], context: CodegenContext) => CodeFragment[]
4849
> = []
4950

50-
withId = <T>(fn: () => T, map: Record<string, string | null>): T => {
51+
withId<T>(fn: () => T, map: Record<string, string | null>): T {
5152
const { identifiers } = this
5253
const ids = Object.keys(map)
5354

@@ -62,6 +63,12 @@ export class CodegenContext {
6263
return ret
6364
}
6465

66+
enterBlock(block: BlockIRNode) {
67+
const parent = this.block
68+
this.block = block
69+
return () => (this.block = parent)
70+
}
71+
6572
constructor(
6673
public ir: RootIRNode,
6774
options: CodegenOptions,
@@ -84,6 +91,7 @@ export class CodegenContext {
8491
expressionPlugins: [],
8592
}
8693
this.options = extend(defaultOptions, options)
94+
this.block = ir.block
8795

8896
const [code, push] = buildCodeFragment()
8997
this.code = code

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

Lines changed: 5 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,4 @@
1-
import {
2-
type BlockIRNode,
3-
IRNodeTypes,
4-
type OperationNode,
5-
type WithDirectiveIRNode,
6-
} from '../ir'
1+
import type { BlockIRNode } from '../ir'
72
import {
83
type CodeFragment,
94
INDENT_END,
@@ -13,7 +8,6 @@ import {
138
genCall,
149
} from './utils'
1510
import type { CodegenContext } from '../generate'
16-
import { genWithDirective } from './directive'
1711
import { genEffects, genOperations } from './operation'
1812
import { genChildren } from './template'
1913
import { genMulti } from './utils'
@@ -38,12 +32,14 @@ export function genBlock(
3832
}
3933

4034
export function genBlockContent(
41-
{ dynamic, effect, operation, returns }: BlockIRNode,
35+
block: BlockIRNode,
4236
context: CodegenContext,
4337
root?: boolean,
4438
customReturns?: (returns: CodeFragment[]) => CodeFragment[],
4539
): CodeFragment[] {
4640
const [frag, push] = buildCodeFragment()
41+
const { dynamic, effect, operation, returns } = block
42+
const resetBlock = context.enterBlock(block)
4743

4844
if (root)
4945
for (const name of context.ir.component) {
@@ -61,10 +57,6 @@ export function genBlockContent(
6157
push(...genChildren(child, context, child.id!))
6258
}
6359

64-
for (const directives of groupDirective(operation)) {
65-
push(...genWithDirective(directives, context))
66-
}
67-
6860
push(...genOperations(operation, context))
6961
push(
7062
...(context.genEffects.length
@@ -80,19 +72,6 @@ export function genBlockContent(
8072
: [`n${returns[0]}`]
8173
push(...(customReturns ? customReturns(returnsCode) : returnsCode))
8274

75+
resetBlock()
8376
return frag
8477
}
85-
86-
function groupDirective(operation: OperationNode[]): WithDirectiveIRNode[][] {
87-
const directiveOps = operation.filter(
88-
(oper): oper is WithDirectiveIRNode =>
89-
oper.type === IRNodeTypes.WITH_DIRECTIVE,
90-
)
91-
92-
const directiveMap: Record<number, WithDirectiveIRNode[]> = {}
93-
for (const oper of directiveOps) {
94-
if (!directiveMap[oper.element]) directiveMap[oper.element] = []
95-
directiveMap[oper.element].push(oper)
96-
}
97-
return Object.values(directiveMap)
98-
}

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

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ import { genExpression } from './expression'
1313
import { genPropKey } from './prop'
1414
import { createSimpleExpression } from '@vue/compiler-dom'
1515
import { genEventHandler } from './event'
16-
import { genDirectiveModifiers } from './directive'
16+
import { genDirectiveModifiers, genDirectivesForElement } from './directive'
1717
import { genModelHandler } from './modelValue'
1818

1919
// TODO: generate component slots
@@ -36,6 +36,7 @@ export function genCreateComponent(
3636
rawProps || (isRoot ? 'null' : false),
3737
isRoot && 'true',
3838
),
39+
...genDirectivesForElement(oper.id, context),
3940
]
4041

4142
function genTag() {

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

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,16 @@ import { camelize } from '@vue/shared'
33
import { genExpression } from './expression'
44
import type { CodegenContext } from '../generate'
55
import { type CodeFragment, NEWLINE, genCall, genMulti } from './utils'
6-
import type { WithDirectiveIRNode } from '../ir'
6+
import {
7+
IRNodeTypes,
8+
type OperationNode,
9+
type WithDirectiveIRNode,
10+
} from '../ir'
11+
12+
export function genDirectivesForElement(id: number, context: CodegenContext) {
13+
const dirs = filterDirectives(id, context.block.operation)
14+
return dirs.length ? genWithDirective(dirs, context) : []
15+
}
716

817
export function genWithDirective(
918
opers: WithDirectiveIRNode[],
@@ -72,3 +81,13 @@ export function genDirectiveModifiers(modifiers: string[]) {
7281
)
7382
.join(', ')
7483
}
84+
85+
function filterDirectives(
86+
id: number,
87+
operations: OperationNode[],
88+
): WithDirectiveIRNode[] {
89+
return operations.filter(
90+
(oper): oper is WithDirectiveIRNode =>
91+
oper.type === IRNodeTypes.WITH_DIRECTIVE && oper.element === id,
92+
)
93+
}

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

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import type { CodegenContext } from '../generate'
22
import { DynamicFlag, type IRDynamicInfo } from '../ir'
3+
import { genDirectivesForElement } from './directive'
34
import { type CodeFragment, NEWLINE, buildCodeFragment, genCall } from './utils'
45

56
export function genTemplates(
@@ -27,6 +28,7 @@ export function genChildren(
2728

2829
if (id !== undefined && template !== undefined) {
2930
push(NEWLINE, `const n${id} = t${template}()`)
31+
push(...genDirectivesForElement(id, context))
3032
}
3133

3234
let prev: [id: number, elementIndex: number] | undefined
@@ -71,6 +73,7 @@ export function genChildren(
7173
)
7274
}
7375
}
76+
push(...genDirectivesForElement(id, context))
7477
prev = [id, elementIndex]
7578
push(...genChildren(child, context, id, []))
7679
}

0 commit comments

Comments
 (0)