Skip to content

Commit

Permalink
fix(deadcode): sequence expressions in switch discriminant (#881)
Browse files Browse the repository at this point in the history
* fix(deadcode): sequence expressions in switch discriminant

* fix null case to expressionStatement
  • Loading branch information
boopathi committed Jul 17, 2018
1 parent a24dd06 commit 650149b
Show file tree
Hide file tree
Showing 2 changed files with 72 additions and 7 deletions.
44 changes: 37 additions & 7 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Expand Up @@ -563,10 +563,37 @@ module.exports = ({ types: t, traverse }) => {

SwitchStatement: {
exit(path) {
const evaluated = evaluate(path.get("discriminant"), { tdz: this.tdz });
const discriminantPath = path.get("discriminant");
const evaluated = evaluate(discriminantPath, { tdz: this.tdz });

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;
const cases = path.get("cases");

Expand All @@ -582,7 +609,9 @@ module.exports = ({ types: t, traverse }) => {
continue;
}

const testResult = evaluate(test, { tdz: this.tdz });
const testResult = evaluate(test, {
tdz: this.tdz
});

// if we are not able to deternine a test during
// compile time, we terminate immediately
Expand Down Expand Up @@ -613,13 +642,14 @@ module.exports = ({ types: t, traverse }) => {
// we extract vars from the entire switch statement
// and there will be duplicates which
// will be again removed by DCE
replaceSwitch([...extractVars(path), ...result.statements]);
replaceSwitch([
...extractVars(path),
...beforeTest,
...result.statements
]);

function getStatementsUntilBreak(start) {
const result = {
bail: false,
statements: []
};
const result = { bail: false, statements: [] };

for (let i = start; i < cases.length; i++) {
const consequent = cases[i].get("consequent");
Expand Down
35 changes: 35 additions & 0 deletions packages/babel-preset-minify/__tests__/preset-tests.js
Expand Up @@ -170,4 +170,39 @@ describe("preset", () => {
var bar;
`
);

thePlugin(
"should fix issue#880 - switch test in deadcode after simplify",
`
(function () {
const test = 2;
console.log("before switch");
switch (test) {
case 1:
console.log("case 1");
break;
case 2:
console.log("case 2");
break;
case 3:
default:
console.log("case 3");
break;
}
console.log("after switch");
})();
`,
`
(function () {
console.log("before switch");
console.log("case 2");
console.log("after switch");
})();
`
);
});

0 comments on commit 650149b

Please sign in to comment.