From b6d9675d7644c0634222ac6d122b4ed97b1711ce Mon Sep 17 00:00:00 2001 From: Vignesh Shanmugam Date: Fri, 11 May 2018 16:14:50 +0200 Subject: [PATCH] handle removed nodes in evaluation phase --- .../babel-helper-evaluate-path/src/index.js | 37 ++++++++++--------- .../src/index.js | 14 ++++--- .../__tests__/preset-tests.js | 1 - 3 files changed, 27 insertions(+), 25 deletions(-) diff --git a/packages/babel-helper-evaluate-path/src/index.js b/packages/babel-helper-evaluate-path/src/index.js index 43c49844c..abb866a70 100644 --- a/packages/babel-helper-evaluate-path/src/index.js +++ b/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 = { @@ -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 @@ -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 diff --git a/packages/babel-plugin-minify-dead-code-elimination/src/index.js b/packages/babel-plugin-minify-dead-code-elimination/src/index.js index 92ee8f130..a15b14e3f 100644 --- a/packages/babel-plugin-minify-dead-code-elimination/src/index.js +++ b/packages/babel-plugin-minify-dead-code-elimination/src/index.js @@ -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 = []; @@ -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) { @@ -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 => diff --git a/packages/babel-preset-minify/__tests__/preset-tests.js b/packages/babel-preset-minify/__tests__/preset-tests.js index dd3a6060d..11f016130 100644 --- a/packages/babel-preset-minify/__tests__/preset-tests.js +++ b/packages/babel-preset-minify/__tests__/preset-tests.js @@ -190,7 +190,6 @@ describe("preset", () => { `, ` var bar; - bar && alert('bug!'); ` ); });