Skip to content

Commit

Permalink
Reduce dependency on lodash functions: includes, uniq, repeat, isinte…
Browse files Browse the repository at this point in the history
…ger (#11790)

* Replace lodash 'includes' usage with Array.prototype.includes

* Replace lodash 'values' usage with Object.values

* Replace lodash 'uniq' usage with Array.from(new Set(...))

* Property safety: ensure that ignoreSuites/ignoreTasks are populated prior to access

* Property safety: ensure that blacklistTypes is populated prior to access

* Revert "Replace lodash 'values' usage with Object.values"

This reverts commit 9fd3679.

* Replace lodash 'repeat' usage with String.prototype.repeat

* Replace lodash 'isinteger' usage with Number.isInteger

* Remove explicit lodash dependency from babel-generator package

* Update packages/babel-helper-transform-fixture-test-runner/src/index.js

Co-authored-by: Brian Ng <bng412@gmail.com>

* Rely on optional chaining operator as sole boolean check

* Handle additional optional chaining operator simplification

* Update type signature

Co-authored-by: Brian Ng <bng412@gmail.com>
  • Loading branch information
jayaddison and existentialism committed Jul 6, 2020
1 parent c664fbd commit 3e55270
Show file tree
Hide file tree
Showing 9 changed files with 17 additions and 27 deletions.
3 changes: 1 addition & 2 deletions packages/babel-cli/src/babel/options.js
Expand Up @@ -4,7 +4,6 @@ import fs from "fs";

import commander from "commander";
import { version } from "@babel/core";
import uniq from "lodash/uniq";
import glob from "glob";

import pkg from "../../package.json";
Expand Down Expand Up @@ -195,7 +194,7 @@ export default function parseArgv(args: Array<string>): CmdOptions | null {
return globbed.concat(files);
}, []);

filenames = uniq(filenames);
filenames = Array.from(new Set(filenames));

filenames.forEach(function (filename) {
if (!fs.existsSync(filename)) {
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-cli/src/babel/util.js
Expand Up @@ -2,7 +2,6 @@

import readdirRecursive from "fs-readdir-recursive";
import * as babel from "@babel/core";
import includes from "lodash/includes";
import path from "path";
import fs from "fs";

Expand Down Expand Up @@ -47,7 +46,7 @@ export function isCompilableExtension(
): boolean {
const exts = altExts || babel.DEFAULT_EXTENSIONS;
const ext = path.extname(filename);
return includes(exts, ext);
return exts.includes(ext);
}

export function addSourceMappingUrl(code: string, loc: string): string {
Expand Down
1 change: 0 additions & 1 deletion packages/babel-generator/package.json
Expand Up @@ -20,7 +20,6 @@
"dependencies": {
"@babel/types": "^7.10.4",
"jsesc": "^2.5.1",
"lodash": "^4.17.13",
"source-map": "^0.5.0"
},
"devDependencies": {
Expand Down
8 changes: 3 additions & 5 deletions packages/babel-generator/src/printer.js
@@ -1,5 +1,3 @@
import isInteger from "lodash/isInteger";
import repeat from "lodash/repeat";
import Buffer from "./buffer";
import * as n from "./node";
import * as t from "@babel/types";
Expand Down Expand Up @@ -138,7 +136,7 @@ export default class Printer {
// Integer tokens need special handling because they cannot have '.'s inserted
// immediately after them.
this._endsWithInteger =
isInteger(+str) &&
Number.isInteger(+str) &&
!NON_DECIMAL_LITERAL.test(str) &&
!SCIENTIFIC_NOTATION.test(str) &&
!ZERO_DECIMAL_INTEGER.test(str) &&
Expand Down Expand Up @@ -324,7 +322,7 @@ export default class Printer {
*/

_getIndent(): string {
return repeat(this.format.indent.style, this._indent);
return this.format.indent.style.repeat(this._indent);
}

/**
Expand Down Expand Up @@ -616,7 +614,7 @@ export default class Printer {
this._getIndent().length,
this._buf.getCurrentColumn(),
);
val = val.replace(/\n(?!$)/g, `\n${repeat(" ", indentSize)}`);
val = val.replace(/\n(?!$)/g, `\n${" ".repeat(indentSize)}`);
}

// Avoid creating //* comments
Expand Down
Expand Up @@ -5,7 +5,6 @@ import getFixtures from "@babel/helper-fixtures";
import sourceMap from "source-map";
import { codeFrameColumns } from "@babel/code-frame";
import defaults from "lodash/defaults";
import includes from "lodash/includes";
import escapeRegExp from "lodash/escapeRegExp";
import * as helpers from "./helpers";
import extend from "lodash/extend";
Expand Down Expand Up @@ -352,7 +351,7 @@ export default function (
const suites = getFixtures(fixturesLoc);

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

describe(name + "/" + testSuite.title, function () {
jest.addMatchers({
Expand All @@ -361,8 +360,8 @@ export default function (

for (const task of testSuite.tests) {
if (
includes(suiteOpts.ignoreTasks, task.title) ||
includes(suiteOpts.ignoreTasks, testSuite.title + "/" + task.title)
suiteOpts.ignoreTasks?.includes(task.title) ||
suiteOpts.ignoreTasks?.includes(testSuite.title + "/" + task.title)
) {
continue;
}
Expand Down
5 changes: 2 additions & 3 deletions packages/babel-traverse/src/index.js
@@ -1,6 +1,5 @@
import TraversalContext from "./context";
import * as visitors from "./visitors";
import includes from "lodash/includes";
import * as t from "@babel/types";
import * as cache from "./cache";

Expand Down Expand Up @@ -87,10 +86,10 @@ function hasBlacklistedType(path, state) {
traverse.hasType = function (
tree: Object,
type: Object,
blacklistTypes: Array<string>,
blacklistTypes?: Array<string>,
): boolean {
// the node we're searching in is blacklisted
if (includes(blacklistTypes, tree.type)) return false;
if (blacklistTypes?.includes(tree.type)) return false;

// the type we're looking for is the same as the passed node
if (tree.type === type) return true;
Expand Down
3 changes: 1 addition & 2 deletions packages/babel-traverse/src/path/introspection.js
@@ -1,7 +1,6 @@
// This file contains methods responsible for introspecting the current path for certain values.

import type NodePath from "./index";
import includes from "lodash/includes";
import * as t from "@babel/types";

/**
Expand Down Expand Up @@ -149,7 +148,7 @@ export function isStatementOrBlock() {
) {
return false;
} else {
return includes(t.STATEMENT_OR_BLOCK_KEYS, this.key);
return t.STATEMENT_OR_BLOCK_KEYS.includes(this.key);
}
}

Expand Down
8 changes: 3 additions & 5 deletions packages/babel-traverse/src/scope/index.js
@@ -1,5 +1,3 @@
import includes from "lodash/includes";
import repeat from "lodash/repeat";
import Renamer from "./lib/renamer";
import type NodePath from "../path";
import traverse from "../index";
Expand Down Expand Up @@ -502,7 +500,7 @@ export default class Scope {
}

dump() {
const sep = repeat("-", 60);
const sep = "-".repeat(60);
console.log(sep);
let scope = this;
do {
Expand Down Expand Up @@ -1038,8 +1036,8 @@ export default class Scope {
if (this.hasOwnBinding(name)) return true;
if (this.parentHasBinding(name, noGlobals)) return true;
if (this.hasUid(name)) return true;
if (!noGlobals && includes(Scope.globals, name)) return true;
if (!noGlobals && includes(Scope.contextVariables, name)) return true;
if (!noGlobals && Scope.globals.includes(name)) return true;
if (!noGlobals && Scope.contextVariables.includes(name)) return true;
return false;
}

Expand Down
6 changes: 3 additions & 3 deletions packages/babel-types/src/utils/inherit.js
@@ -1,12 +1,12 @@
// @flow
import uniq from "lodash/uniq";

export default function inherit(
key: string,
child: Object,
parent: Object,
): void {
if (child && parent) {
child[key] = uniq([].concat(child[key], parent[key]).filter(Boolean));
child[key] = Array.from(
new Set([].concat(child[key], parent[key]).filter(Boolean)),
);
}
}

0 comments on commit 3e55270

Please sign in to comment.