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

unkownutil周りのライブラリを上げて、型判定周りを整理 #93

Merged
merged 4 commits into from
Jul 27, 2024

Conversation

kamecha
Copy link
Owner

@kamecha kamecha commented Jul 27, 2024

#60
↑これに関連する

Summary by CodeRabbit

  • New Features

    • Enhanced type safety for input parameters across various components by implementing assertion checks.
    • Introduced type predicates for validating message and user account structures to improve data integrity.
  • Bug Fixes

    • Improved error handling by replacing earlier validation methods with more robust assertion mechanisms, reducing potential runtime errors.
  • Documentation

    • Added a new type checking file defining strict types for messaging entities, enhancing clarity and usability for developers.

Copy link

coderabbitai bot commented Jul 27, 2024

Walkthrough

The updates across various files enhance type safety and validation mechanisms by transitioning from utility functions to assertion-based checks. This shift from ensure functions to assert calls improves error handling and code readability, ensuring that variables conform to expected types before processing. Overall, the changes lead to a more robust and maintainable code structure, minimizing potential runtime errors.

Changes

Files Change Summary
denops/@ddc-sources/*.ts Updated onInit methods in the Source class to use assert for string validation instead of ensureString, enhancing type safety.
denops/@ddu-kinds/*.ts Replaced ensureNumber and ensureArray with assert checks, enhancing validation for limit and ret variables.
denops/traqvim/*.ts Replaced various ensure functions with assert, improving validation for timeline messages and other parameters, enhancing type safety and error handling.
denops/traqvim/deps.ts Transitioned exports from utility functions (ensure...) to assertion-based utilities (assert, is), enhancing type safety in the codebase.
denops/traqvim/type_check.ts Introduced type predicates for MessageStamp, UserAccountState, User, and Message to validate data structures, enhancing type safety.

Sequence Diagram(s)

sequenceDiagram
    participant User
    participant App
    participant Source
    participant Validator

    User->>App: Initiates action
    App->>Source: Call onInit()
    Source->>Validator: Validate path
    Validator-->>Source: Assert path is string
    Source-->>App: Proceed with valid path
    App-->>User: Action completed
Loading

🐇 "In a world so neat and bright,
We check our paths, make things right.
With assertions bold and true,
Our code now dances, fresh and new!
Hooray for types, let errors flee,
A happy rabbit, that's me! 🐰✨"


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

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

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.

@kamecha
Copy link
Owner Author

kamecha commented Jul 27, 2024

asを使用してる箇所があったら積極的にassertとかに変更しておきたい

一度asしてて、知らないうちにvim固まっちゃった事あるので...

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: 3

Outside diff range, codebase verification and nitpick comments (1)
denops/@ddc-sources/stamp.ts (1)

19-19: Ensure proper error handling for onInit function calls

The onInit function calls in the following files do not handle the potential assertion error correctly. Please add appropriate error handling to manage assertion failures:

  • denops/@ddc-sources/stamp.ts
  • denops/@ddu-sources/channel_rec.ts
  • denops/@ddu-sources/channel.ts
Analysis chain

LGTM! But verify the function usage in the codebase.

The code changes are approved.

However, ensure that all function calls to onInit handle the potential assertion error correctly.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `onInit` handle the potential assertion error correctly.

# Test: Search for the function usage. Expect: Proper error handling in the calling code.
rg --type python -A 5 $'onInit'

Length of output: 61


Script:

#!/bin/bash
# Description: Verify all function calls to `onInit` handle the potential assertion error correctly.

# Test: Search for the function usage. Expect: Proper error handling in the calling code.
rg --type ts -A 5 $'onInit'

Length of output: 1352

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 3452e46 and 1c58296.

Files selected for processing (8)
  • denops/@ddc-sources/stamp.ts (2 hunks)
  • denops/@ddu-kinds/channel.ts (3 hunks)
  • denops/@ddu-sources/channel.ts (2 hunks)
  • denops/@ddu-sources/channel_rec.ts (2 hunks)
  • denops/traqvim/action.ts (8 hunks)
  • denops/traqvim/deps.ts (1 hunks)
  • denops/traqvim/main.ts (16 hunks)
  • denops/traqvim/type_check.ts (1 hunks)
Additional comments not posted (43)
denops/@ddc-sources/stamp.ts (1)

2-6: LGTM!

The import statements have been updated correctly to use the assert and is utilities from the new version of unknownutil.

denops/traqvim/deps.ts (2)

4-4: LGTM!

The export statements have been updated correctly to use the assert and is utilities from the new version of unknownutil.


5-5: LGTM!

The export of Predicate has been added correctly to enhance type safety.

denops/traqvim/type_check.ts (4)

4-10: LGTM!

The predicate isMessageStamp is well-defined and ensures type safety for traq.MessageStamp.


12-19: LGTM!

The predicate isUserAccountState is well-defined and ensures type safety for traq.UserAccountState.


21-29: LGTM!

The predicate isUser is well-defined and ensures type safety for traq.User.


31-51: LGTM!

The predicate isMessage is well-defined and ensures type safety for Message. The nested usage of isMessage in the quote property is a good approach for recursive type checking.

denops/@ddu-sources/channel_rec.ts (1)

22-22: LGTM!

The change to use assert with is.String improves type safety and error handling.

denops/@ddu-kinds/channel.ts (2)

36-36: LGTM!

The change to use assert with is.Number improves type safety and error handling.


77-77: LGTM!

The change to use assert with is.ArrayOf(is.String) improves type safety and error handling.

denops/@ddu-sources/channel.ts (1)

21-21: LGTM!

The change to use assert with is.String improves type safety and error handling.

denops/traqvim/action.ts (7)

60-60: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


83-83: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


107-107: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


132-132: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


213-213: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


243-243: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


Line range hint 1-12:
LGTM! Updated imports for type safety.

The additions of assert, is, and isMessage are consistent with the updated validation methods and improve type safety.

denops/traqvim/main.ts (25)

29-29: LGTM! Improved type safety with assert.

The use of assert(path, is.String) enhances type safety and ensures that the path is a string.


57-57: LGTM! Improved type safety with assert.

The use of assert(choice, is.Number) enhances type safety and ensures that the choice is a number.


82-82: LGTM! Improved type safety with assert.

The use of assert(limit, is.Number) enhances type safety and ensures that the limit is a number.


94-94: LGTM! Improved type safety with assert.

The use of assert(channelPath, is.String) enhances type safety and ensures that the channelPath is a string.


102-102: LGTM! Improved type safety with assert.

The use of assert(channelID, is.String) enhances type safety and ensures that the channelID is a string.


104-104: LGTM! Improved type safety with assert.

The use of assert(limit, is.Number) enhances type safety and ensures that the limit is a number.


121-122: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) and assert(bufName, is.String) enhances type safety and ensures that bufNum is a number and bufName is a string.


132-132: LGTM! Improved type safety with assert.

The use of assert(channelID, is.String) enhances type safety and ensures that the channelID is a string.


134-134: LGTM! Improved type safety with assert.

The use of assert(limit, is.Number) enhances type safety and ensures that the limit is a number.


147-148: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) and assert(bufName, is.String) enhances type safety and ensures that bufNum is a number and bufName is a string.


152-152: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


155-155: LGTM! Improved type safety with assert.

The use of assert(channelID, is.String) enhances type safety and ensures that the channelID is a string.


157-157: LGTM! Improved type safety with assert.

The use of assert(limit, is.Number) enhances type safety and ensures that the limit is a number.


184-185: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) and assert(bufName, is.String) enhances type safety and ensures that bufNum is a number and bufName is a string.


189-189: LGTM! Improved type safety with assert.

The use of assert(timeline, is.ArrayOf(isMessage)) enhances type safety and ensures that the timeline is an array of Message objects.


192-192: LGTM! Improved type safety with assert.

The use of assert(channelID, is.String) enhances type safety and ensures that the channelID is a string.


194-194: LGTM! Improved type safety with assert.

The use of assert(limit, is.Number) enhances type safety and ensures that the limit is a number.


219-220: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) and assert(bufName, is.String) enhances type safety and ensures that bufNum is a number and bufName is a string.


222-222: LGTM! Improved type safety with assert.

The use of assert(channelID, is.String) enhances type safety and ensures that the channelID is a string.


252-252: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) enhances type safety and ensures that bufNum is a number.


254-254: LGTM! Improved type safety with assert.

The use of assert(channelID, is.String) enhances type safety and ensures that the channelID is a string.


270-270: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) enhances type safety and ensures that bufNum is a number.


278-278: LGTM! Improved type safety with assert.

The use of assert(choice, is.Number) enhances type safety and ensures that the choice is a number.


300-300: LGTM! Improved type safety with assert.

The use of assert(bufNum, is.Number) enhances type safety and ensures that bufNum is a number.


311-311: LGTM! Improved type safety with assert.

The use of assert(messageBufNum, is.Number) enhances type safety and ensures that the messageBufNum is a number.

Comment on lines +2 to +6
assert,
dduVim,
dduVimSource,
Denops,
ensureString,
is,
Copy link

Choose a reason for hiding this comment

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

Remove unused imports.

The dduVim, dduVimSource, and Denops imports are not used in this file.

-import {
-  assert,
-  dduVim,
-  dduVimSource,
-  Denops,
-  is,
-  vars,
-} from "../traqvim/deps.ts";
+import { assert, is, vars } from "../traqvim/deps.ts";
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
assert,
dduVim,
dduVimSource,
Denops,
ensureString,
is,
import { assert, is, vars } from "../traqvim/deps.ts";

ensureNumber,
vars,
} from "../traqvim/deps.ts";
import { assert, dduVim, Denops, is, vars } from "../traqvim/deps.ts";
Copy link

Choose a reason for hiding this comment

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

Remove unused imports.

The vars import is not used in this file.

-import { assert, dduVim, Denops, is, vars } from "../traqvim/deps.ts";
+import { assert, dduVim, Denops, is } from "../traqvim/deps.ts";
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
import { assert, dduVim, Denops, is, vars } from "../traqvim/deps.ts";
import { assert, dduVim, Denops, is } from "../traqvim/deps.ts";

Comment on lines +2 to +7
assert,
dduVim,
dduVimSource,
Denops,
ensureString,
helper,
is,
Copy link

Choose a reason for hiding this comment

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

Remove unused imports.

The dduVim, dduVimSource, Denops, and helper imports are not used in this file.

-import {
-  assert,
-  dduVim,
-  dduVimSource,
-  Denops,
-  helper,
-  is,
-  vars,
-} from "../traqvim/deps.ts";
+import { assert, is, vars } from "../traqvim/deps.ts";
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
assert,
dduVim,
dduVimSource,
Denops,
ensureString,
helper,
is,
import { assert, is, vars } from "../traqvim/deps.ts";

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: 0

Outside diff range, codebase verification and nitpick comments (1)
denops/traqvim/model.ts (1)

136-136: Inconsistent validation of getMeInfo results

The function getMeInfo is used in two places within denops/traqvim/model.ts. While the second usage includes validation with ensure, the first usage does not. To maintain consistency and ensure type safety, consider adding ensure validation in the first usage as well.

  • First usage: denops/traqvim/model.ts (lines following const me = await getMeInfo();)
  • Second usage: denops/traqvim/model.ts (lines following const me = await getMeInfo(); with ensure validation)
Analysis chain

LGTM! But verify the function usage in the codebase.

The use of ensure with is.String enhances type safety and robustness.

However, ensure that all function calls to getMeInfo are properly validated.

Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `getMeInfo` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `ensure`.
rg --type python -A 5 $'getMeInfo'

Length of output: 64


Script:

#!/bin/bash
# Description: Verify all function calls to `getMeInfo` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `ensure`.
rg --type ts -A 5 'getMeInfo'

Length of output: 890

Review details

Configuration used: CodeRabbit UI
Review profile: CHILL

Commits

Files that changed from the base of the PR and between 1c58296 and 603b5a2.

Files selected for processing (7)
  • denops/@ddc-sources/stamp.ts (2 hunks)
  • denops/@ddu-columns/channel.ts (2 hunks)
  • denops/@ddu-kinds/channel.ts (4 hunks)
  • denops/traqvim/deps.ts (1 hunks)
  • denops/traqvim/main.ts (16 hunks)
  • denops/traqvim/model.ts (2 hunks)
  • denops/traqvim/type_check.ts (1 hunks)
Files skipped from review due to trivial changes (1)
  • denops/traqvim/type_check.ts
Files skipped from review as they are similar to previous changes (3)
  • denops/@ddc-sources/stamp.ts
  • denops/traqvim/deps.ts
  • denops/traqvim/main.ts
Additional comments not posted (4)
denops/@ddu-columns/channel.ts (1)

39-39: LGTM! But verify the function usage in the codebase.

The use of ensure with is.Number enhances type safety and robustness.

However, ensure that all function calls to fn.strwidth are properly validated.

Verification successful

Let's correct the file type and re-run the search for fn.strwidth function calls in TypeScript files.


Let's correct the file type by specifying the .ts extension and re-run the search for fn.strwidth function calls.


Verified: The function usage is properly validated.

The fn.strwidth function is used in denops/@ddu-columns/channel.ts and is correctly validated with ensure and is.Number.

  • denops/@ddu-columns/channel.ts, line 39: const width = ensure(await fn.strwidth(args.denops, text), is.Number);
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `fn.strwidth` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `ensure`.
rg --type python -A 5 $'fn.strwidth'

Length of output: 66


Script:

#!/bin/bash
# Description: Verify all function calls to `fn.strwidth` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `ensure`.
rg --type typescript -A 5 $'fn.strwidth'

Length of output: 74


Script:

#!/bin/bash
# Description: Verify all function calls to `fn.strwidth` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `ensure`.
rg --glob '*.ts' -A 5 $'fn.strwidth'

Length of output: 434

denops/@ddu-kinds/channel.ts (3)

2-2: LGTM!

The inclusion of assert suggests a shift towards more explicit type assertions, enhancing type safety.


21-22: LGTM!

The use of is.ObjectOf with is.String ensures that id is a string, enhancing type safety.


52-52: LGTM! But verify the function usage in the codebase.

The use of assert with is.Number enhances type safety and error handling.

However, ensure that all function calls to vars.globals.get are properly validated.

Verification successful

Verified: All instances of vars.globals.get are properly validated with assert.

The use of assert with is.String and is.Number ensures that the values retrieved by vars.globals.get are properly validated, enhancing type safety and error handling.

  • denops/@ddc-sources/stamp.ts
  • denops/traqvim/main.ts
  • denops/@ddu-kinds/channel.ts
  • denops/@ddu-sources/channel.ts
  • denops/@ddu-sources/channel_rec.ts
Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Verify all function calls to `vars.globals.get` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `assert`.
rg --type python -A 5 $'vars.globals.get'

Length of output: 71


Script:

#!/bin/bash
# Description: Verify all function calls to `vars.globals.get` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `assert`.
rg --type typescript -A 5 'vars.globals.get'

Length of output: 79


Script:

#!/bin/bash
# Description: Verify all function calls to `vars.globals.get` are properly validated.

# Test: Search for the function usage. Expect: Proper validation with `assert`.
rg --type ts -A 5 'vars.globals.get'

Length of output: 3851

@kamecha kamecha merged commit cf5101e into master Jul 27, 2024
1 check passed
@kamecha kamecha deleted the refacter/update-unkownutil-library branch July 27, 2024 13:09
@kamecha
Copy link
Owner Author

kamecha commented Jul 27, 2024

Predicateだとメタデータの情報が落ちるらしいから、折をみてsatisfiesとかいうの使ってみたい

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.

1 participant