Skip to content

fix(custom-element): prevent defineCustomElement from mutating the options object #13791

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

LeeSamFong
Copy link

@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 defineCustomElement does not alter the original options object.

Copy link

coderabbitai bot commented Aug 21, 2025

Walkthrough

Adds a shallow-clone safeguard for the options argument in defineCustomElement to avoid mutating the input object, and introduces a unit test confirming that the original component options remain unchanged.

Changes

Cohort / File(s) Summary
Custom Element options immutability test
packages/runtime-dom/__tests__/customElement.spec.ts
New test ensures defineCustomElement does not mutate the passed component options object.
Custom Element options cloning in runtime
packages/runtime-dom/src/apiCustomElement.ts
Adds shallow cloning of plain-object options via extend({}, options) before defineComponent to prevent side effects during processing.

Sequence Diagram(s)

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

  User->>API: defineCustomElement(FooOptions, userOptions)
  alt userOptions is plain object
    API->>Util: clone = extend({}, userOptions)
    API->>Vue: defineComponent(clone + extraOptions)
  else not a plain object
    API->>Vue: defineComponent(userOptions)
  end
  API-->>User: VueCustomElement class
  note over API,Vue: Input options object is not mutated
Loading

Estimated code review effort

🎯 2 (Simple) | ⏱️ ~10 minutes

Poem

I twitch my nose at mutable woes,
A gentle clone, and off it goes.
Options safe, no hidden shove—
Custom elements fit like a glove.
Thump-thump! I ship with glee,
Carrot commits: side-effect free. 🥕

Tip

🔌 Remote MCP (Model Context Protocol) integration is now available!

Pro plan users can now connect to remote MCP servers from the Integrations page. Connect with popular remote MCPs such as Notion and Linear to add more context to your reviews and chats.

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

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 101 kB (+18 B) 38.4 kB (+7 B) 34.6 kB (+20 B)
vue.global.prod.js 159 kB (+18 B) 58.5 kB (+7 B) 52.2 kB (+58 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 (+26 B) 22.8 kB (+17 B) 20.8 kB (+16 B)
overall 68.5 kB 26.4 kB 24.1 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: 436866d

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.

2 participants