Skip to content

Commit 7b6e578

Browse files
sindresorhusnovemberborn
authored andcommitted
Use newer ES syntax where possible
As detected via XO. Also apply other XO fixes.
1 parent 783944f commit 7b6e578

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

76 files changed

+305
-169
lines changed

api.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,9 +45,8 @@ class Api extends Emittery {
4545
this._precompiler = null;
4646
}
4747

48-
run(files, runtimeOptions) {
48+
run(files, runtimeOptions = {}) {
4949
const apiOptions = this.options;
50-
runtimeOptions = runtimeOptions || {};
5150

5251
// Each run will have its own status. It can only be created when test files
5352
// have been found.
@@ -161,9 +160,10 @@ class Api extends Emittery {
161160
if (cachePath) {
162161
acc[realpath] = cachePath;
163162
}
164-
} catch (err) {
165-
throw Object.assign(err, {file});
163+
} catch (error) {
164+
throw Object.assign(error, {file});
166165
}
166+
167167
return acc;
168168
}, {})
169169
};
@@ -175,6 +175,7 @@ class Api extends Emittery {
175175
if (apiOptions.concurrency > 0) {
176176
concurrency = apiOptions.concurrency;
177177
}
178+
178179
if (apiOptions.serial) {
179180
concurrency = 1;
180181
}
@@ -198,6 +199,7 @@ class Api extends Emittery {
198199
} else {
199200
options.precompiled = {};
200201
}
202+
201203
if (runtimeOptions.updateSnapshots) {
202204
// Don't use in Object.assign() since it'll override options.updateSnapshots even when false.
203205
options.updateSnapshots = true;
@@ -245,6 +247,7 @@ class Api extends Emittery {
245247
filename => {
246248
throw new Error(`Cannot apply full precompilation, possible bad usage: ${filename}`);
247249
};
250+
248251
let precompileEnhancementsOnly = () => null;
249252
if (compileEnhancements) {
250253
precompileEnhancementsOnly = this.options.extensions.enhancementsOnly.length > 0 ?

bench/run.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ for (let i = 0; i < 11; i++) {
101101
const results = {};
102102

103103
Promise.each(combined, definition => {
104-
const args = definition.args;
104+
const {args} = definition;
105105

106106
return runTests(args).then(result => {
107107
const key = result.args.join(' ');

lib/assert.js

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -221,10 +221,7 @@ function assertExpectations({assertion, actual, expectations, message, prefix, s
221221
}
222222

223223
function wrapAssertions(callbacks) {
224-
const pass = callbacks.pass;
225-
const pending = callbacks.pending;
226-
const fail = callbacks.fail;
227-
224+
const {pass, pending, fail} = callbacks;
228225
const noop = () => {};
229226

230227
const assertions = {
@@ -336,6 +333,7 @@ function wrapAssertions(callbacks) {
336333
try {
337334
retval.catch(noop);
338335
} catch (_) {}
336+
339337
fail(this, new AssertionError({
340338
assertion: 'throws',
341339
message,
@@ -539,6 +537,7 @@ function wrapAssertions(callbacks) {
539537
} else if (optionsOrMessage) {
540538
options.id = optionsOrMessage.id;
541539
}
540+
542541
options.expected = expected;
543542
options.message = message;
544543

@@ -633,6 +632,7 @@ function wrapAssertions(callbacks) {
633632
values: [formatWithLabel('Called with:', string)]
634633
});
635634
}
635+
636636
if (!(regex instanceof RegExp)) {
637637
throw new AssertionError({
638638
assertion: 'regex',
@@ -663,6 +663,7 @@ function wrapAssertions(callbacks) {
663663
values: [formatWithLabel('Called with:', string)]
664664
});
665665
}
666+
666667
if (!(regex instanceof RegExp)) {
667668
throw new AssertionError({
668669
assertion: 'notRegex',
@@ -687,4 +688,5 @@ function wrapAssertions(callbacks) {
687688

688689
return Object.assign(assertions, enhancedAssertions);
689690
}
691+
690692
exports.wrapAssertions = wrapAssertions;

lib/ava-files.js

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -101,9 +101,7 @@ const getDefaultIgnorePatterns = () => defaultIgnore.map(dir => `${dir}/**/*`);
101101
const matchable = process.platform === 'win32' ? slash : (path => path);
102102

103103
class AvaFiles {
104-
constructor(options) {
105-
options = options || {};
106-
104+
constructor(options = {}) {
107105
let files = (options.files || []).map(file => {
108106
// `./` should be removed from the beginning of patterns because
109107
// otherwise they won't match change events from Chokidar
@@ -121,6 +119,7 @@ class AvaFiles {
121119
if (files.length === 0) {
122120
files = defaultIncludePatterns(this.extensionPattern);
123121
}
122+
124123
this.files = files;
125124
this.sources = options.sources || [];
126125
this.cwd = options.cwd || process.cwd();
@@ -202,7 +201,7 @@ class AvaFiles {
202201
}
203202

204203
isTest(filePath) {
205-
const excludePatterns = this.excludePatterns;
204+
const {excludePatterns} = this;
206205
const initialPatterns = this.files.concat(excludePatterns);
207206

208207
// Like in `api.js`, tests must be `.js` files and not start with `_`

lib/babel-pipeline.js

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,7 @@ function hashPartialTestConfig({babelrc, config, options: {plugins, presets}}, p
114114
inputs.push(stripBomBuf(fs.readFileSync(babelrc)));
115115
}
116116
}
117+
117118
if (config) {
118119
inputs.push(config, stripBomBuf(fs.readFileSync(config)));
119120
}
@@ -196,16 +197,20 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
196197
if (!testOptions.plugins.some(containsAsyncGenerators)) { // TODO: Remove once Babel can parse this syntax unaided.
197198
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-async-generators', 'plugin'));
198199
}
200+
199201
if (!testOptions.plugins.some(containsObjectRestSpread)) { // TODO: Remove once Babel can parse this syntax unaided.
200202
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-object-rest-spread', 'plugin'));
201203
}
204+
202205
if (!testOptions.plugins.some(containsOptionalCatchBinding)) { // TODO: Remove once Babel can parse this syntax unaided.
203206
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-optional-catch-binding', 'plugin'));
204207
}
208+
205209
if (ensureStage4 && !testOptions.presets.some(containsStage4)) {
206210
// Apply last.
207211
testOptions.presets.unshift(createConfigItem('../stage-4', 'preset'));
208212
}
213+
209214
if (compileEnhancements && !testOptions.presets.some(containsTransformTestFiles)) {
210215
// Apply first.
211216
testOptions.presets.push(createConfigItem('@ava/babel-preset-transform-test-files', 'preset', {powerAssert: true}));

lib/chalk.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,15 @@ exports.get = () => {
66
if (!ctx) {
77
throw new Error('Chalk has not yet been configured');
88
}
9+
910
return ctx;
1011
};
12+
1113
exports.set = options => {
1214
if (ctx) {
1315
throw new Error('Chalk has already been configured');
1416
}
17+
1518
ctx = new Chalk(options);
1619
return ctx;
1720
};

lib/code-excerpt.js

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -8,15 +8,12 @@ const chalk = require('./chalk').get();
88
const formatLineNumber = (lineNumber, maxLineNumber) =>
99
' '.repeat(Math.max(0, String(maxLineNumber).length - String(lineNumber).length)) + lineNumber;
1010

11-
module.exports = (source, options) => {
11+
module.exports = (source, options = {}) => {
1212
if (!source.isWithinProject || source.isDependency) {
1313
return null;
1414
}
1515

16-
const file = source.file;
17-
const line = source.line;
18-
19-
options = options || {};
16+
const {file, line} = source;
2017
const maxWidth = options.maxWidth || 80;
2118

2219
let contents;

lib/context-ref.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ class LateBinding extends ContextRef {
3131
if (!this.bound) {
3232
this.set(clone(this.ref.get()));
3333
}
34+
3435
return super.get();
3536
}
3637

lib/create-chain.js

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,10 @@
22
const chainRegistry = new WeakMap();
33

44
function startChain(name, call, defaults) {
5-
const fn = function () {
6-
call(Object.assign({}, defaults), Array.from(arguments));
5+
const fn = (...args) => {
6+
call(Object.assign({}, defaults), args);
77
};
8+
89
Object.defineProperty(fn, 'name', {value: name});
910
chainRegistry.set(fn, {call, defaults, fullName: name});
1011
return fn;
@@ -15,9 +16,10 @@ function extendChain(prev, name, flag) {
1516
flag = name;
1617
}
1718

18-
const fn = function () {
19-
callWithFlag(prev, flag, Array.from(arguments));
19+
const fn = (...args) => {
20+
callWithFlag(prev, flag, args);
2021
};
22+
2123
const fullName = `${chainRegistry.get(prev).fullName}.${name}`;
2224
Object.defineProperty(fn, 'name', {value: fullName});
2325
prev[name] = fn;
@@ -55,6 +57,7 @@ function createHookChain(hook, isAfterHook) {
5557
extendChain(hook.always, 'skip', 'skipped');
5658
extendChain(hook.always.cb, 'skip', 'skipped');
5759
}
60+
5861
return hook;
5962
}
6063

@@ -107,4 +110,5 @@ function createChain(fn, defaults) {
107110

108111
return root;
109112
}
113+
110114
module.exports = createChain;

lib/enhance-assert.js

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,11 @@ const enhanceAssert = (pass, fail, assertions) => {
3737
return empower(assertions, {
3838
destructive: true,
3939
onError(event) {
40-
const error = event.error;
40+
const {error} = event;
4141
if (event.powerAssertContext) { // Context may be missing in internal tests.
4242
error.statements = formatter(event.powerAssertContext);
4343
}
44+
4445
fail(this, error);
4546
},
4647
onSuccess() {
@@ -50,4 +51,5 @@ const enhanceAssert = (pass, fail, assertions) => {
5051
bindReceiver: false
5152
});
5253
};
54+
5355
module.exports = enhanceAssert;

0 commit comments

Comments
 (0)