Replace wrap-warning-with-env-check with an eslint plugin (#17540)

* 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
This commit is contained in:
Laura buns
2019-12-06 18:25:54 +00:00
committed by Dan Abramov
parent acfe4b21b2
commit b43eec7eaa
35 changed files with 933 additions and 550 deletions
@@ -8,7 +8,7 @@
'use strict';
let babel = require('@babel/core');
let wrapWarningWithEnvCheck = require('../wrap-warning-with-env-check');
let wrapWarningWithEnvCheck = require('../lift-warning-conditional-argument');
function transform(input) {
return babel.transform(input, {
@@ -23,7 +23,7 @@ function compare(input, output) {
let oldEnv;
describe('wrap-warning-with-env-check', () => {
describe('lift-warning-conditional-argument', () => {
beforeEach(() => {
oldEnv = process.env.NODE_ENV;
process.env.NODE_ENV = '';
@@ -36,14 +36,14 @@ describe('wrap-warning-with-env-check', () => {
it('should wrap warning calls', () => {
compare(
"warning(condition, 'a %s b', 'c');",
"__DEV__ ? !condition ? warning(false, 'a %s b', 'c') : void 0 : void 0;"
"!condition ? warning(false, 'a %s b', 'c') : void 0;"
);
});
it('should wrap warningWithoutStack calls', () => {
compare(
"warningWithoutStack(condition, 'a %s b', 'c');",
"__DEV__ ? !condition ? warningWithoutStack(false, 'a %s b', 'c') : void 0 : void 0;"
"!condition ? warningWithoutStack(false, 'a %s b', 'c') : void 0;"
);
});
@@ -9,8 +9,6 @@
module.exports = function(babel, options) {
const t = babel.types;
const DEV_EXPRESSION = t.identifier('__DEV__');
const SEEN_SYMBOL = Symbol('expression.seen');
return {
@@ -34,10 +32,8 @@ module.exports = function(babel, options) {
//
// into this:
//
// if (__DEV__) {
// if (!condition) {
// warning(false, argument, argument);
// }
// if (!condition) {
// warning(false, argument, argument);
// }
//
// The goal is to strip out warning calls entirely in production
@@ -50,13 +46,8 @@ module.exports = function(babel, options) {
newNode[SEEN_SYMBOL] = true;
path.replaceWith(
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.ifStatement(
t.unaryExpression('!', condition),
t.expressionStatement(newNode)
),
])
t.unaryExpression('!', condition),
t.expressionStatement(newNode)
)
);
}