Skip to content

Commit

Permalink
Pull in Babel 6 version
Browse files Browse the repository at this point in the history
  • Loading branch information
taion committed Mar 9, 2016
1 parent 73f1312 commit 7164955
Show file tree
Hide file tree
Showing 2 changed files with 100 additions and 91 deletions.
2 changes: 1 addition & 1 deletion LICENSE
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ BSD License

For fbjs software

Copyright (c) 2013-2015, Facebook, Inc.
Copyright (c) 2013-present, Facebook, Inc.
All rights reserved.

Redistribution and use in source and binary forms, with or without modification,
Expand Down
189 changes: 99 additions & 90 deletions dev-expression.js
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@
module.exports = function(babel) {
var t = babel.types;

var SEEN_SYMBOL = Symbol();

var DEV_EXPRESSION = t.binaryExpression(
'!==',
t.memberExpression(
Expand All @@ -23,100 +25,107 @@ module.exports = function(babel) {
t.identifier('NODE_ENV'),
false
),
t.literal('production')
t.stringLiteral('production')
);

return new babel.Transformer('fbjs.dev-expression', {
Identifier: {
enter: function(node, parent) {
// Do nothing when testing
if (process.env.NODE_ENV === 'test') {
return undefined;
}
// replace __DEV__ with process.env.NODE_ENV !== 'production'
if (this.isIdentifier({name: '__DEV__'})) {
return DEV_EXPRESSION;
}
return {
visitor: {
Identifier: {
enter: function(path) {
// Do nothing when testing
if (process.env.NODE_ENV === 'test') {
return undefined;
}
// replace __DEV__ with process.env.NODE_ENV !== 'production'
if (path.isIdentifier({name: '__DEV__'})) {
path.replaceWith(DEV_EXPRESSION);
}
},
},
},
CallExpression: {
exit: function(node, parent) {
// Do nothing when testing
if (process.env.NODE_ENV === 'test') {
return undefined;
}
if (this.get('callee').isIdentifier({name: 'invariant'})) {
// Turns this code:
//
// invariant(condition, argument, argument);
//
// into this:
//
// if (!condition) {
// if ("production" !== process.env.NODE_ENV) {
// invariant(false, argument, argument);
// } else {
// invariant(false);
// }
// }
//
// Specifically this does 2 things:
// 1. Checks the condition first, preventing an extra function call.
// 2. Adds an environment check so that verbose error messages aren't
// shipped to production.
// The generated code is longer than the original code but will dead
// code removal in a minifier will strip that out.
var condition = node.arguments[0];
return t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.expressionStatement(
t.callExpression(
node.callee,
[t.literal(false)].concat(node.arguments.slice(1))
)
),
]),
t.blockStatement([
t.expressionStatement(
t.callExpression(
node.callee,
[t.literal(false)]
)
),
])
),
])
);
} else if (this.get('callee').isIdentifier({name: 'warning'})) {
// Turns this code:
//
// warning(condition, argument, argument);
//
// into this:
//
// if ("production" !== process.env.NODE_ENV) {
// warning(condition, argument, argument);
// }
//
// The goal is to strip out warning calls entirely in production. We
// don't need the same optimizations for conditions that we use for
// invariant because we don't care about an extra call in __DEV__
CallExpression: {
exit: function(path) {
var node = path.node;
// Do nothing when testing
if (process.env.NODE_ENV === 'test') {
return;
}
// Ignore if it's already been processed
if (node[SEEN_SYMBOL]) {
return;
}
if (path.get('callee').isIdentifier({name: 'invariant'})) {
// Turns this code:
//
// invariant(condition, argument, argument);
//
// into this:
//
// if (!condition) {
// if ("production" !== process.env.NODE_ENV) {
// invariant(false, argument, argument);
// } else {
// invariant(false);
// }
// }
//
// Specifically this does 2 things:
// 1. Checks the condition first, preventing an extra function call.
// 2. Adds an environment check so that verbose error messages aren't
// shipped to production.
// The generated code is longer than the original code but will dead
// code removal in a minifier will strip that out.
var condition = node.arguments[0];
var devInvariant = t.callExpression(
node.callee,
[t.booleanLiteral(false)].concat(node.arguments.slice(1))
);
devInvariant[SEEN_SYMBOL] = true;
var prodInvariant = t.callExpression(
node.callee,
[t.booleanLiteral(false)]
);
prodInvariant[SEEN_SYMBOL] = true;
path.replaceWith(t.ifStatement(
t.unaryExpression('!', condition),
t.blockStatement([
t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.expressionStatement(devInvariant),
]),
t.blockStatement([
t.expressionStatement(prodInvariant),
])
),
])
));
} else if (path.get('callee').isIdentifier({name: 'warning'})) {
// Turns this code:
//
// warning(condition, argument, argument);
//
// into this:
//
// if ("production" !== process.env.NODE_ENV) {
// warning(condition, argument, argument);
// }
//
// The goal is to strip out warning calls entirely in production. We
// don't need the same optimizations for conditions that we use for
// invariant because we don't care about an extra call in __DEV__

return t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.expressionStatement(
node
),
])
);
}
node[SEEN_SYMBOL] = true;
path.replaceWith(t.ifStatement(
DEV_EXPRESSION,
t.blockStatement([
t.expressionStatement(
node
),
])
));
}
},
},
},
});

};
};

0 comments on commit 7164955

Please sign in to comment.