From 94b5f92e39904f1730c08f11c7c6f23890d19f20 Mon Sep 17 00:00:00 2001 From: James Addison Date: Wed, 14 Oct 2020 19:10:44 +0100 Subject: [PATCH] Replace lodash 'defaults' usage with ES6 Spread initializer (#11797) Co-authored-by: Corey Farrell --- packages/babel-cli/src/babel/dir.js | 14 ++----- packages/babel-cli/src/babel/file.js | 42 +++++++------------ .../stdout.txt | 2 +- .../stdout.txt | 2 +- .../env - SHOW_CONFIG_FOR-windows/stdout.txt | 2 +- .../babel/env - SHOW_CONFIG_FOR/stdout.txt | 2 +- .../src/index.js | 9 ++-- packages/babel-traverse/src/scope/index.js | 7 +++- 8 files changed, 33 insertions(+), 47 deletions(-) diff --git a/packages/babel-cli/src/babel/dir.js b/packages/babel-cli/src/babel/dir.js index 7356574504..bbdcf4ac32 100644 --- a/packages/babel-cli/src/babel/dir.js +++ b/packages/babel-cli/src/babel/dir.js @@ -1,6 +1,5 @@ // @flow -import defaults from "lodash/defaults"; import debounce from "lodash/debounce"; import { sync as makeDirSync } from "make-dir"; import slash from "slash"; @@ -48,15 +47,10 @@ export default async function ({ const dest = getDest(relative, base); try { - const res = await util.compile( - src, - defaults( - { - sourceFileName: slash(path.relative(dest + "/..", src)), - }, - babelOptions, - ), - ); + const res = await util.compile(src, { + ...babelOptions, + sourceFileName: slash(path.relative(dest + "/..", src)), + }); if (!res) return FILE_TYPE.IGNORED; diff --git a/packages/babel-cli/src/babel/file.js b/packages/babel-cli/src/babel/file.js index 89e780acd9..a6927157d5 100644 --- a/packages/babel-cli/src/babel/file.js +++ b/packages/babel-cli/src/babel/file.js @@ -1,7 +1,6 @@ // @flow import convertSourceMap from "convert-source-map"; -import defaults from "lodash/defaults"; import sourceMap from "source-map"; import slash from "slash"; import { sync as makeDirSync } from "make-dir"; @@ -127,16 +126,10 @@ export default async function ({ async function stdin(): Promise { const code = await readStdin(); - const res = await util.transform( - cliOptions.filename, - code, - defaults( - { - sourceFileName: "stdin", - }, - babelOptions, - ), - ); + const res = await util.transform(cliOptions.filename, code, { + ...babelOptions, + sourceFileName: "stdin", + }); output([res]); } @@ -177,22 +170,17 @@ export default async function ({ sourceFilename = slash(sourceFilename); try { - return await util.compile( - filename, - defaults( - { - sourceFileName: sourceFilename, - // Since we're compiling everything to be merged together, - // "inline" applies to the final output file, but not to the individual - // files being concatenated. - sourceMaps: - babelOptions.sourceMaps === "inline" - ? true - : babelOptions.sourceMaps, - }, - babelOptions, - ), - ); + return await util.compile(filename, { + ...babelOptions, + sourceFileName: sourceFilename, + // Since we're compiling everything to be merged together, + // "inline" applies to the final output file, but not to the individual + // files being concatenated. + sourceMaps: + babelOptions.sourceMaps === "inline" + ? true + : babelOptions.sourceMaps, + }); } catch (err) { if (!cliOptions.watch) { throw err; diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt index 303ac82e7f..b9a7167fab 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files-windows/stdout.txt @@ -15,7 +15,6 @@ config /.babelrc programmatic options from @babel/cli { - "sourceFileName": "src/foo.js", "presets": [ "//packages//babel-preset-react" ], @@ -24,6 +23,7 @@ programmatic options from @babel/cli "//packages//babel-plugin-transform-strict-mode", "//packages//babel-plugin-transform-modules-commonjs" ], + "sourceFileName": "src/foo.js", "caller": { "name": "@babel/cli" }, diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt index 0f982f0e98..1a70215b9c 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR with multiple files/stdout.txt @@ -15,7 +15,6 @@ config /.babelrc programmatic options from @babel/cli { - "sourceFileName": "src/foo.js", "presets": [ "/packages/babel-preset-react" ], @@ -24,6 +23,7 @@ programmatic options from @babel/cli "/packages/babel-plugin-transform-strict-mode", "/packages/babel-plugin-transform-modules-commonjs" ], + "sourceFileName": "src/foo.js", "caller": { "name": "@babel/cli" }, diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt index 5d5860f58f..3a3d6a02c7 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR-windows/stdout.txt @@ -15,7 +15,6 @@ config /.babelrc programmatic options from @babel/cli { - "sourceFileName": "../src/foo.js", "presets": [ "//packages//babel-preset-react" ], @@ -24,6 +23,7 @@ programmatic options from @babel/cli "//packages//babel-plugin-transform-strict-mode", "//packages//babel-plugin-transform-modules-commonjs" ], + "sourceFileName": "../src/foo.js", "caller": { "name": "@babel/cli" }, diff --git a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt index b648f9d8bb..fed8218c93 100644 --- a/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt +++ b/packages/babel-cli/test/fixtures/babel/env - SHOW_CONFIG_FOR/stdout.txt @@ -110,7 +110,6 @@ config /.babelrc programmatic options from @babel/cli { - "sourceFileName": "./src/index.js", "presets": [ "/packages/babel-preset-react" ], @@ -120,6 +119,7 @@ programmatic options from @babel/cli "/packages/babel-plugin-transform-modules-commonjs" ], "configFile": "./my-config.js", + "sourceFileName": "./src/index.js", "caller": { "name": "@babel/cli" }, diff --git a/packages/babel-helper-transform-fixture-test-runner/src/index.js b/packages/babel-helper-transform-fixture-test-runner/src/index.js index 75187153e1..30dc091523 100644 --- a/packages/babel-helper-transform-fixture-test-runner/src/index.js +++ b/packages/babel-helper-transform-fixture-test-runner/src/index.js @@ -4,7 +4,6 @@ import { buildExternalHelpers } from "@babel/core"; import getFixtures from "@babel/helper-fixtures"; import sourceMap from "source-map"; import { codeFrameColumns } from "@babel/code-frame"; -import defaults from "lodash/defaults"; import escapeRegExp from "lodash/escapeRegExp"; import * as helpers from "./helpers"; import merge from "lodash/merge"; @@ -442,9 +441,11 @@ export default function ( run(task); } - defaults(task.options, { - sourceMap: !!(task.sourceMappings || task.sourceMap), - }); + if ("sourceMap" in task.options === false) { + task.options.sourceMap = !!( + task.sourceMappings || task.sourceMap + ); + } Object.assign(task.options, taskOpts); diff --git a/packages/babel-traverse/src/scope/index.js b/packages/babel-traverse/src/scope/index.js index 321db204cc..c5ca4a4a54 100644 --- a/packages/babel-traverse/src/scope/index.js +++ b/packages/babel-traverse/src/scope/index.js @@ -1,7 +1,6 @@ import Renamer from "./lib/renamer"; import type NodePath from "../path"; import traverse from "../index"; -import defaults from "lodash/defaults"; import Binding from "./binding"; import globals from "globals"; import * as t from "@babel/types"; @@ -956,7 +955,11 @@ export default class Scope { let scope = this; do { - defaults(ids, scope.bindings); + for (const key of Object.keys(scope.bindings)) { + if (key in ids === false) { + ids[key] = scope.bindings[key]; + } + } scope = scope.parent; } while (scope);