-
-
Notifications
You must be signed in to change notification settings - Fork 8.9k
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
Conversation
… argument of defineCustomElement
WalkthroughReworks 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
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
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~20 minutes Poem
📜 Recent review detailsConfiguration used: CodeRabbit UI Review profile: CHILL Plan: Pro 💡 Knowledge Base configuration:
You can enable these sources in your CodeRabbit configuration. 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
✨ Finishing Touches
🧪 Generate unit tests
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. 🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed help? Create a ticket on our support page for assistance with any issues or questions. CodeRabbit Commands (Invoked using PR/Issue comments)Type Other keywords and placeholders
CodeRabbit Configuration File (
|
There was a problem hiding this 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 whenextraOptions
are merged later viaextend(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, andextraOptions
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.
📒 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.
Size ReportBundles
Usages
|
@vue/compiler-core
@vue/compiler-dom
@vue/compiler-sfc
@vue/compiler-ssr
@vue/reactivity
@vue/runtime-core
@vue/runtime-dom
@vue/server-renderer
@vue/shared
vue
@vue/compat
commit: |
/ecosystem-ci run |
📝 Ran ecosystem CI: Open
|
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