remove dead code

This commit is contained in:
Sebastian McKenzie 2015-05-25 15:28:23 +01:00
parent f639f4bea7
commit a195701de2
7 changed files with 13 additions and 46 deletions

View File

@ -34,13 +34,13 @@ export function ReferencedIdentifier(node, parent, scope) {
if (!replacement) return; if (!replacement) return;
// ensure it's a "pure" type // ensure it's a "pure" type
if (!scope.isPure(replacement)) return; if (!scope.isPure(replacement, true)) return;
if (t.isClass(replacement) || t.isFunction(replacement)) { if (t.isClass(replacement) || t.isFunction(replacement)) {
// don't change this if it's in a different scope, this can be bad // 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 // for performance since it may be inside a loop or deeply nested in
// hot code // hot code
if (!binding.path.scope.parent.is(scope)) return; if (binding.path.scope.parent !== scope) return;
} }
if (this.findParent((node) => node === replacement)) { if (this.findParent((node) => node === replacement)) {
@ -63,7 +63,7 @@ export function FunctionDeclaration(node, parent, scope) {
export { FunctionDeclaration as ClassDeclaration }; export { FunctionDeclaration as ClassDeclaration };
export function VariableDeclarator(node, parent, scope) { 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); FunctionDeclaration.apply(this, arguments);
} }

View File

@ -5,8 +5,7 @@ export var metadata = {
optional: true optional: true
}; };
export function Identifier(node, parent, scope, file) { export function ReferencedIdentifier(node, parent, scope, file) {
if (!this.isReferenced()) return;
if (scope.hasBinding(node.name)) return; if (scope.hasBinding(node.name)) return;
// get the closest declaration to offer as a suggestion // get the closest declaration to offer as a suggestion

View File

@ -69,7 +69,7 @@ export default class PathHoister {
if (scope.path.isFunction()) { if (scope.path.isFunction()) {
if (this.hasOwnParamBindings(scope)) { if (this.hasOwnParamBindings(scope)) {
// should ignore this scope since it's ourselves // 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 // needs to be attached to the body
return scope.path.get("body").get("body")[0]; return scope.path.get("body").get("body")[0];

View File

@ -2,8 +2,8 @@ import * as t from "../../../types";
export var ReferencedIdentifier = { export var ReferencedIdentifier = {
types: ["Identifier", "JSXIdentifier"], types: ["Identifier", "JSXIdentifier"],
checkPath(path, opts) { checkPath({ node, parent }, opts) {
return t.isReferencedIdentifier(path.node, path.parent, opts); return (t.isIdentifier(node, opts) || t.isJSXIdentifier(node, opts)) && t.isReferenced(node, parent);
} }
}; };

View File

@ -137,7 +137,7 @@ export default class Scope {
if (cached && cached.parent === parent) { if (cached && cached.parent === parent) {
return cached; return cached;
} else { } else {
//path.setData("scope", this); path.setData("scope", this);
} }
this.parent = parent; this.parent = parent;
@ -149,7 +149,7 @@ export default class Scope {
} }
static globals = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys)); static globals = flatten([globals.builtin, globals.browser, globals.node].map(Object.keys));
static contextVariables = ["this", "arguments", "super"]; static contextVariables = ["this", "arguments", "super", "undefined"];
/** /**
* Description * Description
@ -546,12 +546,14 @@ export default class Scope {
* Description * Description
*/ */
isPure(node) { isPure(node, constantsOnly) {
if (t.isIdentifier(node)) { if (t.isIdentifier(node)) {
var bindingInfo = this.getBinding(node.name); var bindingInfo = this.getBinding(node.name);
return bindingInfo && bindingInfo.constant; return !!bindingInfo && (!constantsOnly || (constantsOnly && bindingInfo.constant));
} else if (t.isClass(node)) { } else if (t.isClass(node)) {
return !node.superClass; return !node.superClass;
} else if (t.isBinary(node)) {
return this.isPure(node.left, constantsOnly) && this.isPure(node.right, constantsOnly);
} else { } else {
return t.isPure(node); return t.isPure(node);
} }

View File

@ -56,29 +56,3 @@ getBindingIdentifiers.keys = {
ArrayPattern: ["elements"], ArrayPattern: ["elements"],
ObjectPattern: ["properties"] ObjectPattern: ["properties"]
}; };
/**
* Description
*/
export function getLastStatements(node: Object): Array<Object> {
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;
}

View File

@ -112,14 +112,6 @@ export function isReferenced(node: Object, parent: Object): boolean {
return true; 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 * Check if the input `name` is a valid identifier name
* and isn't a reserved word. * and isn't a reserved word.