Skip to content

fix(compiler-sfc): properly analyze patch flag of vFor + (ref or vnodeHook or dir) #11682

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 4 commits into
base: main
Choose a base branch
from

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Aug 22, 2024

close #9239
close #12569

I’m not sure if this change is correct. Here is my understanding:
When an element uses ref, vnodeHook, or custom directives, the patchFlag should be NEED_PATCH. However, when they are used together with v-for, the patchFlag gets lost.
related commit 1c9a481

playground
playground with this PR

The following problems will be fixed

The last two are from #9240 (comment) created by skirtles-code

Summary by CodeRabbit

  • Bug Fixes
    • Improved detection of elements requiring the NEED_PATCH flag when using v-for with static or setup-bound ref, custom directives, or vnode hooks.
  • Tests
    • Added new test cases to ensure correct patch flag assignment for elements with v-for combined with various features.

@edison1105 edison1105 changed the title fix(compiler-sfc): properly analyze patch flag of vFor fix(compiler-sfc): properly analyze patch flag of vFor + (ref or vnodeHook or dir) Aug 22, 2024
Copy link

github-actions bot commented Aug 22, 2024

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.2 kB 34.4 kB
vue.global.prod.js 159 kB (+7 B) 58.4 kB (+3 B) 52 kB (+80 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 23 kB 20.9 kB
defineCustomElement 59.3 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented Aug 22, 2024

Open in StackBlitz

@vue/compiler-core

npm i https://pkg.pr.new/@vue/compiler-core@11682

@vue/compiler-dom

npm i https://pkg.pr.new/@vue/compiler-dom@11682

@vue/compiler-ssr

npm i https://pkg.pr.new/@vue/compiler-ssr@11682

@vue/compiler-sfc

npm i https://pkg.pr.new/@vue/compiler-sfc@11682

@vue/reactivity

npm i https://pkg.pr.new/@vue/reactivity@11682

@vue/runtime-core

npm i https://pkg.pr.new/@vue/runtime-core@11682

@vue/runtime-dom

npm i https://pkg.pr.new/@vue/runtime-dom@11682

@vue/server-renderer

npm i https://pkg.pr.new/@vue/server-renderer@11682

@vue/shared

npm i https://pkg.pr.new/@vue/shared@11682

vue

npm i https://pkg.pr.new/vue@11682

@vue/compat

npm i https://pkg.pr.new/@vue/compat@11682

commit: 4780e7c

@edison1105
Copy link
Member Author

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Aug 22, 2024

📝 Ran ecosystem CI: Open

suite result latest scheduled
language-tools failure failure
nuxt failure failure
pinia success success
primevue success success
quasar success success
radix-vue success success
router success success
test-utils success success
vant success success
vite-plugin-vue success success
vitepress success success
vue-i18n success success
vue-macros success success
vuetify success success
vueuse success success
vue-simple-compiler success success

Copy link

coderabbitai bot commented May 14, 2025

Walkthrough

The changes update the patch flag analysis in the element transform logic to ensure that ref props inside v-for are not skipped when determining if the NEED_PATCH flag is required. New tests are added to verify that elements with v-for and certain features correctly receive the NEED_PATCH flag.

Changes

File(s) Change Summary
packages/compiler-core/src/transforms/transformElement.ts Refined patch flag analysis: ref props are no longer skipped as constant, ensuring correct patch flag logic. Removed a redundant condition involving shouldUseBlock.
packages/compiler-core/tests/transforms/transformElement.spec.ts Added four new test cases to verify NEED_PATCH flag for elements with v-for and static/bound ref, custom directives, or vnode hooks.

Sequence Diagram(s)

sequenceDiagram
    participant Template
    participant Compiler
    participant PatchFlagAnalyzer

    Template->>Compiler: Template with v-for and ref/custom directive
    Compiler->>PatchFlagAnalyzer: Analyze element props and directives
    PatchFlagAnalyzer-->>Compiler: Detects 'ref' in v-for, sets NEED_PATCH flag
    Compiler-->>Template: Generates code with correct patch flags
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure that ref attributes inside v-for elements trigger correct patching and do not accumulate refs incorrectly (#9239)
Ensure that custom directive unmounted hooks are triggered on v-for elements by setting proper patch flags (#12569)

Suggested labels

ready to merge, :hammer: p3-minor-bug

Poem

A patch for the patch flag, oh what a delight!
Now refs in v-for will behave just right.
Directives and hooks, all get their due,
No more ref arrays endlessly grew!
🐇 With each test that passes, we hop with glee—
The code is now bug-free, as it should be!


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 1f98a9c and 4780e7c.

⛔ Files ignored due to path filters (2)
  • packages/compiler-core/__tests__/transforms/__snapshots__/cacheStatic.spec.ts.snap is excluded by !**/*.snap
  • packages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (2)
  • packages/compiler-core/__tests__/transforms/transformElement.spec.ts (1 hunks)
  • packages/compiler-core/src/transforms/transformElement.ts (1 hunks)
🔇 Additional comments (5)
packages/compiler-core/__tests__/transforms/transformElement.spec.ts (4)

1031-1036: Good addition of test case for vFor + static ref.

This test case validates that elements with v-for directive and a static ref attribute correctly receive the NEED_PATCH flag, which is essential for proper rendering.


1038-1043: Good addition of test case for vFor + custom directives.

This test ensures that when an element uses both v-for and a custom directive, the correct NEED_PATCH flag is applied, which was missing in the previous implementation.


1045-1055: Good addition of test case for vFor + vnode hooks.

This test properly verifies that elements with both v-for and vnode hooks (like @vue:updated) correctly receive the NEED_PATCH flag. The test uses baseCompile with appropriate options, which is the right approach for testing hooks.


1057-1069: Good addition of test case for vFor + setup function ref.

This comprehensive test verifies that elements with v-for and a dynamic :ref bound to a setup function (constant) correctly receive the NEED_PATCH flag. The test properly simulates a real-world scenario with binding metadata configuration.

packages/compiler-core/src/transforms/transformElement.ts (1)

459-465: Fix ensures proper patch flag analysis for refs in v-for context.

The changes here correctly fix the issue by preventing the compiler from skipping ref props during patch flag analysis, even when they have constant values. This ensures that elements with both v-for and refs (especially those bound to setup functions) correctly receive the NEED_PATCH flag.

The updated comment clearly explains the logic: props are skipped only if they are cached handlers or have constant values, with a specific exclusion for :ref="setRefFn" which is a setup-const that still needs to trigger patching.

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🐞 bug Something isn't working ready for review This PR requires more reviews scope: sfc
Projects
None yet
2 participants