fix(compiler-sfc): properly analyze patch flag of vFor + (ref or vnodeHook or dir)
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 https://github.com/vuejs/core/commit/1c9a4810fcdd2b6c1c6c3be077aebbecbfcbcf1e
playground playground with this PR
The following problems will be fixed
- vFor + ref / with this PR
- vFor + ref(function) / with this PR
- vFor + vnode hook / with this PR
- vFor + custom directive / with this PR
- #12569
The last two are from https://github.com/vuejs/core/pull/9240#discussion_r1348179952 created by skirtles-code
Summary by CodeRabbit
- Bug Fixes
- Improved detection of elements requiring the
NEED_PATCHflag when usingv-forwith static or setup-boundref, custom directives, or vnode hooks.
- Improved detection of elements requiring the
- Tests
- Added new test cases to ensure correct patch flag assignment for elements with
v-forcombined with various features.
- Added new test cases to ensure correct patch flag assignment for elements with
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 |
@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
/ecosystem-ci run
π Ran ecosystem CI: Open
| suite | result | latest scheduled |
|---|---|---|
| language-tools | :x: failure | :x: failure |
| nuxt | :x: failure | :x: failure |
| pinia | :white_check_mark: success | :white_check_mark: success |
| primevue | :white_check_mark: success | :white_check_mark: success |
| quasar | :white_check_mark: success | :white_check_mark: success |
| radix-vue | :white_check_mark: success | :white_check_mark: success |
| router | :white_check_mark: success | :white_check_mark: success |
| test-utils | :white_check_mark: success | :white_check_mark: success |
| vant | :white_check_mark: success | :white_check_mark: success |
| vite-plugin-vue | :white_check_mark: success | :white_check_mark: success |
| vitepress | :white_check_mark: success | :white_check_mark: success |
| vue-i18n | :white_check_mark: success | :white_check_mark: success |
| vue-macros | :white_check_mark: success | :white_check_mark: success |
| vuetify | :white_check_mark: success | :white_check_mark: success |
| vueuse | :white_check_mark: success | :white_check_mark: success |
| vue-simple-compiler | :white_check_mark: success | :white_check_mark: success |
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
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 1f98a9c493d01c21befa90107f0593bc92a58932 and 4780e7c64dc9befa7208bdff98bfc1524530b3c8.
β Files ignored due to path filters (2)
packages/compiler-core/__tests__/transforms/__snapshots__/cacheStatic.spec.ts.snapis excluded by!**/*.snappackages/compiler-core/__tests__/transforms/__snapshots__/transformText.spec.ts.snapis 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-fordirective and a staticrefattribute correctly receive theNEED_PATCHflag, 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-forand a custom directive, the correctNEED_PATCHflag 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-forand vnode hooks (like@vue:updated) correctly receive theNEED_PATCHflag. The test usesbaseCompilewith 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-forand a dynamic:refbound to a setup function (constant) correctly receive theNEED_PATCHflag. 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
refprops during patch flag analysis, even when they have constant values. This ensures that elements with bothv-forand refs (especially those bound to setup functions) correctly receive theNEED_PATCHflag.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
@coderabbitaiin a new review comment at the desired location with your query. Examples:@coderabbitai explain this code block.@coderabbitai modularize this function.
- PR comments: Tag
@coderabbitaiin 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 pauseto pause the reviews on a PR.@coderabbitai resumeto resume the paused reviews.@coderabbitai reviewto trigger an incremental review. This is useful when automatic reviews are disabled for the repository.@coderabbitai full reviewto do a full review from scratch and review all the files again.@coderabbitai summaryto regenerate the summary of the PR.@coderabbitai generate docstringsto generate docstrings for this PR.@coderabbitai generate sequence diagramto generate a sequence diagram of the changes in this PR.@coderabbitai resolveresolve all the CodeRabbit review comments.@coderabbitai configurationto show the current CodeRabbit configuration for the repository.@coderabbitai helpto get help.
Other keywords and placeholders
- Add
@coderabbitai ignoreanywhere in the PR description to prevent this PR from being reviewed. - Add
@coderabbitai summaryto generate the high-level summary at a specific location in the PR description. - Add
@coderabbitaianywhere in the PR title to generate the title automatically.
CodeRabbit Configuration File (.coderabbit.yaml)
- You can programmatically configure CodeRabbit by adding a
.coderabbit.yamlfile 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.