Repository navigation
fix(keep-alive): ignore comment nodes - #6026
amirkian007 wants to merge 12 commits into
Conversation
node children should not be overwritten to keep comments in the build output
❌ Deploy Preview for vuejs-coverage failed.
|
|
@tony19 hi can you re-review my new changes please ? the re-request review button not working... |
tony19
left a comment
There was a problem hiding this comment.
LGTM. Thanks for the updates!
|
Comments cannot be ignored directly |
|
comments need final rendering on dom |
yeah comments should be rendered on the dom and the same problem exists for other built-in components too like Transition ,Suspense and etc . i dont know if it is a bug or not. but if it is, i think the fix for that should be submitted in another pr , this one is to just fix the warning issue with the compiler |
WalkthroughFiltering of COMMENT nodes was added to KeepAlive handling across compiler and runtime: the compiler validates using non-comment children and emits a DEV error for >1; runtime selects the first non-comment child. A new helper to locate the first non-comment child was added to BaseTransition and tests were added/updated. Changes
Sequence Diagram(s)sequenceDiagram
participant DevTemplate as Template Source
participant Compiler as Compiler Transform
participant Validator as KEEP_ALIVE Validator
participant Runtime as KeepAlive Runtime
participant BaseTrans as BaseTransition Helper
participant DOM as Rendered Output
DevTemplate->>Compiler: Parse <KeepAlive> with children
Compiler->>Validator: Provide children list
Validator->>Validator: Filter out COMMENT nodes
alt >1 non-comment child (DEV)
Validator-->>Compiler: Emit DEV error (X_KEEP_ALIVE_INVALID_CHILDREN)
Compiler-->>DOM: Report validation error
else <=1 non-comment child
Validator-->>Compiler: Valid
Compiler->>Runtime: Compile node (children preserved)
Runtime->>BaseTrans: locateFirstNonCommentChild(children, warnMsg)
alt multiple non-comment found
BaseTrans-->>Runtime: return first non-comment (hasFound=true) + warn
else single or none
BaseTrans-->>Runtime: return child or none (hasFound=false)
end
Runtime->>DOM: Render selected child or none
end
Estimated code review effort🎯 4 (Complex) | ⏱️ ~45 minutes
Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/compiler-core/src/transforms/transformElement.ts (1)
147-169: LGTM! Comment filtering for KeepAlive is correctly implemented.The compiler correctly filters comment nodes when processing
<KeepAlive>and uses the filtered children for validation and error reporting. The validation only triggers in__DEV__mode when multiple non-comment children are detected.Minor: The comment on line 158 could be more precise:
- //filter out potential comment nodes + // Filter out comment nodes for validation
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
⛔ Files ignored due to path filters (1)
packages/compiler-core/__tests__/transforms/__snapshots__/transformElement.spec.ts.snapis excluded by!**/*.snap
📒 Files selected for processing (3)
packages/compiler-core/__tests__/transforms/transformElement.spec.ts(1 hunks)packages/compiler-core/src/transforms/transformElement.ts(3 hunks)packages/runtime-core/src/components/KeepAlive.ts(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
packages/runtime-core/src/components/KeepAlive.ts (2)
packages/runtime-core/src/vnode.ts (1)
Comment(70-70)packages/runtime-core/src/index.ts (1)
Comment(113-113)
packages/compiler-core/src/transforms/transformElement.ts (1)
packages/compiler-core/src/errors.ts (1)
createCompilerError(24-38)
🔇 Additional comments (8)
packages/compiler-core/src/transforms/transformElement.ts (1)
185-186: Single-child optimization correctly uses filtered children.The single-child path now correctly uses the
childrenvariable (filtered forKEEP_ALIVE) instead ofnode.children, ensuring comments don't prevent the optimization.packages/compiler-core/__tests__/transforms/transformElement.spec.ts (5)
1386-1401: Well-designed test helper.The
checkWarninghelper correctly captures compiler errors via theonErrorcallback and validates warning behavior based on theshouldWarnflag.
1403-1410: LGTM!Test correctly verifies that
<KeepAlive>with a single child does not trigger a warning.
1412-1421: LGTM! Critical test for the PR fix.This test correctly verifies that
<KeepAlive>with one component child and multiple comments does not trigger a warning, confirming that comments are properly ignored during validation.
1423-1443: LGTM! Negative test cases are correctly implemented.These tests verify that:
- Multiple component children correctly trigger a warning (lines 1423-1431)
- Comments don't suppress warnings when multiple component children are present (lines 1433-1443)
1445-1456: LGTM! Snapshot test provides regression protection.The snapshot test verifies that the generated code correctly handles comments within
<KeepAlive>, ensuring stable output and preventing future regressions.packages/runtime-core/src/components/KeepAlive.ts (2)
103-108: Verify if SSR comment handling in KeepAlive causes hydration issues.The SSR path (lines 103-108) returns all children without filtering comments, while the client-side render function explicitly filters out comments when selecting the cached component (lines 280-287). No SSR-specific tests exist for KeepAlive with comment children.
This difference may be intentional (SSR is a simple pass-through since caching doesn't apply), but it's unclear whether returning comments in the SSR output causes hydration mismatches or other issues when the client-side rendering filters them. Please verify:
- Whether SSR should also filter comments for consistency
- If this behavior has been tested or documented
- Whether excluding this filtering causes any hydration or rendering issues in practice
278-292: Verify the all-comments edge case handling is intentional.The code at lines 289-292 returns the
childrenarray when all children are comments, while the single-child path at lines 299-300 returnsrawVNodedirectly. This inconsistency lacks test coverage—no runtime test validates the all-comments scenario. Confirm this behavior is correct or consider returningnullor the first comment vnode to align with the single-child pattern.
…in transformElement
… child into a reusable function
There was a problem hiding this comment.
Actionable comments posted: 0
🧹 Nitpick comments (1)
packages/runtime-core/src/components/BaseTransition.ts (1)
273-305: Comment-aware child selection helper looks correct; consider clarifying empty / no-match semanticsThe helper correctly:
- Selects the first non-comment child in both DEV and prod.
- Emits the existing
<transition>multi-child warning only when there are 2+ non-comment children, ignoring pure comments.- Keeps prior behavior for single-child and all-comment arrays via the default
child = children[0].Minor, optional API nits:
locateFirstNonCommentChildassumeschildren.length > 0(viachildren[0]). That’s true for all current call sites here (and, per the PR summary, likely for KeepAlive’s usage in multi-child paths), but it might be worth documenting this invariant in a short comment or asserting early in DEV to prevent accidental misuse.- Since the function returns
{ child, hasFound }, callers should treathasFoundas the authoritative flag for “found a non-comment vnode”. To avoid future misuse, you could either (a) make the return typechild: VNode | undefinedwhenhasFoundis false, or (b) add a brief JSDoc noting thatchildmay still be a comment (or undefined ifchildrenis empty) whenhasFoundis false.Overall, the change cleanly centralizes the "ignore comment children but still warn on multiple real children" behavior and looks good.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/compiler-core/src/transforms/transformElement.ts(2 hunks)packages/runtime-core/src/components/BaseTransition.ts(1 hunks)packages/runtime-core/src/components/KeepAlive.ts(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (2)
- packages/compiler-core/src/transforms/transformElement.ts
- packages/runtime-core/src/components/KeepAlive.ts
🧰 Additional context used
🧬 Code graph analysis (1)
packages/runtime-core/src/components/BaseTransition.ts (1)
packages/runtime-core/src/vnode.ts (2)
VNode(160-257)Comment(70-70)
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
vue
@vue/compat
commit: |
Size ReportBundles
Usages
|
|
@edison1105 |
KeepAlive does not ignore comments and the compiler throws an error when a component and a comment are wrapped with KeepAlive, which is obviously a bug.
error reproduction :
sfc.vuejs.org
thanks.
Summary by CodeRabbit
Bug Fixes
Tests
✏️ Tip: You can customize this high-level summary in your review settings.