Skip to content

Commit 47e984d

Browse files
committed
refactor(compiler): provide _ctx and _cache via arguments
1 parent ade07c6 commit 47e984d

21 files changed

+129
-188
lines changed

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

+19-34
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22

33
exports[`compiler: codegen ArrayExpression 1`] = `
44
"
5-
return function render() {
5+
return function render(_ctx, _cache) {
66
with (this) {
77
return [
88
foo,
@@ -14,18 +14,14 @@ return function render() {
1414

1515
exports[`compiler: codegen CacheExpression 1`] = `
1616
"
17-
export function render() {
18-
const _ctx = this
19-
const _cache = _ctx.$cache
17+
export function render(_ctx, _cache) {
2018
return _cache[1] || (_cache[1] = foo)
2119
}"
2220
`;
2321

2422
exports[`compiler: codegen CacheExpression w/ isVNode: true 1`] = `
2523
"
26-
export function render() {
27-
const _ctx = this
28-
const _cache = _ctx.$cache
24+
export function render(_ctx, _cache) {
2925
return _cache[1] || (
3026
_setBlockTracking(-1),
3127
_cache[1] = foo,
@@ -37,7 +33,7 @@ export function render() {
3733

3834
exports[`compiler: codegen ConditionalExpression 1`] = `
3935
"
40-
return function render() {
36+
return function render(_ctx, _cache) {
4137
with (this) {
4238
return ok
4339
? foo()
@@ -50,7 +46,7 @@ return function render() {
5046

5147
exports[`compiler: codegen Element (callExpression + objectExpression + TemplateChildNode[]) 1`] = `
5248
"
53-
return function render() {
49+
return function render(_ctx, _cache) {
5450
with (this) {
5551
return _createVNode(\\"div\\", {
5652
id: \\"foo\\",
@@ -65,7 +61,7 @@ return function render() {
6561

6662
exports[`compiler: codegen SequenceExpression 1`] = `
6763
"
68-
return function render() {
64+
return function render(_ctx, _cache) {
6965
with (this) {
7066
return (foo, bar(baz))
7167
}
@@ -74,7 +70,7 @@ return function render() {
7470

7571
exports[`compiler: codegen assets + temps 1`] = `
7672
"
77-
return function render() {
73+
return function render(_ctx, _cache) {
7874
with (this) {
7975
const _component_Foo = _resolveComponent(\\"Foo\\")
8076
const _component_bar_baz = _resolveComponent(\\"bar-baz\\")
@@ -90,7 +86,7 @@ return function render() {
9086

9187
exports[`compiler: codegen comment 1`] = `
9288
"
93-
return function render() {
89+
return function render(_ctx, _cache) {
9490
with (this) {
9591
return _createCommentVNode(\\"foo\\")
9692
}
@@ -99,7 +95,7 @@ return function render() {
9995

10096
exports[`compiler: codegen compound expression 1`] = `
10197
"
102-
return function render() {
98+
return function render(_ctx, _cache) {
10399
with (this) {
104100
return _ctx.foo + _toDisplayString(bar) + nested
105101
}
@@ -108,7 +104,7 @@ return function render() {
108104

109105
exports[`compiler: codegen forNode 1`] = `
110106
"
111-
return function render() {
107+
return function render(_ctx, _cache) {
112108
with (this) {
113109
return (foo, bar)
114110
}
@@ -118,7 +114,7 @@ return function render() {
118114
exports[`compiler: codegen function mode preamble 1`] = `
119115
"const _Vue = Vue
120116
121-
return function render() {
117+
return function render(_ctx, _cache) {
122118
with (this) {
123119
const { createVNode: _createVNode, resolveDirective: _resolveDirective } = _Vue
124120
@@ -130,8 +126,7 @@ return function render() {
130126
exports[`compiler: codegen function mode preamble w/ prefixIdentifiers: true 1`] = `
131127
"const { createVNode: _createVNode, resolveDirective: _resolveDirective } = Vue
132128
133-
return function render() {
134-
const _ctx = this
129+
return function render(_ctx, _cache) {
135130
return null
136131
}"
137132
`;
@@ -141,7 +136,7 @@ exports[`compiler: codegen hoists 1`] = `
141136
const _hoisted_1 = hello
142137
const _hoisted_2 = { id: \\"foo\\" }
143138
144-
return function render() {
139+
return function render(_ctx, _cache) {
145140
with (this) {
146141
return null
147142
}
@@ -150,7 +145,7 @@ return function render() {
150145

151146
exports[`compiler: codegen ifNode 1`] = `
152147
"
153-
return function render() {
148+
return function render(_ctx, _cache) {
154149
with (this) {
155150
return (foo, bar)
156151
}
@@ -159,7 +154,7 @@ return function render() {
159154

160155
exports[`compiler: codegen interpolation 1`] = `
161156
"
162-
return function render() {
157+
return function render(_ctx, _cache) {
163158
with (this) {
164159
return _toDisplayString(hello)
165160
}
@@ -169,8 +164,7 @@ return function render() {
169164
exports[`compiler: codegen module mode preamble 1`] = `
170165
"import { createVNode as _createVNode, resolveDirective as _resolveDirective } from \\"vue\\"
171166
172-
export function render() {
173-
const _ctx = this
167+
export function render(_ctx, _cache) {
174168
return null
175169
}"
176170
`;
@@ -181,23 +175,14 @@ exports[`compiler: codegen module mode preamble w/ optimizeBindings: true 1`] =
181175
// Binding optimization for webpack code-split
182176
const _createVNode = createVNode, _resolveDirective = resolveDirective
183177
184-
export function render() {
185-
const _ctx = this
186-
return null
187-
}"
188-
`;
189-
190-
exports[`compiler: codegen prefixIdentifiers: true should inject _ctx statement 1`] = `
191-
"
192-
return function render() {
193-
const _ctx = this
178+
export function render(_ctx, _cache) {
194179
return null
195180
}"
196181
`;
197182

198183
exports[`compiler: codegen static text 1`] = `
199184
"
200-
return function render() {
185+
return function render(_ctx, _cache) {
201186
with (this) {
202187
return \\"hello\\"
203188
}
@@ -206,7 +191,7 @@ return function render() {
206191

207192
exports[`compiler: codegen temps 1`] = `
208193
"
209-
return function render() {
194+
return function render(_ctx, _cache) {
210195
with (this) {
211196
let _temp0, _temp1, _temp2
212197

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

+3-5
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
exports[`compiler: integration tests function mode 1`] = `
44
"const _Vue = Vue
55
6-
return function render() {
6+
return function render(_ctx, _cache) {
77
with (this) {
88
const { toDisplayString: _toDisplayString, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode, Fragment: _Fragment, renderList: _renderList, createTextVNode: _createTextVNode } = _Vue
99
@@ -28,8 +28,7 @@ return function render() {
2828
exports[`compiler: integration tests function mode w/ prefixIdentifiers: true 1`] = `
2929
"const { toDisplayString: _toDisplayString, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode, Fragment: _Fragment, renderList: _renderList, createTextVNode: _createTextVNode } = Vue
3030
31-
return function render() {
32-
const _ctx = this
31+
return function render(_ctx, _cache) {
3332
return (_openBlock(), _createBlock(\\"div\\", {
3433
id: \\"foo\\",
3534
class: _ctx.bar.baz
@@ -50,8 +49,7 @@ return function render() {
5049
exports[`compiler: integration tests module mode 1`] = `
5150
"import { toDisplayString as _toDisplayString, createVNode as _createVNode, openBlock as _openBlock, createBlock as _createBlock, createCommentVNode as _createCommentVNode, Fragment as _Fragment, renderList as _renderList, createTextVNode as _createTextVNode } from \\"vue\\"
5251
53-
export function render() {
54-
const _ctx = this
52+
export function render(_ctx, _cache) {
5553
return (_openBlock(), _createBlock(\\"div\\", {
5654
id: \\"foo\\",
5755
class: _ctx.bar.baz

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

+7-11
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,18 @@
11
// Jest Snapshot v1, https://goo.gl/fbAQLP
22

33
exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = `
4-
"import { createVNode as _createVNode, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\"
4+
"import { createVNode as _createVNode, toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\"
55
const _withId = _withScopeId(\\"test\\")
66
77
_pushScopeId(\\"test\\")
88
const _hoisted_1 = _createVNode(\\"div\\", null, \\"hello\\")
99
const _hoisted_2 = _createVNode(\\"div\\", null, \\"world\\")
1010
_popScopeId()
1111
12-
export const render = _withId(function render() {
13-
const _ctx = this
12+
export const render = _withId(function render(_ctx, _cache) {
1413
return (_openBlock(), _createBlock(\\"div\\", null, [
1514
_hoisted_1,
15+
_createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */),
1616
_hoisted_2
1717
]))
1818
})"
@@ -22,8 +22,7 @@ exports[`scopeId compiler support should wrap default slot 1`] = `
2222
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\"
2323
const _withId = _withScopeId(\\"test\\")
2424
25-
export const render = _withId(function render() {
26-
const _ctx = this
25+
export const render = _withId(function render(_ctx, _cache) {
2726
const _component_Child = _resolveComponent(\\"Child\\")
2827
2928
return (_openBlock(), _createBlock(_component_Child, null, {
@@ -39,8 +38,7 @@ exports[`scopeId compiler support should wrap dynamic slots 1`] = `
3938
"import { createVNode as _createVNode, resolveComponent as _resolveComponent, renderList as _renderList, createSlots as _createSlots, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\"
4039
const _withId = _withScopeId(\\"test\\")
4140
42-
export const render = _withId(function render() {
43-
const _ctx = this
41+
export const render = _withId(function render(_ctx, _cache) {
4442
const _component_Child = _resolveComponent(\\"Child\\")
4543
4644
return (_openBlock(), _createBlock(_component_Child, null, _createSlots({ _compiled: true }, [
@@ -68,8 +66,7 @@ exports[`scopeId compiler support should wrap named slots 1`] = `
6866
"import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createVNode as _createVNode, resolveComponent as _resolveComponent, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\"
6967
const _withId = _withScopeId(\\"test\\")
7068
71-
export const render = _withId(function render() {
72-
const _ctx = this
69+
export const render = _withId(function render(_ctx, _cache) {
7370
const _component_Child = _resolveComponent(\\"Child\\")
7471
7572
return (_openBlock(), _createBlock(_component_Child, null, {
@@ -88,8 +85,7 @@ exports[`scopeId compiler support should wrap render function 1`] = `
8885
"import { createVNode as _createVNode, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\"
8986
const _withId = _withScopeId(\\"test\\")
9087
91-
export const render = _withId(function render() {
92-
const _ctx = this
88+
export const render = _withId(function render(_ctx, _cache) {
9389
return (_openBlock(), _createBlock(\\"div\\"))
9490
})"
9591
`;

packages/compiler-core/__tests__/codegen.spec.ts

+1-9
Original file line numberDiff line numberDiff line change
@@ -183,12 +183,6 @@ describe('compiler: codegen', () => {
183183
expect(code).toMatchSnapshot()
184184
})
185185

186-
test('prefixIdentifiers: true should inject _ctx statement', () => {
187-
const { code } = generate(createRoot(), { prefixIdentifiers: true })
188-
expect(code).toMatch(`const _ctx = this\n`)
189-
expect(code).toMatchSnapshot()
190-
})
191-
192186
test('static text', () => {
193187
const { code } = generate(
194188
createRoot({
@@ -422,7 +416,6 @@ describe('compiler: codegen', () => {
422416
prefixIdentifiers: true
423417
}
424418
)
425-
expect(code).toMatch(`const _cache = _ctx.$cache`)
426419
expect(code).toMatch(`_cache[1] || (_cache[1] = foo)`)
427420
expect(code).toMatchSnapshot()
428421
})
@@ -442,7 +435,6 @@ describe('compiler: codegen', () => {
442435
prefixIdentifiers: true
443436
}
444437
)
445-
expect(code).toMatch(`const _cache = _ctx.$cache`)
446438
expect(code).toMatch(
447439
`
448440
_cache[1] || (
@@ -596,7 +588,7 @@ describe('compiler: codegen', () => {
596588
)
597589
expect(code).toMatchInlineSnapshot(`
598590
"
599-
return function render() {
591+
return function render(_ctx, _cache) {
600592
with (this) {
601593
return foo = bar
602594
}

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

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ describe('scopeId compiler support', () => {
1919
})
2020
expect(ast.helpers).toContain(WITH_SCOPE_ID)
2121
expect(code).toMatch(`const _withId = _withScopeId("test")`)
22-
expect(code).toMatch(`export const render = _withId(function render() {`)
22+
expect(code).toMatch(`export const render = _withId(function render(`)
2323
expect(code).toMatchSnapshot()
2424
})
2525

@@ -68,7 +68,7 @@ describe('scopeId compiler support', () => {
6868

6969
test('should push scopeId for hoisted nodes', () => {
7070
const { ast, code } = baseCompile(
71-
`<div><div>hello</div><div>world</div></div>`,
71+
`<div><div>hello</div>{{ foo }}<div>world</div></div>`,
7272
{
7373
mode: 'module',
7474
scopeId: 'test',

0 commit comments

Comments
 (0)