Skip to content

Commit

Permalink
[eslint] fully enable @ljharb eslint config
Browse files Browse the repository at this point in the history
Merge branch 'eslint'
  • Loading branch information
ljharb committed Jul 26, 2021
2 parents 8a0ab53 + ae8b5c0 commit 9d3c5b4
Show file tree
Hide file tree
Showing 53 changed files with 401 additions and 312 deletions.
101 changes: 84 additions & 17 deletions .eslintrc
Expand Up @@ -4,42 +4,78 @@
"browser": true,
"node": true,
},
"extends": "@ljharb",
"globals": {
"Promise": false,
},
"rules": {
"comma-dangle": ["error", "never"],
"array-bracket-spacing": "off",
"complexity": "off",
"eqeqeq": ["error", "always", { "null": "ignore" }],
"func-style": "warn",
"indent": ["error", 4],
"key-spacing": "error",
"quotes": ["error", "single", {
"avoidEscape": true,
}],
"semi": ["error", "always"],
"space-before-function-paren": ["error", {
"anonymous": "always",
"named": "never",
}],
"no-undef": "error",
"no-useless-escape": "error",
"no-magic-numbers": "off",
"max-lines": "warn",
"max-lines-per-function": "warn",
"max-statements": "warn",
"max-statements-per-line": [2, { "max": 2 }],
"multiline-comment-style": "off",
"no-negated-condition": "off",
"no-use-before-define": "warn",
"no-underscore-dangle": "warn",
"operator-linebreak": ["error", "before"],
"space-unary-ops": ["error", {
"words": false,
"nonwords": false,
}],
"strict": "error",
"sort-keys": "warn",
},
"ignorePatterns": [ "syntax-error.*" ],
"overrides": [
{
"files": ["*.mjs", "test/import/package_type/*.js"],
"extends": "@ljharb/eslint-config/esm",
},
{
"files": ["bin/**"],
"rules": {
"global-require": "off",
"no-process-exit": "off",
"quote-props": ["error", "as-needed", {
"keywords": false,
}],
},
},
{
"files": ["bin/import-or-require.js"],
"parserOptions": {
"ecmaVersion": 2020,
},
},
{
"files": ["index.js"],
"rules": {
"no-param-reassign": "warn",
},
},
{
"files": ["lib/results.js"],
"rules": {
"no-cond-assign": "warn",
"no-param-reassign": "warn",
"no-plusplus": "warn",
},
},
{
"files": ["lib/test.js"],
"rules": {
"eqeqeq": "warn",
"func-name-matching": "off",
"max-params": "off",
"no-continue": "off",
"no-invalid-this": "off",
"no-param-reassign": "warn",
"no-plusplus": "warn",
"no-multi-assign": "off",
"no-restricted-syntax": "off",
},
},
{
"files": ["test/async-await/*"],
"parserOptions": {
Expand All @@ -51,6 +87,37 @@
"globals": {
"g": false,
},
"rules": {
"no-new-func": "off",
},
},
{
"files": ["example/**"],
"rules": {
"array-bracket-newline": "off",
"global-require": "off",
"no-console": "off",
},
},
{
"files": ["test/**"],
"rules": {
"dot-notation": [2, {
"allowKeywords": true,
"allowPattern": "throws"
}],
"id-length": "off",
"max-len": "off",
"max-lines-per-function": "off",
"no-plusplus": "off",
"no-throw-literal": "off",
},
},
{
"files": ["test/*/**"],
"rules": {
"camelcase": "off",
},
},
],
}
3 changes: 2 additions & 1 deletion bin/import-or-require.js
Expand Up @@ -3,7 +3,8 @@
const { extname: extnamePath } = require('path');
const getPackageType = require('get-package-type');

module.exports = function (file) {
// eslint-disable-next-line consistent-return
module.exports = function importOrRequire(file) {
const ext = extnamePath(file);

if (ext === '.mjs' || (ext === '.js' && getPackageType.sync(file) === 'module')) {
Expand Down
12 changes: 6 additions & 6 deletions bin/tape
Expand Up @@ -25,7 +25,7 @@ if (typeof opts.require === 'string') {
}

opts.require.forEach(function (module) {
var options = { basedir: cwd, extensions: Object.keys(require.extensions) };
var options = { basedir: cwd, extensions: Object.keys(require.extensions) };
if (module) {
/* This check ensures we ignore `-r ""`, trailing `-r`, or
* other silly things the user might (inadvertently) be doing.
Expand All @@ -47,13 +47,13 @@ if (typeof opts.ignore === 'string') {
var files = opts._.reduce(function (result, arg) {
// If glob does not match, `files` will be an empty array.
// Note: `glob.sync` may throw an error and crash the node process.
var files = glob.sync(arg);
var globFiles = glob.sync(arg);

if (!Array.isArray(files)) {
if (!Array.isArray(globFiles)) {
throw new TypeError('unknown error: glob.sync("' + arg + '") did not return an array or throw. Please report this.');
}

return result.concat(files);
return result.concat(globFiles);
}, []).filter(function (file) {
return !matcher || !matcher.shouldIgnore(file);
}).map(function (file) {
Expand All @@ -75,13 +75,13 @@ function importFiles(hasSupport) {

tape.wait();

var promise = files.reduce(function (promise, file) {
var filesPromise = files.reduce(function (promise, file) {
return promise ? promise.then(function () {
return importOrRequire(file);
}) : importOrRequire(file);
}, null);

return promise ? promise.then(function () { tape.run(); }) : tape.run();
return filesPromise ? filesPromise.then(function () { tape.run(); }) : tape.run();
}

// vim: ft=javascript
2 changes: 1 addition & 1 deletion example/array.js
Expand Up @@ -25,7 +25,7 @@ test('array', function (t) {
[ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]
];

Function(['fn','g'], output)(
Function(['fn', 'g'], output)(
function (xs) {
t.same(arrays.shift(), xs);
return xs;
Expand Down
2 changes: 1 addition & 1 deletion example/fail.js
Expand Up @@ -25,7 +25,7 @@ test('array', function (t) {
[ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]
];

Function(['fn','g'], output)(
Function(['fn', 'g'], output)(
function (xs) {
t.same(arrays.shift(), xs);
return xs;
Expand Down
2 changes: 1 addition & 1 deletion example/nested_fail.js
Expand Up @@ -34,7 +34,7 @@ test('nested array test', function (t) {
[ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]
];

Function(['fn','g'], output)(
Function(['fn', 'g'], output)(
function (xs) {
t.same(arrays.shift(), xs);
return xs;
Expand Down
2 changes: 1 addition & 1 deletion example/too_many_fail.js
Expand Up @@ -25,7 +25,7 @@ test('array', function (t) {
[ [ 1, 2, [ 3, 4 ] ], [ 5, 6 ] ]
];

Function(['fn','g'], output)(
Function(['fn', 'g'], output)(
function (xs) {
t.same(arrays.shift(), xs);
return xs;
Expand Down
4 changes: 2 additions & 2 deletions example/two.js
Expand Up @@ -6,13 +6,13 @@ test('one', function (t) {
t.plan(2);
t.ok(true);
setTimeout(function () {
t.equal(1+3, 4);
t.equal(1 + 3, 4);
}, 100);
});

test('two', function (t) {
t.plan(3);
t.equal(5, 2+3);
t.equal(5, 2 + 3);
setTimeout(function () {
t.equal('a'.charCodeAt(0), 97);
t.ok(true);
Expand Down
38 changes: 20 additions & 18 deletions index.js
Expand Up @@ -7,16 +7,15 @@ var createResult = require('./lib/results');
var through = require('through');

var canEmitExit = typeof process !== 'undefined' && process
&& typeof process.on === 'function' && process.browser !== true
;
&& typeof process.on === 'function' && process.browser !== true;
var canExit = typeof process !== 'undefined' && process
&& typeof process.exit === 'function'
;
&& typeof process.exit === 'function';

module.exports = (function () {
var wait = false;
var harness;
var lazyLoad = function () {
// eslint-disable-next-line no-invalid-this
return getHarness().apply(this, arguments);
};

Expand All @@ -27,7 +26,7 @@ module.exports = (function () {
lazyLoad.run = function () {
var run = getHarness().run;

if (run) run();
if (run) { run(); }
};

lazyLoad.only = function () {
Expand Down Expand Up @@ -57,12 +56,12 @@ module.exports = (function () {
return lazyLoad;

function getHarness(opts) {
if (!opts) opts = {};
if (!opts) { opts = {}; }
opts.autoclose = !canEmitExit;
if (!harness) harness = createExitHarness(opts, wait);
if (!harness) { harness = createExitHarness(opts, wait); }
return harness;
}
})();
}());

function createExitHarness(conf, wait) {
var config = conf || {};
Expand All @@ -78,8 +77,8 @@ function createExitHarness(conf, wait) {
run();
}

if (config.exit === false) return harness;
if (!canEmitExit || !canExit) return harness;
if (config.exit === false) { return harness; }
if (!canEmitExit || !canExit) { return harness; }

process.on('exit', function (code) {
// let the process exit cleanly.
Expand All @@ -91,28 +90,31 @@ function createExitHarness(conf, wait) {
var only = harness._results._only;
for (var i = 0; i < harness._tests.length; i++) {
var t = harness._tests[i];
if (only && t !== only) continue;
t._exit();
if (!only || t === only) {
t._exit();
}
}
}
harness.close();

process.removeAllListeners('exit'); // necessary for node v0.6
process.exit(code || harness._exitCode);
process.exit(code || harness._exitCode); // eslint-disable-line no-process-exit
});

return harness;

function run() {
if (running) return;
if (running) { return; }
running = true;
var stream = harness.createStream({ objectMode: config.objectMode });
var es = stream.pipe(config.stream || createDefaultStream());
if (canEmitExit) {
// TODO: use `err` arg?
// eslint-disable-next-line no-unused-vars
es.on('error', function (err) { harness._exitCode = 1; });
}
stream.on('end', function () { ended = true; });
};
}
}

module.exports.createHarness = createHarness;
Expand All @@ -135,9 +137,9 @@ function createHarness(conf_) {
inspectCode(st_);
});
st.on('result', function (r) {
if (!r.todo && !r.ok && typeof r !== 'string') test._exitCode = 1;
if (!r.todo && !r.ok && typeof r !== 'string') { test._exitCode = 1; }
});
})(t);
}(t));

results.push(t);
return t;
Expand All @@ -160,7 +162,7 @@ function createHarness(conf_) {

var only = false;
test.only = function () {
if (only) throw new Error('there can only be one only test');
if (only) { throw new Error('there can only be one only test'); }
only = true;
var t = test.apply(null, arguments);
results.only(t);
Expand Down
26 changes: 17 additions & 9 deletions lib/default_stream.js
Expand Up @@ -12,20 +12,28 @@ module.exports = function () {
for (var i = 0; i < buf.length; i++) {
var c = typeof buf === 'string'
? buf.charAt(i)
: String.fromCharCode(buf[i])
;
if (c === '\n') flush();
else line += c;
: String.fromCharCode(buf[i]);
if (c === '\n') {
flush();
} else {
line += c;
}
}
}

function flush() {
if (fs.writeSync && /^win/.test(process.platform)) {
try { fs.writeSync(1, line + '\n'); }
catch (e) { stream.emit('error', e); }
if (fs.writeSync && (/^win/).test(process.platform)) {
try {
fs.writeSync(1, line + '\n');
} catch (e) {
stream.emit('error', e);
}
} else {
try { console.log(line); }
catch (e) { stream.emit('error', e); }
try {
console.log(line); // eslint-disable-line no-console
} catch (e) {
stream.emit('error', e);
}
}
line = '';
}
Expand Down

0 comments on commit 9d3c5b4

Please sign in to comment.