8000 fix(compiler-core): prevent root comments from blocking static node hoisting by alex-snezhko · Pull Request #13345 · vuejs/core · GitHub
[go: up one dir, main page]

Skip to content

fix(compiler-core): prevent root comments from blocking static node hoisting #13345

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 statemen 8000 t. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

alex-snezhko
Copy link
@alex-snezhko alex-snezhko commented May 17, 2025

Fixes #13344

Filter out the comment children for the hoisting logic of the root template node, resulting in the non-comment child receiving the hoisted properties.

8000

Summary by CodeRabbit

  • Tests

    • Added a new test to verify correct handling of static prop hoisting when the root contains a single element and comments.
  • Refactor

    • Improved logic to better detect a single element root by ignoring comment nodes, ensuring more accurate processing in templates.

Copy link
coderabbitai bot commented May 17, 2025

Walkthrough

The changes update the logic determining whether a root node has a single element child by introducing a new function that ignores comment nodes. This ensures static prop hoisting works correctly even when comments are present. A corresponding test case is added to verify this behavior, and related internal function usage is updated.

Changes

File(s) Change Summary
packages/compiler-core/src/transforms/cacheStatic.ts Replaced isSingleElementRoot with getSingleElementRoot, which ignores comments and returns the single element node or null. Function signature and return type changed.
packages/compiler-core/src/transform.ts Updated to use getSingleElementRoot in place of isSingleElementRoot for determining single element roots in codegen logic.
packages/compiler-core/tests/transforms/cacheStatic.spec.ts Added a test case to verify static prop hoisting for a root with a single element and a preceding comment node.

Sequence Diagram(s)

sequenceDiagram
    participant RootNode
    participant CacheStaticTransform
    participant getSingleElementRoot
    participant Codegen

    RootNode->>CacheStaticTransform: Initiate static prop hoisting
    CacheStaticTransform->>getSingleElementRoot: Check for single element root (ignoring comments)
    getSingleElementRoot-->>CacheStaticTransform: Return single element node or null
    CacheStaticTransform->>Codegen: Hoist props if single element node found
    Codegen-->>RootNode: Generate code with hoisted props (comments preserved)
Loading

Assessment against linked issues

Objective Addressed Explanation
Ensure fallthrough attributes are applied when root contains a single element and comments (Issue #13344)
Ignore comment nodes when determining single element root for static prop hoisting (Issue #13344)

Poem

A root with a comment, a single child too,
Now props are hoisted, just as they should do.
No more confusion, no fallthrough lost,
A hop and a skip—bug fixed at no cost!
🐇✨

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.

✨ Finishing Touches
  • 📝 Generate Docstrings

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.

❤️ Share
🪧 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.

< 8000 /p>

Copy link
@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Nitpick comments (2)
packages/compiler-core/__tests__/transforms/cacheStatic.spec.ts (1)

546-571: Good test case for verifying the comment node exclusion behavior.

This test verifies that static props hoisting works correctly when the root has both a comment node and a single element. It properly tests that:

  1. A single cache entry is created
  2. The root element's static attributes are hoisted
  3. The comment node is preserved in the output children
  4. The single element is correctly handled despite the comment node's presence

Consider removing the console.log statement on line 569 since it was likely added for debugging purposes and isn't necessary for the test.

-    console.log(generate(root).code)
packages/compiler-core/src/transform.ts (1)

359-374: Logic improved to handle comment nodes in single element roots.

The implementation now correctly retrieves the single element child while excluding comment nodes, which fixes the issue described in #13344 where fallthrough attributes were not being properly applied when comments were present in the root node.

Consider using optional chaining as suggested by the static analysis tool:

-    if (singleElementRootChild && singleElementRootChild.codegenNode) {
+    if (singleElementRootChild?.codegenNode) {

This would reduce code verbosity while maintaining the same functionality.

🧰 Tools
🪛 Biome (1.9.4)

[error] 361-361: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 163b365 and f6ec77a.

⛔ Files ignored due to path filters (1)
  • packages/compiler-core/__tests__/transforms/__snapshots__/cacheStatic.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (3)
  • packages/compiler-core/__tests__/transforms/cacheStatic.spec.ts (1 hunks)
  • packages/compiler-core/src/transform.ts (3 hunks)
  • packages/compiler-core/src/transforms/cacheStatic.ts (1 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/compiler-core/src/transform.ts (1)
packages/compiler-core/src/transforms/cacheStatic.ts (1)
  • getSingleElementRoot (48-57)
packages/compiler-core/src/transforms/cacheStatic.ts (2)
packages/compiler-core/src/ast.ts (4)
  • RootNode (104-121)
  • PlainElementNode (140-149)
  • ComponentNode (151-159)
  • TemplateNode (170-174)
packages/compiler-core/src/utils.ts (1)
  • isSlotOutlet (358-362)
🪛 Biome (1.9.4)
packages/compiler-core/src/transform.ts

[error] 361-361: Change to an optional chain.

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (2)
packages/compiler-core/src/transform.ts (1)

40-40: Updated import aligns with the implementation change.

The import now correctly references the new getSingleElementRoot function instead of the old isSingleElementRoot function.

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

44-44: Updated usage of the new function with proper boolean conversion.

The double negation (!!) properly converts the return value of getSingleElementRoot (which can be an element node or null) to a boolean value, maintaining the expected behavior while using the new implementation.

Comment on lines +48 to 57
export function getSingleElementRoot(
root: RootNode,
child: TemplateChildNode,
): child is PlainElementNode | ComponentNode | TemplateNode {
const { children } = root
return (
children.length === 1 &&
child.type === NodeTypes.ELEMENT &&
!isSlotOutlet(child)
)
): PlainElementNode | ComponentNode | TemplateNode | null {
const children = root.children.filter(x => x.type !== NodeTypes.COMMENT)
return children.length === 1 &&
children[0].type === NodeTypes.ELEMENT &&
!isSlotOutlet(children[0])
? children[0]
: null
}
Copy link

Choose a reason for hiding this comment

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

⚠️ Potential issue

Key implementation change to filter out comment nodes.

This is the core change that fixes issue #13344. The new getSingleElementRoot function:

  1. Filters out comment nodes from the children array
  2. Checks if there's exactly one element child that's not a slot outlet
  3. Returns the actual element node or null (instead of a boolean)

This ensures that static prop hoisting works correctly even when comment nodes are present, which addresses the fallthrough attributes issue in the PR description.

While thoroughly filtering out all comment nodes is correct for this specific use case (hoisting static props), ensure that this doesn't break any other functionality that might rely on comments being processed. The test case appropriately verifies that the comment node is still present in the final output.

The implementation is clean and well-focused on the specific issue being fixed.

🤖 Prompt for AI Agents
In packages/compiler-core/src/transforms/cacheStatic.ts around lines 48 to 57,
update the getSingleElementRoot function to filter out comment nodes from the
root's children before checking if there is exactly one element child that is
not a slot outlet. Return the element node if conditions are met, otherwise
return null. This change ensures static prop hoisting works correctly when
comment nodes are present without affecting other functionality that relies on
comments.

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.3 kB 34.5 kB
vue.global.prod.js 159 kB (+20 B) 58.5 kB (+6 B) 52 kB (+24 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.8 kB 23 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.6 kB 26.4 kB 24.1 kB

Copy link
pkg-pr-new bot commented May 17, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-ssr

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

@vue/compiler-sfc

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: f6ec77a

@edison1105
Copy link
Member

/ecosystem-ci run

8000

@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: compiler 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. labels May 17, 2025
@vue-bot
Copy link
Contributor
vue-bot commented May 17, 2025

📝 Ran ecosystem CI: Open

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

@edison1105 edison1105 changed the title fix(compiler-core): fallthrough attributes with comment in root of child with certain structure (fix #13344) fix(compiler-core): prevent root comments from blocking static node hoisting May 17, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. ready to merge The PR is ready to be merged. scope: compiler
Projects
None yet
3 participants
0