Skip to content

Commit

Permalink
[eslint] add npm run lint
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 22, 2022
1 parent 1671ad5 commit 4399c97
Show file tree
Hide file tree
Showing 6 changed files with 220 additions and 179 deletions.
32 changes: 32 additions & 0 deletions .eslintrc
@@ -0,0 +1,32 @@
{
"root": true,

"extends": "@ljharb",

"rules": {
"func-style": ["error", "declaration"],
"max-lines-per-function": "off",
"max-statements-per-line": ["error", { "max": 2 }],
"no-plusplus": "warn",
"sort-keys": "off",
},

"overrides": [
{
"files": "bin/**",
"env": {
"node": true,
},
"rules": {
"no-process-exit": "off",
}
},
{
"files": "example/**",
"rules": {
"no-magic-numbers": "off",
"no-plusplus": "off",
},
},
],
}
67 changes: 33 additions & 34 deletions bin/cmd.js
@@ -1,4 +1,7 @@
#!/usr/bin/env node

'use strict';

var faucet = require('../');
var minimist = require('minimist');
var defined = require('defined');
Expand All @@ -9,65 +12,61 @@ var spawn = require('child_process').spawn;
var fs = require('fs');
var path = require('path');

function jsFile(x) { return (/\.js$/i).test(x); }

var argv = minimist(process.argv.slice(2));
var tap = faucet({
width: defined(argv.w, argv.width, process.stdout.isTTY
? process.stdout.columns - 5
: 0
)
width: defined(argv.w, argv.width, process.stdout.isTTY
? process.stdout.columns - 5
: 0)
});
process.on('exit', function (code) {
if (code === 0 && tap.exitCode !== 0) {
process.exit(tap.exitCode);
}
if (code === 0 && tap.exitCode !== 0) {
process.exit(tap.exitCode);
}
});
process.stdout.on('error', function () {});

if (!process.stdin.isTTY || argv._[0] === '-') {
process.stdin.pipe(tap).pipe(process.stdout);
return;
process.stdin.pipe(tap).pipe(process.stdout);
return;
}

var files = argv._.reduce(function (acc, file) {
if (fs.statSync(file).isDirectory()) {
return acc.concat(fs.readdirSync(file).map(function (x) {
return path.join(file, x);
}).filter(jsFile));
}
else return acc.concat(file);
if (fs.statSync(file).isDirectory()) {
return acc.concat(fs.readdirSync(file).map(function (x) {
return path.join(file, x);
}).filter(jsFile));
}
return acc.concat(file);
}, []);

if (files.length === 0 && fs.existsSync('test')) {
files.push.apply(files, fs.readdirSync('test').map(function (x) {
return path.join('test', x);
}).filter(jsFile));
files.push.apply(files, fs.readdirSync('test').map(function (x) {
return path.join('test', x);
}).filter(jsFile));
}
if (files.length === 0 && fs.existsSync('tests')) {
files.push.apply(files, fs.readdirSync('tests').map(function (x) {
return path.join('tests', x);
}).filter(jsFile));
files.push.apply(files, fs.readdirSync('tests').map(function (x) {
return path.join('tests', x);
}).filter(jsFile));
}

if (files.length === 0) {
console.error('usage: `faucet [FILES]` or `| faucet`\n');
console.error(
'No test files or stdin provided and no files in test/ or tests/'
+ ' directories found.'
);
return process.exit(1);
console.error('usage: `faucet [FILES]` or `| faucet`\n');
console.error('No test files or stdin provided and no files in test/ or tests/ directories found.');
process.exit(1);
}

var tape = spawn(tapeCmd, files);
tape.stderr.pipe(process.stderr);
tape.stdout.pipe(tap).pipe(process.stdout);

var tapeCode;
tape.on('exit', function (code) { tapeCode = code });
tape.on('exit', function (code) { tapeCode = code; });
process.on('exit', function (code) {
if (code === 0 && tapeCode !== 0) {
console.error('# non-zero exit from the `tape` command');
process.exit(tapeCode);
}
if (code === 0 && tapeCode !== 0) {
console.error('# non-zero exit from the `tape` command');
process.exit(tapeCode);
}
});

function jsFile (x) { return /\.js$/i.test(x) }
22 changes: 13 additions & 9 deletions example/tape.js
@@ -1,13 +1,17 @@
'use strict';

var test = require('tape');

test('beep boop', function (t) {
t.plan(2);

t.equal(1 + 1, 2);
setTimeout(function () {
t.deepEqual(
'ABC'.toLowerCase().split(''),
['a','b','c']
);
});
t.plan(2);

t.equal(1 + 1, 2);
setTimeout(function () {
t.deepEqual(
'ABC'.toLowerCase().split(''),
[
'a', 'b', 'c'
]
);
});
});
44 changes: 25 additions & 19 deletions example/test.js
@@ -1,27 +1,33 @@
'use strict';

var test = require('tape');
function getMessage () {
var msgs = [ 'yes', 'equals', 'matches', 'yep', 'pretty much', 'woo' ];
return msgs[Math.floor(Math.random() * msgs.length)];

function getMessage() {
var msgs = [
'yes', 'equals', 'matches', 'yep', 'pretty much', 'woo'
];
return msgs[Math.floor(Math.random() * msgs.length)];
}

test('beep affirmative', function (t) {
t.plan(24);
var i = 0, n = 0;
var iv = setInterval(function () {
t.equal(i++, n++, getMessage());
if (i === 24) clearInterval(iv);
}, 50);
t.plan(24);
var i = 0,
n = 0;
var iv = setInterval(function () {
t.equal(i++, n++, getMessage());
if (i === 24) { clearInterval(iv); }
}, 50);
});

test('boop exterminate', function (t) {
t.plan(20);
var i = 0, n = 0;
var iv = setInterval(function () {
if ((i + 2) % 8 === 0) {
t.equal(i, n + 6, getMessage())
}
else t.equal(i, n, getMessage());
i++; n++;
if (i === 20) clearInterval(iv);
}, 100);
t.plan(20);
var i = 0,
n = 0;
var iv = setInterval(function () {
if ((i + 2) % 8 === 0) {
t.equal(i, n + 6, getMessage());
} else { t.equal(i, n, getMessage()); }
i++; n++;
if (i === 20) { clearInterval(iv); }
}, 100);
});

0 comments on commit 4399c97

Please sign in to comment.