Skip to content

fix(compiler-core): using v-once inside template with v-if error #7730

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 33 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
33 commits
Select commit Hold shift + click to select a range
15a59e4
feat(compiler-core): using v-once inside template with v-if error
baiwusanyu-c Feb 16, 2023
ef2a903
feat(compiler-core): update code
baiwusanyu-c Feb 16, 2023
e5dc332
feat(compiler-core): added unit test
baiwusanyu-c Feb 16, 2023
2126b2c
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Feb 21, 2023
2295fae
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Feb 22, 2023
d2b0ed8
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Mar 17, 2023
23a6584
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Mar 20, 2023
ecfedc1
Merge remote-tracking branch 'origin/main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Mar 23, 2023
1dadc60
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Mar 27, 2023
9889e6e
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 6, 2023
08df85e
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 10, 2023
9aa3972
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 11, 2023
32f77d9
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 14, 2023
ff8ff86
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 17, 2023
05c8659
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 19, 2023
24468f1
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 20, 2023
90695cd
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Apr 25, 2023
c66c49c
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 4, 2023
dd8fe45
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 9, 2023
0697229
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 12, 2023
685e76c
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 16, 2023
5842843
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 17, 2023
941da92
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 23, 2023
eb2296e
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c May 29, 2023
834654f
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Sep 28, 2023
03c135e
Delete awd.html
baiwusanyu-c Sep 28, 2023
dd7dc2c
Merge branch 'main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Nov 10, 2023
3d6b261
Merge remote-tracking branch 'origin/main' into bwsy/fix/vOnceAndvIf
baiwusanyu-c Jan 3, 2024
52da0a4
[autofix.ci] apply automated fixes
autofix-ci[bot] Jan 3, 2024
27dc34c
chore: updaed unit test snap
baiwusanyu-c Jan 3, 2024
5ca1018
chore: updaed unit test snap
baiwusanyu-c Jan 3, 2024
fe3d99a
Merge remote-tracking branch 'upstream/main' into bwsy/fix/vOnceAndvIf
Sep 5, 2024
220e3f1
[autofix.ci] apply automated fixes
autofix-ci[bot] Sep 5, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,25 @@ return function render(_ctx, _cache) {
}"
`;

exports[`compiler: v-if > codegen > template v-if w/ v-once 1`] = `
"const _Vue = Vue

return function render(_ctx, _cache) {
with (_ctx) {
const { setBlockTracking: _setBlockTracking, openBlock: _openBlock, createElementBlock: _createElementBlock, createCommentVNode: _createCommentVNode } = _Vue

return true
? _cache[0] || (
_setBlockTracking(-1),
(_cache[0] = (_openBlock(), _createElementBlock("p", { key: 0 }, "foo"))).cacheIndex = 0,
_setBlockTracking(1),
_cache[0]
)
: _createCommentVNode("v-if", true)
}
}"
`;

exports[`compiler: v-if > codegen > v-if + v-else 1`] = `
"const _Vue = Vue

Expand Down
21 changes: 20 additions & 1 deletion packages/compiler-core/__tests__/transforms/vIf.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { baseParse as parse } from '../../src/parser'
import { transform } from '../../src/transform'
import { transformIf } from '../../src/transforms/vIf'
import { transformOnce } from '../../src/transforms/vOnce'
import { transformElement } from '../../src/transforms/transformElement'
import { transformSlotOutlet } from '../../src/transforms/transformSlotOutlet'
import {
Expand Down Expand Up @@ -35,7 +36,12 @@ function parseWithIfTransform(
) {
const ast = parse(template, options)
transform(ast, {
nodeTransforms: [transformIf, transformSlotOutlet, transformElement],
nodeTransforms: [
transformOnce,
transformIf,
transformSlotOutlet,
transformElement,
],
...options,
})
if (!options.onError) {
Expand Down Expand Up @@ -425,6 +431,19 @@ describe('compiler: v-if', () => {
expect(generate(root).code).toMatchSnapshot()
})

test('template v-if w/ v-once', () => {
const {
root,
node: { codegenNode },
} = parseWithIfTransform(
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The parseWithIfTransform does not contain transformOnce inside, which makes the resulting snap look unreasonable, and does not contain v-once related code.
Let's pass transformOnce to parseWithIfTransform to make the snap more sense.

`<template v-if="true"><p v-once>foo</p></template>`,
)
expect(codegenNode.consequent).toMatchObject({
type: NodeTypes.JS_CACHE_EXPRESSION,
})
expect(generate(root).code).toMatchSnapshot()
})

test('v-if on <slot/>', () => {
const {
root,
Expand Down
9 changes: 7 additions & 2 deletions packages/compiler-core/src/transforms/vIf.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,12 @@ import { ErrorCodes, createCompilerError } from '../errors'
import { processExpression } from './transformExpression'
import { validateBrowserExpression } from '../validateExpression'
import { CREATE_COMMENT, FRAGMENT } from '../runtimeHelpers'
import { findDir, findProp, getMemoedVNodeCall, injectProp } from '../utils'
import {
findDir,
findProp,
getMemoedOrOnceVNodeCall,
injectProp,
} from '../utils'
import { PatchFlagNames, PatchFlags } from '@vue/shared'

export const transformIf: NodeTransform = createStructuralDirectiveTransform(
Expand Down Expand Up @@ -294,7 +299,7 @@ function createChildrenCodegenNode(
const ret = (firstChild as ElementNode).codegenNode as
| BlockCodegenNode
| MemoExpression
const vnodeCall = getMemoedVNodeCall(ret)
const vnodeCall = getMemoedOrOnceVNodeCall(ret)
Comment on lines 299 to +302
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm wondering about the casting of ret. Should that also include CacheExpression?

I realise this would lead to type changes being needed elsewhere, but the incorrect casting may be hiding further bugs.

// Change createVNode to createBlock.
if (vnodeCall.type === NodeTypes.VNODE_CALL) {
convertToBlock(vnodeCall, context)
Expand Down
9 changes: 6 additions & 3 deletions packages/compiler-core/src/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -553,13 +553,16 @@ export function hasScopeRef(
}
}

export function getMemoedVNodeCall(
node: BlockCodegenNode | MemoExpression,
export function getMemoedOrOnceVNodeCall(
node: BlockCodegenNode | MemoExpression | CacheExpression,
): VNodeCall | RenderSlotCall {
if (node.type === NodeTypes.JS_CALL_EXPRESSION && node.callee === WITH_MEMO) {
return node.arguments[1].returns as VNodeCall
} else {
return node
// #7752
return node.type === NodeTypes.JS_CACHE_EXPRESSION
? (node.value as VNodeCall)
: (node as VNodeCall)
}
}

Expand Down