Skip to content

Commit

Permalink
chore: replace airbnb with xo style
Browse files Browse the repository at this point in the history
  • Loading branch information
plasticrake committed Aug 11, 2024
1 parent 2cfd4c5 commit b5f767e
Show file tree
Hide file tree
Showing 10 changed files with 12,063 additions and 276 deletions.
4 changes: 3 additions & 1 deletion .prettierrc.json
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
{
"printWidth": 80,
"useTabs": false,
"tabWidth": 2,
"semi": true,
"singleQuote": true,
"trailingComma": "es5"
"bracketSpacing": true,
"trailingComma": "all"
}
30 changes: 30 additions & 0 deletions .xo-config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
{
"prettier": true,
"rules": {
"capitalized-comments": "off",
"eqeqeq": ["error", "always", { "null": "ignore" }],
"import/order": ["error", { "newlines-between": "ignore" }],
"no-eq-null": "off",
"no-shadow": ["error"],
"unicorn/no-negated-condition": "off",
"unicorn/prefer-module": "off",
"unicorn/prevent-abbreviations": "off"
},
"overrides": [
{
"prettier": true,
"files": "test/**/*.js",
"envs": ["node", "mocha"],
"rules": {
"func-names": "off",
"prefer-arrow-callback": "off"
}
},
{
"files": "examples/**/*.js",
"rules": {
"no-new": "off"
}
}
]
}
6 changes: 3 additions & 3 deletions lib/cli.js
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#!/usr/bin/env node

/* eslint-env node */
/* eslint-disable no-console */

const path = require('path');
const path = require('node:path');
const process = require('node:process');
const yargs = require('yargs/yargs');

const { loadOptions, YARGS_PARSER_CONFIG } = require('mocha/lib/cli/options');

const { version } = require('../package.json');
const run = require('./run');
const run = require('./run.js');

exports.main = (argv = process.argv.slice(2)) => {
// ensure we can require() from current working directory
Expand Down
27 changes: 15 additions & 12 deletions lib/mocha-json-runner.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,12 +58,12 @@ class MochaJsonRunner extends Mocha.Runner {
};
}

this.failHooks = function failHooks(hooks) {
hooks.forEach((hook) => {
this.failHooks = function (hooks) {
for (const hook of hooks) {
hook.run((err) => {
this.failHook(hook, err);
});
});
}
};

/**
Expand All @@ -76,7 +76,7 @@ class MochaJsonRunner extends Mocha.Runner {
* @param {Function} fn
* @return {Runner} Runner instance.
*/
this.run = function run(fn = () => {}) {
this.run = function (fn = () => {}) {
this.emit(EVENT_RUN_BEGIN);

const runSuite = (suite) => {
Expand All @@ -90,24 +90,26 @@ class MochaJsonRunner extends Mocha.Runner {
if (failingBeforeAllHooks.length > 0) {
this.failHooks(failingBeforeAllHooks);
} else {
// eslint-disable-next-line no-restricted-syntax
for (const test of suite.tests) {
switch (test.state) {
case STATE_PASSED:
case STATE_PASSED: {
this.emit(EVENT_TEST_PASS, test);
if (!hasStats) this.stats.passes += 1;
break;
}

case STATE_FAILED:
case STATE_FAILED: {
test.run((err) => {
this.fail(test, err); // Runner will emit EVENT_TEST_FAIL
if (!hasStats) this.stats.failures += 1;
});
break;
}

default:
default: {
if (test.pending) {
this.emit(EVENT_TEST_PENDING, test);
// eslint-disable-next-line max-depth
if (!hasStats) this.stats.pending += 1;
} else if (failingBeforeEachHooks.length > 0) {
this.failHooks(failingBeforeEachHooks);
Expand All @@ -117,14 +119,15 @@ class MochaJsonRunner extends Mocha.Runner {
break; // skip remaining tests in suite
} else if (config.warnOnMissingState) {
// could be a test that did not match a grep
// eslint-disable-next-line no-console
console.error(
`Unexpected test.state: ${
test.state
} and not pending. test: ${test.fullTitle()}`
} and not pending. test: ${test.fullTitle()}`,
);
}
}
}

this.emit(EVENT_TEST_END, test);
if (!hasStats) this.stats.tests += 1;
}
Expand All @@ -133,9 +136,9 @@ class MochaJsonRunner extends Mocha.Runner {
failingBeforeEachHooks.length === 0 &&
failingAfterEachHooks.length === 0
) {
suite.suites.forEach((childSuite) => {
for (const childSuite of suite.suites) {
runSuite(childSuite);
});
}
}
}

Expand Down
37 changes: 19 additions & 18 deletions lib/run.js
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
/* eslint-disable unicorn/no-process-exit */
/* eslint-env node */
/* eslint-disable no-console */

/**
* Definition for Mocha's default ("run tests") command
Expand All @@ -8,8 +8,8 @@
* @private
*/

const fs = require('fs');

const fs = require('node:fs');
const process = require('node:process');
const Mocha = require('mocha');
const { createInvalidArgumentValueError } = require('mocha/lib/errors');

Expand All @@ -26,12 +26,10 @@ const {
const defaults = require('mocha/lib/mocharc');
const { types, aliases } = require('mocha/lib/cli/run-option-metadata');

const MochaJsonRunner = require('./mocha-json-runner');
const MochaJsonRunner = require('./mocha-json-runner.js');

// Mocha v8.2 changed the name of this function to `validateLegacyPlugin`
if (validateLegacyPlugin == null) {
validateLegacyPlugin = validatePlugin;
}
validateLegacyPlugin ??= validatePlugin;

/**
* Logical option groups
Expand Down Expand Up @@ -88,24 +86,27 @@ exports.builder = (yargs) =>
description: 'List built-in reporters & exit',
},
'reporter-option': {
coerce: (opts) => {
coerce(opts) {
if (opts == null) return undefined;

return list(opts).reduce((acc, opt) => {
const result = {};
const optsList = list(opts);
for (const opt of optsList) {
const pair = opt.split('=');

if (pair.length > 2 || !pair.length) {
if (pair.length > 2 || pair.length === 0) {
throw createInvalidArgumentValueError(
`invalid reporter option '${opt}'`,
'--reporter-option',
opt,
'expected "key=value" format'
'expected "key=value" format',
);
}

acc[pair[0]] = pair.length === 2 ? pair[1] : true;
return acc;
}, {});
result[pair[0]] = pair.length === 2 ? pair[1] : true;
}

return result;
},
description: 'Reporter-specific options (<k=v,[k1=v1,..]>)',
group: GROUPS.OUTPUT,
Expand All @@ -129,12 +130,12 @@ exports.builder = (yargs) =>
})
.check((argv) => {
// "one-and-dones"; let yargs handle help and version
Object.keys(ONE_AND_DONES).forEach((opt) => {
for (const opt of Object.keys(ONE_AND_DONES)) {
if (argv[opt]) {
ONE_AND_DONES[opt].call(null, yargs);
process.exit();
}
});
}

// load requires first, because it can impact "plugin" validation
handleRequires(argv.require);
Expand All @@ -159,8 +160,8 @@ exports.handler = (argv) => {
let json;
try {
const file = argv.json;
json = fs.readFileSync(file, 'utf-8');
} catch (err) {
json = fs.readFileSync(file, 'utf8');
} catch {
console.error('Error: No test files found');
process.exit(1);
}
Expand Down
Loading

0 comments on commit b5f767e

Please sign in to comment.