Permalink
Browse files

Use newer ES syntax where possible

As detected via XO. Also apply other XO fixes.
  • Loading branch information...
sindresorhus authored and novemberborn committed Dec 17, 2018
1 parent 783944f commit 7b6e578dce32a6561e61ed6e2be6de8237b37800
Showing with 305 additions and 169 deletions.
  1. +7 βˆ’4 api.js
  2. +1 βˆ’1 bench/run.js
  3. +6 βˆ’4 lib/assert.js
  4. +3 βˆ’4 lib/ava-files.js
  5. +5 βˆ’0 lib/babel-pipeline.js
  6. +3 βˆ’0 lib/chalk.js
  7. +2 βˆ’5 lib/code-excerpt.js
  8. +1 βˆ’0 lib/context-ref.js
  9. +8 βˆ’4 lib/create-chain.js
  10. +3 βˆ’1 lib/enhance-assert.js
  11. +1 βˆ’0 lib/extensions.js
  12. +4 βˆ’0 lib/load-config.js
  13. +1 βˆ’0 lib/reporters/format-serialized-error.js
  14. +3 βˆ’5 lib/reporters/improper-usage-messages.js
  15. +22 βˆ’1 lib/reporters/mini.js
  16. +6 βˆ’0 lib/reporters/tap.js
  17. +20 βˆ’1 lib/reporters/verbose.js
  18. +3 βˆ’2 lib/reporters/while-corked.js
  19. +13 βˆ’9 lib/run-status.js
  20. +7 βˆ’2 lib/runner.js
  21. +5 βˆ’0 lib/serialize-error.js
  22. +12 βˆ’6 lib/snapshot-manager.js
  23. +7 βˆ’3 lib/test.js
  24. +3 βˆ’1 lib/watcher.js
  25. +4 βˆ’0 lib/worker/dependency-tracker.js
  26. +1 βˆ’1 lib/worker/fake-tty.js
  27. +3 βˆ’0 lib/worker/ipc.js
  28. +3 βˆ’2 lib/worker/main.js
  29. +3 βˆ’0 lib/worker/options.js
  30. +1 βˆ’0 lib/worker/precompiler-hook.js
  31. +18 βˆ’18 package-lock.json
  32. +1 βˆ’5 package.json
  33. +4 βˆ’3 test/api.js
  34. +6 βˆ’1 test/assert.js
  35. +3 βˆ’4 test/ava-files.js
  36. +1 βˆ’1 test/beautify-stack.js
  37. +1 βˆ’1 test/code-excerpt.js
  38. +1 βˆ’1 test/fixture/babel-plugin-test-capitalizer.js
  39. +2 βˆ’2 test/fixture/babel-plugin-test-doubler.js
  40. +3 βˆ’0 test/fixture/formatting.js
  41. +2 βˆ’0 test/fixture/report/regular/test.js
  42. +2 βˆ’0 test/fixture/trigger-worker-exception/hack.js
  43. +2 βˆ’1 test/helper/cli.js
  44. +2 βˆ’0 test/helper/report.js
  45. +3 βˆ’0 test/helper/tty-stream.js
  46. +1 βˆ’1 test/hooks.js
  47. +1 βˆ’1 test/integration/assorted.js
  48. +1 βˆ’1 test/integration/babel.js
  49. +1 βˆ’1 test/integration/compilation.js
  50. +1 βˆ’1 test/integration/concurrency.js
  51. +1 βˆ’1 test/integration/config.js
  52. +1 βˆ’1 test/integration/extensions.js
  53. +1 βˆ’1 test/integration/node-assertions.js
  54. +1 βˆ’1 test/integration/parallel-runs.js
  55. +1 βˆ’1 test/integration/repl.js
  56. +5 βˆ’1 test/integration/snapshots.js
  57. +1 βˆ’1 test/integration/stack-traces.js
  58. +1 βˆ’1 test/integration/stalled.js
  59. +1 βˆ’1 test/integration/t-throws.js
  60. +1 βˆ’1 test/integration/tty.js
  61. +9 βˆ’8 test/integration/watcher.js
  62. +1 βˆ’1 test/observable.js
  63. +1 βˆ’1 test/profile.js
  64. +1 βˆ’1 test/promise.js
  65. +1 βˆ’1 test/reporters/improper-usage-messages.js
  66. +1 βˆ’1 test/reporters/mini.js
  67. +11 βˆ’11 test/reporters/mini.regular.log
  68. +1 βˆ’1 test/reporters/prefix-title.js
  69. +1 βˆ’1 test/reporters/tap.js
  70. +1 βˆ’1 test/reporters/tap.regular.log
  71. +1 βˆ’1 test/reporters/verbose.js
  72. +11 βˆ’11 test/reporters/verbose.regular.log
  73. +18 βˆ’12 test/runner.js
  74. +1 βˆ’1 test/serialize-error.js
  75. +2 βˆ’1 test/test.js
  76. +13 βˆ’8 test/watcher.js
11 api.js
@@ -45,9 +45,8 @@ class Api extends Emittery {
this._precompiler = null;
}

run(files, runtimeOptions) {
run(files, runtimeOptions = {}) {
const apiOptions = this.options;
runtimeOptions = runtimeOptions || {};

// Each run will have its own status. It can only be created when test files
// have been found.
@@ -161,9 +160,10 @@ class Api extends Emittery {
if (cachePath) {
acc[realpath] = cachePath;
}
} catch (err) {
throw Object.assign(err, {file});
} catch (error) {
throw Object.assign(error, {file});
}

return acc;
}, {})
};
@@ -175,6 +175,7 @@ class Api extends Emittery {
if (apiOptions.concurrency > 0) {
concurrency = apiOptions.concurrency;
}

if (apiOptions.serial) {
concurrency = 1;
}
@@ -198,6 +199,7 @@ class Api extends Emittery {
} else {
options.precompiled = {};
}

if (runtimeOptions.updateSnapshots) {
// Don't use in Object.assign() since it'll override options.updateSnapshots even when false.
options.updateSnapshots = true;
@@ -245,6 +247,7 @@ class Api extends Emittery {
filename => {
throw new Error(`Cannot apply full precompilation, possible bad usage: ${filename}`);
};

let precompileEnhancementsOnly = () => null;
if (compileEnhancements) {
precompileEnhancementsOnly = this.options.extensions.enhancementsOnly.length > 0 ?
@@ -101,7 +101,7 @@ for (let i = 0; i < 11; i++) {
const results = {};

Promise.each(combined, definition => {
const args = definition.args;
const {args} = definition;

return runTests(args).then(result => {
const key = result.args.join(' ');
@@ -221,10 +221,7 @@ function assertExpectations({assertion, actual, expectations, message, prefix, s
}

function wrapAssertions(callbacks) {
const pass = callbacks.pass;
const pending = callbacks.pending;
const fail = callbacks.fail;

const {pass, pending, fail} = callbacks;
const noop = () => {};

const assertions = {
@@ -336,6 +333,7 @@ function wrapAssertions(callbacks) {
try {
retval.catch(noop);
} catch (_) {}

fail(this, new AssertionError({
assertion: 'throws',
message,
@@ -539,6 +537,7 @@ function wrapAssertions(callbacks) {
} else if (optionsOrMessage) {
options.id = optionsOrMessage.id;
}

options.expected = expected;
options.message = message;

@@ -633,6 +632,7 @@ function wrapAssertions(callbacks) {
values: [formatWithLabel('Called with:', string)]
});
}

if (!(regex instanceof RegExp)) {
throw new AssertionError({
assertion: 'regex',
@@ -663,6 +663,7 @@ function wrapAssertions(callbacks) {
values: [formatWithLabel('Called with:', string)]
});
}

if (!(regex instanceof RegExp)) {
throw new AssertionError({
assertion: 'notRegex',
@@ -687,4 +688,5 @@ function wrapAssertions(callbacks) {

return Object.assign(assertions, enhancedAssertions);
}

exports.wrapAssertions = wrapAssertions;
@@ -101,9 +101,7 @@ const getDefaultIgnorePatterns = () => defaultIgnore.map(dir => `${dir}/**/*`);
const matchable = process.platform === 'win32' ? slash : (path => path);

class AvaFiles {
constructor(options) {
options = options || {};

constructor(options = {}) {
let files = (options.files || []).map(file => {
// `./` should be removed from the beginning of patterns because
// otherwise they won't match change events from Chokidar
@@ -121,6 +119,7 @@ class AvaFiles {
if (files.length === 0) {
files = defaultIncludePatterns(this.extensionPattern);
}

this.files = files;
this.sources = options.sources || [];
this.cwd = options.cwd || process.cwd();
@@ -202,7 +201,7 @@ class AvaFiles {
}

isTest(filePath) {
const excludePatterns = this.excludePatterns;
const {excludePatterns} = this;
const initialPatterns = this.files.concat(excludePatterns);

// Like in `api.js`, tests must be `.js` files and not start with `_`
@@ -114,6 +114,7 @@ function hashPartialTestConfig({babelrc, config, options: {plugins, presets}}, p
inputs.push(stripBomBuf(fs.readFileSync(babelrc)));
}
}

if (config) {
inputs.push(config, stripBomBuf(fs.readFileSync(config)));
}
@@ -196,16 +197,20 @@ function build(projectDir, cacheDir, userOptions, compileEnhancements) {
if (!testOptions.plugins.some(containsAsyncGenerators)) { // TODO: Remove once Babel can parse this syntax unaided.
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-async-generators', 'plugin'));
}

if (!testOptions.plugins.some(containsObjectRestSpread)) { // TODO: Remove once Babel can parse this syntax unaided.
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-object-rest-spread', 'plugin'));
}

if (!testOptions.plugins.some(containsOptionalCatchBinding)) { // TODO: Remove once Babel can parse this syntax unaided.
testOptions.plugins.unshift(createConfigItem('@babel/plugin-syntax-optional-catch-binding', 'plugin'));
}

if (ensureStage4 && !testOptions.presets.some(containsStage4)) {
// Apply last.
testOptions.presets.unshift(createConfigItem('../stage-4', 'preset'));
}

if (compileEnhancements && !testOptions.presets.some(containsTransformTestFiles)) {
// Apply first.
testOptions.presets.push(createConfigItem('@ava/babel-preset-transform-test-files', 'preset', {powerAssert: true}));
@@ -6,12 +6,15 @@ exports.get = () => {
if (!ctx) {
throw new Error('Chalk has not yet been configured');
}

return ctx;
};

exports.set = options => {
if (ctx) {
throw new Error('Chalk has already been configured');
}

ctx = new Chalk(options);
return ctx;
};
@@ -8,15 +8,12 @@ const chalk = require('./chalk').get();
const formatLineNumber = (lineNumber, maxLineNumber) =>
' '.repeat(Math.max(0, String(maxLineNumber).length - String(lineNumber).length)) + lineNumber;

module.exports = (source, options) => {
module.exports = (source, options = {}) => {
if (!source.isWithinProject || source.isDependency) {
return null;
}

const file = source.file;
const line = source.line;

options = options || {};
const {file, line} = source;
const maxWidth = options.maxWidth || 80;

let contents;
@@ -31,6 +31,7 @@ class LateBinding extends ContextRef {
if (!this.bound) {
this.set(clone(this.ref.get()));
}

return super.get();
}

@@ -2,9 +2,10 @@
const chainRegistry = new WeakMap();

function startChain(name, call, defaults) {
const fn = function () {
call(Object.assign({}, defaults), Array.from(arguments));
const fn = (...args) => {
call(Object.assign({}, defaults), args);
};

Object.defineProperty(fn, 'name', {value: name});
chainRegistry.set(fn, {call, defaults, fullName: name});
return fn;
@@ -15,9 +16,10 @@ function extendChain(prev, name, flag) {
flag = name;
}

const fn = function () {
callWithFlag(prev, flag, Array.from(arguments));
const fn = (...args) => {
callWithFlag(prev, flag, args);
};

const fullName = `${chainRegistry.get(prev).fullName}.${name}`;
Object.defineProperty(fn, 'name', {value: fullName});
prev[name] = fn;
@@ -55,6 +57,7 @@ function createHookChain(hook, isAfterHook) {
extendChain(hook.always, 'skip', 'skipped');
extendChain(hook.always.cb, 'skip', 'skipped');
}

return hook;
}

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

return root;
}

module.exports = createChain;
@@ -37,10 +37,11 @@ const enhanceAssert = (pass, fail, assertions) => {
return empower(assertions, {
destructive: true,
onError(event) {
const error = event.error;
const {error} = event;
if (event.powerAssertContext) { // Context may be missing in internal tests.
error.statements = formatter(event.powerAssertContext);
}

fail(this, error);
},
onSuccess() {
@@ -50,4 +51,5 @@ const enhanceAssert = (pass, fail, assertions) => {
bindReceiver: false
});
};

module.exports = enhanceAssert;
@@ -19,6 +19,7 @@ module.exports = (enhancementsOnly, babelConfig) => {
seen.add('js');
full.push('js');
}

if (!babelConfig && enhancementsOnly.length === 0) {
seen.add('js');
enhancementsOnly.push('js');
@@ -39,6 +39,7 @@ function loadConfig(defaults = {}) {
if (fileConf && typeof fileConf.then === 'function') {
throw new TypeError('ava.config.js must not export a promise');
}

if (!isPlainObject(fileConf) && typeof fileConf !== 'function') {
throw new TypeError('ava.config.js must export a plain object or factory function');
}
@@ -48,15 +49,18 @@ function loadConfig(defaults = {}) {
if (fileConf && typeof fileConf.then === 'function') {
throw new TypeError('Factory method exported by ava.config.js must not return a promise');
}

if (!isPlainObject(fileConf)) {
throw new TypeError('Factory method exported by ava.config.js must return a plain object');
}
}

if ('ava' in fileConf) {
throw new Error('Encountered \'ava\' property in ava.config.js; avoid wrapping the configuration');
}
}

return Object.assign({}, defaults, fileConf, packageConf, {projectDir});
}

module.exports = loadConfig;
@@ -23,4 +23,5 @@ function formatSerializedError(error) {
formatted = trimOffNewlines(formatted);
return {formatted, printMessage};
}

module.exports = formatSerializedError;
@@ -7,7 +7,7 @@ exports.forError = error => {
return null;
}

const assertion = error.assertion;
const {assertion} = error;
if (assertion === 'throws' || assertion === 'notThrows') {
return `Try wrapping the first argument to \`t.${assertion}()\` in a function:
@@ -19,8 +19,7 @@ Visit the following URL for more details:
}

if (assertion === 'snapshot') {
const name = error.improperUsage.name;
const snapPath = error.improperUsage.snapPath;
const {name, snapPath} = error.improperUsage;

if (name === 'ChecksumError') {
return `The snapshot file is corrupted.
@@ -39,8 +38,7 @@ Please run AVA again with the ${chalk.cyan('--update-snapshots')} flag to upgrad
}

if (name === 'VersionMismatchError') {
const snapVersion = error.improperUsage.snapVersion;
const expectedVersion = error.improperUsage.expectedVersion;
const {snapVersion, expectedVersion} = error.improperUsage;
const upgradeMessage = snapVersion < expectedVersion ?
`Please run AVA again with the ${chalk.cyan('--update-snapshots')} flag to upgrade.` :
'You should upgrade AVA.';
Oops, something went wrong.

0 comments on commit 7b6e578

Please sign in to comment.