From 658f13e030e1c526a44bfb1714982348dd3e9541 Mon Sep 17 00:00:00 2001 From: Andres Suarez Date: Sun, 25 Dec 2016 23:50:49 -0500 Subject: [PATCH] Replace uses of "lodash/each" with native equivalents --- packages/babel-cli/src/babel/index.js | 9 ++++--- .../src/tools/build-external-helpers.js | 3 +-- .../babel-generator/src/node/whitespace.js | 20 +++++++------- packages/babel-helper-define-map/src/index.js | 10 ++++--- packages/babel-register/src/node.js | 6 ++--- packages/babel-runtime/scripts/build-dist.js | 26 ++++++++++--------- packages/babel-types/src/index.js | 13 +++++----- 7 files changed, 45 insertions(+), 42 deletions(-) diff --git a/packages/babel-cli/src/babel/index.js b/packages/babel-cli/src/babel/index.js index 0e1472d953..3f7f143987 100755 --- a/packages/babel-cli/src/babel/index.js +++ b/packages/babel-cli/src/babel/index.js @@ -9,10 +9,10 @@ const kebabCase = require("lodash/kebabCase"); const options = require("babel-core").options; const util = require("babel-core").util; const uniq = require("lodash/uniq"); -const each = require("lodash/each"); const glob = require("glob"); -each(options, function (option, key) { +Object.keys(options).forEach(function (key) { + const option = options[key]; if (option.hidden) return; let arg = kebabCase(key); @@ -69,7 +69,7 @@ let filenames = commander.args.reduce(function (globbed, input) { filenames = uniq(filenames); -each(filenames, function (filename) { +filenames.forEach(function (filename) { if (!fs.existsSync(filename)) { errors.push(filename + " doesn't exist"); } @@ -106,7 +106,8 @@ if (errors.length) { const opts = exports.opts = {}; -each(options, function (opt, key) { +Object.keys(options).forEach(function (key) { + const opt = options[key]; if (commander[key] !== undefined && commander[key] !== opt.default) { opts[key] = commander[key]; } diff --git a/packages/babel-core/src/tools/build-external-helpers.js b/packages/babel-core/src/tools/build-external-helpers.js index 237daf532c..b97d25c2d1 100644 --- a/packages/babel-core/src/tools/build-external-helpers.js +++ b/packages/babel-core/src/tools/build-external-helpers.js @@ -4,7 +4,6 @@ import * as helpers from "babel-helpers"; import generator from "babel-generator"; import * as messages from "babel-messages"; import template from "babel-template"; -import each from "lodash/each"; import * as t from "babel-types"; const buildUmdWrapper = template(` @@ -73,7 +72,7 @@ function buildVar(namespace, builder) { } function buildHelpers(body, namespace, whitelist) { - each(helpers.list, function (name) { + helpers.list.forEach(function (name) { if (whitelist && whitelist.indexOf(name) < 0) return; const key = t.identifier(name); diff --git a/packages/babel-generator/src/node/whitespace.js b/packages/babel-generator/src/node/whitespace.js index c3d7c89a61..1c47e594ec 100644 --- a/packages/babel-generator/src/node/whitespace.js +++ b/packages/babel-generator/src/node/whitespace.js @@ -1,4 +1,3 @@ -import each from "lodash/each"; import map from "lodash/map"; import * as t from "babel-types"; @@ -211,19 +210,18 @@ exports.list = { * Add whitespace tests for nodes and their aliases. */ -each({ - Function: true, - Class: true, - Loop: true, - LabeledStatement: true, - SwitchStatement: true, - TryStatement: true -}, function (amounts, type) { +[ + ["Function", true], + ["Class", true], + ["Loop", true], + ["LabeledStatement", true], + ["SwitchStatement", true], + ["TryStatement", true] +].forEach(function ([type, amounts]) { if (typeof amounts === "boolean") { amounts = { after: amounts, before: amounts }; } - - each([type].concat(t.FLIPPED_ALIAS_KEYS[type] || []), function (type) { + [type].concat(t.FLIPPED_ALIAS_KEYS[type] || []).forEach(function (type) { exports.nodes[type] = function () { return amounts; }; diff --git a/packages/babel-helper-define-map/src/index.js b/packages/babel-helper-define-map/src/index.js index 09de4f9b58..05fa5ee886 100644 --- a/packages/babel-helper-define-map/src/index.js +++ b/packages/babel-helper-define-map/src/index.js @@ -1,7 +1,6 @@ /* eslint max-len: 0 */ import nameFunction from "babel-helper-function-name"; -import each from "lodash/each"; import has from "lodash/has"; import * as t from "babel-types"; @@ -101,12 +100,14 @@ export function toComputedObjectFromClass(obj: Object): Object { export function toClassObject(mutatorMap: Object): Object { const objExpr = t.objectExpression([]); - each(mutatorMap, function (map) { + Object.keys(mutatorMap).forEach(function (mutatorMapKey) { + const map = mutatorMap[mutatorMapKey]; const mapNode = t.objectExpression([]); const propNode = t.objectProperty(map._key, mapNode, map._computed); - each(map, function (node, key) { + Object.keys(map).forEach(function (key) { + let node = map[key]; if (key[0] === "_") return; const inheritNode = node; @@ -126,7 +127,8 @@ export function toClassObject(mutatorMap: Object): Object { } export function toDefineObject(mutatorMap: Object): Object { - each(mutatorMap, function (map) { + Object.keys(mutatorMap).forEach(function (key) { + const map = mutatorMap[key]; if (map.value) map.writable = t.booleanLiteral(true); map.configurable = t.booleanLiteral(true); map.enumerable = t.booleanLiteral(true); diff --git a/packages/babel-register/src/node.js b/packages/babel-register/src/node.js index 19e052f500..87f92d7a24 100644 --- a/packages/babel-register/src/node.js +++ b/packages/babel-register/src/node.js @@ -3,7 +3,6 @@ import sourceMapSupport from "source-map-support"; import * as registerCache from "./cache"; import extend from "lodash/extend"; import * as babel from "babel-core"; -import each from "lodash/each"; import { util, OptionManager } from "babel-core"; import fs from "fs"; import path from "path"; @@ -112,7 +111,8 @@ function registerExtension(ext) { } function hookExtensions(_exts) { - each(oldHandlers, function (old, ext) { + Object.keys(oldHandlers).forEach(function (ext) { + const old = oldHandlers[ext]; if (old === undefined) { delete require.extensions[ext]; } else { @@ -122,7 +122,7 @@ function hookExtensions(_exts) { oldHandlers = {}; - each(_exts, function (ext) { + _exts.forEach(function (ext) { oldHandlers[ext] = require.extensions[ext]; registerExtension(ext); }); diff --git a/packages/babel-runtime/scripts/build-dist.js b/packages/babel-runtime/scripts/build-dist.js index 2b06be4c79..8933c6e135 100644 --- a/packages/babel-runtime/scripts/build-dist.js +++ b/packages/babel-runtime/scripts/build-dist.js @@ -1,34 +1,36 @@ var outputFile = require("output-file-sync"); var kebabCase = require("lodash/kebabCase"); -var each = require("lodash/each"); var fs = require("fs"); var coreDefinitions = require("babel-plugin-transform-runtime").definitions; var paths = ["is-iterable", "get-iterator"]; -each(coreDefinitions.builtins, function (path) { +Object.keys(coreDefinitions.builtins).forEach(function (key) { + const path = coreDefinitions.builtins[key]; paths.push(path); }); -each(coreDefinitions.methods, function (props) { - each(props, function (path) { +Object.keys(coreDefinitions.methods).forEach(function (key) { + const props = coreDefinitions.methods[key]; + Object.keys(props).forEach(function (key2) { + const path = props[key2]; paths.push(path); }); }); -each(paths, function (path) { +paths.forEach(function (path) { writeFile("core-js/" + path + ".js", defaultify('require("core-js/library/fn/' + path + '")')); }); // Should be removed in the next major release: -var legacy = { - "string/pad-left": "string/pad-start", - "string/pad-right": "string/pad-end" -}; +var legacy = [ + ["string/pad-left", "string/pad-start"], + ["string/pad-right", "string/pad-end"] +]; -each(legacy, function (value, key) { - writeFile("core-js/" + key + ".js", defaultify('require("core-js/library/fn/' + value + '")')); +legacy.forEach(function([a, b]) { + writeFile("core-js/" + a + ".js", defaultify('require("core-js/library/fn/' + b + '")')); }); var helpers = require("babel-helpers"); @@ -118,7 +120,7 @@ function buildHelper(helperName) { }).code; } -each(helpers.list, function (helperName) { +helpers.list.forEach(function (helperName) { writeFile("helpers/" + helperName + ".js", buildHelper(helperName)); // compat diff --git a/packages/babel-types/src/index.js b/packages/babel-types/src/index.js index 09cbe39ad1..54edc7d981 100644 --- a/packages/babel-types/src/index.js +++ b/packages/babel-types/src/index.js @@ -1,7 +1,6 @@ import toFastProperties from "to-fast-properties"; import compact from "lodash/compact"; import loClone from "lodash/clone"; -import each from "lodash/each"; import uniq from "lodash/uniq"; const t = exports; @@ -72,8 +71,8 @@ for (const type in t.VISITOR_KEYS) { t.FLIPPED_ALIAS_KEYS = {}; -each(t.ALIAS_KEYS, function (aliases, type) { - each(aliases, function (alias) { +Object.keys(t.ALIAS_KEYS).forEach(function (type) { + t.ALIAS_KEYS[type].forEach(function (alias) { const types = t.FLIPPED_ALIAS_KEYS[alias] = t.FLIPPED_ALIAS_KEYS[alias] || []; types.push(type); }); @@ -83,8 +82,8 @@ each(t.ALIAS_KEYS, function (aliases, type) { * Registers `is[Alias]` and `assert[Alias]` functions for all aliases. */ -each(t.FLIPPED_ALIAS_KEYS, function (types, type) { - t[type.toUpperCase() + "_TYPES"] = types; +Object.keys(t.FLIPPED_ALIAS_KEYS).forEach(function (type) { + t[type.toUpperCase() + "_TYPES"] = t.FLIPPED_ALIAS_KEYS[type]; registerType(type); }); @@ -139,7 +138,9 @@ export function isType(nodeType: string, targetType: string): boolean { * Description */ -each(t.BUILDER_KEYS, function (keys, type) { +Object.keys(t.BUILDER_KEYS).forEach(function (type) { + const keys = t.BUILDER_KEYS[type]; + function builder() { if (arguments.length > keys.length) { throw new Error(