提交 8fdb4d84 编写于 作者: fxy060608's avatar fxy060608

wip(uts): 修复 v-once

上级 92ae3050
...@@ -34,7 +34,7 @@ describe('compiler:codegen', () => { ...@@ -34,7 +34,7 @@ describe('compiler:codegen', () => {
assert( assert(
`<view/>`, `<view/>`,
` `
function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cache = this.$!.renderCache\n return createElementVNode("view")\n}`, function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cache = this.$.renderCache\n return createElementVNode("view")\n}`,
{ {
targetLanguage: 'kotlin', targetLanguage: 'kotlin',
mode: 'function', mode: 'function',
...@@ -44,7 +44,7 @@ function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cach ...@@ -44,7 +44,7 @@ function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cach
test(`UTSComponents`, () => { test(`UTSComponents`, () => {
assert( assert(
`<view><uts-hello/><uts-hello/></view>`, `<view><uts-hello/><uts-hello/></view>`,
`import { UtsHelloElement } from 'uts.sdk.modules.utsHello';\nfunction PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cache = this.$!.renderCache\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name)\n ])\n}`, `import { UtsHelloElement } from 'uts.sdk.modules.utsHello';\nfunction PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cache = this.$.renderCache\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name)\n ])\n}`,
{ {
targetLanguage: 'kotlin', targetLanguage: 'kotlin',
mode: 'function', mode: 'function',
...@@ -61,7 +61,7 @@ function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cach ...@@ -61,7 +61,7 @@ function PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cach
) )
assert( assert(
`<view><uts-hello/><uts-hello/><uts-hello1/></view>`, `<view><uts-hello/><uts-hello/><uts-hello1/></view>`,
`import { UtsHelloElement } from 'uts.sdk.modules.utsHello';import { UtsHello1Element } from 'uts.sdk.modules.utsHello';\nfunction PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cache = this.$!.renderCache\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHello1Component.name)\n ])\n}`, `import { UtsHelloElement } from 'uts.sdk.modules.utsHello';import { UtsHello1Element } from 'uts.sdk.modules.utsHello';\nfunction PagesIndexIndexRender(): VNode | null {\nconst _ctx = this\nconst _cache = this.$.renderCache\n return createElementVNode("view", null, [\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHelloComponent.name),\n createElementVNode(uts.sdk.modules.utsHello.UtsHello1Component.name)\n ])\n}`,
{ {
targetLanguage: 'kotlin', targetLanguage: 'kotlin',
mode: 'function', mode: 'function',
...@@ -92,7 +92,7 @@ import _easycom_custom1 from '@/components/custom1/custom1.vue' ...@@ -92,7 +92,7 @@ import _easycom_custom1 from '@/components/custom1/custom1.vue'
import _easycom_index from '@/components/index/index.vue' import _easycom_index from '@/components/index/index.vue'
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_custom = resolveEasyComponent("custom",_easycom_custom) const _component_custom = resolveEasyComponent("custom",_easycom_custom)
const _component_custom1 = resolveEasyComponent("custom1",_easycom_custom1) const _component_custom1 = resolveEasyComponent("custom1",_easycom_custom1)
const _component_index = resolveEasyComponent("index",_easycom_index) const _component_index = resolveEasyComponent("index",_easycom_index)
...@@ -121,7 +121,7 @@ const _component_index1 = resolveComponent("index1") ...@@ -121,7 +121,7 @@ const _component_index1 = resolveComponent("index1")
` `
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_index = resolveComponent("index", true) const _component_index = resolveComponent("index", true)
return createVNode(_component_index) return createVNode(_component_index)
...@@ -141,7 +141,7 @@ import _easycom_custom1 from '@/components/custom1/custom1.vue' ...@@ -141,7 +141,7 @@ import _easycom_custom1 from '@/components/custom1/custom1.vue'
import _easycom_index from '@/components/index/index.vue' import _easycom_index from '@/components/index/index.vue'
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_custom = resolveEasyComponent("custom",_easycom_custom) const _component_custom = resolveEasyComponent("custom",_easycom_custom)
const _component_custom1 = resolveEasyComponent("custom1",_easycom_custom1) const _component_custom1 = resolveEasyComponent("custom1",_easycom_custom1)
const _component_index = resolveEasyComponent("index",_easycom_index) const _component_index = resolveEasyComponent("index",_easycom_index)
...@@ -185,7 +185,7 @@ import _imports_0 from './logo.png' ...@@ -185,7 +185,7 @@ import _imports_0 from './logo.png'
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
return createElementVNode(\"image\", utsMapOf({ src: _imports_0 })) return createElementVNode(\"image\", utsMapOf({ src: _imports_0 }))
}`, }`,
{ {
......
...@@ -4,7 +4,7 @@ exports[`compiler: v-memo transform on component 1`] = ` ...@@ -4,7 +4,7 @@ exports[`compiler: v-memo transform on component 1`] = `
" "
function Render(): VNode | null { function Render(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_Comp = resolveComponent("Comp") const _component_Comp = resolveComponent("Comp")
return createElementVNode("view", null, [ return createElementVNode("view", null, [
...@@ -17,7 +17,7 @@ exports[`compiler: v-memo transform on normal element 1`] = ` ...@@ -17,7 +17,7 @@ exports[`compiler: v-memo transform on normal element 1`] = `
" "
function Render(): VNode | null { function Render(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
return createElementVNode("view", null, [ return createElementVNode("view", null, [
withMemo([_ctx.x], (): VNode => createElementVNode("view"), _cache, 0) withMemo([_ctx.x], (): VNode => createElementVNode("view"), _cache, 0)
]) ])
...@@ -28,7 +28,7 @@ exports[`compiler: v-memo transform on root element 1`] = ` ...@@ -28,7 +28,7 @@ exports[`compiler: v-memo transform on root element 1`] = `
" "
function Render(): VNode | null { function Render(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
return withMemo([_ctx.x], (): VNode => createElementVNode("view"), _cache, 0) return withMemo([_ctx.x], (): VNode => createElementVNode("view"), _cache, 0)
}" }"
`; `;
...@@ -37,7 +37,7 @@ exports[`compiler: v-memo transform on template v-for 1`] = ` ...@@ -37,7 +37,7 @@ exports[`compiler: v-memo transform on template v-for 1`] = `
" "
function Render(): VNode | null { function Render(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
return createElementVNode("view", null, [ return createElementVNode("view", null, [
createElementVNode(Fragment, null, RenderHelpers.renderList(_ctx.list, ({ x, y }, _, _, _cached): VNode => { createElementVNode(Fragment, null, RenderHelpers.renderList(_ctx.list, ({ x, y }, _, _, _cached): VNode => {
const _memo: Array<any | null> = ([x, y === _ctx.z]) const _memo: Array<any | null> = ([x, y === _ctx.z])
...@@ -54,7 +54,7 @@ exports[`compiler: v-memo transform on v-for 1`] = ` ...@@ -54,7 +54,7 @@ exports[`compiler: v-memo transform on v-for 1`] = `
" "
function Render(): VNode | null { function Render(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
return createElementVNode("view", null, [ return createElementVNode("view", null, [
createElementVNode(Fragment, null, RenderHelpers.renderList(_ctx.list, ({ x, y }, _, _, _cached): VNode => { createElementVNode(Fragment, null, RenderHelpers.renderList(_ctx.list, ({ x, y }, _, _, _cached): VNode => {
const _memo: Array<any | null> = ([x, y === _ctx.z]) const _memo: Array<any | null> = ([x, y === _ctx.z])
...@@ -73,7 +73,7 @@ exports[`compiler: v-memo transform on v-if 1`] = ` ...@@ -73,7 +73,7 @@ exports[`compiler: v-memo transform on v-if 1`] = `
" "
function Render(): VNode | null { function Render(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_Comp = resolveComponent("Comp") const _component_Comp = resolveComponent("Comp")
return createElementVNode("view", null, [ return createElementVNode("view", null, [
......
// Jest Snapshot v1, https://goo.gl/fbAQLP // Jest Snapshot v1, https://goo.gl/fbAQLP
exports[`compiler: v-once transform as root node 1`] = ` exports[`compiler: v-once transform as root node 1`] = `
"_cache[0] ?? run((): VNode | null => { "resolveCache(_cache, 0, (): VNode => {
setBlockTracking(-1) setBlockTracking(-1)
_cache[0] = createElementVNode("view", utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"]) _cache[0] = createElementVNode("view", utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"])
setBlockTracking(1) setBlockTracking(1)
return _cache[0] as VNode | null return _cache[0] as VNode
})" })"
`; `;
exports[`compiler: v-once transform on component 1`] = ` exports[`compiler: v-once transform on component 1`] = `
"createElementVNode("view", null, [ "createElementVNode("view", null, [
_cache[0] ?? run((): VNode | null => { resolveCache(_cache, 0, (): VNode => {
setBlockTracking(-1) setBlockTracking(-1)
_cache[0] = createVNode(_component_Comp, utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"]) _cache[0] = createVNode(_component_Comp, utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"])
setBlockTracking(1) setBlockTracking(1)
return _cache[0] as VNode | null return _cache[0] as VNode
}) })
])" ])"
`; `;
exports[`compiler: v-once transform on nested plain element 1`] = ` exports[`compiler: v-once transform on nested plain element 1`] = `
"createElementVNode("view", null, [ "createElementVNode("view", null, [
_cache[0] ?? run((): VNode | null => { resolveCache(_cache, 0, (): VNode => {
setBlockTracking(-1) setBlockTracking(-1)
_cache[0] = createElementVNode("view", utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"]) _cache[0] = createElementVNode("view", utsMapOf({ id: foo }), null, 8 /* PROPS */, ["id"])
setBlockTracking(1) setBlockTracking(1)
return _cache[0] as VNode | null return _cache[0] as VNode
}) })
])" ])"
`; `;
exports[`compiler: v-once transform on slot outlet 1`] = ` exports[`compiler: v-once transform on slot outlet 1`] = `
"createElementVNode("view", null, [ "createElementVNode("view", null, [
_cache[0] ?? run((): VNode | null => { resolveCache(_cache, 0, (): VNode => {
setBlockTracking(-1) setBlockTracking(-1)
_cache[0] = renderSlot($slots, "default") _cache[0] = renderSlot($slots, "default")
setBlockTracking(1) setBlockTracking(1)
return _cache[0] as VNode | null return _cache[0] as VNode
}) })
])" ])"
`; `;
...@@ -7,7 +7,7 @@ describe('compiler: component', () => { ...@@ -7,7 +7,7 @@ describe('compiler: component', () => {
` `
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_Foo = resolveComponent("Foo") const _component_Foo = resolveComponent("Foo")
return createElementVNode("view", null, [ return createElementVNode("view", null, [
......
...@@ -95,7 +95,7 @@ describe('compiler: slot', () => { ...@@ -95,7 +95,7 @@ describe('compiler: slot', () => {
` `
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
return createElementVNode("view", null, [ return createElementVNode("view", null, [
renderSlot(_ctx.$slots, "default", utsMapOf({ data: "data" })) renderSlot(_ctx.$slots, "default", utsMapOf({ data: "data" }))
]) ])
...@@ -113,7 +113,7 @@ const _cache = this.$!.renderCache ...@@ -113,7 +113,7 @@ const _cache = this.$!.renderCache
` `
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_Foo = resolveComponent("Foo") const _component_Foo = resolveComponent("Foo")
return createElementVNode("view", null, [ return createElementVNode("view", null, [
...@@ -150,7 +150,7 @@ const _component_Foo = resolveComponent("Foo") ...@@ -150,7 +150,7 @@ const _component_Foo = resolveComponent("Foo")
` `
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_Foo = resolveComponent("Foo") const _component_Foo = resolveComponent("Foo")
return createElementVNode("view", null, [ return createElementVNode("view", null, [
...@@ -175,7 +175,7 @@ const _component_Foo = resolveComponent("Foo") ...@@ -175,7 +175,7 @@ const _component_Foo = resolveComponent("Foo")
` `
function PagesIndexIndexRender(): VNode | null { function PagesIndexIndexRender(): VNode | null {
const _ctx = this const _ctx = this
const _cache = this.$!.renderCache const _cache = this.$.renderCache
const _component_Foo = resolveComponent("Foo") const _component_Foo = resolveComponent("Foo")
return createElementVNode("view", null, [ return createElementVNode("view", null, [
......
...@@ -45,6 +45,7 @@ import { addEasyComponentAutoImports, genRenderFunctionDecl } from './utils' ...@@ -45,6 +45,7 @@ import { addEasyComponentAutoImports, genRenderFunctionDecl } from './utils'
import { import {
IS_TRUE, IS_TRUE,
RENDER_LIST, RENDER_LIST,
RESOLVE_CACHE,
RESOLVE_COMPONENT, RESOLVE_COMPONENT,
RESOLVE_DIRECTIVE, RESOLVE_DIRECTIVE,
RESOLVE_EASY_COMPONENT, RESOLVE_EASY_COMPONENT,
...@@ -214,7 +215,7 @@ export function generate( ...@@ -214,7 +215,7 @@ export function generate(
newline() newline()
push(`const _ctx = this`) push(`const _ctx = this`)
newline() newline()
push(`const _cache = this.$!.renderCache`) push(`const _cache = this.$.renderCache`)
// generate asset resolution statements // generate asset resolution statements
if (ast.components.length) { if (ast.components.length) {
newline() newline()
...@@ -822,7 +823,7 @@ function genConditionalExpression( ...@@ -822,7 +823,7 @@ function genConditionalExpression(
function genCacheExpression(node: CacheExpression, context: CodegenContext) { function genCacheExpression(node: CacheExpression, context: CodegenContext) {
const { push, helper, indent, deindent, newline } = context const { push, helper, indent, deindent, newline } = context
push(`_cache[${node.index}] ?? run((): VNode | null => {`) push(`${helperNameMap[RESOLVE_CACHE]}(_cache, ${node.index}, (): VNode => {`)
if (node.isVNode) { if (node.isVNode) {
indent() indent()
push(`${helper(SET_BLOCK_TRACKING)}(-1)`) push(`${helper(SET_BLOCK_TRACKING)}(-1)`)
...@@ -834,7 +835,7 @@ function genCacheExpression(node: CacheExpression, context: CodegenContext) { ...@@ -834,7 +835,7 @@ function genCacheExpression(node: CacheExpression, context: CodegenContext) {
newline() newline()
push(`${helper(SET_BLOCK_TRACKING)}(1)`) push(`${helper(SET_BLOCK_TRACKING)}(1)`)
newline() newline()
push(`return _cache[${node.index}] as VNode | null`) push(`return _cache[${node.index}] as VNode`)
deindent() deindent()
} }
push(`})`) push(`})`)
......
...@@ -15,6 +15,8 @@ export const V_ON_WITH_MODIFIERS = Symbol(`vOnModifiersGuard`) ...@@ -15,6 +15,8 @@ export const V_ON_WITH_MODIFIERS = Symbol(`vOnModifiersGuard`)
export const WITH_SLOT_CTX = Symbol(`withSlotCtx`) export const WITH_SLOT_CTX = Symbol(`withSlotCtx`)
export const WITH_SCOPED_SLOT_CTX = Symbol(`withScopedSlotCtx`) export const WITH_SCOPED_SLOT_CTX = Symbol(`withScopedSlotCtx`)
export const RESOLVE_CACHE = Symbol(`resolveCache`)
registerRuntimeHelpers({ registerRuntimeHelpers({
[IS_TRUE]: 'isTrue', [IS_TRUE]: 'isTrue',
[V_SHOW]: 'vShow', [V_SHOW]: 'vShow',
...@@ -28,4 +30,5 @@ registerRuntimeHelpers({ ...@@ -28,4 +30,5 @@ registerRuntimeHelpers({
[V_ON_WITH_MODIFIERS]: `withModifiers`, [V_ON_WITH_MODIFIERS]: `withModifiers`,
[WITH_SLOT_CTX]: `withSlotCtx`, [WITH_SLOT_CTX]: `withSlotCtx`,
[WITH_SCOPED_SLOT_CTX]: `withScopedSlotCtx`, [WITH_SCOPED_SLOT_CTX]: `withScopedSlotCtx`,
[RESOLVE_CACHE]: `resolveCache`,
}) })
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册