Skip to content

Commit

Permalink
Remove remaining lodash dependencies (#13139)
Browse files Browse the repository at this point in the history
  • Loading branch information
nicolo-ribaudo committed Apr 13, 2021
1 parent b35c78f commit d24bd7c
Show file tree
Hide file tree
Showing 16 changed files with 56 additions and 57 deletions.
4 changes: 2 additions & 2 deletions eslint/babel-eslint-plugin/package.json
Expand Up @@ -40,7 +40,7 @@
"eslint-rule-composer": "^0.3.0"
},
"devDependencies": {
"eslint": "^7.5.0",
"lodash": "^4.17.20"
"clone-deep": "^4.0.1",
"eslint": "^7.5.0"
}
}
2 changes: 1 addition & 1 deletion eslint/babel-eslint-plugin/test/rules/no-invalid-this.js
@@ -1,4 +1,4 @@
import cloneDeep from "lodash/cloneDeep";
import cloneDeep from "clone-deep";
import rule from "../../src/rules/no-invalid-this";
import RuleTester from "../../../babel-eslint-shared-fixtures/utils/RuleTester";

Expand Down
1 change: 0 additions & 1 deletion packages/babel-cli/package.json
Expand Up @@ -28,7 +28,6 @@
"convert-source-map": "^1.1.0",
"fs-readdir-recursive": "^1.1.0",
"glob": "^7.0.0",
"lodash": "^4.17.19",
"make-dir": "^2.1.0",
"slash": "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0",
"source-map": "^0.5.0"
Expand Down
37 changes: 16 additions & 21 deletions packages/babel-cli/src/babel/dir.js
@@ -1,6 +1,5 @@
// @flow

import debounce from "lodash/debounce";
import slash from "slash";
import path from "path";
import fs from "fs";
Expand Down Expand Up @@ -135,27 +134,23 @@ export default async function ({
let compiledFiles = 0;
let startTime = null;

const logSuccess = debounce(
function () {
if (startTime === null) {
// This should never happen, but just in case it's better
// to ignore the log message rather than making @babel/cli crash.
return;
}
const logSuccess = util.debounce(function () {
if (startTime === null) {
// This should never happen, but just in case it's better
// to ignore the log message rather than making @babel/cli crash.
return;
}

const diff = process.hrtime(startTime);

console.log(
`Successfully compiled ${compiledFiles} ${
compiledFiles !== 1 ? "files" : "file"
} with Babel (${diff[0] * 1e3 + Math.round(diff[1] / 1e6)}ms).`,
);
compiledFiles = 0;
startTime = null;
},
100,
{ trailing: true },
);
const diff = process.hrtime(startTime);

console.log(
`Successfully compiled ${compiledFiles} ${
compiledFiles !== 1 ? "files" : "file"
} with Babel (${diff[0] * 1e3 + Math.round(diff[1] / 1e6)}ms).`,
);
compiledFiles = 0;
startTime = null;
}, 100);

if (!cliOptions.skipInitialBuild) {
if (cliOptions.deleteDirOnStart) {
Expand Down
13 changes: 13 additions & 0 deletions packages/babel-cli/src/babel/util.js
Expand Up @@ -141,3 +141,16 @@ export function withExtension(filename: string, ext: string = ".js") {
const newBasename = path.basename(filename, path.extname(filename)) + ext;
return path.join(path.dirname(filename), newBasename);
}

export function debounce(fn: () => void, time: number) {
let timer;
function debounced() {
clearTimeout(timer);
timer = setTimeout(fn, time);
}
debounced.flush = () => {
clearTimeout(timer);
fn();
};
return debounced;
}
1 change: 0 additions & 1 deletion packages/babel-core/package.json
Expand Up @@ -68,7 +68,6 @@
"@babel/helper-transform-fixture-test-runner": "workspace:*",
"@types/convert-source-map": "^1.5.1",
"@types/debug": "^4.1.0",
"@types/lodash": "^4.14.150",
"@types/resolve": "^1.3.2",
"@types/semver": "^5.4.0",
"@types/source-map": "^0.5.0"
Expand Down
1 change: 0 additions & 1 deletion packages/babel-generator/package.json
Expand Up @@ -27,7 +27,6 @@
"@babel/helper-fixtures": "workspace:*",
"@babel/parser": "workspace:*",
"@types/jsesc": "^2.5.0",
"@types/lodash": "^4.14.150",
"@types/source-map": "^0.5.0"
}
}
@@ -1,4 +1,4 @@
import last from "lodash/last"
import last from "lo-dash/last"

export default class Container {
last(key) {
Expand Down
Expand Up @@ -5,7 +5,7 @@ Object.defineProperty(exports, "__esModule", {
});
exports.default = void 0;

var _last2 = babelHelpers.interopRequireDefault(require("lodash/last"));
var _last2 = babelHelpers.interopRequireDefault(require("lo-dash/last"));

let Container = /*#__PURE__*/function () {
function Container() {
Expand Down
2 changes: 1 addition & 1 deletion packages/babel-register/package.json
Expand Up @@ -17,8 +17,8 @@
"./lib/nodeWrapper.js": "./lib/browser.js"
},
"dependencies": {
"clone-deep": "^4.0.1",
"find-cache-dir": "condition:BABEL_8_BREAKING ? ^3.3.1 : ^2.0.0",
"lodash": "^4.17.19",
"make-dir": "^2.1.0",
"pirates": "^4.0.0",
"source-map-support": "^0.5.16"
Expand Down
4 changes: 2 additions & 2 deletions packages/babel-register/src/node.js
@@ -1,4 +1,4 @@
import deepClone from "lodash/cloneDeep";
import cloneDeep from "clone-deep";
import sourceMapSupport from "source-map-support";
import * as registerCache from "./cache";
import * as babel from "@babel/core";
Expand Down Expand Up @@ -42,7 +42,7 @@ function compile(code, filename) {
// sourceRoot can be overwritten
{
sourceRoot: path.dirname(filename) + path.sep,
...deepClone(transformOpts),
...cloneDeep(transformOpts),
filename,
},
);
Expand Down
Expand Up @@ -22,7 +22,6 @@ register( {

console.log(
JSON.stringify({
convertSourceMap: require('convert-source-map').fromObject.toString(),
isPlainObject: require('lodash/isPlainObject').toString()
convertSourceMap: require('convert-source-map').fromObject.toString()
})
);
6 changes: 2 additions & 4 deletions packages/babel-register/test/index.js
Expand Up @@ -198,16 +198,14 @@ describe("@babel/register", function () {

test("transforms modules used within register", callback => {
// Need a clean environment without `convert-source-map`
// and `lodash/isPlainObject` already in the require cache,
// so we spawn a separate process
// already in the require cache, so we spawn a separate process

spawnNode([internalModulesTestFile], output => {
let err;

try {
const { convertSourceMap, isPlainObject } = JSON.parse(output);
const { convertSourceMap } = JSON.parse(output);
expect(convertSourceMap).toMatch("/* transformed */");
expect(isPlainObject).toMatch("/* transformed */");
} catch (e) {
err = e;
}
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-types/package.json
Expand Up @@ -25,13 +25,11 @@
},
"dependencies": {
"@babel/helper-validator-identifier": "workspace:^7.12.11",
"lodash": "^4.17.19",
"to-fast-properties": "^2.0.0"
},
"devDependencies": {
"@babel/generator": "workspace:*",
"@babel/parser": "workspace:*",
"@types/lodash": "^4.14.162",
"chalk": "^4.1.0"
}
}
17 changes: 14 additions & 3 deletions packages/babel-types/src/converters/valueToNode.ts
@@ -1,4 +1,3 @@
import isPlainObject from "lodash/isPlainObject";
import isValidIdentifier from "../validators/isValidIdentifier";
import {
identifier,
Expand All @@ -25,15 +24,27 @@ export default valueToNode as {
(value: RegExp): t.RegExpLiteral;
(value: ReadonlyArray<unknown>): t.ArrayExpression;

// this throws with objects that are not PlainObject according to lodash,
// this throws with objects that are not plain objects,
// or if there are non-valueToNode-able values
(value: object): t.ObjectExpression;

(value: unknown): t.Expression;
};

const objectToString: (value: object) => string = Function.call.bind(
Object.prototype.toString,
);

function isRegExp(value): value is RegExp {
return Object.prototype.toString.call(value) === "[object RegExp]";
return objectToString(value) === "[object RegExp]";
}

function isPlainObject(value): value is object {
if (typeof value !== "object" || value === null) {
return false;
}
const proto = Object.getPrototypeOf(value);
return proto === null || proto === Object.prototype;
}

function valueToNode(value: unknown): t.Expression {
Expand Down
16 changes: 2 additions & 14 deletions yarn.lock
Expand Up @@ -94,7 +94,6 @@ __metadata:
convert-source-map: ^1.1.0
fs-readdir-recursive: ^1.1.0
glob: ^7.0.0
lodash: ^4.17.19
make-dir: ^2.1.0
rimraf: ^3.0.0
slash: "condition:BABEL_8_BREAKING ? ^3.0.0 : ^2.0.0"
Expand Down Expand Up @@ -213,7 +212,6 @@ __metadata:
"@babel/types": "workspace:^7.13.14"
"@types/convert-source-map": ^1.5.1
"@types/debug": ^4.1.0
"@types/lodash": ^4.14.150
"@types/resolve": ^1.3.2
"@types/semver": ^5.4.0
"@types/source-map": ^0.5.0
Expand Down Expand Up @@ -274,9 +272,9 @@ __metadata:
version: 0.0.0-use.local
resolution: "@babel/eslint-plugin@workspace:eslint/babel-eslint-plugin"
dependencies:
clone-deep: ^4.0.1
eslint: ^7.5.0
eslint-rule-composer: ^0.3.0
lodash: ^4.17.20
peerDependencies:
"@babel/eslint-parser": ">=7.11.0"
eslint: ">=7.5.0"
Expand Down Expand Up @@ -331,7 +329,6 @@ __metadata:
"@babel/parser": "workspace:*"
"@babel/types": "workspace:^7.13.0"
"@types/jsesc": ^2.5.0
"@types/lodash": ^4.14.150
"@types/source-map": ^0.5.0
jsesc: "condition: BABEL_8_BREAKING ? ^3.0.2 : ^2.5.1"
source-map: ^0.5.0
Expand Down Expand Up @@ -3286,8 +3283,8 @@ __metadata:
"@babel/core": "workspace:*"
"@babel/plugin-transform-modules-commonjs": "workspace:*"
browserify: ^16.5.2
clone-deep: ^4.0.1
find-cache-dir: "condition:BABEL_8_BREAKING ? ^3.3.1 : ^2.0.0"
lodash: ^4.17.19
make-dir: ^2.1.0
pirates: ^4.0.0
source-map-support: ^0.5.16
Expand Down Expand Up @@ -3519,9 +3516,7 @@ __metadata:
"@babel/generator": "workspace:*"
"@babel/helper-validator-identifier": "workspace:^7.12.11"
"@babel/parser": "workspace:*"
"@types/lodash": ^4.14.162
chalk: ^4.1.0
lodash: ^4.17.19
to-fast-properties: ^2.0.0
languageName: unknown
linkType: soft
Expand Down Expand Up @@ -4142,13 +4137,6 @@ __metadata:
languageName: node
linkType: hard

"@types/lodash@npm:^4.14.150, @types/lodash@npm:^4.14.162":
version: 4.14.168
resolution: "@types/lodash@npm:4.14.168"
checksum: 9a4e25f89fc035b9f0388f1f7be85e5eff49f9e6db0d93432c9a89fce0916f8a89db4e8290415f7ea02de6b00d3573826378dcb655b7b2d20530a6e8d6dd6fd0
languageName: node
linkType: hard

"@types/minimatch@npm:*":
version: 3.0.3
resolution: "@types/minimatch@npm:3.0.3"
Expand Down

0 comments on commit d24bd7c

Please sign in to comment.