Skip to content

Commit

Permalink
handle removed nodes in evaluation phase
Browse files Browse the repository at this point in the history
  • Loading branch information
vigneshshanmugam committed May 11, 2018
1 parent feee1ec commit b6d9675
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 25 deletions.
37 changes: 19 additions & 18 deletions packages/babel-helper-evaluate-path/src/index.js
@@ -1,12 +1,14 @@
"use strict";

const t = require("@babel/types");

module.exports = function evaluate(path, { tdz = false } = {}) {
if (path.isReferencedIdentifier()) {
return evaluateIdentifier(path);
if (!tdz && !path.isReferencedIdentifier()) {
return baseEvaluate(path);
}

if (!tdz) {
return baseEvaluate(path);
if (path.isReferencedIdentifier()) {
return evaluateIdentifier(path);
}

const state = {
Expand Down Expand Up @@ -119,26 +121,27 @@ function evaluateBasedOnControlFlow(binding, refPath) {
if (binding.kind === "var") {
// early-exit
const declaration = binding.path.parentPath;

if (
declaration.parentPath.isIfStatement() ||
declaration.parentPath.isLoop() ||
declaration.parentPath.isSwitchCase()
t.isIfStatement(declaration.parentPath) ||
t.isLoop(declaration.parentPath) ||
t.isSwitchCase(declaration.parentPath)
) {
if (declaration.parentPath.removed) {
return { confident: true, value: void 0 };
}
return { shouldDeopt: true };
}

const fnParent = binding.path.getFunctionParent();
if (!fnParent) {
return { shouldDeopt: true };
}
const fnParent = (
binding.path.scope.getFunctionParent() ||
binding.path.scope.getProgramParent()
).path;

let blockParent = binding.path.scope.getBlockParent().path;
if (!blockParent) {
return { shouldDeopt: true };
}

if (blockParent === fnParent) {
if (!fnParent.isProgram()) blockParent = blockParent.get("body");
if (blockParent === fnParent && !fnParent.isProgram()) {
blockParent = blockParent.get("body");
}

// detect Usage Outside Init Scope
Expand All @@ -164,8 +167,6 @@ function evaluateBasedOnControlFlow(binding, refPath) {
) {
return { confident: true, value: void 0 };
}

return { shouldDeopt: true };
}
} else if (binding.kind === "let" || binding.kind === "const") {
// binding.path is the declarator
Expand Down
14 changes: 8 additions & 6 deletions packages/babel-plugin-minify-dead-code-elimination/src/index.js
Expand Up @@ -5,6 +5,11 @@ const { markEvalScopes, hasEval } = require("babel-helper-mark-eval-scopes");
const removeUseStrict = require("./remove-use-strict");
const evaluate = require("babel-helper-evaluate-path");

function evaluateTruthy(path) {
const res = evaluate(path);
if (res.confident) return !!res.value;
}

function prevSiblings(path) {
const parentPath = path.parentPath;
const siblings = [];
Expand Down Expand Up @@ -548,7 +553,7 @@ module.exports = ({ types: t, traverse }) => {

ConditionalExpression(path) {
const { node } = path;
const evaluateTest = path.get("test").evaluateTruthy();
const evaluateTest = evaluateTruthy(path.get("test"));
if (evaluateTest === true) {
path.replaceWith(node.consequent);
} else if (evaluateTest === false) {
Expand Down Expand Up @@ -976,14 +981,11 @@ module.exports = ({ types: t, traverse }) => {
} else {
path.traverse({
VariableDeclaration(varPath) {
const { node } = varPath;
if (!varPath.isVariableDeclaration({ kind: "var" })) return;

if (node.kind !== "var") {
return;
}
if (!isSameFunctionScope(varPath, path)) return;

for (const decl of node.declarations) {
for (const decl of varPath.node.declarations) {
const bindingIds = Object.keys(t.getBindingIdentifiers(decl.id));
declarators.push(
...bindingIds.map(name =>
Expand Down
1 change: 0 additions & 1 deletion packages/babel-preset-minify/__tests__/preset-tests.js
Expand Up @@ -190,7 +190,6 @@ describe("preset", () => {
`,
`
var bar;
bar && alert('bug!');
`
);
});

0 comments on commit b6d9675

Please sign in to comment.