Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(deadcode): sequence expressions in switch discriminant #881

Merged
merged 2 commits into from Jul 17, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
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");
})();
`
);
});