-
Notifications
You must be signed in to change notification settings - Fork 667
docs(adr): add development-warnings adr #2928
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
Merged
Merged
Changes from 2 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,60 @@ | ||
| # ADR XXX: Development Warnings | ||
|
|
||
| ## Status | ||
|
|
||
| Proposed | ||
|
|
||
| ## Context | ||
|
|
||
| There are situations where we would like to provide warnings to developers who | ||
| use `@primer/react` that something may be deprecated, unsupported, etc. Often, | ||
| these will be emitted using `console.warn()`. When using `console.warn()` by | ||
| itself, we run into a situation where code that is only meant for development | ||
| is included in production code. As a result, it would be helpful to establish | ||
| patterns around how to provide warnings to developers in order to make sure | ||
| that: | ||
|
|
||
| - Calls to `console.warn()` do not appear in production | ||
| - Code related to development checks, warnings, or messages are removed from | ||
| production code | ||
|
|
||
| ## Decision | ||
|
|
||
| Code that is meant for development-only warnings or checks **must** be wrapped within a | ||
| `__DEV__` block. | ||
|
|
||
| ```tsx | ||
| function ExampleComponent() { | ||
| if (__DEV__) { | ||
| // This code only runs in development | ||
| } | ||
| } | ||
| ``` | ||
|
|
||
| Under-the-hood, the `__DEV__` block will be compiled to a `NODE_ENV` check so | ||
| that it is stripped when `NODE_ENV` is set to `'production'`. | ||
|
|
||
| > **Note** | ||
| > Contributors may wrap hooks within a `__DEV__` block even though hooks are not | ||
| > meant to be called conditionally. This is because the `__DEV__` check can be | ||
| > considered constant in that it will always be true or false for the | ||
| > environment (development or production). | ||
|
|
||
| When a contributor would like to communicate a warning to a developer, they | ||
| should use the `warning()` helper. | ||
|
|
||
| ```ts | ||
| warning(condition, 'This is the message that is logged when condition is false-y') | ||
| ``` | ||
|
|
||
| This helper allows you to provide a `condition`. When the condition is false-y | ||
| it will emit the message provided. This helper is automatically wrapped in a | ||
| `__DEV__` block and will be removed from production builds. | ||
|
|
||
| For more complex conditions, a contributor may combine `console.warn()` with | ||
| `__DEV__` when `warning()` does not suffice. | ||
|
|
||
| ### Impact | ||
|
|
||
| - Calls to `console.warn()` will be replaced by `warning()` or will be wrapped | ||
| in a `__DEV__` block | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.
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.
I would have guessed that the message would display if the condition is truthy. I read that code as "warn if condition"
If this is a convention, I'm fine to keep it. But I wanted to mention my initial confusion.
If the name of the helper was something like
assert(), I think falsey would make more sense.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.
Ohh, this is how I think it was as well the entire time - now realised that 🙈
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.
Totally makes sense! Happy to flip it if that's more intuitive for folks 👍
I think the convention stems from
invariant()ultimately, and things likeassert(), that frame things from the perspective of "this condition must be met". When it's not met, throw an error, log a warning, etcTo contextualize the two, here is the warning from
Headingin the two styles: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.
Thanks Josh! Warning when the condition truthy feels more intuitive to me. That said, it is totally cool to me if we want follow the convention that it stems from.
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.
Let's do truth-y then! I'll update the ADR accordingly 👍