babel-plugin-minify-dead-code-elimination
Advanced tools
Comparing version 0.5.0-alpha.e9f96ffe to 0.5.0
@@ -13,2 +13,7 @@ "use strict"; | ||
function evaluateTruthy(path) { | ||
const res = evaluate(path); | ||
if (res.confident) return !!res.value; | ||
} | ||
function prevSiblings(path) { | ||
@@ -274,6 +279,10 @@ const parentPath = path.parentPath; | ||
if (p.isAssignmentExpression() && !p.get("right").isPure()) { | ||
mutations.push(() => p.replaceWith(p.get("right"))); | ||
} else { | ||
mutations.push(() => removeOrVoid(p)); | ||
if (p.isAssignmentExpression()) { | ||
if (t.isArrayPattern(p.node.left) || t.isObjectPattern(p.node.left)) { | ||
bail = true; | ||
} else if (p.get("right").isPure()) { | ||
mutations.push(() => removeOrVoid(p)); | ||
} else { | ||
mutations.push(() => p.replaceWith(p.get("right"))); | ||
} | ||
} | ||
@@ -594,3 +603,3 @@ }); | ||
const node = path.node; | ||
const evaluateTest = path.get("test").evaluateTruthy(); | ||
const evaluateTest = evaluateTruthy(path.get("test")); | ||
@@ -606,6 +615,27 @@ if (evaluateTest === true) { | ||
exit(path) { | ||
const evaluated = evaluate(path.get("discriminant"), { | ||
const discriminantPath = path.get("discriminant"); | ||
const evaluated = evaluate(discriminantPath, { | ||
tdz: this.tdz | ||
}); | ||
if (!evaluated.confident) return; | ||
if (!evaluated.confident) return; // the simplify transformation might have brought in the previous | ||
// expressions into the switch's test expression and instead of | ||
// bailing out of impure path, we collect the impurities of it's | ||
// a sequence expression and bail out if the primary test itself | ||
// is impure | ||
let beforeTest = []; | ||
if (t.isSequenceExpression(discriminantPath.node)) { | ||
const expressions = discriminantPath.get("expressions"); | ||
const lastExpression = expressions[expressions.length - 1]; | ||
if (!lastExpression.isPure()) { | ||
return; | ||
} | ||
beforeTest = [t.expressionStatement(t.sequenceExpression(expressions.slice(0, expressions.length - 1).map(path => path.node)))]; | ||
} else if (!discriminantPath.isPure()) { | ||
return; | ||
} | ||
const discriminant = evaluated.value; | ||
@@ -655,3 +685,3 @@ const cases = path.get("cases"); | ||
replaceSwitch([...extractVars(path), ...result.statements]); | ||
replaceSwitch([...extractVars(path), ...beforeTest, ...result.statements]); | ||
@@ -739,3 +769,9 @@ function getStatementsUntilBreak(start) { | ||
} else { | ||
path.remove(); | ||
const init = path.get("init"); | ||
if (init.node && !init.isPure()) { | ||
path.replaceWith(init); | ||
} else { | ||
path.remove(); | ||
} | ||
} | ||
@@ -742,0 +778,0 @@ } |
{ | ||
"name": "babel-plugin-minify-dead-code-elimination", | ||
"version": "0.5.0-alpha.e9f96ffe", | ||
"version": "0.5.0", | ||
"description": "", | ||
@@ -15,7 +15,8 @@ "keywords": [ | ||
"dependencies": { | ||
"babel-helper-evaluate-path": "^0.5.0-alpha.e9f96ffe", | ||
"babel-helper-mark-eval-scopes": "^0.5.0-alpha.e9f96ffe", | ||
"babel-helper-remove-or-void": "^0.5.0-alpha.e9f96ffe", | ||
"babel-helper-evaluate-path": "^0.5.0", | ||
"babel-helper-mark-eval-scopes": "^0.4.3", | ||
"babel-helper-remove-or-void": "^0.4.3", | ||
"lodash.some": "^4.6.0" | ||
} | ||
}, | ||
"gitHead": "4de390008da4a486b37819109d2021a0957ad405" | ||
} |
@@ -34,3 +34,3 @@ # babel-plugin-minify-dead-code-elimination | ||
```sh | ||
npm install babel-plugin-minify-dead-code-elimination | ||
npm install babel-plugin-minify-dead-code-elimination --save-dev | ||
``` | ||
@@ -37,0 +37,0 @@ |
47801
5
1193
+ Addedbabel-helper-mark-eval-scopes@0.4.3(transitive)
+ Addedbabel-helper-remove-or-void@0.4.3(transitive)
- Removedbabel-helper-mark-eval-scopes@0.5.0-alpha.e9f96ffe(transitive)
- Removedbabel-helper-remove-or-void@0.5.0-alpha.e9f96ffe(transitive)