Skip to content
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

fix: use StringMap type for better type safety in utility functions #3220

Closed
wants to merge 2 commits into from

Conversation

Gaic4o
Copy link
Contributor

@Gaic4o Gaic4o commented Jun 9, 2024

📝 Description

Refactored utility functions to use UnknownMap type (Record<string, unknown>) for better type safety and flexibility.

⛳️ Current behavior (updates)

  • Functions used Record<string, string> or Object, causing potential type issues and limited flexibility.
  • Used @ts-ignore to bypass type checking.

🚀 New behavior

  • Functions now use UnknownMap type for better type safety and flexibility.
  • Removed @ts-ignore comments by addressing type issues directly.
  • Updated function signatures to handle unknown values more robustly.

💣 Is this a breaking change (Yes/No):

No

Summary by CodeRabbit

  • Refactor
    • Enhanced type safety and clarity by updating internal functions to utilize more specific type definitions.

@Gaic4o Gaic4o requested a review from jrgarciadev as a code owner June 9, 2024 04:11
Copy link

changeset-bot bot commented Jun 9, 2024

🦋 Changeset detected

Latest commit: af990ae

The changes in this PR will be included in the next version bump.

This PR includes changesets to release 2 packages
Name Type
@nextui-org/theme Patch
@nextui-org/react Patch

Not sure what this means? Click here to learn what changesets are.

Click here if you're a maintainer who wants to add another changeset to this PR

Copy link

vercel bot commented Jun 9, 2024

The latest updates on your projects. Learn more about Vercel for Git ↗︎

Name Status Preview Comments Updated (UTC)
nextui-storybook-v2 ✅ Ready (Inspect) Visit Preview 💬 Add feedback Jun 9, 2024 1:03pm

Copy link

vercel bot commented Jun 9, 2024

@Gaic4o is attempting to deploy a commit to the NextUI Inc Team on Vercel.

A member of the Team first needs to authorize it.

Copy link
Contributor

coderabbitai bot commented Jun 9, 2024

Walkthrough

The update focuses on enhancing type safety and flexibility within the codebase by replacing the usage of Object with the ColorMap type in functions such as swapColorValues and removeDefaultKeys. This change applies to the packages/core/theme/src/utils/object.ts file, ensuring more robust type checking and reducing potential runtime errors while maintaining the existing logic.

Changes

Files Change Summary
.../theme/src/utils/object.ts Updated swapColorValues and removeDefaultKeys to use ColorMap type for parameters and return values.

Possibly related PRs


Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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.

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

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
Contributor

@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.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between a06422f and 7ce3a5b.

Files selected for processing (2)
  • .changeset/late-buttons-fail.md (1 hunks)
  • packages/core/theme/src/utils/object.ts (2 hunks)
Additional comments not posted (4)
.changeset/late-buttons-fail.md (1)

1-5: Changeset description accurately reflects the PR changes.

packages/core/theme/src/utils/object.ts (3)

3-3: Type alias UnknownMap defined correctly for enhanced type safety.


52-52: The flattenThemeObject function uses the flatten library correctly and ensures type safety by casting the result to UnknownMap. Good use of generics and type casting.


5-26: The swapColorValues function correctly implements the swapping logic using the UnknownMap type. Ensure that all calls to this function are updated to handle the new type.

Verification successful

The calls to swapColorValues in packages/core/theme/src/colors/semantic.ts appear to be passing arguments like common.zinc, common.blue, etc. To ensure these arguments match the new UnknownMap type, we need to verify the type of common.zinc and similar variables.


The commonColors object is defined in packages/core/theme/src/colors/common.ts and includes properties like zinc, blue, etc. To ensure these properties match the UnknownMap type, we need to verify the types of zinc, blue, and other similar properties.


The properties zinc and blue in commonColors are defined as objects with string keys and color values, which align with the UnknownMap type expected by swapColorValues.

  • zinc is defined in packages/core/theme/src/colors/zinc.ts as an object with string keys and color values.
  • blue is defined in packages/core/theme/src/colors/blue.ts similarly.

Therefore, the calls to swapColorValues in semantic.ts are correctly passing arguments that match the UnknownMap type.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify all function calls to `swapColorValues` match the new signature.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type typescript $'swapColorValues'

Length of output: 73



Script:

#!/bin/bash
# Description: Verify all function calls to `swapColorValues` match the new signature.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg 'swapColorValues' --glob '*.ts'

Length of output: 799



Script:

#!/bin/bash
# Description: Verify the context of calls to `swapColorValues` in `semantic.ts` to ensure they handle the new `UnknownMap` type.

# Extract relevant lines around each call to `swapColorValues`.
rg 'swapColorValues' --glob 'packages/core/theme/src/colors/semantic.ts' -A 3 -B 3

Length of output: 3208



Script:

#!/bin/bash
# Description: Verify the type of variables like `common.zinc` used in calls to `swapColorValues`.

# Search for the definition of `common` in `common.ts`.
rg 'commonColors as common' --glob 'packages/core/theme/src/colors/common.ts' -A 3 -B 3

Length of output: 87



Script:

#!/bin/bash
# Description: Verify the type of variables like `common.zinc` used in calls to `swapColorValues`.

# Search for the definition of `commonColors` in `common.ts`.
rg 'commonColors' --glob 'packages/core/theme/src/colors/common.ts' -A 3 -B 3

Length of output: 726



Script:

#!/bin/bash
# Description: Verify the types of properties like `zinc` and `blue` in `commonColors`.

# Search for the definitions of `zinc` and `blue` in their respective files.
rg 'export' --glob 'packages/core/theme/src/colors/zinc.ts' -A 3 -B 3
rg 'export' --glob 'packages/core/theme/src/colors/blue.ts' -A 3 -B 3

Length of output: 603

packages/core/theme/src/utils/object.ts Outdated Show resolved Hide resolved
Copy link
Member

@wingkwong wingkwong left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Any reason to use unknown while the type for the value is known?

.changeset/late-buttons-fail.md Outdated Show resolved Hide resolved
Copy link
Contributor

@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

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 7ce3a5b and 83ae7f3.

Files selected for processing (2)
  • .changeset/late-buttons-fail.md (1 hunks)
  • packages/core/theme/src/utils/object.ts (2 hunks)
Files skipped from review as they are similar to previous changes (2)
  • .changeset/late-buttons-fail.md
  • packages/core/theme/src/utils/object.ts

@Gaic4o Gaic4o changed the title fix: use UnknownMap type for better type safety in utility functions fix: use StringMap type for better type safety in utility functions Jun 10, 2024
packages/core/theme/src/utils/object.ts Outdated Show resolved Hide resolved
@Gaic4o Gaic4o requested a review from wingkwong September 8, 2024 14:40
Copy link
Contributor

@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

Outside diff range, codebase verification and nitpick comments (1)
packages/core/theme/src/utils/object.ts (1)

6-7: Function signature and internal typing are correctly updated.

The updates to swapColorValues function are consistent with the PR's objectives. Consider adding a comment explaining the swapping logic for better maintainability.

Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 83ae7f3 and af990ae.

Files selected for processing (1)
  • packages/core/theme/src/utils/object.ts (2 hunks)
Additional comments not posted (2)
packages/core/theme/src/utils/object.ts (2)

3-4: Type definitions are appropriate for the intended enhancements.

The ColorValue and ColorMap types are well-defined and align with the PR's goal to enhance type safety.


53-53: Proper use of type casting in flattenThemeObject.

The explicit casting to ColorMap is appropriate and ensures type safety.

packages/core/theme/src/utils/object.ts Show resolved Hide resolved
@Gaic4o
Copy link
Contributor Author

Gaic4o commented Sep 23, 2024

@wingkwong Hello, I've modified the types based on the feedback you provided earlier. The pull request is still open, so please review it when you have a chance. If further changes aren't needed, I'll plan to close it. thank you

@wingkwong
Copy link
Member

Closing - let's keep it as it is first.

@wingkwong wingkwong closed this Sep 24, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants