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
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 17 additions & 0 deletions src/tiny-invariant.flow.js
Original file line number Diff line number Diff line change
Expand Up @@ -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

* __invariant__
*
* 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.
* @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}
* @example
* ```tsx
* import invariant from 'tiny-invariant';
*
* // throws when 1 no longer equals 1
* invariant(1 === 1, 'Maths is broken');
* ```
*/
export default function invariant(condition: mixed, message?: string | (() => string)) {
if (condition) {
return;
Expand Down
22 changes: 17 additions & 5 deletions src/tiny-invariant.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,25 @@
const isProduction: boolean = process.env.NODE_ENV === 'production';
const prefix: string = 'Invariant failed';

// Throw an error if the condition fails
// Strip out error messages for production
// > Not providing an inline default argument for message as the result is smaller
/**
* __invariant__
*
* 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

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

* @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 :)

* ```tsx
* import invariant from 'tiny-invariant';
*
* // throws when 1 no longer equals 1
* invariant(1 === 1, 'Maths is broken');
* ```
*/
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

// the message takes a fair amount of effort to compute
message?: string | (() => string),
): asserts condition {
if (condition) {
Expand Down