-
-
Notifications
You must be signed in to change notification settings - Fork 283
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
chore: refactors readme.md file of js websocket client to reduce cognitive complexity #1480
base: master
Are you sure you want to change the base?
Conversation
|
WalkthroughThe changes refactor the code for generating operation message examples in the README file. The inline logic for extracting message examples has been replaced with a new function, Changes
Assessment against linked issues
Possibly related PRs
Suggested labels
Suggested reviewers
Poem
✨ Finishing Touches
🪧 TipsChatThere are 3 ways to chat with CodeRabbit:
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 (
|
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/templates/clients/websocket/javascript/template/README.md.js (2)
104-116
: Consider supporting all channels and examples.
Currently, only the first channel and first example are used. If future expansions require capturing additional channels or examples, modify this function to handle them accordingly.
130-140
: Validate fallback snippet.
Ifoperations
is empty, the fallback snippet documentssendEchoMessage
. Double-check that this method is indeed available, or clarify that it’s a placeholder if absent.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (1)
packages/templates/clients/websocket/javascript/template/README.md.js
(2 hunks)
⏰ Context from checks skipped due to timeout of 90000ms (2)
- GitHub Check: Test generator as dependency with Node 20
- GitHub Check: Test generator as dependency with Node 18
🔇 Additional comments (2)
packages/templates/clients/websocket/javascript/template/README.md.js (2)
50-50
: EnsuregenerateOperationsSection
handles multiple operations gracefully.
While this call looks correct, please verify that the returned markdown is formatted nicely for scenarios where multiple operations exist.
118-128
: Looks good!
This function effectively formats operations. No immediate issues found.
Hi @derberg, I have written the functions in the same file, as these helper functions are related to this file only and putting them in the helpers package didn't make much sense. However, we can improve the code quality by breaking this file into components similar to the client.js file in the same folder. |
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.
actually helpers that help extract stuff from AsyncAPI documents should be placed in https://github.com/asyncapi/generator/tree/master/packages/helpers
and they should have small scope - meaning, they should return simple data. For example one helper should be as simple as giving you a list, actually probably a map of all the operations with examples.
string building, the resulting string, is in the scope of components, like for example https://github.com/asyncapi/generator/blob/master/packages/templates/clients/websocket/javascript/components/Connect.js
@derberg thanks for clarifying. Based on the code I have refactored, I am working on making reusable, small scoped helper functions and keeping in mind that they can be used across various templates. I will get back to you when the refactoring of the code is done using these helper functions. |
|
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 (1)
packages/helpers/src/operations.js (1)
19-23
: Clean implementation with modern JavaScript features.The function uses modern JavaScript features like:
- Optional chaining (
examples?.()
)- Array methods (
flatMap
,map
,filter
)- Short-circuit evaluation with
||
One minor suggestion:
const examples = messages.flatMap(message => message.examples?.() || []); // filter out any undefined/null results, for example if the payload is not defined return examples.map(example => example.payload()).filter(Boolean); -}; +}The semicolon after the function closing brace is unnecessary in JavaScript and not used elsewhere in the codebase.
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
packages/helpers/src/index.js
(1 hunks)packages/helpers/src/operations.js
(1 hunks)packages/templates/clients/websocket/javascript/template/README.md.js
(3 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
- packages/templates/clients/websocket/javascript/template/README.md.js
⏰ Context from checks skipped due to timeout of 90000ms (3)
- GitHub Check: Test generator as dependency with Node 20
- GitHub Check: Test generator as dependency with Node 18
- GitHub Check: Test NodeJS PR - windows-latest
🔇 Additional comments (4)
packages/helpers/src/index.js (1)
1-1
: Clean export of the new helper function.The import and export of the new
getOperationMessageExamplePayloads
function follows the established pattern in the file. This change properly makes the function available through the helpers package, which is good for code organization and reusability.Also applies to: 8-9
packages/helpers/src/operations.js (3)
1-8
: Well-documented function with clear JSDoc comments.The JSDoc documentation provides comprehensive information about the function's purpose, parameters, return value, and possible errors. This is excellent for code maintainability and developer experience.
9-18
: Good error handling and early returns for edge cases.The function properly validates input and handles edge cases by:
- Checking if the operation parameter exists
- Returning early with empty arrays when no channels or messages are found
This approach prevents unnecessary processing and makes the code more robust.
25-27
: Clean module exports.The module exports follow the standard CommonJS pattern, making the function available for import from other files.
Hi @derberg, |
This PR refactors the code of readme.md.js file of javascript websocket client by breaking code into functions. This is done to reduce the cognitive complexity of the main function.
Resolves #1471
Summary by CodeRabbit