-
Notifications
You must be signed in to change notification settings - Fork 49.2k
Replace wrap-warning-with-env-check
with an eslint plugin
#17540
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
Conversation
This pull request is automatically built and testable in CodeSandbox. To see build info of the built libraries, click here or the icon next to each commit SHA. Latest deployment of this branch, based on commit 21158df:
|
}, | ||
/* calls wrapped on an if with && */ | ||
{ | ||
code: 'if (banana && __DEV__ && potato && kitten) { warning(test) }', |
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.
This feels too fragile and easy to get wrong, let's just disallow this.
const RuleTester = require('eslint').RuleTester; | ||
const ruleTester = new RuleTester(); | ||
|
||
ruleTester.run('no-production-logging', rule, { |
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 also check lowPriorityWarning (and its WithoutStack variant) in tests
Details of bundled changes.Comparing: acfe4b2...21158df react-dom
react-native-renderer
Size changes (stable) |
}, | ||
/* calls wrapped on an outer bound */ | ||
{ | ||
code: 'if (__DEV__) { if (potato) { while (true) { warning(test) } } }', |
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.
Nit: let's make this multiline so it's easier to read tests
Details of bundled changes.Comparing: acfe4b2...21158df react-native-renderer
react
react-dom
ReactDOM: size: 0.0%, gzip: 0.0% React: size: 0.0%, gzip: 0.0% Size changes (experimental) |
} | ||
} | ||
|
||
function hasIfInParents(node) { |
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.
Nit: hasIfDEVInParents
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 make the check stricter and ask for __DEV__
specifically, and then fix up the remaining three or four cases
a34904e
to
1870097
Compare
This makes it easier to see what's going on and matches dominant style in the codebase.
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.
Made a few tweaks. Looks good to go.
* Replace Babel plugin with an ESLint plugin * Fix ESLint rule violations * Move shared conditions higher * Test formatting nits * Tweak ESLint rule * Bugfix: inside else branch, 'if' tests are not satisfactory * Use a stricter check for exactly if (__DEV__) This makes it easier to see what's going on and matches dominant style in the codebase. * Fix remaining files after stricter check
Part 1.5 of #16753
This PR removes the
__DEV__
wrapping part ofwrap-warning-with-env-check
and replaces it with a functionally identical eslint plugin (no-production-logging
).The babel rule gets renamed tolift-warning-conditional-argument
because that's what it does! In its current stage this will fail builds since we still got all the unwrapped calls in the actual codebase.The plugin supports
&&
statements likeif( __DEV__ && React.suspenseIsEnabled)
but fails any top level||
s. Have a look at the tests and code because I might have some unchecked assumptions about static code analysis here (and about how we strip out the__DEV__
checks at compile time lol) but i think this covers the cases we wanna cover.Caveats
The generated development builds will include an extra block around the checks, from this original code
eslint (in this branch) will make the editor rewrite it to
Which babel will transform (using ternaries) to
And then rollup will rewrite
__DEV__
to true (for a dev bundle)And finally, commonjs remove the useless if (this seems like a happy side effect rather than something we are doing on purpose for the dev builds - as for prod we run the closure compiler). However it will keep the block scope:
This makes sense to me, this block scope is pretty absurd but we can't remove blocks automatically (at least not naively) because in some cases it can affect the scope of variables.
We didn't have this before because babel would transform everything using ternaries, which don't start a block scope. Since we are moving the DEV check to the userland now I don't think we want people writing this manually:
Next steps
How do we merge this? The way I see it we can either