Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Use native or lodash util module where full "lodash" is required #5042

Merged
merged 1 commit into from Jan 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
5 changes: 2 additions & 3 deletions packages/babel-cli/src/_babel-node.js
Expand Up @@ -7,7 +7,6 @@ import repl from "repl";
import { util } from "babel-core";
import * as babel from "babel-core";
import vm from "vm";
import _ from "lodash";
import "babel-polyfill";
import register from "babel-register";

Expand Down Expand Up @@ -94,7 +93,7 @@ if (program.eval || program.print) {

const result = _eval(code, global.__filename);
if (program.print) {
const output = _.isString(result) ? result : inspect(result);
const output = typeof result === "string" ? result : inspect(result);
process.stdout.write(output + "\n");
}
} else {
Expand All @@ -104,7 +103,7 @@ if (program.eval || program.print) {

let i = 0;
let ignoreNext = false;
_.each(args, function (arg, i2) {
args.forEach(function (arg, i2) {
if (ignoreNext) {
ignoreNext = false;
return;
Expand Down
9 changes: 4 additions & 5 deletions packages/babel-cli/src/babel/dir.js
Expand Up @@ -3,7 +3,6 @@ const slash = require("slash");
const path = require("path");
const util = require("./util");
const fs = require("fs");
const _ = require("lodash");

module.exports = function (commander, filenames) {
function write(src, relative) {
Expand Down Expand Up @@ -51,7 +50,7 @@ module.exports = function (commander, filenames) {
if (stat.isDirectory(filename)) {
const dirname = filename;

_.each(util.readdir(dirname), function (filename) {
util.readdir(dirname).forEach(function (filename) {
const src = path.join(dirname, filename);
handleFile(src, filename);
});
Expand All @@ -61,19 +60,19 @@ module.exports = function (commander, filenames) {
}

if (!commander.skipInitialBuild) {
_.each(filenames, handle);
filenames.forEach(handle);
}

if (commander.watch) {
const chokidar = util.requireChokidar();

_.each(filenames, function (dirname) {
filenames.forEach(function (dirname) {
const watcher = chokidar.watch(dirname, {
persistent: true,
ignoreInitial: true
});

_.each(["add", "change"], function (type) {
["add", "change"].forEach(function (type) {
watcher.on(type, function (filename) {
const relative = path.relative(dirname, filename) || filename;
try {
Expand Down
9 changes: 4 additions & 5 deletions packages/babel-cli/src/babel/file.js
Expand Up @@ -4,7 +4,6 @@ const slash = require("slash");
const path = require("path");
const util = require("./util");
const fs = require("fs");
const _ = require("lodash");

module.exports = function (commander, filenames, opts) {
if (commander.sourceMaps === "inline") {
Expand All @@ -22,7 +21,7 @@ module.exports = function (commander, filenames, opts) {
let code = "";
let offset = 0;

_.each(results, function (result) {
results.forEach(function (result) {
code += result.code + "\n";

if (result.map) {
Expand Down Expand Up @@ -107,22 +106,22 @@ module.exports = function (commander, filenames, opts) {
const _filenames = [];
results = [];

_.each(filenames, function (filename) {
filenames.forEach(function (filename) {
if (!fs.existsSync(filename)) return;

const stat = fs.statSync(filename);
if (stat.isDirectory()) {
const dirname = filename;

_.each(util.readdirFilter(filename), function (filename) {
util.readdirFilter(filename).forEach(function (filename) {
_filenames.push(path.join(dirname, filename));
});
} else {
_filenames.push(filename);
}
});

_.each(_filenames, function (filename) {
_filenames.forEach(function (filename) {
if (util.shouldIgnore(filename)) return;

let sourceFilename = filename;
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-cli/src/babel/util.js
@@ -1,11 +1,11 @@
const commander = require("commander");
const defaults = require("lodash/defaults");
const readdir = require("fs-readdir-recursive");
const index = require("./index");
const babel = require("babel-core");
const util = require("babel-core").util;
const path = require("path");
const fs = require("fs");
const _ = require("lodash");

export function chmod(src, dest) {
fs.chmodSync(dest, fs.statSync(src).mode);
Expand Down Expand Up @@ -34,7 +34,7 @@ export function log(msg) {
}

export function transform(filename, code, opts) {
opts = _.defaults(opts || {}, index.opts);
opts = defaults(opts || {}, index.opts);
opts.filename = filename;

const result = babel.transform(code, opts);
Expand Down
23 changes: 13 additions & 10 deletions packages/babel-cli/test/index.js
@@ -1,13 +1,14 @@
const includes = require("lodash/includes");
const readdir = require("fs-readdir-recursive");
const helper = require("babel-helper-fixtures");
const assert = require("assert");
const rimraf = require("rimraf");
const outputFileSync = require("output-file-sync");
const child = require("child_process");
const merge = require("lodash/merge");
const path = require("path");
const chai = require("chai");
const fs = require("fs");
const _ = require("lodash");

const fixtureLoc = path.join(__dirname, "fixtures");
const tmpLoc = path.join(__dirname, "tmp");
Expand All @@ -25,15 +26,16 @@ const pluginLocs = [
const readDir = function (loc) {
const files = {};
if (fs.existsSync(loc)) {
_.each(readdir(loc), function (filename) {
readdir(loc).forEach(function (filename) {
files[filename] = helper.readFile(path.join(loc, filename));
});
}
return files;
};

const saveInFiles = function (files) {
_.each(files, function (content, filename) {
Object.keys(files).forEach(function (filename) {
const content = files[filename];
outputFileSync(filename, content);
});
};
Expand All @@ -44,7 +46,7 @@ const assertTest = function (stdout, stderr, opts) {

if (opts.stderr) {
if (opts.stderrContains) {
assert.ok(_.includes(stderr, expectStderr), "stderr " + JSON.stringify(stderr) + " didn't contain " + JSON.stringify(expectStderr));
assert.ok(includes(stderr, expectStderr), "stderr " + JSON.stringify(stderr) + " didn't contain " + JSON.stringify(expectStderr));
} else {
chai.expect(stderr).to.equal(expectStderr, "stderr didn't match");
}
Expand All @@ -58,15 +60,16 @@ const assertTest = function (stdout, stderr, opts) {

if (opts.stdout) {
if (opts.stdoutContains) {
assert.ok(_.includes(stdout, expectStdout), "stdout " + JSON.stringify(stdout) + " didn't contain " + JSON.stringify(expectStdout));
assert.ok(includes(stdout, expectStdout), "stdout " + JSON.stringify(stdout) + " didn't contain " + JSON.stringify(expectStdout));
} else {
chai.expect(stdout).to.equal(expectStdout, "stdout didn't match");
}
} else if (stdout) {
throw new Error("stdout:\n" + stdout);
}

_.each(opts.outFiles, function (expect, filename) {
Object.keys(opts.outFiles, function (filename) {
const expect = opts.outFiles[filename];
const actual = helper.readFile(filename);
chai.expect(actual).to.equal(expect, "out-file " + filename);
});
Expand Down Expand Up @@ -134,12 +137,12 @@ const clear = function () {
process.chdir(tmpLoc);
};

_.each(fs.readdirSync(fixtureLoc), function (binName) {
fs.readdirSync(fixtureLoc).forEach(function (binName) {
if (binName[0] === ".") return;

const suiteLoc = path.join(fixtureLoc, binName);
describe("bin/" + binName, function () {
_.each(fs.readdirSync(suiteLoc), function (testName) {
fs.readdirSync(suiteLoc).forEach(function (testName) {
if (testName[0] === ".") return;

const testLoc = path.join(suiteLoc, testName);
Expand All @@ -149,9 +152,9 @@ _.each(fs.readdirSync(fixtureLoc), function (binName) {
};

const optionsLoc = path.join(testLoc, "options.json");
if (fs.existsSync(optionsLoc)) _.merge(opts, require(optionsLoc));
if (fs.existsSync(optionsLoc)) merge(opts, require(optionsLoc));

_.each(["stdout", "stdin", "stderr"], function (key) {
["stdout", "stdin", "stderr"].forEach(function (key) {
const loc = path.join(testLoc, key + ".txt");
if (fs.existsSync(loc)) {
opts[key] = helper.readFile(loc);
Expand Down
9 changes: 4 additions & 5 deletions packages/babel-generator/test/index.js
Expand Up @@ -5,17 +5,16 @@ const assert = require("assert");
const parse = require("babylon").parse;
const chai = require("chai");
const t = require("babel-types");
const _ = require("lodash");
const fs = require("fs");
const path = require("path");

describe("generation", function () {
it("completeness", function () {
_.each(t.VISITOR_KEYS, function (keys, type) {
Object.keys(t.VISITOR_KEYS).forEach(function (type) {
assert.ok(!!Printer.prototype[type], type + " should exist");
});

_.each(Printer.prototype, function (fn, type) {
Object.keys(Printer.prototype).forEach(function (type) {
if (!/[A-Z]/.test(type[0])) return;
assert.ok(t.VISITOR_KEYS[type], type + " should not exist");
});
Expand All @@ -26,7 +25,7 @@ describe("generation", function () {
"a.js": "function hi (msg) { console.log(msg); }\n",
"b.js": "hi('hello');\n"
};
const parsed = _.keys(sources).reduce(function (_parsed, filename) {
const parsed = Object.keys(sources).reduce(function (_parsed, filename) {
_parsed[filename] = parse(sources[filename], { sourceFilename: filename });
return _parsed;
}, {});
Expand Down Expand Up @@ -297,7 +296,7 @@ const suites = require("babel-helper-fixtures").default(__dirname + "/fixtures")

suites.forEach(function (testSuite) {
describe("generation/" + testSuite.title, function () {
_.each(testSuite.tests, function (task) {
testSuite.tests.forEach(function (task) {
it(task.title, !task.disabled && function () {
const expect = task.expect;
const actual = task.actual;
Expand Down
13 changes: 8 additions & 5 deletions packages/babel-helper-fixtures/src/index.js
@@ -1,7 +1,10 @@
import cloneDeep from "lodash/cloneDeep";
import trimEnd from "lodash/trimEnd";
import resolve from "try-resolve";
import clone from "lodash/clone";
import merge from "lodash/merge";
import path from "path";
import fs from "fs";
import _ from "lodash";

function humanize(val, noext) {
if (noext) val = path.basename(val, path.extname(val));
Expand Down Expand Up @@ -58,7 +61,7 @@ export default function get(entryLoc): Array<Suite> {
if (shouldIgnore(suiteName)) continue;

const suite = {
options: _.clone(rootOpts),
options: clone(rootOpts),
tests: [],
title: humanize(suiteName),
filename: entryLoc + "/" + suiteName
Expand Down Expand Up @@ -95,10 +98,10 @@ export default function get(entryLoc): Array<Suite> {
expectLocAlias += "on";
}

const taskOpts = _.cloneDeep(suite.options);
const taskOpts = cloneDeep(suite.options);

const taskOptsLoc = resolve(taskDir + "/options");
if (taskOptsLoc) _.merge(taskOpts, require(taskOptsLoc));
if (taskOptsLoc) merge(taskOpts, require(taskOptsLoc));

const test = {
optionsDir: taskOptsLoc ? path.dirname(taskOptsLoc) : null,
Expand Down Expand Up @@ -162,7 +165,7 @@ export function multiple(entryLoc, ignore?: Array<string>) {

export function readFile(filename) {
if (fs.existsSync(filename)) {
let file = _.trimEnd(fs.readFileSync(filename, "utf8"));
let file = trimEnd(fs.readFileSync(filename, "utf8"));
file = file.replace(/\r\n/g, "\n");
return file;
} else {
Expand Down
19 changes: 11 additions & 8 deletions packages/babel-helper-transform-fixture-test-runner/src/index.js
Expand Up @@ -3,10 +3,13 @@ import { buildExternalHelpers } from "babel-core";
import getFixtures from "babel-helper-fixtures";
import sourceMap from "source-map";
import codeFrame from "babel-code-frame";
import defaults from "lodash/defaults";
import includes from "lodash/includes";
import * as helpers from "./helpers";
import extend from "lodash/extend";
import merge from "lodash/merge";
import assert from "assert";
import chai from "chai";
import _ from "lodash";
import "babel-polyfill";
import fs from "fs";
import path from "path";
Expand Down Expand Up @@ -43,7 +46,7 @@ function run(task) {
const optionsDir = task.optionsDir;

function getOpts(self) {
const newOpts = _.merge({
const newOpts = merge({
filename: self.loc,
}, opts);

Expand Down Expand Up @@ -98,7 +101,7 @@ function run(task) {
if (task.sourceMappings) {
const consumer = new sourceMap.SourceMapConsumer(result.map);

_.each(task.sourceMappings, function (mapping) {
task.sourceMappings.forEach(function (mapping) {
const actual = mapping.original;

const expect = consumer.originalPositionFor(mapping.generated);
Expand Down Expand Up @@ -138,19 +141,19 @@ export default function (
const suites = getFixtures(fixturesLoc);

for (const testSuite of suites) {
if (_.includes(suiteOpts.ignoreSuites, testSuite.title)) continue;
if (includes(suiteOpts.ignoreSuites, testSuite.title)) continue;

describe(name + "/" + testSuite.title, function () {
for (const task of testSuite.tests) {
if (_.includes(suiteOpts.ignoreTasks, task.title) ||
_.includes(suiteOpts.ignoreTasks, testSuite.title + "/" + task.title)) continue;
if (includes(suiteOpts.ignoreTasks, task.title) ||
includes(suiteOpts.ignoreTasks, testSuite.title + "/" + task.title)) continue;

it(task.title, !task.disabled && function () {
function runTask() {
run(task);
}

_.defaults(task.options, {
defaults(task.options, {
filenameRelative: task.expect.filename,
sourceFileName: task.actual.filename,
sourceMapTarget: task.expect.filename,
Expand All @@ -159,7 +162,7 @@ export default function (
sourceMap: !!(task.sourceMappings || task.sourceMap),
});

_.extend(task.options, taskOpts);
extend(task.options, taskOpts);
Copy link
Member

@Jessidhia Jessidhia Dec 26, 2016

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Object.assign, or is it intended that taskOpts prototype keys are copied? How do you even get things in taskOpts' prototype since it comes from JSON?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't know if it's intended for taskOpts prototype keys to be copied, so I'm doing the safe thing here and maintaining the exact same behavior. If this PR is merged, then I'll do another pass at removing some of these utils like in #5043.


if (dynamicOpts) dynamicOpts(task.options, task);

Expand Down
4 changes: 2 additions & 2 deletions packages/babel-preset-es2015/test/traceur.js
@@ -1,4 +1,4 @@
const _ = require("lodash");
const includes = require("lodash/includes");

require("babel-helper-transform-fixture-test-runner")(__dirname + "/fixtures/traceur", "traceur", {
ignoreSuites: [
Expand Down Expand Up @@ -79,7 +79,7 @@ require("babel-helper-transform-fixture-test-runner")(__dirname + "/fixtures/tra
}, {

}, function (opts, task) {
if (_.includes(task.exec.loc, "module.js")) {
if (includes(task.exec.loc, "module.js")) {
opts.plugins.push("transform-es2015-modules-commonjs");
} else {
opts.sourceType = "script";
Expand Down