Skip to content

Conversation

LeeSamFong
Copy link
Contributor

@LeeSamFong LeeSamFong commented Aug 21, 2025

This PR is to fix the problem that the 2nd argument will accidentally mutate the 1st argument of defineCustomElement(), which will cause exceptions in the following scenarios:
https://codesandbox.io/p/devbox/test-jwvgql?embed=1&file=%2Fsrc%2Fmain.ts

Summary by CodeRabbit

  • Bug Fixes
    • Prevents mutation of component option objects when defining custom elements, ensuring passed options remain unchanged and reducing unintended side effects. No API changes.
  • Tests
    • Added a unit test validating that defining custom elements does not alter the original options object.

Copy link

coderabbitai bot commented Aug 21, 2025

Walkthrough

Reworks defineCustomElement to avoid mutating the original component options by reassigning a shallow-extended copy to the local Comp variable; adds a unit test asserting the original component options object is unchanged after calling defineCustomElement.

Changes

Cohort / File(s) Summary
Custom element immutability test
packages/runtime-dom/__tests__/customElement.spec.ts
New unit test verifying defineCustomElement(Foo, { shadowRoot: false }) does not mutate the original Foo options object.
Prevent in-place mutation of Comp in runtime
packages/runtime-dom/src/apiCustomElement.ts
Change Comp declaration from const to let; when defineComponent(options, extraOptions) returns a plain object, reassign Comp = extend({}, Comp, extraOptions) instead of calling extend(Comp, extraOptions), preventing side‑effects on the original Comp object.

Sequence Diagram(s)

sequenceDiagram
  participant User as User Code
  participant API as defineCustomElement
  participant Define as defineComponent
  participant Util as extend(...)

  User->>API: defineCustomElement(Comp, extraOptions)
  API->>Define: defineComponent(Comp, extraOptions)
  alt defineComponent returns plain object
    API->>Util: newComp = extend({}, Comp, extraOptions)
    API-->>API: Comp = newComp (reassigned)
  else defineComponent returns class/other
    API-->>API: Comp unchanged (no in-place extend)
  end
  API-->>User: VueCustomElement class (uses local Comp)
  note right of API: original Comp object passed by user is not mutated
Loading

Estimated code review effort

🎯 3 (Moderate) | ⏱️ ~20 minutes

Poem

I nibble code with careful paws,
No sneaky change, no hidden laws.
A copy made soft, the original kept—
No mutated secrets while I’ve leapt.
Thump! A clean commit, and then I hop. 🐇


📜 Recent review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between 436866d and 1be15af.

📒 Files selected for processing (1)
  • packages/runtime-dom/src/apiCustomElement.ts (1 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • packages/runtime-dom/src/apiCustomElement.ts
✨ Finishing Touches
  • 📝 Generate Docstrings
🧪 Generate unit tests
  • Create PR with unit tests
  • Post copyable unit tests in a comment

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.
    • 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.
  • 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 the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

CodeRabbit Commands (Invoked using PR/Issue comments)

Type @coderabbitai help to get the list of available commands.

Other keywords and placeholders

  • Add @coderabbitai ignore or @coderabbit 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

Status, Documentation and Community

  • Visit our Status Page to check the current availability of CodeRabbit.
  • 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

@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 (2)
packages/runtime-dom/src/apiCustomElement.ts (1)

175-175: Shallow-clone to prevent unintended mutation of caller’s options (good, targeted fix).

Cloning only when options is a plain object neatly avoids mutating the original input when extraOptions are merged later via extend(Comp, extraOptions). Scope and runtime behavior of existing overloads remain intact.

To make the rationale future-proof and prevent accidental removal, consider a short inline comment:

-  if (isPlainObject(options)) options = extend({}, options)
+  // Avoid mutating user-supplied options when extraOptions are merged (#13791).
+  if (isPlainObject(options)) options = extend({}, options)
packages/runtime-dom/__tests__/customElement.spec.ts (1)

1777-1788: Optionally harden the test by asserting separation of identities and effective merge.

Capturing the return value lets us verify that:

  • the runtime def used by the CE is not the same object as the caller’s options, and
  • extraOptions took effect on the returned definition.

Apply this diff within the test:

-    defineCustomElement(Foo, { shadowRoot: false })
+    const E = defineCustomElement(Foo, { shadowRoot: false })
+
+    // Returned def should not alias the caller's options
+    expect((E as any).def).not.toBe(Foo)
+    // And extraOptions should be present on the runtime def
+    expect((E as any).def.shadowRoot).toBe(false)
📜 Review details

Configuration used: CodeRabbit UI

Review profile: CHILL

Plan: Pro

💡 Knowledge Base configuration:

  • MCP integration is disabled by default for public repositories
  • Jira integration is disabled by default for public repositories
  • Linear integration is disabled by default for public repositories

You can enable these sources in your CodeRabbit configuration.

📥 Commits

Reviewing files that changed from the base of the PR and between a48ffda and 436866d.

📒 Files selected for processing (2)
  • packages/runtime-dom/__tests__/customElement.spec.ts (1 hunks)
  • packages/runtime-dom/src/apiCustomElement.ts (1 hunks)
🧰 Additional context used
🧬 Code graph analysis (2)
packages/runtime-dom/__tests__/customElement.spec.ts (2)
packages/runtime-dom/src/apiCustomElement.ts (1)
  • defineCustomElement (167-186)
packages/runtime-dom/src/index.ts (1)
  • defineCustomElement (255-255)
packages/runtime-dom/src/apiCustomElement.ts (1)
packages/shared/src/general.ts (2)
  • isPlainObject (74-75)
  • extend (24-24)
⏰ 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 (1)
packages/runtime-dom/__tests__/customElement.spec.ts (1)

1777-1788: Regression test covers the mutation bug succinctly.

This asserts the original options object stays untouched when passing extraOptions. Clear and low-noise.

@LeeSamFong LeeSamFong changed the title fix(custom-element): the 2nd argument can unexpectedly mutate the 1st argument of defineCustomElement fix(custom-element): the 2nd argument can unexpected mutation the 1st argument of defineCustomElement Aug 21, 2025
@edison1105 edison1105 changed the title fix(custom-element): the 2nd argument can unexpected mutation the 1st argument of defineCustomElement fix(custom-element): prevent defineCustomElement from mutating the options object Aug 22, 2025
@edison1105 edison1105 added ready to merge The PR is ready to be merged. 🔨 p3-minor-bug Priority 3: this fixes a bug, but is an edge case that only affects very specific usage. scope: custom elements labels Aug 22, 2025
Copy link

github-actions bot commented Aug 22, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+7 B) 38.4 kB (+3 B) 34.6 kB (+31 B)
vue.global.prod.js 159 kB (+7 B) 58.5 kB (+3 B) 52.1 kB (+34 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 (+7 B) 22.8 kB (+2 B) 20.8 kB (+8 B)
overall 68.5 kB 26.4 kB 24 kB

Copy link

pkg-pr-new bot commented Aug 22, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: 1be15af

@edison1105
Copy link
Member

/ecosystem-ci run

@vue-bot
Copy link
Contributor

vue-bot commented Sep 2, 2025

📝 Ran ecosystem CI: Open

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

@edison1105 edison1105 merged commit e322436 into vuejs:main Sep 2, 2025
18 of 19 checks passed
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: custom elements
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants