Skip to content

Commit e5f19d9

Browse files
committed
Modernize some syntax
1 parent 6cc417b commit e5f19d9

23 files changed

+62
-62
lines changed

examples/macros/test.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ function macro(t, a, b, expected) {
66
t.is(sum(a, b), expected);
77
}
88

9-
macro.title = (providedTitle, a, b, expected) => `${providedTitle || ''} ${a}+${b} = ${expected}`.trim();
9+
macro.title = (providedTitle, a, b, expected) => `${providedTitle ?? ''} ${a}+${b} = ${expected}`.trim();
1010

1111
test(macro, 2, 2, 4);
1212
test(macro, 3, 3, 6);

lib/api.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ export default class Api extends Emittery {
164164

165165
const selectionInsights = {
166166
filter,
167-
ignoredFilterPatternFiles: selectedFiles.ignoredFilterPatternFiles || [],
167+
ignoredFilterPatternFiles: selectedFiles.ignoredFilterPatternFiles ?? [],
168168
testFileCount: testFiles.length,
169169
selectionCount: selectedFiles.length,
170170
};
@@ -176,7 +176,7 @@ export default class Api extends Emittery {
176176

177177
// The files must be in the same order across all runs, so sort them.
178178
const defaultComparator = (a, b) => a.localeCompare(b, [], {numeric: true});
179-
selectedFiles = selectedFiles.sort(this.options.sortTestFiles || defaultComparator);
179+
selectedFiles = selectedFiles.sort(this.options.sortTestFiles ?? defaultComparator);
180180
selectedFiles = chunkd(selectedFiles, currentIndex, totalRuns);
181181

182182
const currentFileCount = selectedFiles.length;
@@ -202,7 +202,7 @@ export default class Api extends Emittery {
202202
filePathPrefix: getFilePathPrefix(selectedFiles),
203203
files: selectedFiles,
204204
matching: apiOptions.match.length > 0,
205-
previousFailures: runtimeOptions.previousFailures || 0,
205+
previousFailures: runtimeOptions.previousFailures ?? 0,
206206
runOnlyExclusive: runtimeOptions.runOnlyExclusive === true,
207207
firstRun: runtimeOptions.firstRun ?? true,
208208
status: runStatus,
@@ -305,7 +305,7 @@ export default class Api extends Emittery {
305305
const files = scheduler.storeFailedTestFiles(runStatus, this.options.cacheEnabled === false ? null : this._createCacheDir());
306306
runStatus.emitStateChange({type: 'touched-files', files});
307307
} catch (error) {
308-
if (error && error.name === 'AggregateError') {
308+
if (error?.name === 'AggregateError') {
309309
for (const error_ of error.errors) {
310310
runStatus.emitStateChange({type: 'internal-error', err: serializeError('Internal error', false, error_)});
311311
}

lib/assert.js

+16-16
Original file line numberDiff line numberDiff line change
@@ -34,22 +34,22 @@ const notImplemented = () => {
3434

3535
export class AssertionError extends Error {
3636
constructor(options) {
37-
super(options.message || '');
37+
super(options.message ?? '');
3838
this.name = 'AssertionError';
3939

4040
this.assertion = options.assertion;
4141
this.fixedSource = options.fixedSource;
42-
this.improperUsage = options.improperUsage || false;
42+
this.improperUsage = options.improperUsage ?? false;
4343
this.actualStack = options.actualStack;
4444
this.operator = options.operator;
45-
this.values = options.values || [];
45+
this.values = options.values ?? [];
4646

4747
// Raw expected and actual objects are stored for custom reporters
4848
// (such as wallaby.js), that manage worker processes directly and
4949
// use the values for custom diff views
5050
this.raw = options.raw;
5151

52-
this.savedError = options.savedError || getErrorWithLongStackTrace();
52+
this.savedError = options.savedError ?? getErrorWithLongStackTrace();
5353
}
5454
}
5555

@@ -303,7 +303,7 @@ export class Assertions {
303303

304304
fail(new AssertionError({
305305
assertion: 'fail',
306-
message: message || 'Test failed via `t.fail()`',
306+
message: message ?? 'Test failed via `t.fail()`',
307307
}));
308308

309309
return false;
@@ -320,8 +320,8 @@ export class Assertions {
320320
}
321321

322322
const result = concordance.compare(actual, expected, concordanceOptions);
323-
const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);
324-
const expectedDescriptor = result.expected || concordance.describe(expected, concordanceOptions);
323+
const actualDescriptor = result.actual ?? concordance.describe(actual, concordanceOptions);
324+
const expectedDescriptor = result.expected ?? concordance.describe(expected, concordanceOptions);
325325

326326
if (result.pass) {
327327
fail(new AssertionError({
@@ -372,8 +372,8 @@ export class Assertions {
372372
return true;
373373
}
374374

375-
const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);
376-
const expectedDescriptor = result.expected || concordance.describe(expected, concordanceOptions);
375+
const actualDescriptor = result.actual ?? concordance.describe(actual, concordanceOptions);
376+
const expectedDescriptor = result.expected ?? concordance.describe(expected, concordanceOptions);
377377
fail(new AssertionError({
378378
assertion: 'deepEqual',
379379
message,
@@ -390,7 +390,7 @@ export class Assertions {
390390

391391
const result = concordance.compare(actual, expected, concordanceOptions);
392392
if (result.pass) {
393-
const actualDescriptor = result.actual || concordance.describe(actual, concordanceOptions);
393+
const actualDescriptor = result.actual ?? concordance.describe(actual, concordanceOptions);
394394
fail(new AssertionError({
395395
assertion: 'notDeepEqual',
396396
message,
@@ -442,8 +442,8 @@ export class Assertions {
442442
return true;
443443
}
444444

445-
const actualDescriptor = result.actual || concordance.describe(comparable, concordanceOptions);
446-
const expectedDescriptor = result.expected || concordance.describe(selector, concordanceOptions);
445+
const actualDescriptor = result.actual ?? concordance.describe(comparable, concordanceOptions);
446+
const expectedDescriptor = result.expected ?? concordance.describe(selector, concordanceOptions);
447447
fail(new AssertionError({
448448
assertion: 'like',
449449
message,
@@ -715,7 +715,7 @@ export class Assertions {
715715
return false;
716716
}
717717

718-
if (message && message.id !== undefined) {
718+
if (message?.id !== undefined) {
719719
fail(new AssertionError({
720720
assertion: 'snapshot',
721721
message: 'AVA 4 no longer supports snapshot IDs',
@@ -755,7 +755,7 @@ export class Assertions {
755755

756756
fail(new AssertionError({
757757
assertion: 'snapshot',
758-
message: message || 'Could not compare snapshot',
758+
message: message ?? 'Could not compare snapshot',
759759
improperUsage,
760760
}));
761761
return false;
@@ -769,14 +769,14 @@ export class Assertions {
769769
if (result.actual) {
770770
fail(new AssertionError({
771771
assertion: 'snapshot',
772-
message: message || 'Did not match snapshot',
772+
message: message ?? 'Did not match snapshot',
773773
values: [formatDescriptorDiff(result.actual, result.expected, {invert: true})],
774774
}));
775775
} else {
776776
// This can only occur in CI environments.
777777
fail(new AssertionError({
778778
assertion: 'snapshot',
779-
message: message || 'No snapshot available — new snapshots are not created in CI environments',
779+
message: message ?? 'No snapshot available — new snapshots are not created in CI environments',
780780
}));
781781
}
782782

lib/cli.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -173,7 +173,7 @@ export default async function loadCli() { // eslint-disable-line complexity
173173
type: 'string',
174174
}), argv => {
175175
if (activeInspector) {
176-
debug.files = argv.pattern || [];
176+
debug.files = argv.pattern ?? [];
177177
}
178178
})
179179
.command(
@@ -283,7 +283,7 @@ export default async function loadCli() { // eslint-disable-line complexity
283283

284284
process.exit(0); // eslint-disable-line unicorn/no-process-exit
285285
} catch (error) {
286-
exit(`Error removing AVA cache files in ${cacheDir}\n\n${chalk.gray((error && error.stack) || error)}`);
286+
exit(`Error removing AVA cache files in ${cacheDir}\n\n${chalk.gray(error?.stack ?? error)}`);
287287
}
288288
}
289289

@@ -344,7 +344,7 @@ export default async function loadCli() { // eslint-disable-line complexity
344344
}
345345
}
346346

347-
const {type: defaultModuleType = 'commonjs'} = projectPackageObject || {};
347+
const {type: defaultModuleType = 'commonjs'} = projectPackageObject ?? {};
348348

349349
const providers = [];
350350
if (Object.hasOwn(conf, 'typescript')) {
@@ -404,7 +404,7 @@ export default async function loadCli() { // eslint-disable-line complexity
404404

405405
const match = combined.match === '' ? [] : arrify(combined.match);
406406

407-
const input = debug ? debug.files : (argv.pattern || []);
407+
const input = debug ? debug.files : (argv.pattern ?? []);
408408
const filter = input
409409
.map(pattern => splitPatternAndLineNumbers(pattern))
410410
.map(({pattern, ...rest}) => ({
@@ -415,7 +415,7 @@ export default async function loadCli() { // eslint-disable-line complexity
415415
const api = new Api({
416416
cacheEnabled: combined.cache !== false,
417417
chalkOptions,
418-
concurrency: combined.concurrency || 0,
418+
concurrency: combined.concurrency ?? 0,
419419
workerThreads: combined.workerThreads !== false,
420420
debug,
421421
enableAva5DependencyTracking: argv.watch && conf.watchMode?.implementation === 'ava5+chokidar',
@@ -436,7 +436,7 @@ export default async function loadCli() { // eslint-disable-line complexity
436436
require: arrify(combined.require),
437437
serial: combined.serial,
438438
snapshotDir: combined.snapshotDir ? path.resolve(projectDir, combined.snapshotDir) : null,
439-
timeout: combined.timeout || '10s',
439+
timeout: combined.timeout ?? '10s',
440440
updateSnapshots: combined.updateSnapshots,
441441
workerArgv: argv['--'],
442442
});

lib/code-excerpt.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -8,13 +8,13 @@ import {chalk} from './chalk.js';
88
const formatLineNumber = (lineNumber, maxLineNumber) =>
99
' '.repeat(Math.max(0, String(maxLineNumber).length - String(lineNumber).length)) + lineNumber;
1010

11-
export default function exceptCode(source, options = {}) {
11+
export default function excerptCode(source, options = {}) {
1212
if (!source.isWithinProject || source.isDependency) {
1313
return null;
1414
}
1515

1616
const {file, line} = source;
17-
const maxWidth = options.maxWidth || 80;
17+
const maxWidth = (options.maxWidth ?? 0) || 80;
1818

1919
let contents;
2020
try {

lib/eslint-plugin-helper-worker.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ const buildGlobs = ({conf, providers, projectDir, overrideExtensions, overrideFi
3333
cwd: projectDir,
3434
...normalizeGlobs({
3535
extensions,
36-
files: overrideFiles || conf.files,
36+
files: overrideFiles ?? conf.files,
3737
providers,
3838
}),
3939
};

lib/fork.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ const createWorker = (options, execArgv) => {
3737

3838
// Ensure we've seen this event before we terminate the worker thread, as a
3939
// workaround for https://github.com/nodejs/node/issues/38418.
40-
const starting = pEvent(worker, 'message', ({ava}) => ava && ava.type === 'starting');
40+
const starting = pEvent(worker, 'message', ({ava}) => ava?.type === 'starting');
4141

4242
close = async () => {
4343
try {

lib/load-config.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ function resolveConfigFile(configFile) {
4141
return configFile;
4242
}
4343

44-
const gitScmFile = process.env.AVA_FAKE_SCM_ROOT || '.git';
44+
const gitScmFile = process.env.AVA_FAKE_SCM_ROOT ?? '.git';
4545

4646
async function findRepoRoot(fromDir) {
4747
const {root} = path.parse(fromDir);

lib/parse-test-args.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
const buildTitle = (raw, implementation, args) => {
2-
let value = implementation && implementation.title ? implementation.title(raw, ...args) : raw;
2+
let value = implementation?.title?.(raw, ...args) ?? raw;
33
const isValid = typeof value === 'string';
44
if (isValid) {
55
value = value.trim().replace(/\s+/g, ' ');
@@ -20,7 +20,7 @@ export default function parseTestArgs(args) {
2020

2121
return {
2222
args,
23-
implementation: implementation && implementation.exec ? implementation.exec : implementation,
23+
implementation: implementation?.exec ?? implementation,
2424
title: buildTitle(rawTitle, implementation, args),
2525
};
2626
}

lib/reporters/default.js

+5-5
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ class LineWriter extends stream.Writable {
2525
super();
2626

2727
this.dest = dest;
28-
this.columns = dest.columns || 80;
28+
this.columns = dest.columns ?? 80;
2929
this.lastLineIsEmpty = false;
3030
}
3131

@@ -91,7 +91,7 @@ export default class Reporter {
9191
this.consumeStateChange = decorateWriter(this.consumeStateChange);
9292
this.endRun = decorateWriter(this.endRun);
9393

94-
this.durationThreshold = durationThreshold || 100;
94+
this.durationThreshold = durationThreshold ?? 100;
9595
this.lineWriter = new LineWriter(this.reportStream);
9696

9797
this.reset();
@@ -390,7 +390,7 @@ export default class Reporter {
390390
return this.lineWriter.writeLine(string);
391391
}
392392

393-
string = string || '';
393+
string ??= '';
394394
if (string !== '') {
395395
string += os.EOL;
396396
}
@@ -428,7 +428,7 @@ export default class Reporter {
428428
}
429429

430430
writeErr(event) {
431-
if (event.err.name === 'TSError' && event.err.object && event.err.object.diagnosticText) {
431+
if (event.err.name === 'TSError' && event.err.object?.diagnosticText) {
432432
this.lineWriter.writeLine(colors.errorStack(event.err.object.diagnosticText));
433433
this.lineWriter.writeLine();
434434
return;
@@ -495,7 +495,7 @@ export default class Reporter {
495495
}
496496

497497
writeLogs(event, surroundLines) {
498-
if (event.logs && event.logs.length > 0) {
498+
if (event.logs?.length > 0) {
499499
if (surroundLines) {
500500
this.lineWriter.writeLine();
501501
}

lib/reporters/tap.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ export default class TapReporter {
108108

109109
writeCrash(evt, title) {
110110
this.crashCount++;
111-
this.reportStream.write(supertap.test(title || evt.err.summary || evt.type, {
111+
this.reportStream.write(supertap.test(title ?? evt.err.summary ?? evt.type, {
112112
comment: evt.logs,
113113
error: evt.err ? dumpError(evt.err) : null,
114114
index: ++this.i,

lib/runner.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,12 +17,12 @@ export default class Runner extends Emittery {
1717
constructor(options = {}) {
1818
super();
1919

20-
this.experiments = options.experiments || {};
20+
this.experiments = options.experiments ?? {};
2121
this.failFast = options.failFast === true;
2222
this.failWithoutAssertions = options.failWithoutAssertions !== false;
2323
this.file = options.file;
2424
this.checkSelectedByLineNumbers = options.checkSelectedByLineNumbers;
25-
this.match = options.match || [];
25+
this.match = options.match ?? [];
2626
this.projectDir = options.projectDir;
2727
this.recordNewSnapshots = options.recordNewSnapshots === true;
2828
this.runOnlyExclusive = options.runOnlyExclusive === true;
@@ -151,7 +151,7 @@ export default class Runner extends Emittery {
151151
}
152152

153153
const task = {
154-
title: title.value || fallbackTitle,
154+
title: title.value ?? fallbackTitle,
155155
implementation,
156156
args,
157157
metadata: {...metadata},
@@ -301,7 +301,7 @@ export default class Runner extends Emittery {
301301
skipSnapshot: this.boundSkipSnapshot,
302302
updateSnapshots: this.updateSnapshots,
303303
metadata: task.metadata,
304-
title: `${task.title}${titleSuffix || ''}`,
304+
title: `${task.title}${titleSuffix ?? ''}`,
305305
isHook: true,
306306
testPassed,
307307
notifyTimeoutUpdate: this.notifyTimeoutUpdate,

lib/snapshot-manager.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -259,8 +259,8 @@ class Manager {
259259

260260
const block = this.newBlocksByTitle.get(options.belongsTo);
261261

262-
const snapshot = block && block.snapshots[options.index];
263-
const data = snapshot && snapshot.data;
262+
const snapshot = block?.snapshots[options.index];
263+
const data = snapshot?.data;
264264

265265
if (!data) {
266266
if (!this.recordNewSnapshots) {
@@ -332,7 +332,7 @@ class Manager {
332332

333333
skipSnapshot({belongsTo, index, deferRecording}) {
334334
const oldBlock = this.oldBlocksByTitle.get(belongsTo);
335-
let snapshot = oldBlock && oldBlock.snapshots[index];
335+
let snapshot = oldBlock?.snapshots[index];
336336

337337
if (!snapshot) {
338338
snapshot = {};

lib/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -194,7 +194,7 @@ class ExecutionContext extends Assertions {
194194
export default class Test {
195195
constructor(options) {
196196
this.contextRef = options.contextRef;
197-
this.experiments = options.experiments || {};
197+
this.experiments = options.experiments ?? {};
198198
this.failWithoutAssertions = options.failWithoutAssertions;
199199
this.fn = options.fn;
200200
this.isHook = options.isHook === true;
@@ -420,7 +420,7 @@ export default class Test {
420420
this.clearTimeout();
421421
this.timeoutMs = ms;
422422
this.timeoutTimer = nowAndTimers.setCappedTimeout(() => {
423-
this.saveFirstError(new Error(message || 'Test timeout exceeded'));
423+
this.saveFirstError(new Error(message ?? 'Test timeout exceeded'));
424424

425425
if (this.finishDueToTimeout) {
426426
this.finishDueToTimeout();

0 commit comments

Comments
 (0)