Skip to content

Commit 4dc8ffc

Browse files
committed
fix: simplify and use correct ctx in withCtx
1 parent fd3418d commit 4dc8ffc

File tree

8 files changed

+44
-45
lines changed

8 files changed

+44
-45
lines changed

packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap

+5-5
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ export const render = _withId(function render(_ctx, _cache) {
2828
return (_openBlock(), _createBlock(_component_Child, null, {
2929
default: _withId(() => [
3030
_createVNode(\\"div\\")
31-
], _ctx),
31+
]),
3232
_: 1
3333
}))
3434
})"
@@ -47,15 +47,15 @@ export const render = _withId(function render(_ctx, _cache) {
4747
name: \\"foo\\",
4848
fn: _withId(() => [
4949
_createVNode(\\"div\\")
50-
], _ctx)
50+
])
5151
}
5252
: undefined,
5353
_renderList(_ctx.list, (i) => {
5454
return {
5555
name: i,
5656
fn: _withId(() => [
5757
_createVNode(\\"div\\")
58-
], _ctx)
58+
])
5959
}
6060
})
6161
]), 1024 /* DYNAMIC_SLOTS */))
@@ -72,10 +72,10 @@ export const render = _withId(function render(_ctx, _cache) {
7272
return (_openBlock(), _createBlock(_component_Child, null, {
7373
foo: _withId(({ msg }) => [
7474
_createTextVNode(_toDisplayString(msg), 1 /* TEXT */)
75-
], _ctx),
75+
]),
7676
bar: _withId(() => [
7777
_createVNode(\\"div\\")
78-
], _ctx),
78+
]),
7979
_: 1
8080
}))
8181
})"

packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap

+1-1
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,7 @@ return function render(_ctx, _cache) {
246246
return (_openBlock(), _createBlock(_component_Comp, null, {
247247
default: _withCtx(({ foo }) => [
248248
_createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */)
249-
], _ctx),
249+
]),
250250
_: 1
251251
}))
252252
}

packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap

+16-16
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,8 @@ return function render(_ctx, _cache) {
77
const _component_Comp = _resolveComponent(\\"Comp\\")
88
99
return (_openBlock(), _createBlock(_component_Comp, null, {
10-
[_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
11-
[_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
10+
[_ctx.one]: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
11+
[_ctx.two]: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
1212
_: 1
1313
}, 1024 /* DYNAMIC_SLOTS */))
1414
}"
@@ -23,7 +23,7 @@ return function render(_ctx, _cache) {
2323
return (_openBlock(), _createBlock(_component_Comp, null, {
2424
default: _withCtx(() => [
2525
_createVNode(\\"div\\")
26-
], _ctx),
26+
]),
2727
_: 1
2828
}))
2929
}"
@@ -39,7 +39,7 @@ return function render(_ctx, _cache) {
3939
_renderList(_ctx.list, (name) => {
4040
return {
4141
name: name,
42-
fn: _withCtx(() => [_toDisplayString(name)], _ctx)
42+
fn: _withCtx(() => [_toDisplayString(name)])
4343
}
4444
})
4545
]), 1024 /* DYNAMIC_SLOTS */))
@@ -56,7 +56,7 @@ return function render(_ctx, _cache) {
5656
(_ctx.ok)
5757
? {
5858
name: \\"one\\",
59-
fn: _withCtx((props) => [_toDisplayString(props)], _ctx)
59+
fn: _withCtx((props) => [_toDisplayString(props)])
6060
}
6161
: undefined
6262
]), 1024 /* DYNAMIC_SLOTS */))
@@ -76,16 +76,16 @@ return function render(_ctx, _cache) {
7676
ok
7777
? {
7878
name: \\"one\\",
79-
fn: _withCtx(() => [\\"foo\\"], _ctx)
79+
fn: _withCtx(() => [\\"foo\\"])
8080
}
8181
: orNot
8282
? {
8383
name: \\"two\\",
84-
fn: _withCtx((props) => [\\"bar\\"], _ctx)
84+
fn: _withCtx((props) => [\\"bar\\"])
8585
}
8686
: {
8787
name: \\"one\\",
88-
fn: _withCtx(() => [\\"baz\\"], _ctx)
88+
fn: _withCtx(() => [\\"baz\\"])
8989
}
9090
]), 1024 /* DYNAMIC_SLOTS */))
9191
}
@@ -105,7 +105,7 @@ return function render(_ctx, _cache) {
105105
ok
106106
? {
107107
name: \\"one\\",
108-
fn: _withCtx(() => [\\"hello\\"], _ctx)
108+
fn: _withCtx(() => [\\"hello\\"])
109109
}
110110
: undefined
111111
]), 1024 /* DYNAMIC_SLOTS */))
@@ -120,8 +120,8 @@ return function render(_ctx, _cache) {
120120
const _component_Comp = _resolveComponent(\\"Comp\\")
121121
122122
return (_openBlock(), _createBlock(_component_Comp, null, {
123-
one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
124-
two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)], _ctx),
123+
one: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
124+
two: _withCtx(({ bar }) => [_toDisplayString(_ctx.foo), _toDisplayString(bar)]),
125125
_: 1
126126
}))
127127
}"
@@ -137,11 +137,11 @@ return function render(_ctx, _cache) {
137137
const _component_Comp = _resolveComponent(\\"Comp\\")
138138
139139
return (_openBlock(), _createBlock(_component_Comp, null, {
140-
one: _withCtx(() => [\\"foo\\"], _ctx),
140+
one: _withCtx(() => [\\"foo\\"]),
141141
default: _withCtx(() => [
142142
\\"bar\\",
143143
_createVNode(\\"span\\")
144-
], _ctx),
144+
]),
145145
_: 1
146146
}))
147147
}
@@ -158,14 +158,14 @@ return function render(_ctx, _cache) {
158158
return (_openBlock(), _createBlock(_component_Comp, null, {
159159
default: _withCtx(({ foo }) => [
160160
_createVNode(_component_Inner, null, {
161-
default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)], _ctx),
161+
default: _withCtx(({ bar }) => [_toDisplayString(foo), _toDisplayString(bar), _toDisplayString(_ctx.baz)]),
162162
_: 1
163163
}, 1024 /* DYNAMIC_SLOTS */),
164164
\\" \\",
165165
_toDisplayString(foo),
166166
_toDisplayString(_ctx.bar),
167167
_toDisplayString(_ctx.baz)
168-
], _ctx),
168+
]),
169169
_: 1
170170
}))
171171
}"
@@ -178,7 +178,7 @@ return function render(_ctx, _cache) {
178178
const _component_Comp = _resolveComponent(\\"Comp\\")
179179
180180
return (_openBlock(), _createBlock(_component_Comp, null, {
181-
default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)], _ctx),
181+
default: _withCtx(({ foo }) => [_toDisplayString(foo), _toDisplayString(_ctx.bar)]),
182182
_: 1
183183
}))
184184
}"

packages/compiler-core/src/codegen.ts

+4-3
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,8 @@ import {
4949
WITH_DIRECTIVES,
5050
CREATE_BLOCK,
5151
OPEN_BLOCK,
52-
CREATE_STATIC
52+
CREATE_STATIC,
53+
WITH_CTX
5354
} from './runtimeHelpers'
5455
import { ImportItem } from './transform'
5556

@@ -769,7 +770,7 @@ function genFunctionExpression(
769770
if (genScopeId) {
770771
push(`_withId(`)
771772
} else if (isSlot) {
772-
push(`_withCtx(`)
773+
push(`_${helperNameMap[WITH_CTX]}(`)
773774
}
774775
push(`(`, node)
775776
if (isArray(params)) {
@@ -799,7 +800,7 @@ function genFunctionExpression(
799800
push(`}`)
800801
}
801802
if (genScopeId || isSlot) {
802-
push(`, _ctx)`)
803+
push(`)`)
803804
}
804805
}
805806

packages/compiler-ssr/__tests__/ssrComponent.spec.ts

+8-8
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,7 @@ describe('ssr: components', () => {
6060
_createVNode(\\"div\\")
6161
]
6262
}
63-
}, _ctx),
63+
}),
6464
_: 1
6565
}, _parent))
6666
}"
@@ -85,7 +85,7 @@ describe('ssr: components', () => {
8585
_createTextVNode(_toDisplayString(msg + _ctx.outer), 1 /* TEXT */)
8686
]
8787
}
88-
}, _ctx),
88+
}),
8989
_: 1
9090
}, _parent))
9191
}"
@@ -114,7 +114,7 @@ describe('ssr: components', () => {
114114
_createTextVNode(\\"foo\\")
115115
]
116116
}
117-
}, _ctx),
117+
}),
118118
named: _withCtx((_, _push, _parent, _scopeId) => {
119119
if (_push) {
120120
_push(\`bar\`)
@@ -123,7 +123,7 @@ describe('ssr: components', () => {
123123
_createTextVNode(\\"bar\\")
124124
]
125125
}
126-
}, _ctx),
126+
}),
127127
_: 1
128128
}, _parent))
129129
}"
@@ -154,7 +154,7 @@ describe('ssr: components', () => {
154154
_createTextVNode(\\"foo\\")
155155
]
156156
}
157-
}, _ctx)
157+
})
158158
}
159159
: undefined
160160
]), _parent))
@@ -186,7 +186,7 @@ describe('ssr: components', () => {
186186
_createTextVNode(_toDisplayString(msg + _ctx.key + _ctx.bar), 1 /* TEXT */)
187187
]
188188
}
189-
}, _ctx)
189+
})
190190
}
191191
})
192192
]), _parent))
@@ -238,7 +238,7 @@ describe('ssr: components', () => {
238238
: _createCommentVNode(\\"v-if\\", true)
239239
]
240240
}
241-
}, _ctx),
241+
}),
242242
bar: _withCtx(({ ok }, _push, _parent, _scopeId) => {
243243
if (_push) {
244244
if (ok) {
@@ -261,7 +261,7 @@ describe('ssr: components', () => {
261261
: _createCommentVNode(\\"v-if\\", true)
262262
]
263263
}
264-
}, _ctx),
264+
}),
265265
_: 1
266266
}, _parent))
267267
}"

packages/compiler-ssr/__tests__/ssrScopeId.spec.ts

+5-5
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ describe('ssr: scopeId', () => {
3838
_createTextVNode(\\"foo\\")
3939
]
4040
}
41-
}, _ctx),
41+
}),
4242
_: 1
4343
}, _parent))
4444
}"
@@ -66,7 +66,7 @@ describe('ssr: scopeId', () => {
6666
_createVNode(\\"span\\", null, \\"hello\\")
6767
]
6868
}
69-
}, _ctx),
69+
}),
7070
_: 1
7171
}, _parent))
7272
}"
@@ -99,7 +99,7 @@ describe('ssr: scopeId', () => {
9999
_createVNode(\\"span\\")
100100
]
101101
}
102-
}, _ctx),
102+
}),
103103
_: 1
104104
}, _parent))
105105
} else {
@@ -108,12 +108,12 @@ describe('ssr: scopeId', () => {
108108
_createVNode(_component_bar, null, {
109109
default: _withCtx(() => [
110110
_createVNode(\\"span\\")
111-
], _ctx),
111+
]),
112112
_: 1
113113
})
114114
]
115115
}
116-
}, _ctx),
116+
}),
117117
_: 1
118118
}, _parent))
119119
}"

packages/runtime-core/src/helpers/scopeId.ts

+3-5
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22
// These are only used in esm-bundler builds, but since exports cannot be
33
// conditional, we can only drop inner implementations in non-bundler builds.
44

5-
import { ComponentInternalInstance } from '../component'
65
import { withCtx } from './withRenderContext'
76

87
export let currentScopeId: string | null = null
@@ -23,14 +22,13 @@ export function popScopeId() {
2322

2423
export function withScopeId(id: string): <T extends Function>(fn: T) => T {
2524
if (__BUNDLER__) {
26-
return ((fn: Function, ctx?: ComponentInternalInstance) => {
27-
return withCtx(function(this: any) {
25+
return ((fn: Function) =>
26+
withCtx(function(this: any) {
2827
pushScopeId(id)
2928
const res = fn.apply(this, arguments)
3029
popScopeId()
3130
return res
32-
}, ctx)
33-
}) as any
31+
})) as any
3432
} else {
3533
return undefined as any
3634
}

packages/runtime-core/src/helpers/withRenderContext.ts

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
11
import { Slot } from '../componentSlots'
2-
import { ComponentInternalInstance } from '../component'
32
import {
43
setCurrentRenderingInstance,
54
currentRenderingInstance
65
} from '../componentRenderUtils'
6+
import { ComponentInternalInstance } from '../component'
77

88
export function withCtx(
99
fn: Slot,
10-
ctx: ComponentInternalInstance | null | undefined
10+
ctx: ComponentInternalInstance | null = currentRenderingInstance
1111
) {
1212
if (!ctx) return fn
1313
return function renderFnWithContext() {

0 commit comments

Comments
 (0)