From a22fa556fc46b19bcccbc558a8734c3ca615cdbb Mon Sep 17 00:00:00 2001 From: Simen Bekkhus Date: Sun, 31 Dec 2023 16:27:47 +0100 Subject: [PATCH] chore: avoid objects as default parameters (#14821) --- .eslintrc.cjs | 1 - packages/jest-haste-map/src/index.ts | 8 +++++--- packages/jest-message-util/src/index.ts | 19 +++++++++++-------- .../src/GitHubActionsReporter.ts | 2 +- .../jest-transform/src/ScriptTransformer.ts | 8 +++++--- packages/jest-util/src/deepCyclicCopy.ts | 3 ++- 6 files changed, 24 insertions(+), 17 deletions(-) diff --git a/.eslintrc.cjs b/.eslintrc.cjs index 6f195a6f1cd2..c368874f1b0d 100644 --- a/.eslintrc.cjs +++ b/.eslintrc.cjs @@ -697,7 +697,6 @@ module.exports = { // TODO: turn on at some point 'unicorn/error-message': 'off', - 'unicorn/no-object-as-default-parameter': 'off', 'unicorn/prefer-object-from-entries': 'off', 'unicorn/prefer-string-replace-all': 'off', diff --git a/packages/jest-haste-map/src/index.ts b/packages/jest-haste-map/src/index.ts index 354104331353..22bace8a9f22 100644 --- a/packages/jest-haste-map/src/index.ts +++ b/packages/jest-haste-map/src/index.ts @@ -131,6 +131,8 @@ const VCS_DIRECTORIES = ['.git', '.hg', '.sl'] .map(vcs => escapePathForRegex(path.sep + vcs + path.sep)) .join('|'); +type WorkerOptions = {forceInBand: boolean}; + /** * HasteMap is a JavaScript implementation of Facebook's haste module system. * @@ -450,7 +452,7 @@ class HasteMap extends EventEmitter implements IHasteMap { map: ModuleMapData, mocks: MockData, filePath: string, - workerOptions?: {forceInBand: boolean}, + workerOptions?: WorkerOptions, ): Promise | null { const rootDir = this._options.rootDir; @@ -738,10 +740,10 @@ class HasteMap extends EventEmitter implements IHasteMap { * Creates workers or parses files and extracts metadata in-process. */ private _getWorker( - options = {forceInBand: false}, + options: WorkerOptions | undefined, ): JestWorkerFarm | HasteWorker { if (!this._worker) { - if (options.forceInBand || this._options.maxWorkers <= 1) { + if (options?.forceInBand || this._options.maxWorkers <= 1) { this._worker = {getSha1, worker}; } else { this._worker = new Worker(require.resolve('./worker'), { diff --git a/packages/jest-message-util/src/index.ts b/packages/jest-message-util/src/index.ts index f51b7def8187..5cc67e50ef2a 100644 --- a/packages/jest-message-util/src/index.ts +++ b/packages/jest-message-util/src/index.ts @@ -310,12 +310,15 @@ export const formatPath = ( return STACK_TRACE_COLOR(match[1]) + filePath + STACK_TRACE_COLOR(match[3]); }; -export const getStackTraceLines = ( +export function getStackTraceLines( stack: string, - options: StackTraceOptions = {noCodeFrame: false, noStackTrace: false}, -): Array => removeInternalStackEntries(stack.split(/\n/), options); + options?: StackTraceOptions, +): Array { + options = {noCodeFrame: false, noStackTrace: false, ...options}; + return removeInternalStackEntries(stack.split(/\n/), options); +} -export const getTopFrame = (lines: Array): Frame | null => { +export function getTopFrame(lines: Array): Frame | null { for (const line of lines) { if (line.includes(PATH_NODE_MODULES) || line.includes(PATH_JEST_PACKAGES)) { continue; @@ -332,14 +335,14 @@ export const getTopFrame = (lines: Array): Frame | null => { } return null; -}; +} -export const formatStackTrace = ( +export function formatStackTrace( stack: string, config: StackTraceConfig, options: StackTraceOptions, testPath?: string, -): string => { +): string { const lines = getStackTraceLines(stack, options); let renderedCallsite = ''; const relativeTestPath = testPath @@ -376,7 +379,7 @@ export const formatStackTrace = ( return renderedCallsite ? `${renderedCallsite}\n${stacktrace}` : `\n${stacktrace}`; -}; +} type FailedResults = Array<{ /** Stringified version of the error */ diff --git a/packages/jest-reporters/src/GitHubActionsReporter.ts b/packages/jest-reporters/src/GitHubActionsReporter.ts index 84865805d3b4..80e657461a10 100644 --- a/packages/jest-reporters/src/GitHubActionsReporter.ts +++ b/packages/jest-reporters/src/GitHubActionsReporter.ts @@ -69,7 +69,7 @@ export default class GitHubActionsReporter extends BaseReporter { constructor( _globalConfig: Config.GlobalConfig, - reporterOptions: {silent?: boolean} = {silent: true}, + reporterOptions: {silent?: boolean} = {}, ) { super(); this.options = { diff --git a/packages/jest-transform/src/ScriptTransformer.ts b/packages/jest-transform/src/ScriptTransformer.ts index aa6b6fd29095..3340521c0c7b 100644 --- a/packages/jest-transform/src/ScriptTransformer.ts +++ b/packages/jest-transform/src/ScriptTransformer.ts @@ -772,15 +772,17 @@ class ScriptTransformer { async requireAndTranspileModule( moduleName: string, callback?: (module: ModuleType) => void | Promise, - options: RequireAndTranspileModuleOptions = { + options?: RequireAndTranspileModuleOptions, + ): Promise { + options = { applyInteropRequireDefault: true, instrument: false, supportsDynamicImport: false, supportsExportNamespaceFrom: false, supportsStaticESM: false, supportsTopLevelAwait: false, - }, - ): Promise { + ...options, + }; let transforming = false; const {applyInteropRequireDefault, ...transformOptions} = options; const revertHook = addHook( diff --git a/packages/jest-util/src/deepCyclicCopy.ts b/packages/jest-util/src/deepCyclicCopy.ts index d516468306ed..ed76e3ce32ac 100644 --- a/packages/jest-util/src/deepCyclicCopy.ts +++ b/packages/jest-util/src/deepCyclicCopy.ts @@ -14,9 +14,10 @@ export type DeepCyclicCopyOptions = { export default function deepCyclicCopy( value: T, - options: DeepCyclicCopyOptions = {blacklist: EMPTY, keepPrototype: false}, + options?: DeepCyclicCopyOptions, cycles = new WeakMap(), ): T { + options = {blacklist: EMPTY, keepPrototype: false, ...options}; if (typeof value !== 'object' || value === null || Buffer.isBuffer(value)) { return value; } else if (cycles.has(value)) {