Skip to content

Commit

Permalink
Check presence of process.env in the code transformed by the babel pl…
Browse files Browse the repository at this point in the history
…ugin
  • Loading branch information
diegohaz committed Jan 9, 2020
1 parent be1f921 commit 0765740
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 19 deletions.
30 changes: 22 additions & 8 deletions packages/warning/src/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,30 @@ const pkg = require( '../package.json' );
function babelPlugin( { types: t } ) {
const seen = Symbol();

const binaryExpression = t.binaryExpression(
const typeofProcessExpression = t.binaryExpression(
'!==',
t.memberExpression(
t.memberExpression( t.identifier( 'process' ), t.identifier( 'env' ), false ),
t.identifier( 'NODE_ENV' ),
false
),
t.unaryExpression( 'typeof', t.identifier( 'process' ), false ),
t.stringLiteral( 'undefined' )
);

const processEnvExpression = t.memberExpression(
t.identifier( 'process' ),
t.identifier( 'env' ),
false
);

const nodeEnvCheckExpression = t.binaryExpression(
'!==',
t.memberExpression( processEnvExpression, t.identifier( 'NODE_ENV' ), false ),
t.stringLiteral( 'production' )
);

const logicalExpression = t.logicalExpression(
'&&',
t.logicalExpression( '&&', typeofProcessExpression, processEnvExpression ),
nodeEnvCheckExpression
);

return {
visitor: {
ImportDeclaration( path, state ) {
Expand Down Expand Up @@ -45,11 +59,11 @@ function babelPlugin( { types: t } ) {
// Turns this code:
// warning(condition, argument, argument);
// into this:
// process.env.NODE_ENV !== "production" ? warning(condition, argument, argument) : void 0;
// typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning(condition, argument, argument) : void 0;
node[ seen ] = true;
path.replaceWith(
t.ifStatement(
binaryExpression,
logicalExpression,
t.blockStatement( [ t.expressionStatement( node ) ] )
)
);
Expand Down
22 changes: 11 additions & 11 deletions packages/warning/src/test/babel-plugin.js
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ describe( 'babel-plugin', function() {
),
join(
'import warning from "@wordpress/warning";',
'process.env.NODE_ENV !== "production" ? warning(true, "a", "b") : void 0;'
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning(true, "a", "b") : void 0;'
)
);
} );
Expand All @@ -44,7 +44,7 @@ describe( 'babel-plugin', function() {
it( 'should replace warning calls without import declaration with plugin options', () => {
compare(
'warning(true, "a", "b");',
'process.env.NODE_ENV !== "production" ? warning(true, "a", "b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning(true, "a", "b") : void 0;',
{ callee: 'warning' }
);
} );
Expand All @@ -59,9 +59,9 @@ describe( 'babel-plugin', function() {
),
join(
'import warning from "@wordpress/warning";',
'process.env.NODE_ENV !== "production" ? warning(true, "a", "b") : void 0;',
'process.env.NODE_ENV !== "production" ? warning(false, "b", "a") : void 0;',
'process.env.NODE_ENV !== "production" ? warning(cond, "c") : void 0;'
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning(true, "a", "b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning(false, "b", "a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warning(cond, "c") : void 0;'
)
);
} );
Expand All @@ -76,9 +76,9 @@ describe( 'babel-plugin', function() {
),
join(
'import warn from "@wordpress/warning";',
'process.env.NODE_ENV !== "production" ? warn(true, "a", "b") : void 0;',
'process.env.NODE_ENV !== "production" ? warn(false, "b", "a") : void 0;',
'process.env.NODE_ENV !== "production" ? warn(cond, "c") : void 0;'
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn(true, "a", "b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn(false, "b", "a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn(cond, "c") : void 0;'
)
);
} );
Expand All @@ -93,9 +93,9 @@ describe( 'babel-plugin', function() {
),
join(
'import warn from "@wordpress/warning";',
'process.env.NODE_ENV !== "production" ? warn(true, "a", "b") : void 0;',
'process.env.NODE_ENV !== "production" ? warn(false, "b", "a") : void 0;',
'process.env.NODE_ENV !== "production" ? warn(cond, "c") : void 0;'
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn(true, "a", "b") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn(false, "b", "a") : void 0;',
'typeof process !== "undefined" && process.env && process.env.NODE_ENV !== "production" ? warn(cond, "c") : void 0;'
)
);
} );
Expand Down

0 comments on commit 0765740

Please sign in to comment.