From 47e984d31f08f009a477712e2e82b991a0c046de Mon Sep 17 00:00:00 2001 From: Evan You Date: Mon, 10 Feb 2020 17:29:12 -0500 Subject: [PATCH] refactor(compiler): provide _ctx and _cache via arguments --- .../__snapshots__/codegen.spec.ts.snap | 53 +++++++------------ .../__snapshots__/compile.spec.ts.snap | 8 ++- .../__snapshots__/scopeId.spec.ts.snap | 18 +++---- .../compiler-core/__tests__/codegen.spec.ts | 10 +--- .../compiler-core/__tests__/scopeId.spec.ts | 4 +- .../__snapshots__/hoistStatic.spec.ts.snap | 46 ++++++++-------- .../__snapshots__/transformText.spec.ts.snap | 15 +++--- .../__snapshots__/vFor.spec.ts.snap | 24 ++++----- .../transforms/__snapshots__/vIf.spec.ts.snap | 16 +++--- .../__snapshots__/vModel.spec.ts.snap | 17 +++--- .../__snapshots__/vOnce.spec.ts.snap | 15 ++---- .../__snapshots__/vSlot.spec.ts.snap | 27 ++++------ packages/compiler-core/src/codegen.ts | 13 +---- .../__snapshots__/index.spec.ts.snap | 2 +- .../__snapshots__/vModel.spec.ts.snap | 24 ++++----- .../__snapshots__/vShow.spec.ts.snap | 2 +- .../compileTemplate.spec.ts.snap | 2 +- .../templateTransformAssetUrl.spec.ts.snap | 9 ++-- .../templateTransformSrcset.spec.ts.snap | 3 +- .../__tests__/compileTemplate.spec.ts | 2 +- .../runtime-core/src/componentRenderUtils.ts | 7 ++- 21 files changed, 129 insertions(+), 188 deletions(-) diff --git a/packages/compiler-core/__tests__/__snapshots__/codegen.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/codegen.spec.ts.snap index d38ec84d..8adf793e 100644 --- a/packages/compiler-core/__tests__/__snapshots__/codegen.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/codegen.spec.ts.snap @@ -2,7 +2,7 @@ exports[`compiler: codegen ArrayExpression 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return [ foo, @@ -14,18 +14,14 @@ return function render() { exports[`compiler: codegen CacheExpression 1`] = ` " -export function render() { - const _ctx = this - const _cache = _ctx.$cache +export function render(_ctx, _cache) { return _cache[1] || (_cache[1] = foo) }" `; exports[`compiler: codegen CacheExpression w/ isVNode: true 1`] = ` " -export function render() { - const _ctx = this - const _cache = _ctx.$cache +export function render(_ctx, _cache) { return _cache[1] || ( _setBlockTracking(-1), _cache[1] = foo, @@ -37,7 +33,7 @@ export function render() { exports[`compiler: codegen ConditionalExpression 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return ok ? foo() @@ -50,7 +46,7 @@ return function render() { exports[`compiler: codegen Element (callExpression + objectExpression + TemplateChildNode[]) 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return _createVNode(\\"div\\", { id: \\"foo\\", @@ -65,7 +61,7 @@ return function render() { exports[`compiler: codegen SequenceExpression 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return (foo, bar(baz)) } @@ -74,7 +70,7 @@ return function render() { exports[`compiler: codegen assets + temps 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { const _component_Foo = _resolveComponent(\\"Foo\\") const _component_bar_baz = _resolveComponent(\\"bar-baz\\") @@ -90,7 +86,7 @@ return function render() { exports[`compiler: codegen comment 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return _createCommentVNode(\\"foo\\") } @@ -99,7 +95,7 @@ return function render() { exports[`compiler: codegen compound expression 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return _ctx.foo + _toDisplayString(bar) + nested } @@ -108,7 +104,7 @@ return function render() { exports[`compiler: codegen forNode 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return (foo, bar) } @@ -118,7 +114,7 @@ return function render() { exports[`compiler: codegen function mode preamble 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, resolveDirective: _resolveDirective } = _Vue @@ -130,8 +126,7 @@ return function render() { exports[`compiler: codegen function mode preamble w/ prefixIdentifiers: true 1`] = ` "const { createVNode: _createVNode, resolveDirective: _resolveDirective } = Vue -return function render() { - const _ctx = this +return function render(_ctx, _cache) { return null }" `; @@ -141,7 +136,7 @@ exports[`compiler: codegen hoists 1`] = ` const _hoisted_1 = hello const _hoisted_2 = { id: \\"foo\\" } -return function render() { +return function render(_ctx, _cache) { with (this) { return null } @@ -150,7 +145,7 @@ return function render() { exports[`compiler: codegen ifNode 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return (foo, bar) } @@ -159,7 +154,7 @@ return function render() { exports[`compiler: codegen interpolation 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return _toDisplayString(hello) } @@ -169,8 +164,7 @@ return function render() { exports[`compiler: codegen module mode preamble 1`] = ` "import { createVNode as _createVNode, resolveDirective as _resolveDirective } from \\"vue\\" -export function render() { - const _ctx = this +export function render(_ctx, _cache) { return null }" `; @@ -181,23 +175,14 @@ exports[`compiler: codegen module mode preamble w/ optimizeBindings: true 1`] = // Binding optimization for webpack code-split const _createVNode = createVNode, _resolveDirective = resolveDirective -export function render() { - const _ctx = this - return null -}" -`; - -exports[`compiler: codegen prefixIdentifiers: true should inject _ctx statement 1`] = ` -" -return function render() { - const _ctx = this +export function render(_ctx, _cache) { return null }" `; exports[`compiler: codegen static text 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { return \\"hello\\" } @@ -206,7 +191,7 @@ return function render() { exports[`compiler: codegen temps 1`] = ` " -return function render() { +return function render(_ctx, _cache) { with (this) { let _temp0, _temp1, _temp2 diff --git a/packages/compiler-core/__tests__/__snapshots__/compile.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/compile.spec.ts.snap index 5b51457a..21168dc0 100644 --- a/packages/compiler-core/__tests__/__snapshots__/compile.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/compile.spec.ts.snap @@ -3,7 +3,7 @@ exports[`compiler: integration tests function mode 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { toDisplayString: _toDisplayString, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode, Fragment: _Fragment, renderList: _renderList, createTextVNode: _createTextVNode } = _Vue @@ -28,8 +28,7 @@ return function render() { exports[`compiler: integration tests function mode w/ prefixIdentifiers: true 1`] = ` "const { toDisplayString: _toDisplayString, createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode, Fragment: _Fragment, renderList: _renderList, createTextVNode: _createTextVNode } = Vue -return function render() { - const _ctx = this +return function render(_ctx, _cache) { return (_openBlock(), _createBlock(\\"div\\", { id: \\"foo\\", class: _ctx.bar.baz @@ -50,8 +49,7 @@ return function render() { exports[`compiler: integration tests module mode 1`] = ` "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\\" -export function render() { - const _ctx = this +export function render(_ctx, _cache) { return (_openBlock(), _createBlock(\\"div\\", { id: \\"foo\\", class: _ctx.bar.baz diff --git a/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap b/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap index d5e7e571..6193c0ac 100644 --- a/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap +++ b/packages/compiler-core/__tests__/__snapshots__/scopeId.spec.ts.snap @@ -1,7 +1,7 @@ // Jest Snapshot v1, https://goo.gl/fbAQLP exports[`scopeId compiler support should push scopeId for hoisted nodes 1`] = ` -"import { createVNode as _createVNode, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId, pushScopeId as _pushScopeId, popScopeId as _popScopeId } from \\"vue\\" +"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\\" const _withId = _withScopeId(\\"test\\") _pushScopeId(\\"test\\") @@ -9,10 +9,10 @@ const _hoisted_1 = _createVNode(\\"div\\", null, \\"hello\\") const _hoisted_2 = _createVNode(\\"div\\", null, \\"world\\") _popScopeId() -export const render = _withId(function render() { - const _ctx = this +export const render = _withId(function render(_ctx, _cache) { return (_openBlock(), _createBlock(\\"div\\", null, [ _hoisted_1, + _createTextVNode(_toDisplayString(_ctx.foo), 1 /* TEXT */), _hoisted_2 ])) })" @@ -22,8 +22,7 @@ exports[`scopeId compiler support should wrap default slot 1`] = ` "import { createVNode as _createVNode, resolveComponent as _resolveComponent, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\" const _withId = _withScopeId(\\"test\\") -export const render = _withId(function render() { - const _ctx = this +export const render = _withId(function render(_ctx, _cache) { const _component_Child = _resolveComponent(\\"Child\\") return (_openBlock(), _createBlock(_component_Child, null, { @@ -39,8 +38,7 @@ exports[`scopeId compiler support should wrap dynamic slots 1`] = ` "import { createVNode as _createVNode, resolveComponent as _resolveComponent, renderList as _renderList, createSlots as _createSlots, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\" const _withId = _withScopeId(\\"test\\") -export const render = _withId(function render() { - const _ctx = this +export const render = _withId(function render(_ctx, _cache) { const _component_Child = _resolveComponent(\\"Child\\") return (_openBlock(), _createBlock(_component_Child, null, _createSlots({ _compiled: true }, [ @@ -68,8 +66,7 @@ exports[`scopeId compiler support should wrap named slots 1`] = ` "import { toDisplayString as _toDisplayString, createTextVNode as _createTextVNode, createVNode as _createVNode, resolveComponent as _resolveComponent, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\" const _withId = _withScopeId(\\"test\\") -export const render = _withId(function render() { - const _ctx = this +export const render = _withId(function render(_ctx, _cache) { const _component_Child = _resolveComponent(\\"Child\\") return (_openBlock(), _createBlock(_component_Child, null, { @@ -88,8 +85,7 @@ exports[`scopeId compiler support should wrap render function 1`] = ` "import { createVNode as _createVNode, createBlock as _createBlock, openBlock as _openBlock, withScopeId as _withScopeId } from \\"vue\\" const _withId = _withScopeId(\\"test\\") -export const render = _withId(function render() { - const _ctx = this +export const render = _withId(function render(_ctx, _cache) { return (_openBlock(), _createBlock(\\"div\\")) })" `; diff --git a/packages/compiler-core/__tests__/codegen.spec.ts b/packages/compiler-core/__tests__/codegen.spec.ts index 04e6b017..02add36a 100644 --- a/packages/compiler-core/__tests__/codegen.spec.ts +++ b/packages/compiler-core/__tests__/codegen.spec.ts @@ -183,12 +183,6 @@ describe('compiler: codegen', () => { expect(code).toMatchSnapshot() }) - test('prefixIdentifiers: true should inject _ctx statement', () => { - const { code } = generate(createRoot(), { prefixIdentifiers: true }) - expect(code).toMatch(`const _ctx = this\n`) - expect(code).toMatchSnapshot() - }) - test('static text', () => { const { code } = generate( createRoot({ @@ -422,7 +416,6 @@ describe('compiler: codegen', () => { prefixIdentifiers: true } ) - expect(code).toMatch(`const _cache = _ctx.$cache`) expect(code).toMatch(`_cache[1] || (_cache[1] = foo)`) expect(code).toMatchSnapshot() }) @@ -442,7 +435,6 @@ describe('compiler: codegen', () => { prefixIdentifiers: true } ) - expect(code).toMatch(`const _cache = _ctx.$cache`) expect(code).toMatch( ` _cache[1] || ( @@ -596,7 +588,7 @@ describe('compiler: codegen', () => { ) expect(code).toMatchInlineSnapshot(` " - return function render() { + return function render(_ctx, _cache) { with (this) { return foo = bar } diff --git a/packages/compiler-core/__tests__/scopeId.spec.ts b/packages/compiler-core/__tests__/scopeId.spec.ts index 4991ab34..f009b87a 100644 --- a/packages/compiler-core/__tests__/scopeId.spec.ts +++ b/packages/compiler-core/__tests__/scopeId.spec.ts @@ -19,7 +19,7 @@ describe('scopeId compiler support', () => { }) expect(ast.helpers).toContain(WITH_SCOPE_ID) expect(code).toMatch(`const _withId = _withScopeId("test")`) - expect(code).toMatch(`export const render = _withId(function render() {`) + expect(code).toMatch(`export const render = _withId(function render(`) expect(code).toMatchSnapshot() }) @@ -68,7 +68,7 @@ describe('scopeId compiler support', () => { test('should push scopeId for hoisted nodes', () => { const { ast, code } = baseCompile( - `
hello
world
`, + `
hello
{{ foo }}
world
`, { mode: 'module', scopeId: 'test', diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap index 5a472c56..0b355441 100644 --- a/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/hoistStatic.spec.ts.snap @@ -6,7 +6,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = _createVNode(\\"div\\", { key: \\"foo\\" }) -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -26,7 +26,7 @@ const _hoisted_1 = _createVNode(\\"p\\", null, [ _createVNode(\\"span\\") ]) -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -45,7 +45,7 @@ const _hoisted_1 = _createVNode(\\"div\\", null, [ _createCommentVNode(\\"comment\\") ]) -return function render() { +return function render(_ctx, _cache) { with (this) { const { createCommentVNode: _createCommentVNode, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -63,7 +63,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = _createVNode(\\"span\\") const _hoisted_2 = _createVNode(\\"div\\") -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -81,7 +81,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = _createVNode(\\"span\\", { class: \\"inline\\" }, \\"hello\\") -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -98,7 +98,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = { id: \\"foo\\" } -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, withDirectives: _withDirectives, resolveDirective: _resolveDirective, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -119,7 +119,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = { id: \\"foo\\" } -return function render() { +return function render(_ctx, _cache) { with (this) { const { toDisplayString: _toDisplayString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -136,7 +136,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = { id: \\"foo\\" } -return function render() { +return function render(_ctx, _cache) { with (this) { const { resolveComponent: _resolveComponent, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -157,7 +157,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = { class: { foo: true } } -return function render() { +return function render(_ctx, _cache) { with (this) { const { toDisplayString: _toDisplayString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -179,7 +179,7 @@ const _hoisted_1 = _createVNode(\\"span\\", null, [ _toDisplayString(true) ]) -return function render() { +return function render(_ctx, _cache) { with (this) { const { toDisplayString: _toDisplayString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -196,7 +196,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = _createVNode(\\"span\\", { foo: 0 }, _toDisplayString(1)) -return function render() { +return function render(_ctx, _cache) { with (this) { const { toDisplayString: _toDisplayString, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -210,9 +210,7 @@ return function render() { exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist elements with cached handlers 1`] = ` "import { createVNode as _createVNode, createBlock as _createBlock, openBlock as _openBlock } from \\"vue\\" -export function render() { - const _ctx = this - const _cache = _ctx.$cache +export function render(_ctx, _cache) { return (_openBlock(), _createBlock(\\"div\\", null, [ _createVNode(\\"div\\", null, [ _createVNode(\\"div\\", { @@ -226,7 +224,7 @@ export function render() { exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist expressions that refer scope variables (2) 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { renderList: _renderList, openBlock: _openBlock, createBlock: _createBlock, Fragment: _Fragment, toDisplayString: _toDisplayString, createVNode: _createVNode } = _Vue @@ -244,7 +242,7 @@ return function render() { exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist expressions that refer scope variables (v-slot) 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { toDisplayString: _toDisplayString, resolveComponent: _resolveComponent, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -261,7 +259,7 @@ return function render() { exports[`compiler: hoistStatic transform prefixIdentifiers should NOT hoist expressions that refer scope variables 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { renderList: _renderList, openBlock: _openBlock, createBlock: _createBlock, Fragment: _Fragment, toDisplayString: _toDisplayString, createVNode: _createVNode } = _Vue @@ -279,7 +277,7 @@ return function render() { exports[`compiler: hoistStatic transform should NOT hoist components 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { resolveComponent: _resolveComponent, createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -295,7 +293,7 @@ return function render() { exports[`compiler: hoistStatic transform should NOT hoist element with dynamic key 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -309,7 +307,7 @@ return function render() { exports[`compiler: hoistStatic transform should NOT hoist element with dynamic props 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -323,7 +321,7 @@ return function render() { exports[`compiler: hoistStatic transform should NOT hoist element with dynamic ref 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -337,7 +335,7 @@ return function render() { exports[`compiler: hoistStatic transform should NOT hoist root node 1`] = ` "const _Vue = Vue -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, createBlock: _createBlock, openBlock: _openBlock } = _Vue @@ -353,7 +351,7 @@ const { createVNode: _createVNode } = _Vue const _hoisted_1 = { id: \\"foo\\" } const _hoisted_2 = _createVNode(\\"span\\") -return function render() { +return function render(_ctx, _cache) { with (this) { const { renderList: _renderList, openBlock: _openBlock, createBlock: _createBlock, Fragment: _Fragment, createVNode: _createVNode } = _Vue @@ -378,7 +376,7 @@ const _hoisted_1 = { } const _hoisted_2 = _createVNode(\\"span\\") -return function render() { +return function render(_ctx, _cache) { with (this) { const { createVNode: _createVNode, openBlock: _openBlock, createBlock: _createBlock, createCommentVNode: _createCommentVNode } = _Vue diff --git a/packages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snap b/packages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snap index 227ac7df..8c133b21 100644 --- a/packages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snap +++ b/packages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snap @@ -3,7 +3,7 @@ exports[`compiler: transform text