-
-
Notifications
You must be signed in to change notification settings - Fork 8.8k
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
base: main
Are you sure you want to change the base?
Conversation
… argument of defineCustomElement
WalkthroughAdds 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
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
Estimated code review effort🎯 2 (Simple) | ⏱️ ~10 minutes Poem
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 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: |
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