remove many instances of lodash each in favor of for of loops

This commit is contained in:
Sebastian McKenzie 2015-05-09 17:22:01 +01:00
parent 87d879e236
commit bc9ae5ea8b
12 changed files with 42 additions and 39 deletions

View File

@ -43,13 +43,13 @@ export function JSXElement(node, print) {
if (open.selfClosing) return;
this.indent();
each(node.children, (child) => {
for (var child of (node.children: Array)) {
if (t.isLiteral(child)) {
this.push(child.value, true);
} else {
print(child);
}
});
}
this.dedent();
print(node.closingElement);

View File

@ -15,15 +15,15 @@ export function TemplateLiteral(node, print) {
var quasis = node.quasis;
var len = quasis.length;
each(quasis, (quasi, i) => {
print(quasi);
for (var i = 0; i < len; i++) {
print(quasis[i]);
if (i + 1 < len) {
this.push("${ ");
print(node.expressions[i]);
this.push(" }");
}
});
}
this._push("`");
}

View File

@ -54,7 +54,8 @@ export function ArrayExpression(node, print) {
this.push("[");
each(elems, (elem, i) => {
for (var i = 0; i < elems.length; i++) {
var elem = elems[i];
if (!elem) {
// If the array expression ends with a hole, that hole
// will be ignored by the interpreter, but if it ends with
@ -67,7 +68,7 @@ export function ArrayExpression(node, print) {
print(elem);
if (i < len - 1) this.push(",");
}
});
}
this.push("]");
}

View File

@ -106,9 +106,9 @@ class CodeGenerator {
this.print(ast);
var comments = [];
each(ast.comments, function (comment) {
for (var comment of (ast.comments: Array)) {
if (!comment._displayed) comments.push(comment);
});
}
this._printComments(comments);
return {
@ -249,7 +249,8 @@ class CodeGenerator {
if (opts.indent) this.indent();
each(nodes, (node, i) => {
for (var i = 0; i < nodes.length; i++) {
var node = nodes[i];
print(node, {
statement: opts.statement,
addNewlines: opts.addNewlines,
@ -314,9 +315,9 @@ class CodeGenerator {
nodes.push(node.argument);
}
each(nodes, (node) => {
for (var node of (nodes: Array)) {
comments = comments.concat(this._getComments(key, node));
});
}
return comments;
}
@ -331,19 +332,19 @@ class CodeGenerator {
if (!this.format.comments) return;
if (!comments || !comments.length) return;
each(comments, (comment) => {
for (var comment of comments, (comment) => {
var skip = false;
// find the original comment in the ast and set it as displayed
each(this.ast.comments, function (origComment) {
for (var origComment of (this.ast.comments: Array)) {
if (origComment.start === comment.start) {
// comment has already been output
if (origComment._displayed) skip = true;
origComment._displayed = true;
return false;
break;
}
});
}
if (skip) return;
@ -387,7 +388,7 @@ class CodeGenerator {
// whitespace after
this.newline(this.whitespace.getNewlinesAfter(comment));
});
}
}
}

View File

@ -24,7 +24,6 @@ import clone from "lodash/lang/clone";
import * as util from "../../util";
import * as api from "../../api/node";
import path from "path";
import each from "lodash/collection/each";
import * as t from "../../types";
export default class File {
@ -190,7 +189,8 @@ export default class File {
var stack = [];
// build internal transformers
each(this.pipeline.transformers, function (transformer, key) {
for (var key in this.pipeline.transformers) {
var transformer = this.pipeline.transformers[key];
var pass = transformers[key] = transformer.buildPass(file);
if (pass.canTransform()) {
@ -204,7 +204,7 @@ export default class File {
transformer.manipulateOptions(file.opts, file);
}
}
});
}
// init plugins!
var beforePlugins = [];
@ -487,9 +487,9 @@ export default class File {
this.log.debug("End module formatter init");
this.call("pre");
each(this.transformerStack, function (pass) {
for (var pass of (this.transformerStack: Array)) {
pass.transform();
});
}
this.call("post");
}

View File

@ -60,12 +60,12 @@ var runnerSettersVisitor = {
enter(node, parent, scope, state) {
if (node._importSource === state.source) {
if (t.isVariableDeclaration(node)) {
each(node.declarations, function (declar) {
for (var declar of (node.declarations: Array)) {
state.hoistDeclarators.push(t.variableDeclarator(declar.id));
state.nodes.push(t.expressionStatement(
t.assignmentExpression("=", declar.id, declar.init)
));
});
}
} else {
state.nodes.push(node);
}

View File

@ -22,7 +22,7 @@ var hoistVariablesVisitor = explode({
VariableDeclaration(node, parent, scope) {
if (node.kind !== "var") return;
var bindings = this.getBindingIdentifiers();
var bindings = this.getBindingIdentifiers();
for (var key in bindings) {
scope.push({ id: bindings[key] });
}

View File

@ -14,10 +14,10 @@ import * as t from "../types";
var functionVariableVisitor = {
enter(node, parent, scope, state) {
if (t.isFor(node)) {
each(t.FOR_INIT_KEYS, (key) => {
for (var key of (t.FOR_INIT_KEYS: Array)) {
var declar = this.get(key);
if (declar.isVar()) state.scope.registerBinding("var", declar);
});
}
}
// this block is a function so we'll stop since none of the variables

View File

@ -51,7 +51,7 @@ export function toSequenceExpression(nodes: Array<Object>, scope: Scope): Object
} else if (t.isVariableDeclaration(node)) {
if (node.kind !== "var") return bailed = true; // bailed
each(node.declarations, function (declar) {
for (var declar of (node.declarations: Array)) {
var bindings = t.getBindingIdentifiers(declar);
for (var key in bindings) {
declars.push({
@ -63,7 +63,7 @@ export function toSequenceExpression(nodes: Array<Object>, scope: Scope): Object
if (declar.init) {
exprs.push(t.assignmentExpression("=", declar.id, declar.init));
}
});
}
ensureLastUndefined = true;
continue;

View File

@ -258,9 +258,9 @@ export function buildMatchMemberExpression(match:string, allowPartial?: boolean)
*/
export function removeComments(child: Object): Object {
each(COMMENT_KEYS, function (key) {
for (var key of (COMMENT_KEYS: Array)) {
delete child[key];
});
}
return child;
}
@ -270,9 +270,9 @@ export function removeComments(child: Object): Object {
export function inheritsComments(child: Object, parent: Object): Object {
if (child && parent) {
each(COMMENT_KEYS, function (key) {
for (var key of (COMMENT_KEYS: Array)) {
child[key] = uniq(compact([].concat(child[key], parent[key])));
});
}
}
return child;
}

View File

@ -1,4 +1,3 @@
import isString from "lodash/lang/isString";
import esutils from "esutils";
import * as t from "./index";
@ -115,8 +114,11 @@ export function isReferencedIdentifier(node: Object, parent: Object, opts?: Obje
*/
export function isValidIdentifier(name: string): boolean {
if (!isString(name) || esutils.keyword.isReservedWordES6(name, true)) return false;
return esutils.keyword.isIdentifierNameES6(name);
if (typeof name !== "string" || esutils.keyword.isReservedWordES6(name, true)) {
return false;
} else {
return esutils.keyword.isIdentifierNameES6(name);
}
}
/**

View File

@ -14,7 +14,6 @@ import Module from "module";
import isEmpty from "lodash/lang/isEmpty";
import parse from "./helpers/parse";
import path from "path";
import each from "lodash/collection/each";
import has from "lodash/object/has";
import fs from "fs";
import * as t from "./types";
@ -166,7 +165,7 @@ function loadTemplates() {
throw new ReferenceError(messages.get("missingTemplatesDirectory"));
}
each(fs.readdirSync(templatesLoc), function (name) {
for (var name of (fs.readdirSync(templatesLoc): Array)) {
if (name[0] === ".") return;
var key = path.basename(name, path.extname(name));
@ -174,7 +173,7 @@ function loadTemplates() {
var code = fs.readFileSync(loc, "utf8");
templates[key] = parseTemplate(loc, code);
});
}
return templates;
}