Skip to content

feat(compiler-core): consider component import names for resolving setup component references #13624

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

Conversation

alex-snezhko
Copy link
Contributor

@alex-snezhko alex-snezhko commented Jul 13, 2025

close #9303

If the issue above is not considered a bug then this can be considered a breaking change, though a worthwhile one in my opinion.

Verified that the SFC playground example from the issue works as expected:
Screenshot From 2025-07-12 22-45-02

Summary by CodeRabbit

  • New Features

    • Improved component reference resolution in templates, ensuring correct handling of component names with different casing styles.
    • Enhanced support for distinguishing between components and directives during template compilation.
  • Bug Fixes

    • Fixed issues where components with similar names but different casing could be incorrectly referenced in templates.
  • Tests

    • Added a new test to verify correct component referencing with various casing styles in <script setup>.

Copy link

coderabbitai bot commented Jul 13, 2025

Walkthrough

The changes introduce a new mechanism for tracking "known components" in the Vue compiler's transformation and code generation stages. This includes adding a knownComponents set to transformation contexts, updating component resolution logic to prioritize these known components, and adding a targeted test to verify correct handling of component and function name collisions with different casing.

Changes

File(s) Change Summary
packages/compiler-core/src/codegen.ts Excluded knownComponents from properties inherited by CodegenContext.
packages/compiler-core/src/options.ts Added optional knownComponents?: Set<string> to SharedTransformCodegenOptions.
packages/compiler-core/src/transform.ts Added knownComponents parameter and property to createTransformContext.
packages/compiler-core/src/transforms/transformElement.ts Modified resolveSetupReference to accept isComponent param; updated logic to prefer known components.
packages/compiler-sfc/tests/compileScript.spec.ts Added a test verifying correct component/function name disambiguation with different casing.
packages/compiler-sfc/src/compileScript.ts Tracked known components during script compilation and passed them to template compilation.

Sequence Diagram(s)

sequenceDiagram
    participant SFCCompiler as SFC Compiler
    participant ScriptCompile as compileScript
    participant Transform as createTransformContext
    participant TemplateCompile as compileTemplate

    SFCCompiler->>ScriptCompile: Compile <script setup>
    ScriptCompile->>ScriptCompile: Identify user imports
    ScriptCompile->>ScriptCompile: Add known components to knownComponents set
    ScriptCompile->>TemplateCompile: Pass knownComponents in compilerOptions
    TemplateCompile->>Transform: createTransformContext(knownComponents)
    Transform->>Transform: Provide knownComponents to transform context
    TemplateCompile->>TemplateCompile: Use resolveSetupReference with isComponent flag
    TemplateCompile->>TemplateCompile: Prefer knownComponents for component tags
Loading

Assessment against linked issues

Objective Addressed Explanation
Disambiguate between component tag names and functions with same/different casing (#9303)
Ensure correct rendering of components when both component and function exist (#9303)
Prevent incorrect invocation of functions due to component tag name collision (#9303)

Poem

A bunny hops with nimble feet,
Through code where components and functions meet.
Casing now clear, collisions no more,
Vue's templates render as never before!
With knownComponents in every set,
This rabbit finds no naming threat.
🐇✨

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

Copy link

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB 38.4 kB 34.5 kB
vue.global.prod.js 159 kB (+44 B) 58.5 kB (+15 B) 52 kB (-30 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB 18.2 kB 16.7 kB
createApp 54.5 kB 21.2 kB 19.4 kB
createSSRApp 58.7 kB 22.9 kB 20.9 kB
defineCustomElement 59.5 kB 22.8 kB 20.8 kB
overall 68.5 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented Jul 13, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 5c41ad9

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: 0

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

334-350: Excellent component prioritization logic with minor optimization opportunity.

The implementation correctly prioritizes known components when isComponent is true by checking each name variant against the knownComponents set before falling back to the original binding resolution.

Consider using optional chaining for cleaner code:

-    isKnownComponent:
-      context.knownComponents && context.knownComponents.has(name),
+    isKnownComponent: context.knownComponents?.has(name),
📜 Review details

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

📥 Commits

Reviewing files that changed from the base of the PR and between 347ef1d and 5c41ad9.

⛔ Files ignored due to path filters (1)
  • packages/compiler-sfc/__tests__/__snapshots__/compileScript.spec.ts.snap is excluded by !**/*.snap
📒 Files selected for processing (6)
  • packages/compiler-core/src/codegen.ts (1 hunks)
  • packages/compiler-core/src/options.ts (1 hunks)
  • packages/compiler-core/src/transform.ts (2 hunks)
  • packages/compiler-core/src/transforms/transformElement.ts (3 hunks)
  • packages/compiler-sfc/__tests__/compileScript.spec.ts (1 hunks)
  • packages/compiler-sfc/src/compileScript.ts (2 hunks)
🧰 Additional context used
🧬 Code Graph Analysis (2)
packages/compiler-sfc/__tests__/compileScript.spec.ts (1)
packages/compiler-sfc/__tests__/utils.ts (1)
  • assertCode (27-42)
packages/compiler-core/src/codegen.ts (1)
packages/compiler-core/src/options.ts (1)
  • CodegenOptions (311-353)
🪛 Biome (1.9.4)
packages/compiler-core/src/transforms/transformElement.ts

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

Unsafe fix: Change to an optional chain.

(lint/complexity/useOptionalChain)

⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (3)
  • GitHub Check: Redirect rules
  • GitHub Check: Header rules
  • GitHub Check: Pages changed
🔇 Additional comments (10)
packages/compiler-core/src/options.ts (1)

202-207: LGTM! Clean interface addition with clear documentation.

The new knownComponents property is well-designed with appropriate typing and clear documentation explaining its purpose for component reference disambiguation.

packages/compiler-core/src/codegen.ts (1)

123-126: LGTM! Consistent exclusion of optional property.

The exclusion of knownComponents from the Required<CodegenOptions> type follows the established pattern for other optional properties like bindingMetadata and inline.

packages/compiler-sfc/__tests__/compileScript.spec.ts (1)

409-433: LGTM! Comprehensive test case for component casing disambiguation.

This test effectively validates the new feature by:

  • Testing different casing styles (PascalCase, camelCase) for imported components
  • Including conflicting local variables with similar names but different casing
  • Verifying correct component resolution in the generated _createVNode calls
  • Following established testing patterns with assertCode() validation

The test provides good coverage for the component reference disambiguation functionality.

packages/compiler-core/src/transform.ts (2)

146-146: LGTM! Clean parameter addition with backward compatibility.

The addition of the knownComponents parameter with a default empty Set<string>() maintains backward compatibility while enabling the new component disambiguation feature.


175-175: LGTM! Proper propagation to transform context.

The knownComponents property is correctly propagated to the TransformContext object, following the established pattern for other options.

packages/compiler-sfc/src/compileScript.ts (2)

729-744: LGTM! Well-structured known components tracking.

The logic correctly identifies known Vue components from user imports by checking for namespace imports, default imports from .vue files, and Vue package imports. The binding metadata assignment is also appropriate - known components get SETUP_CONST while potentially reactive imports get SETUP_MAYBE_REF.


888-888: Good integration with template compiler.

Passing the knownComponents set to the template compiler options enables the enhanced component resolution in the template transformation phase.

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

322-326: Function signature enhancement enables component prioritization.

The addition of the isComponent parameter allows the function to differentiate between component and directive resolution contexts, enabling more precise binding resolution.


289-289: Appropriate component resolution context.

Correctly passing true for isComponent in component resolution scenarios enables the new prioritization logic for known components.

Also applies to: 295-295


899-899: Correct directive resolution context.

Passing false for isComponent in directive resolution maintains the original behavior since directives shouldn't prioritize component bindings.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Incorrectly treating the component tag name as a function with the same name and unexpected calling the function twice
1 participant