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

feat: update adds improved jsdoc based message #173

Closed
wants to merge 3 commits into from

Conversation

DarkPurple141
Copy link

Applies a jsdoc format instead to better explain usage.

* @param condition A boolean condition - if falsey will thrown an error.
* @param message The message provided to accompany the invariant. No inline default argument for message as the result is smaller.
*
* @example
Copy link
Owner

Choose a reason for hiding this comment

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

I like the @example

Copy link
Author

Choose a reason for hiding this comment

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

haha yeah. Playful but I think it helps to make it really clear how to use the fn :)

/**
* Throw an error if the condition fails. Strip out error messages for production.
*
* @param condition A boolean condition - if falsey will thrown an error.
Copy link
Owner

Choose a reason for hiding this comment

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

Do we have to provide definitions for the @params? It feels like doubling up 🤔

Copy link
Owner

Choose a reason for hiding this comment

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

I wonder if this is enough as a jsdoc:

/** Throw an `Error` if the `condition` is falsey */

Copy link
Owner

Choose a reason for hiding this comment

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

What do you think @DarkPurple141 ?

Copy link
Author

Choose a reason for hiding this comment

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

Don't need them necessarily, really just there for completeness.

src/tiny-invariant.ts Outdated Show resolved Hide resolved
@DarkPurple141
Copy link
Author

Hey @alexreardon I've updated this PR and also added type information into the jsdoc - here is how it appears in IDE.

Screenshot 2023-12-03 at 2 07 22 pm

Hovering a param also gives more specific documented feedback based on the param.

Screenshot 2023-12-03 at 2 19 18 pm

The documentation also closely matches what's in the README.md file.

This is compared to how it currently appears in IDE:

Screenshot 2023-12-03 at 2 16 45 pm

While it is slightly more verbose - it doesn't add to the bundle and makes it usage more explicit. For those users not using typescript and inferring based on JSDoc this will also be beneficial.

If any feedback or thoughts let me know. Happy to discuss on the PR!

@@ -4,6 +4,23 @@

const prefix: string = 'Invariant failed';

/**
Copy link
Author

Choose a reason for hiding this comment

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

have also added the same jsdoc to the flow package

export default function invariant(
condition: any,
// Can provide a string, or a function that returns a string for cases where
Copy link
Author

Choose a reason for hiding this comment

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

This is now covered directly in the jsdoc

* @param {boolean} condition A boolean condition - if falsey will thrown an error.
* @param {(string|() => string)} message The message provided to accompany the invariant. Can provide a string, or a function that returns a string for cases where the message takes a fair amount of effort to compute.
*
* @returns {asserts condition is true}
Copy link
Owner

Choose a reason for hiding this comment

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

asserts condition is truthy?

Copy link
Author

Choose a reason for hiding this comment

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

{ } syntax is a hint for the TS server - so this is really
asserts condition (variable) is true <-- which is the ts expression

Can certainly add the explanation as an annotation tho next to it.

*
* The `invariant` function takes a value, and if the value is falsy then the `invariant` function will throw. If the value is truthy, then the function will not throw.
*
* @param {boolean} condition A boolean condition - if falsey will thrown an error.
Copy link
Owner

Choose a reason for hiding this comment

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

the param can be any value

Copy link
Author

Choose a reason for hiding this comment

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

yeah this needs to be corrected - will address

@DarkPurple141
Copy link
Author

@alexreardon I've updated let me know if all good 👍

@alexreardon
Copy link
Owner

Thanks for your effort on this @DarkPurple141. Rather than re-implementing the types in jsdoc, for now I've decided to keep the types in TypeScript, and add jsdoc information to help with clarity for consumers

@alexreardon
Copy link
Owner

#177

@DarkPurple141
Copy link
Author

Hi Alex thanks - what you've merged is a improvement so I appreciate that it'll make it easier to parse in an IDE!

I'm a bit disappointed that we couldn't merge this PR - I spent a decent amount of time nursing it. The types aren't redeclared, typescript annotations still win as per the attached screen grabs. This did however provide type hints where a typescript server is not present which is not uncommon. Maybe this context was lost in the thread of this PR?

I feel like what's been merged is completely fine but borrows heavily from the context of this contribution.

@alexreardon
Copy link
Owner

alexreardon commented Feb 23, 2024

Hey Alex, I am sorry I disappointed you - it was not my intention to do anything wrong by you. I really appreciate your contribution.

When I accept a contribution, I want to completely understand every line as at the end of the day - I'll be on the hook for it. I haven't looked into jsdoc type assertions, and I would want to check that what was in this PR behaved correctly in non-TS environments before merging. I didn't really have the energy to do that right now. I thought I would cut down the scope of this change (for now) to just be about making editor experiences better. Rather than asking you to modify this PR, I thought it would be easier for everybody for me to write and ship a small jsdoc comment based on the language in the existing documentation.

Cheers

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