Skip to content

Commit

Permalink
chore: avoid objects as default parameters (jestjs#14821)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimenB authored Dec 31, 2023
1 parent bd3a7e9 commit a22fa55
Show file tree
Hide file tree
Showing 6 changed files with 24 additions and 17 deletions.
1 change: 0 additions & 1 deletion .eslintrc.cjs
Original file line number Diff line number Diff line change
Expand Up @@ -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',

Expand Down
8 changes: 5 additions & 3 deletions packages/jest-haste-map/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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.
*
Expand Down Expand Up @@ -450,7 +452,7 @@ class HasteMap extends EventEmitter implements IHasteMap {
map: ModuleMapData,
mocks: MockData,
filePath: string,
workerOptions?: {forceInBand: boolean},
workerOptions?: WorkerOptions,
): Promise<void> | null {
const rootDir = this._options.rootDir;

Expand Down Expand Up @@ -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> | 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'), {
Expand Down
19 changes: 11 additions & 8 deletions packages/jest-message-util/src/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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<string> => removeInternalStackEntries(stack.split(/\n/), options);
options?: StackTraceOptions,
): Array<string> {
options = {noCodeFrame: false, noStackTrace: false, ...options};
return removeInternalStackEntries(stack.split(/\n/), options);
}

export const getTopFrame = (lines: Array<string>): Frame | null => {
export function getTopFrame(lines: Array<string>): Frame | null {
for (const line of lines) {
if (line.includes(PATH_NODE_MODULES) || line.includes(PATH_JEST_PACKAGES)) {
continue;
Expand All @@ -332,14 +335,14 @@ export const getTopFrame = (lines: Array<string>): 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
Expand Down Expand Up @@ -376,7 +379,7 @@ export const formatStackTrace = (
return renderedCallsite
? `${renderedCallsite}\n${stacktrace}`
: `\n${stacktrace}`;
};
}

type FailedResults = Array<{
/** Stringified version of the error */
Expand Down
2 changes: 1 addition & 1 deletion packages/jest-reporters/src/GitHubActionsReporter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down
8 changes: 5 additions & 3 deletions packages/jest-transform/src/ScriptTransformer.ts
Original file line number Diff line number Diff line change
Expand Up @@ -772,15 +772,17 @@ class ScriptTransformer {
async requireAndTranspileModule<ModuleType = unknown>(
moduleName: string,
callback?: (module: ModuleType) => void | Promise<void>,
options: RequireAndTranspileModuleOptions = {
options?: RequireAndTranspileModuleOptions,
): Promise<ModuleType> {
options = {
applyInteropRequireDefault: true,
instrument: false,
supportsDynamicImport: false,
supportsExportNamespaceFrom: false,
supportsStaticESM: false,
supportsTopLevelAwait: false,
},
): Promise<ModuleType> {
...options,
};
let transforming = false;
const {applyInteropRequireDefault, ...transformOptions} = options;
const revertHook = addHook(
Expand Down
3 changes: 2 additions & 1 deletion packages/jest-util/src/deepCyclicCopy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,10 @@ export type DeepCyclicCopyOptions = {

export default function deepCyclicCopy<T>(
value: T,
options: DeepCyclicCopyOptions = {blacklist: EMPTY, keepPrototype: false},
options?: DeepCyclicCopyOptions,
cycles = new WeakMap<any, any>(),
): T {
options = {blacklist: EMPTY, keepPrototype: false, ...options};
if (typeof value !== 'object' || value === null || Buffer.isBuffer(value)) {
return value;
} else if (cycles.has(value)) {
Expand Down

0 comments on commit a22fa55

Please sign in to comment.