mirror of https://github.com/status-im/metro.git
Add ternary elimination support to the WPO pass
Summary: public Replaces ``` __DEV__ ? foo() : bar() ``` with ``` bar() ``` for non-dev builds. Reviewed By: davidaurelio Differential Revision: D2663838 fb-gh-sync-id: 30fdfa72dda89fc0d340da359e0f43db2917fcf5
This commit is contained in:
parent
844282c37b
commit
4ddb158c72
|
@ -103,4 +103,13 @@ describe('dead-module-elimination', () => {
|
||||||
require('bar');`
|
require('bar');`
|
||||||
);
|
);
|
||||||
});
|
});
|
||||||
|
|
||||||
|
it('should replace falsy ternaries with alternate expression', () => {
|
||||||
|
compare(
|
||||||
|
`__DEV__ = false;
|
||||||
|
__DEV__ ? foo() : bar();
|
||||||
|
`,
|
||||||
|
`bar();`
|
||||||
|
);
|
||||||
|
});
|
||||||
});
|
});
|
||||||
|
|
|
@ -33,6 +33,28 @@ function CallExpression(path) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
function IfStatement(path) {
|
||||||
|
const { node } = path;
|
||||||
|
|
||||||
|
if (node.test.type === 'Identifier' && node.test.name in globals) {
|
||||||
|
if (globals[node.test.name]) {
|
||||||
|
if (node.consequent.type === 'BlockStatement') {
|
||||||
|
path.replaceWithMultiple(node.consequent.body);
|
||||||
|
} else {
|
||||||
|
path.replaceWith(node.consequent);
|
||||||
|
}
|
||||||
|
} else if (node.alternate) {
|
||||||
|
if (node.alternate.type === 'BlockStatement') {
|
||||||
|
path.replaceWithMultiple(node.alternate.body);
|
||||||
|
} else {
|
||||||
|
path.replaceWith(node.alternate);
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
path.remove();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
module.exports = function () {
|
module.exports = function () {
|
||||||
var firstPass = {
|
var firstPass = {
|
||||||
AssignmentExpression(path) {
|
AssignmentExpression(path) {
|
||||||
|
@ -60,19 +82,8 @@ module.exports = function () {
|
||||||
//scope.removeBinding(node.left.name);
|
//scope.removeBinding(node.left.name);
|
||||||
}
|
}
|
||||||
},
|
},
|
||||||
IfStatement(path) {
|
IfStatement,
|
||||||
const { node } = path;
|
ConditionalExpression: IfStatement,
|
||||||
|
|
||||||
if (node.test.type === 'Identifier' && node.test.name in globals) {
|
|
||||||
if (globals[node.test.name]) {
|
|
||||||
path.replaceWithMultiple(node.consequent.body);
|
|
||||||
} else if (node.alternate) {
|
|
||||||
path.replaceWithMultiple(node.alternate.body);
|
|
||||||
} else {
|
|
||||||
path.remove();
|
|
||||||
}
|
|
||||||
}
|
|
||||||
},
|
|
||||||
Identifier(path) {
|
Identifier(path) {
|
||||||
const { node } = path;
|
const { node } = path;
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue