From bc9ae5ea8b9d11b1b8a810c7a595365ae85b3f99 Mon Sep 17 00:00:00 2001 From: Sebastian McKenzie Date: Sat, 9 May 2015 17:22:01 +0100 Subject: [PATCH] remove many instances of lodash each in favor of for of loops --- src/babel/generation/generators/jsx.js | 4 ++-- .../generators/template-literals.js | 6 +++--- src/babel/generation/generators/types.js | 5 +++-- src/babel/generation/index.js | 21 ++++++++++--------- src/babel/transformation/file/index.js | 10 ++++----- src/babel/transformation/modules/system.js | 4 ++-- src/babel/traversal/path/index.js | 2 +- src/babel/traversal/scope.js | 4 ++-- src/babel/types/converters.js | 4 ++-- src/babel/types/index.js | 8 +++---- src/babel/types/validators.js | 8 ++++--- src/babel/util.js | 5 ++--- 12 files changed, 42 insertions(+), 39 deletions(-) diff --git a/src/babel/generation/generators/jsx.js b/src/babel/generation/generators/jsx.js index 476c330dd7..03d24af495 100644 --- a/src/babel/generation/generators/jsx.js +++ b/src/babel/generation/generators/jsx.js @@ -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); diff --git a/src/babel/generation/generators/template-literals.js b/src/babel/generation/generators/template-literals.js index dded1ad15b..6f8d7ca873 100644 --- a/src/babel/generation/generators/template-literals.js +++ b/src/babel/generation/generators/template-literals.js @@ -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("`"); } diff --git a/src/babel/generation/generators/types.js b/src/babel/generation/generators/types.js index 400f9508b7..c686dc7acf 100644 --- a/src/babel/generation/generators/types.js +++ b/src/babel/generation/generators/types.js @@ -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("]"); } diff --git a/src/babel/generation/index.js b/src/babel/generation/index.js index 790354d645..ecd46f1e26 100644 --- a/src/babel/generation/index.js +++ b/src/babel/generation/index.js @@ -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)); - }); + } } } diff --git a/src/babel/transformation/file/index.js b/src/babel/transformation/file/index.js index 27f0453ed8..70ea6726e9 100644 --- a/src/babel/transformation/file/index.js +++ b/src/babel/transformation/file/index.js @@ -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"); } diff --git a/src/babel/transformation/modules/system.js b/src/babel/transformation/modules/system.js index 4813084446..fe8776a421 100644 --- a/src/babel/transformation/modules/system.js +++ b/src/babel/transformation/modules/system.js @@ -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); } diff --git a/src/babel/traversal/path/index.js b/src/babel/traversal/path/index.js index 8ddff8c401..4b1dba5972 100644 --- a/src/babel/traversal/path/index.js +++ b/src/babel/traversal/path/index.js @@ -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] }); } diff --git a/src/babel/traversal/scope.js b/src/babel/traversal/scope.js index 86cae263d2..7af484f2e0 100644 --- a/src/babel/traversal/scope.js +++ b/src/babel/traversal/scope.js @@ -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 diff --git a/src/babel/types/converters.js b/src/babel/types/converters.js index 9a8faec76e..54672dee5b 100644 --- a/src/babel/types/converters.js +++ b/src/babel/types/converters.js @@ -51,7 +51,7 @@ export function toSequenceExpression(nodes: Array, 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, scope: Scope): Object if (declar.init) { exprs.push(t.assignmentExpression("=", declar.id, declar.init)); } - }); + } ensureLastUndefined = true; continue; diff --git a/src/babel/types/index.js b/src/babel/types/index.js index afefeac01e..dc48fc1fca 100644 --- a/src/babel/types/index.js +++ b/src/babel/types/index.js @@ -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; } diff --git a/src/babel/types/validators.js b/src/babel/types/validators.js index f5985625e4..997c7bfb6e 100644 --- a/src/babel/types/validators.js +++ b/src/babel/types/validators.js @@ -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); + } } /** diff --git a/src/babel/util.js b/src/babel/util.js index d042756832..f92aabadd5 100644 --- a/src/babel/util.js +++ b/src/babel/util.js @@ -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; }