Skip to content

Commit

Permalink
Remove lodash deps (#13057)
Browse files Browse the repository at this point in the history
* inline escapeRegExp instead of using any dep

* inline camelCase

* replace merge with object spread

* copy if array instead of using clone

* inline isRegExp

* review fixes!

* remove escape-string-regexp from package.json and in test

* add error for field defaults that are not primitives or empty arrays

* replace merge with object spread

* yarn
  • Loading branch information
hzoo committed Mar 27, 2021
1 parent 6ac07a1 commit 6b39baf
Show file tree
Hide file tree
Showing 17 changed files with 52 additions and 84 deletions.
5 changes: 3 additions & 2 deletions Gulpfile.mjs
Expand Up @@ -8,7 +8,6 @@ import through from "through2";
import chalk from "chalk";
import newer from "gulp-newer";
import babel from "gulp-babel";
import camelCase from "lodash/camelCase.js";
import fancyLog from "fancy-log";
import filter from "gulp-filter";
import revertPath from "gulp-revert-path";
Expand Down Expand Up @@ -162,7 +161,9 @@ function generateStandalone() {
let allList = "";

for (const plugin of pluginConfig) {
const camelPlugin = camelCase(plugin);
const camelPlugin = plugin.replace(/-[a-z]/g, c =>
c[1].toUpperCase()
);
imports += `import ${camelPlugin} from "@babel/plugin-${plugin}";`;
list += `${camelPlugin},`;
allList += `"${plugin}": ${camelPlugin},`;
Expand Down
12 changes: 0 additions & 12 deletions lib/third-party-libs.js.flow
Expand Up @@ -22,18 +22,6 @@ declare module "json5" {
};
}

declare module "lodash/clone" {
declare export default <T>(obj: T) => T;
}

declare module "lodash/merge" {
declare export default <T: Object>(T, Object) => T;
}

declare module "lodash/escapeRegExp" {
declare export default (toEscape?: string) => string;
}

declare module "semver" {
declare class SemVer {
build: Array<string>;
Expand Down
10 changes: 6 additions & 4 deletions packages/babel-cli/test/index.js
Expand Up @@ -3,8 +3,6 @@ import * as helper from "@babel/helper-fixtures";
import rimraf from "rimraf";
import { sync as makeDirSync } from "make-dir";
import child from "child_process";
import escapeRegExp from "lodash/escapeRegExp";
import merge from "lodash/merge";
import path from "path";
import fs from "fs";
import { fileURLToPath } from "url";
Expand Down Expand Up @@ -58,6 +56,10 @@ const saveInFiles = function (files) {
});
};

function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}

const normalizeOutput = function (str, cwd) {
let result = str
.replace(/\(\d+ms\)/g, "(123ms)")
Expand Down Expand Up @@ -218,7 +220,7 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) {

const testLoc = path.join(suiteLoc, testName);

const opts = {
let opts = {
args: [],
};

Expand All @@ -244,7 +246,7 @@ fs.readdirSync(fixtureLoc).forEach(function (binName) {

delete taskOpts.os;
}
merge(opts, taskOpts);
opts = { args: [], ...taskOpts };
}

["stdout", "stdin", "stderr"].forEach(function (key) {
Expand Down
2 changes: 0 additions & 2 deletions packages/babel-core/package.json
Expand Up @@ -58,10 +58,8 @@
"@babel/types": "workspace:^7.13.13",
"convert-source-map": "^1.7.0",
"debug": "^4.1.0",
"escape-string-regexp": "condition:BABEL_8_BREAKING ? ^4.0.0 : ",
"gensync": "^1.0.0-beta.2",
"json5": "^2.1.2",
"lodash": "^4.17.19",
"semver": "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0",
"source-map": "^0.5.0"
},
Expand Down
5 changes: 0 additions & 5 deletions packages/babel-core/src/config/helpers/escape-regexp.cjs

This file was deleted.

7 changes: 4 additions & 3 deletions packages/babel-core/src/config/pattern-to-regex.js
@@ -1,9 +1,6 @@
// @flow
import path from "path";

// $FlowIgnore
import escapeRegExp from "./helpers/escape-regexp.cjs";

const sep = `\\${path.sep}`;
const endSep = `(?:${sep}|$)`;

Expand All @@ -15,6 +12,10 @@ const starPatLast = `(?:${substitution}${endSep})`;
const starStarPat = `${starPat}*?`;
const starStarPatLast = `${starPat}*?${starPatLast}?`;

function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}

/**
* Implement basic pattern matching that will allow users to do the simple
* tests with * and **. If users want full complex pattern matching, then can
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-core/test/config-chain.js
Expand Up @@ -2,7 +2,6 @@ import fs from "fs";
import os from "os";
import path from "path";
import { fileURLToPath } from "url";
import escapeRegExp from "lodash/escapeRegExp";
import * as babel from "../lib";
import getTargets from "@babel/helper-compilation-targets";

Expand Down Expand Up @@ -63,6 +62,10 @@ function pairs(items) {
return pairs;
}

function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}

async function getTemp(name) {
const cwd = await pfs.mkdtemp(os.tmpdir() + path.sep + name);
const tmp = name => path.join(cwd, name);
Expand Down
Expand Up @@ -19,8 +19,6 @@
"@babel/core": "workspace:^7.13.10",
"@babel/helper-fixtures": "workspace:^7.13.10",
"babel-check-duplicated-nodes": "^1.0.0",
"escape-string-regexp": "condition:BABEL_8_BREAKING ? ^4.0.0 : ",
"lodash": "^4.17.19",
"quick-lru": "5.1.0",
"regenerator-runtime": "^0.13.7",
"source-map": "^0.5.0"
Expand Down

This file was deleted.

31 changes: 15 additions & 16 deletions packages/babel-helper-transform-fixture-test-runner/src/index.ts
Expand Up @@ -8,14 +8,11 @@ import {
import sourceMap from "source-map";
import { codeFrameColumns } from "@babel/code-frame";
import * as helpers from "./helpers";
import merge from "lodash/merge";
import assert from "assert";
import fs from "fs";
import path from "path";
import vm from "vm";
import QuickLRU from "quick-lru";
// @ts-ignore
import escapeRegExp from "./escape-regexp.cjs";
import { fileURLToPath } from "url";

import { createRequire } from "module";
Expand Down Expand Up @@ -202,19 +199,17 @@ function run(task) {

// todo(flow->ts) add proper return type (added any, because empty object is inferred)
function getOpts(self): any {
const newOpts = merge(
{
ast: true,
cwd: path.dirname(self.loc),
filename: self.loc,
filenameRelative: self.filename,
sourceFileName: self.filename,
sourceType: "script",
babelrc: false,
inputSourceMap: task.inputSourceMap || undefined,
},
opts,
);
const newOpts = {
ast: true,
cwd: path.dirname(self.loc),
filename: self.loc,
filenameRelative: self.filename,
sourceFileName: self.filename,
sourceType: "script",
babelrc: false,
inputSourceMap: task.inputSourceMap || undefined,
...opts,
};

return resolveOptionPluginOrPreset(newOpts, optionsDir);
}
Expand Down Expand Up @@ -345,6 +340,10 @@ function validateFile(actualCode, expectedLoc, expectedCode) {
}
}

function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}

function normalizeOutput(code, normalizePathSeparator?) {
const projectRoot = path.resolve(
path.dirname(fileURLToPath(import.meta.url)),
Expand Down
1 change: 0 additions & 1 deletion packages/babel-register/package.json
Expand Up @@ -17,7 +17,6 @@
"./lib/nodeWrapper.js": "./lib/browser.js"
},
"dependencies": {
"escape-string-regexp": "condition:BABEL_8_BREAKING ? ^4.0.0 : ",
"find-cache-dir": "condition:BABEL_8_BREAKING ? ^3.3.1 : ^2.0.0",
"lodash": "^4.17.19",
"make-dir": "^2.1.0",
Expand Down
5 changes: 0 additions & 5 deletions packages/babel-register/src/escape-regexp.cjs

This file was deleted.

5 changes: 4 additions & 1 deletion packages/babel-register/src/node.js
Expand Up @@ -7,7 +7,6 @@ import { addHook } from "pirates";
import fs from "fs";
import path from "path";
import Module from "module";
import escapeRegExp from "./escape-regexp.cjs";

const maps = {};
let transformOpts = {};
Expand Down Expand Up @@ -109,6 +108,10 @@ export function revert() {
if (piratesRevert) piratesRevert();
}

function escapeRegExp(string) {
return string.replace(/[|\\{}()[\]^$+*?.]/g, "\\$&");
}

export default function register(opts?: Object = {}) {
// Clone to avoid mutating the arguments object with the 'delete's below.
opts = {
Expand Down
5 changes: 3 additions & 2 deletions packages/babel-types/src/builders/builder.ts
@@ -1,4 +1,3 @@
import loClone from "lodash/clone";
import { NODE_FIELDS, BUILDER_KEYS } from "../definitions";
import validate from "../validators/validate";
import type * as t from "..";
Expand All @@ -23,7 +22,9 @@ export default function builder<T extends t.Node>(

let arg;
if (i < countArgs) arg = args[i];
if (arg === undefined) arg = loClone(field.default);
if (arg === undefined) {
arg = Array.isArray(field.default) ? [] : field.default;
}

node[key] = arg;
i++;
Expand Down
5 changes: 4 additions & 1 deletion packages/babel-types/src/converters/valueToNode.ts
@@ -1,5 +1,4 @@
import isPlainObject from "lodash/isPlainObject";
import isRegExp from "lodash/isRegExp";
import isValidIdentifier from "../validators/isValidIdentifier";
import {
identifier,
Expand Down Expand Up @@ -33,6 +32,10 @@ export default valueToNode as {
(value: unknown): t.Expression;
};

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

function valueToNode(value: unknown): t.Expression {
// undefined
if (value === undefined) {
Expand Down
10 changes: 9 additions & 1 deletion packages/babel-types/src/definitions/utils.ts
Expand Up @@ -279,8 +279,16 @@ export default function defineType(
const keys = Object.getOwnPropertyNames(inherits.fields);
for (const key of keys) {
const field = inherits.fields[key];
const def = field.default;
if (
Array.isArray(def) ? def.length > 0 : def && typeof def === "object"
) {
throw new Error(
"field defaults can only be primitives or empty arrays currently",
);
}
fields[key] = {
default: field.default,
default: Array.isArray(def) ? [] : def,
optional: field.optional,
validate: field.validate,
};
Expand Down
21 changes: 0 additions & 21 deletions yarn.lock
Expand Up @@ -213,10 +213,8 @@ __metadata:
"@babel/types": "workspace:^7.13.13"
convert-source-map: ^1.7.0
debug: ^4.1.0
escape-string-regexp: "condition:BABEL_8_BREAKING ? ^4.0.0 : "
gensync: ^1.0.0-beta.2
json5: ^2.1.2
lodash: ^4.17.19
semver: "condition:BABEL_8_BREAKING ? ^7.3.4 : ^6.3.0"
source-map: ^0.5.0
languageName: unknown
Expand Down Expand Up @@ -820,8 +818,6 @@ __metadata:
"@babel/helper-fixtures": "workspace:^7.13.10"
"@types/jest": ^25.2.2
babel-check-duplicated-nodes: ^1.0.0
escape-string-regexp: "condition:BABEL_8_BREAKING ? ^4.0.0 : "
lodash: ^4.17.19
quick-lru: 5.1.0
regenerator-runtime: ^0.13.7
source-map: ^0.5.0
Expand Down Expand Up @@ -3257,7 +3253,6 @@ __metadata:
"@babel/core": "workspace:*"
"@babel/plugin-transform-modules-commonjs": "workspace:*"
browserify: ^16.5.2
escape-string-regexp: "condition:BABEL_8_BREAKING ? ^4.0.0 : "
find-cache-dir: "condition:BABEL_8_BREAKING ? ^3.3.1 : ^2.0.0"
lodash: ^4.17.19
make-dir: ^2.1.0
Expand Down Expand Up @@ -7429,22 +7424,6 @@ __metadata:
languageName: node
linkType: hard

"escape-string-regexp-BABEL_8_BREAKING-true@npm:escape-string-regexp@^4.0.0":
version: 4.0.0
resolution: "escape-string-regexp@npm:4.0.0"
checksum: c747be8d5ff7873127e3e0cffe7d2206a37208077fa9c30a3c1bb4f26bebd081c8c24d5fba7a99449f9d20670bea3dc5e1b6098b0f074b099bd38766271a272f
languageName: node
linkType: hard

"escape-string-regexp@condition:BABEL_8_BREAKING ? ^4.0.0 : ":
version: 0.0.0-condition-d458a5
resolution: "escape-string-regexp@condition:BABEL_8_BREAKING?^4.0.0:#d458a5"
dependencies:
escape-string-regexp-BABEL_8_BREAKING-true: "npm:escape-string-regexp@^4.0.0"
checksum: a67cb3a0fc219d92f2c6f0964e302cc079b0ce38bf2bd19257d26865c600bec42befed4dc88aec2bfbe9e62f757ce4bec80a408937bf48c5c779e13d9b238c01
languageName: node
linkType: hard

"escape-string-regexp@npm:^1.0.2, escape-string-regexp@npm:^1.0.5":
version: 1.0.5
resolution: "escape-string-regexp@npm:1.0.5"
Expand Down

0 comments on commit 6b39baf

Please sign in to comment.