Skip to content

fix(compiler-core): ignore whitespace when matching adjacent v-if #12321

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

Merged
merged 3 commits into from
Jun 5, 2025

Conversation

edison1105
Copy link
Member

@edison1105 edison1105 commented Nov 4, 2024

close #9173

Summary by CodeRabbit

  • Bug Fixes

    • Improved detection of adjacent v-if directives for named slots with v-else by ignoring whitespace-only nodes, ensuring more accurate slot transformation.
  • Tests

    • Added a new test case for named slots using v-if and v-else directives to verify correct behavior.

Copy link

github-actions bot commented Nov 4, 2024

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 (+12 B) 58.5 kB (+8 B) 52.1 kB (+40 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.6 kB 18.2 kB 16.6 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 Nov 4, 2024

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: bbaa005

@edison1105 edison1105 added ready to merge The PR is ready to be merged. scope: compiler 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. labels Nov 4, 2024
@vidal7
Copy link

vidal7 commented Jan 23, 2025

Hello. Do you think this PR can be merged soon? I have the issue that this PR is fixing when migrating large Vue 2 repo to Vue 3. I want to preserve the behavior of whitespace config of Vue 2 on Vue 3

@edison1105 edison1105 added 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. and removed 🍰 p2-nice-to-have Priority 2: this is not breaking anything but nice to have it addressed. labels Feb 18, 2025
@vidal7
Copy link

vidal7 commented May 12, 2025

No news? It is still preventing us migrating our large repo to Vue 3 without having visual regressions.

@vidal7
Copy link

vidal7 commented May 23, 2025

@edison1105, is this something preventing you for submiting this PR to reviewers? I would very like this PR being merged.

@edison1105 edison1105 changed the title fix(compiler-core): handle named slot + v-if with preserve whitespace fix(compiler-core): ignore whitespace when matching adjacent v-if Jun 4, 2025
Copy link

coderabbitai bot commented Jun 4, 2025

Walkthrough

A new test was added to verify correct handling of named slots with v-if and v-else directives when whitespace is preserved. The slot transformation logic was updated to ensure the compiler skips over whitespace-only nodes (in addition to comments) when searching for an adjacent v-if before an v-else slot.

Changes

File(s) Change Summary
packages/compiler-core/__tests__/transforms/vSlot.spec.ts Added test for named slots with v-if and v-else using whitespace: 'preserve'.
packages/compiler-core/src/transforms/vSlot.ts Refined loop to skip whitespace-only nodes (and comments) when searching for adjacent v-if.

Sequence Diagram(s)

sequenceDiagram
    participant Parser
    participant Transformer
    participant SlotNode

    Parser->>Transformer: Parse template with v-if/v-else slots (whitespace: 'preserve')
    Transformer->>SlotNode: Iterate backwards to find adjacent v-if for v-else slot
    Note right of Transformer: Skips comments and whitespace-only nodes
    Transformer->>SlotNode: Associates v-else slot with correct v-if slot
Loading

Assessment against linked issues

Objective Addressed Explanation
Fix compiler error when using v-slot with v-if/v-else(-if) and whitespace: "preserve" (#9173)

Assessment against linked issues: Out-of-scope changes

No out-of-scope changes found.

Poem

In the garden of slots, where whitespace grew,
The v-if and v-else now know what to do.
Skipping blank leaves and commenty dew,
The compiler hops on, its logic anew.
🐇✨ No errors in sight, just templates that flow—
The rabbit approves, with a satisfied glow!


📜 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 e53a4ff and 5cc3a7a.

⛔ Files ignored due to path filters (1)
  • packages/compiler-core/__tests__/transforms/__snapshots__/vSlot.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (2)
  • packages/compiler-core/__tests__/transforms/vSlot.spec.ts (1 hunks)
  • packages/compiler-core/src/transforms/vSlot.ts (1 hunks)
⏰ 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/transforms/vSlot.ts (1)

225-225: LGTM! Correctly implements whitespace-aware adjacent v-if detection.

This change properly extends the logic to skip over whitespace-only nodes (in addition to comments) when searching for adjacent v-if directives before v-else(-if) slots. The modification ensures that the compiler preserves Vue 2's whitespace handling behavior during migration to Vue 3, which addresses the core issue described in the PR.

The use of isNonWhitespaceContent(prev) is appropriate as it correctly identifies nodes that contain meaningful content versus whitespace-only text nodes.

packages/compiler-core/__tests__/transforms/vSlot.spec.ts (1)

992-1004: Excellent test coverage for the whitespace-aware v-if detection.

This test case effectively validates the core functionality addressed by this PR:

  • Comprehensive scenario: Tests named slots with v-if + v-else directives separated by whitespace
  • Critical options: Uses whitespace: 'preserve' which is essential for reproducing the Vue 2 migration issue
  • Proper validation: Snapshot testing ensures consistent code generation behavior

The test directly covers the edge case where whitespace between conditional slot templates could break adjacent directive detection, which was the root cause of the migration blocker described in the PR comments.

✨ 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.

@edison1105 edison1105 merged commit 10ebcef into main Jun 5, 2025
16 checks passed
@edison1105 edison1105 deleted the edison/fix/9173 branch June 5, 2025 01:44
@vidal7
Copy link

vidal7 commented Jun 5, 2025

Very much appreciated @edison1105. Thanks!

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
Development

Successfully merging this pull request may close these issues.

vue-template-compiler: whitespace "preserve" option causes compiler error when using v-slot with v-if/v-else-if/v-else
2 participants