Skip to content

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

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

Mini-ghost
Copy link
Contributor

@Mini-ghost Mini-ghost commented May 4, 2025

This PR refactors several inline type checks across the codebase to use existing utility functions from @vue/shared (such as isString, 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

    • Standardized null, undefined, and type checks throughout the codebase by introducing and using new utility functions (isNullish, isNumber, isBoolean, etc.).
    • Improved consistency for value checks in various features, including component options, transitions, directives, and DOM attribute handling.
  • New Features

    • Added utility functions for detecting nullish values, numbers, and booleans.
  • Style

    • Enhanced code clarity and maintainability by unifying type and nullish checks.

Copy link

coderabbitai bot commented May 4, 2025

Walkthrough

This update introduces a new utility function, isNullish, along with isNumber and isBoolean, to the shared type-checking utilities. The codebase is refactored to replace direct nullish (== null, != null) and type (typeof ...) checks with calls to these new utility functions. The changes affect a wide range of files across both runtime-core and runtime-dom, standardizing the approach to nullish and type checking throughout the code. No public APIs or control flows are altered, and no new features are introduced beyond these utility functions.

Changes

File(s) Change Summary
packages/shared/src/general.ts Added isNullish, isNumber, and isBoolean utility functions; updated genCacheKey to use isFunction.
packages/runtime-core/src/* (apiAsyncComponent.ts, apiCreateApp.ts, apiInject.ts, ... ) Replaced direct nullish and type checks with isNullish, isNumber, isBoolean, and other utility functions where applicable.
packages/runtime-dom/src/* (apiCustomElement.ts, components/Transition.ts, ... ) Standardized nullish and type checks using isNullish, isNumber, isString, and isFunction utilities.
packages/shared/src/domAttrConfig.ts, toDisplayString.ts, normalizeProp.ts Updated value and type checks to use new utility functions (isNullish, isNumber).

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
Loading

Poem

In the warren of code, where the checks multiply,
A bunny hopped in with a glint in its eye.
"No more == null!" it cheerfully cried,
"Let isNullish and friends be your guide!"
Now numbers and booleans, so easy to see,
Type-checking is tidy, as neat as can be.
Hippity-hop, code’s clean—thanks to utility!

✨ Finishing Touches
  • 📝 Generate Docstrings

🪧 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.
    • Generate unit testing code for this file.
    • 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. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • 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 src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need 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)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

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

Documentation and Community

  • 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

github-actions bot commented May 4, 2025

Size Report

Bundles

File Size Gzip Brotli
runtime-dom.global.prod.js 100 kB (-204 B) 38.2 kB (-3 B) 34.4 kB (-13 B)
vue.global.prod.js 158 kB (-199 B) 58.3 kB (-31 B) 52 kB (+102 B)

Usages

Name Size Gzip Brotli
createApp (CAPI only) 46.5 kB (-82 B) 18.2 kB (+13 B) 16.7 kB (+8 B)
createApp 54.4 kB (-80 B) 21.2 kB (+14 B) 19.4 kB (+23 B)
createSSRApp 58.6 kB (-93 B) 23 kB (+18 B) 20.9 kB (+16 B)
defineCustomElement 59.2 kB (-112 B) 22.8 kB (+13 B) 20.8 kB (+3 B)
overall 68.4 kB (-126 B) 26.4 kB (-3 B) 24 kB (+17 B)

Copy link

pkg-pr-new bot commented May 4, 2025

Open in StackBlitz

@vue/compiler-core

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

@vue/compiler-dom

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

@vue/compiler-sfc

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

@vue/compiler-ssr

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

@vue/reactivity

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

@vue/runtime-core

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

@vue/runtime-dom

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

@vue/server-renderer

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

@vue/shared

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

vue

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

@vue/compat

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

commit: ee18800

@edison1105
Copy link
Member

Thanks for the PR, but...
see #8505 (comment)

@Mini-ghost
Copy link
Contributor Author

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 runtime-core and runtime-dom might make it slightly more acceptable?

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.

@Mini-ghost Mini-ghost force-pushed the refactor/shared-utils branch from 29e9506 to ee18800 Compare May 4, 2025 02:56
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: 1

📜 Review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro

📥 Commits

Reviewing files that changed from the base of the PR and between 29e9506 and ee18800.

📒 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 new isNullish utility function

The 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 nodes

Replacing the direct nullish check with the isNullish utility function maintains the same behavior while improving code consistency.


492-504: Standardized null check in processText function

The 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 function

Similar 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 function

The 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 textContent

Both 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 dynamicChildren

The null check for dynamicChildren has been refactored to use the isNullish utility function.


1056-1075: Standardized null check in processFragment function

The 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 handling

The 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 function

The 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 patchKeyedChildren

The 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 prevChild

The 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 function

The 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 function

The 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 function

The 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 refactoring

This refactoring successfully replaces direct nullish checks (== null and != null) with the new isNullish 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:

  1. Improved readability - the intent of checking for null/undefined is clearer
  2. Better consistency across the codebase
  3. 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)

Comment on lines +486 to 488
if (!isNullish(ref) && parentComponent) {
setRef(ref, n1 && n1.ref, parentSuspense, n2 || n1, !n2)
}
Copy link

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.

Suggested change
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)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants