-
-
Notifications
You must be signed in to change notification settings - Fork 8.6k
refactor: replace inline type checks with shared utils #13273
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
WalkthroughThis update introduces a new utility function, Changes
Sequence Diagram(s)sequenceDiagram
participant Caller
participant Utility as Shared Utilities
participant Runtime as Runtime Code
Caller->>Runtime: Perform a nullish or type check
Runtime->>Utility: Call isNullish/isNumber/isBoolean(val)
Utility-->>Runtime: Return boolean result
Runtime-->>Caller: Continue logic based on result
Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
SupportNeed 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)
Other keywords and placeholders
CodeRabbit Configuration File (
|
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: |
Thanks for the PR, but... |
Thank you so much for pointing out the previous discussions, it turns out this has come up more often than I expected. I’m really sorry for the added noise and trouble. I agree that the practical benefit of this refactor might not be significant, and I’ve been quite conflicted myself about whether it’s truly necessary. But based on past responses, I was wondering if narrowing the scope to just I truly appreciate your time and feedback. If after consideration you still feel this isn’t worthwhile, I’ll go ahead and close the PR. |
29e9506
to
ee18800
Compare
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: 1
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (35)
packages/runtime-core/src/apiAsyncComponent.ts
(2 hunks)packages/runtime-core/src/apiCreateApp.ts
(2 hunks)packages/runtime-core/src/apiInject.ts
(2 hunks)packages/runtime-core/src/component.ts
(2 hunks)packages/runtime-core/src/componentOptions.ts
(2 hunks)packages/runtime-core/src/componentProps.ts
(5 hunks)packages/runtime-core/src/componentPublicInstance.ts
(2 hunks)packages/runtime-core/src/componentSlots.ts
(3 hunks)packages/runtime-core/src/components/BaseTransition.ts
(3 hunks)packages/runtime-core/src/components/KeepAlive.ts
(3 hunks)packages/runtime-core/src/components/Suspense.ts
(4 hunks)packages/runtime-core/src/components/Teleport.ts
(3 hunks)packages/runtime-core/src/customFormatter.ts
(2 hunks)packages/runtime-core/src/devtools.ts
(2 hunks)packages/runtime-core/src/helpers/renderList.ts
(2 hunks)packages/runtime-core/src/hydration.ts
(5 hunks)packages/runtime-core/src/renderer.ts
(14 hunks)packages/runtime-core/src/rendererTemplateRef.ts
(2 hunks)packages/runtime-core/src/scheduler.ts
(2 hunks)packages/runtime-core/src/vnode.ts
(4 hunks)packages/runtime-core/src/warning.ts
(3 hunks)packages/runtime-dom/src/apiCustomElement.ts
(2 hunks)packages/runtime-dom/src/components/Transition.ts
(3 hunks)packages/runtime-dom/src/components/TransitionGroup.ts
(2 hunks)packages/runtime-dom/src/directives/vModel.ts
(3 hunks)packages/runtime-dom/src/index.ts
(1 hunks)packages/runtime-dom/src/modules/attrs.ts
(3 hunks)packages/runtime-dom/src/modules/class.ts
(2 hunks)packages/runtime-dom/src/modules/props.ts
(4 hunks)packages/runtime-dom/src/modules/style.ts
(3 hunks)packages/runtime-dom/src/nodeOps.ts
(2 hunks)packages/shared/src/domAttrConfig.ts
(2 hunks)packages/shared/src/general.ts
(2 hunks)packages/shared/src/normalizeProp.ts
(2 hunks)packages/shared/src/toDisplayString.ts
(2 hunks)
🚧 Files skipped from review as they are similar to previous changes (34)
- packages/runtime-core/src/rendererTemplateRef.ts
- packages/runtime-core/src/apiInject.ts
- packages/runtime-core/src/devtools.ts
- packages/runtime-dom/src/modules/class.ts
- packages/runtime-core/src/customFormatter.ts
- packages/runtime-dom/src/apiCustomElement.ts
- packages/runtime-core/src/componentPublicInstance.ts
- packages/runtime-dom/src/components/TransitionGroup.ts
- packages/shared/src/toDisplayString.ts
- packages/runtime-dom/src/nodeOps.ts
- packages/runtime-dom/src/directives/vModel.ts
- packages/runtime-core/src/apiAsyncComponent.ts
- packages/shared/src/domAttrConfig.ts
- packages/runtime-core/src/component.ts
- packages/shared/src/normalizeProp.ts
- packages/runtime-dom/src/modules/attrs.ts
- packages/runtime-dom/src/modules/style.ts
- packages/runtime-core/src/helpers/renderList.ts
- packages/runtime-core/src/components/KeepAlive.ts
- packages/runtime-core/src/apiCreateApp.ts
- packages/runtime-dom/src/modules/props.ts
- packages/runtime-core/src/components/Teleport.ts
- packages/runtime-core/src/componentOptions.ts
- packages/runtime-core/src/scheduler.ts
- packages/shared/src/general.ts
- packages/runtime-core/src/components/BaseTransition.ts
- packages/runtime-core/src/vnode.ts
- packages/runtime-core/src/componentSlots.ts
- packages/runtime-core/src/components/Suspense.ts
- packages/runtime-core/src/componentProps.ts
- packages/runtime-dom/src/index.ts
- packages/runtime-core/src/warning.ts
- packages/runtime-core/src/hydration.ts
- packages/runtime-dom/src/components/Transition.ts
🧰 Additional context used
🧬 Code Graph Analysis (1)
packages/runtime-core/src/renderer.ts (3)
packages/shared/src/general.ts (1)
isNullish
(59-59)packages/runtime-core/src/rendererTemplateRef.ts (1)
setRef
(25-166)packages/reactivity/src/effect.ts (2)
pauseTracking
(519-522)resetTracking
(535-538)
🪛 Biome (1.9.4)
packages/runtime-core/src/renderer.ts
[error] 487-487: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
⏰ Context from checks skipped due to timeout of 90000ms (4)
- GitHub Check: Redirect rules
- GitHub Check: Header rules
- GitHub Check: Pages changed
- GitHub Check: test / unit-test-windows
🔇 Additional comments (16)
packages/runtime-core/src/renderer.ts (16)
40-40
: Importing the newisNullish
utility functionThe addition of the
isNullish
import from@vue/shared
supports the refactoring effort to standardize type checking across the codebase.
410-415
: Standardized null check for mounting static nodesReplacing the direct nullish check with the
isNullish
utility function maintains the same behavior while improving code consistency.
492-504
: Standardized null check in processText functionThe null check for the n1 parameter has been refactored to use the
isNullish
utility function, maintaining the same behavior when deciding whether to create a new text node or update an existing one.
512-522
: Standardized null check in processCommentNode functionSimilar to the text node processing, the null check for the n1 parameter has been refactored to use the
isNullish
utility function.
610-631
: Standardized null check in processElement functionThe null check for n1 has been refactored to use
isNullish
, maintaining the same conditional logic for determining whether to mount a new element or patch an existing one.
841-843
: Standardized null checks for innerHTML and textContentBoth null checks have been refactored to use the
isNullish
utility function, preserving the same behavior when determining if these properties need to be cleared.
927-930
: Standardized null check for dynamicChildrenThe null check for dynamicChildren has been refactored to use the
isNullish
utility function.
1056-1075
: Standardized null check in processFragment functionThe null check for n1 has been refactored to use
isNullish
, maintaining the same behavior when determining whether to mount a new fragment or update an existing one.
1104-1108
: Standardized key existence check in Fragment handlingThe check for the presence of a key has been refactored to use
!isNullish(n2.key)
, which is semantically equivalent to the previous check.
1141-1163
: Standardized null check in processComponent functionThe null check for n1 has been refactored to use
isNullish
, preserving the same behavior when determining whether to mount a new component or update an existing one.
1891-1901
: Standardized key existence check in patchKeyedChildrenThe check for the presence of a key in nextChild has been refactored to use
!isNullish(nextChild.key)
instead of a direct comparison.
1927-1940
: Standardized key existence check for prevChildThe check for the presence of a key in prevChild has been refactored to use
!isNullish(prevChild.key)
instead of a direct comparison.
2101-2105
: Standardized ref check in unmount functionThe null check for ref has been refactored to use
!isNullish(ref)
, maintaining the same behavior when determining whether to unset a ref.
2108-2110
: Standardized cacheIndex check in unmount functionThe null check for cacheIndex has been refactored to use
!isNullish(cacheIndex)
, preserving the same behavior for cache cleanup during unmounting.
2381-2403
: Standardized null check in render functionThe null check for vnode has been refactored to use
isNullish(vnode)
, maintaining the same behavior when determining whether to unmount existing nodes or patch with new ones.
1-2581
: Overall assessment of the isNullish refactoringThis refactoring successfully replaces direct nullish checks (
== null
and!= null
) with the newisNullish
utility function from@vue/shared
. The changes maintain the exact same behavior while making the codebase more consistent and explicit about intent.Key benefits of this refactoring:
- Improved readability - the intent of checking for null/undefined is clearer
- Better consistency across the codebase
- Centralized definition of nullish checks, making future changes easier
While there might be a negligible performance impact from using function calls instead of direct operators, the maintainability benefits outweigh this concern.
🧰 Tools
🪛 Biome (1.9.4)
[error] 487-487: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 651-651: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 717-717: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 1218-1219: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 1300-1300: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 1386-1386: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 1479-1479: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 1530-1530: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 1613-1613: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 2063-2063: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 2123-2123: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 2184-2184: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 2375-2375: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
[error] 293-293: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
[error] 293-293: Don't use 'Function' as a type.
Prefer explicitly define the function shape. This type accepts any function-like value, which can be a common source of bugs.
(lint/complexity/noBannedTypes)
if (!isNullish(ref) && parentComponent) { | ||
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2) | ||
} |
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.
💡 Verification agent
🧩 Analysis chain
Refactored ref presence check
The null check for refs has been standardized using the isNullish
utility function.
Based on the static analysis hint, consider using optional chaining here:
🏁 Script executed:
#!/bin/bash
# Check how ref is used in the codebase to determine if optional chaining would be appropriate
ast-grep --pattern $'setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)'
Length of output: 184
🏁 Script executed:
#!/bin/bash
# Verify usage of optional chaining in runtime-core
rg "\?\." -n packages/runtime-core/src
Length of output: 248
Use Optional Chaining for n1.ref
Optional chaining is already in use elsewhere (e.g. warning.ts, devtools.ts), so for consistency and readability replace the logical-AND null guard with n1?.ref
.
• packages/runtime-core/src/renderer.ts: line 487
- setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
+ setRef(ref, n1?.ref, parentSuspense, n2 || n1, !n2)
📝 Committable suggestion
‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.
if (!isNullish(ref) && parentComponent) { | |
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2) | |
} | |
if (!isNullish(ref) && parentComponent) { | |
setRef(ref, n1?.ref, parentSuspense, n2 || n1, !n2) | |
} |
🧰 Tools
🪛 Biome (1.9.4)
[error] 487-487: Change to an optional chain.
Unsafe fix: Change to an optional chain.
(lint/complexity/useOptionalChain)
This PR refactors several inline type checks across the codebase to use existing utility functions from
@vue/shared
(such asisString
,isFunction
, etc.) in order to improve consistency.It also adds a few additional utility functions:
isNumber
isBoolean
isNullish
These utility functions are now available in
@vue/shared
and have been applied where appropriate.Summary by CodeRabbit
Refactor
isNullish
,isNumber
,isBoolean
, etc.).New Features
Style