diff --git a/src/babel/transformation/transformers/minification/dead-code-elimination.js b/src/babel/transformation/transformers/minification/dead-code-elimination.js index b99b1a5486..ec94d976b3 100644 --- a/src/babel/transformation/transformers/minification/dead-code-elimination.js +++ b/src/babel/transformation/transformers/minification/dead-code-elimination.js @@ -34,13 +34,13 @@ export function ReferencedIdentifier(node, parent, scope) { if (!replacement) return; // ensure it's a "pure" type - if (!scope.isPure(replacement)) return; + if (!scope.isPure(replacement, true)) return; if (t.isClass(replacement) || t.isFunction(replacement)) { // don't change this if it's in a different scope, this can be bad // for performance since it may be inside a loop or deeply nested in // hot code - if (!binding.path.scope.parent.is(scope)) return; + if (binding.path.scope.parent !== scope) return; } if (this.findParent((node) => node === replacement)) { @@ -63,7 +63,7 @@ export function FunctionDeclaration(node, parent, scope) { export { FunctionDeclaration as ClassDeclaration }; export function VariableDeclarator(node, parent, scope) { - if (!t.isIdentifier(node.id) || !scope.isPure(node.init)) return; + if (!t.isIdentifier(node.id) || !scope.isPure(node.init, true)) return; FunctionDeclaration.apply(this, arguments); } diff --git a/src/babel/transformation/transformers/validation/undeclared-variable-check.js b/src/babel/transformation/transformers/validation/undeclared-variable-check.js index f8d3b25a78..acb6ad1087 100644 --- a/src/babel/transformation/transformers/validation/undeclared-variable-check.js +++ b/src/babel/transformation/transformers/validation/undeclared-variable-check.js @@ -5,8 +5,7 @@ export var metadata = { optional: true }; -export function Identifier(node, parent, scope, file) { - if (!this.isReferenced()) return; +export function ReferencedIdentifier(node, parent, scope, file) { if (scope.hasBinding(node.name)) return; // get the closest declaration to offer as a suggestion diff --git a/src/babel/traversal/path/lib/hoister.js b/src/babel/traversal/path/lib/hoister.js index 5964e6c088..9ee6ddd291 100644 --- a/src/babel/traversal/path/lib/hoister.js +++ b/src/babel/traversal/path/lib/hoister.js @@ -69,7 +69,7 @@ export default class PathHoister { if (scope.path.isFunction()) { if (this.hasOwnParamBindings(scope)) { // should ignore this scope since it's ourselves - if (this.scope.is(scope)) return; + if (this.scope === scope) return; // needs to be attached to the body return scope.path.get("body").get("body")[0]; diff --git a/src/babel/traversal/path/lib/virtual-types.js b/src/babel/traversal/path/lib/virtual-types.js index 7f6ba09015..0698e92ec0 100644 --- a/src/babel/traversal/path/lib/virtual-types.js +++ b/src/babel/traversal/path/lib/virtual-types.js @@ -2,8 +2,8 @@ import * as t from "../../../types"; export var ReferencedIdentifier = { types: ["Identifier", "JSXIdentifier"], - checkPath(path, opts) { - return t.isReferencedIdentifier(path.node, path.parent, opts); + checkPath({ node, parent }, opts) { + return (t.isIdentifier(node, opts) || t.isJSXIdentifier(node, opts)) && t.isReferenced(node, parent); } }; diff --git a/src/babel/traversal/scope/index.js b/src/babel/traversal/scope/index.js index 15c2c8b64d..92fc9cc6c1 100644 --- a/src/babel/traversal/scope/index.js +++ b/src/babel/traversal/scope/index.js @@ -137,7 +137,7 @@ export default class Scope { if (cached && cached.parent === parent) { return cached; } else { - //path.setData("scope", this); + path.setData("scope", this); } this.parent = parent; @@ -149,7 +149,7 @@ export default class Scope { } static globals = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys)); - static contextVariables = ["this", "arguments", "super"]; + static contextVariables = ["this", "arguments", "super", "undefined"]; /** * Description @@ -546,12 +546,14 @@ export default class Scope { * Description */ - isPure(node) { + isPure(node, constantsOnly) { if (t.isIdentifier(node)) { var bindingInfo = this.getBinding(node.name); - return bindingInfo && bindingInfo.constant; + return !!bindingInfo && (!constantsOnly || (constantsOnly && bindingInfo.constant)); } else if (t.isClass(node)) { return !node.superClass; + } else if (t.isBinary(node)) { + return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly); } else { return t.isPure(node); } diff --git a/src/babel/types/retrievers.js b/src/babel/types/retrievers.js index d325aded1f..fd0c20e351 100644 --- a/src/babel/types/retrievers.js +++ b/src/babel/types/retrievers.js @@ -56,29 +56,3 @@ getBindingIdentifiers.keys = { ArrayPattern: ["elements"], ObjectPattern: ["properties"] }; - -/** - * Description - */ - -export function getLastStatements(node: Object): Array { - var nodes = []; - - var add = function (node) { - nodes = nodes.concat(getLastStatements(node)); - }; - - if (t.isIfStatement(node)) { - add(node.consequent); - add(node.alternate); - } else if (t.isFor(node) || t.isWhile(node)) { - add(node.body); - } else if (t.isProgram(node) || t.isBlockStatement(node)) { - add(node.body[node.body.length - 1]); - } else if (t.isLoop()) { - } else if (node) { - nodes.push(node); - } - - return nodes; -} diff --git a/src/babel/types/validators.js b/src/babel/types/validators.js index 9a066321f2..d393cf40a3 100644 --- a/src/babel/types/validators.js +++ b/src/babel/types/validators.js @@ -112,14 +112,6 @@ export function isReferenced(node: Object, parent: Object): boolean { return true; } -/** - * Check if the input `node` is an `Identifier` and `isReferenced`. - */ - -export function isReferencedIdentifier(node: Object, parent: Object, opts?: Object): boolean { - return (t.isIdentifier(node, opts) || t.isJSXIdentifier(node, opts)) && t.isReferenced(node, parent); -} - /** * Check if the input `name` is a valid identifier name * and isn't a reserved word.