Skip to content

Commit

Permalink
[eslint] enforce no-use-before-define
Browse files Browse the repository at this point in the history
  • Loading branch information
ljharb committed Sep 18, 2022
1 parent 298cb80 commit f8a8a7f
Show file tree
Hide file tree
Showing 12 changed files with 243 additions and 234 deletions.
7 changes: 6 additions & 1 deletion .eslintrc
Expand Up @@ -21,7 +21,6 @@
"multiline-comment-style": "off",
"no-negated-condition": "off",
"no-underscore-dangle": "warn",
"no-use-before-define": "warn",
"object-curly-newline": "off",
"sort-keys": "warn",
},
Expand Down Expand Up @@ -119,5 +118,11 @@
"camelcase": "off",
},
},
{
"files": ["lib/default_stream.js"],
"rules": {
"no-use-before-define": "warn",
},
}
],
}
12 changes: 6 additions & 6 deletions bin/tape
Expand Up @@ -71,12 +71,6 @@ var files = opts._.reduce(function (result, arg) {

var hasImport = require('has-dynamic-import');

hasImport().then(function (hasSupport) {
// the nextTick callback gets called outside the promise chain, avoiding
// promises and unhandled rejections when only loading commonjs files
process.nextTick(importFiles, hasSupport);
});

var tape = require('../');

function importFiles(hasSupport) {
Expand All @@ -97,4 +91,10 @@ function importFiles(hasSupport) {
return filesPromise ? filesPromise.then(function () { tape.run(); }) : tape.run();
}

hasImport().then(function (hasSupport) {
// the nextTick callback gets called outside the promise chain, avoiding
// promises and unhandled rejections when only loading commonjs files
process.nextTick(importFiles, hasSupport);
});

// vim: ft=javascript
137 changes: 70 additions & 67 deletions index.js
Expand Up @@ -14,6 +14,16 @@ var canExit = typeof process !== 'undefined' && process
module.exports = (function () {
var wait = false;
var harness;

function getHarness(opts) {
if (!opts) { opts = {}; }
opts.autoclose = !canEmitExit;
// this override is here since tests fail via nyc if createHarness is moved upwards
// eslint-disable-next-line no-use-before-define
if (!harness) { harness = createExitHarness(opts, wait); }
return harness;
}

var lazyLoad = function () {
// eslint-disable-next-line no-invalid-this
return getHarness().apply(this, arguments);
Expand Down Expand Up @@ -54,75 +64,8 @@ module.exports = (function () {
lazyLoad.getHarness = getHarness;

return lazyLoad;

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

function createExitHarness(conf, wait) {
var config = conf || {};
var harness = createHarness({
autoclose: defined(config.autoclose, false),
noOnly: defined(conf.noOnly, defined(process.env.NODE_TAPE_NO_ONLY_TEST, false))
});
var running = false;
var ended = false;

if (wait) {
harness.run = run;
} else {
run();
}

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

process.on('exit', function (code) {
// let the process exit cleanly.
if (typeof code === 'number' && code !== 0) {
return;
}

if (!ended) {
var only = harness._results._only;
for (var i = 0; i < harness._tests.length; i++) {
var t = harness._tests[i];
if (!only || t === only) {
t._exit();
}
}
}
harness.close();

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

return harness;

function run() {
if (running) { return; }
running = true;
var stream = harness.createStream({ objectMode: config.objectMode });
var es = stream.pipe(config.stream || createDefaultStream());
if (canEmitExit && es) { // in node v0.4, `es` is `undefined`
// 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;
module.exports.Test = Test;
module.exports.test = module.exports; // tap compat
module.exports.test.skip = Test.skip;

function createHarness(conf_) {
var results = createResult();
if (!conf_ || conf_.autoclose !== false) {
Expand Down Expand Up @@ -176,3 +119,63 @@ function createHarness(conf_) {

return test;
}

function createExitHarness(conf, wait) {
var config = conf || {};
var harness = createHarness({
autoclose: defined(config.autoclose, false),
noOnly: defined(conf.noOnly, defined(process.env.NODE_TAPE_NO_ONLY_TEST, false))
});
var running = false;
var ended = false;

function run() {
if (running) { return; }
running = true;
var stream = harness.createStream({ objectMode: config.objectMode });
var es = stream.pipe(config.stream || createDefaultStream());
if (canEmitExit && es) { // in node v0.4, `es` is `undefined`
// 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; });
}

if (wait) {
harness.run = run;
} else {
run();
}

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

process.on('exit', function (code) {
// let the process exit cleanly.
if (typeof code === 'number' && code !== 0) {
return;
}

if (!ended) {
var only = harness._results._only;
for (var i = 0; i < harness._tests.length; i++) {
var t = harness._tests[i];
if (!only || t === only) {
t._exit();
}
}
}
harness.close();

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

return harness;
}

module.exports.createHarness = createHarness;
module.exports.Test = Test;
module.exports.test = module.exports; // tap compat
module.exports.test.skip = Test.skip;
142 changes: 72 additions & 70 deletions lib/results.js
Expand Up @@ -17,13 +17,80 @@ var yamlIndicators = /:|-|\?/;
var nextTick = typeof setImmediate !== 'undefined'
? setImmediate
: process.nextTick;
module.exports = Results;
inherits(Results, EventEmitter);

function coalesceWhiteSpaces(str) {
return $replace(String(str), /\s+/g, ' ');
}

function getNextTest(results) {
if (!results._only) {
return $shift(results.tests);
}

do {
var t = $shift(results.tests);
if (t && results._only === t) {
return t;
}
} while (results.tests.length !== 0);

return void undefined;
}

function invalidYaml(str) {
return $exec(yamlIndicators, str) !== null;
}

function encodeResult(res, count) {
var output = '';
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + coalesceWhiteSpaces(res.name) : '';

if (res.skip) {
output += ' # SKIP' + (typeof res.skip === 'string' ? ' ' + coalesceWhiteSpaces(res.skip) : '');
} else if (res.todo) {
output += ' # TODO' + (typeof res.todo === 'string' ? ' ' + coalesceWhiteSpaces(res.todo) : '');
}

output += '\n';
if (res.ok) { return output; }

var outer = ' ';
var inner = outer + ' ';
output += outer + '---\n';
output += inner + 'operator: ' + res.operator + '\n';

if (has(res, 'expected') || has(res, 'actual')) {
var ex = inspect(res.expected, { depth: res.objectPrintDepth });
var ac = inspect(res.actual, { depth: res.objectPrintDepth });

if (Math.max(ex.length, ac.length) > 65 || invalidYaml(ex) || invalidYaml(ac)) {
output += inner + 'expected: |-\n' + inner + ' ' + ex + '\n';
output += inner + 'actual: |-\n' + inner + ' ' + ac + '\n';
} else {
output += inner + 'expected: ' + ex + '\n';
output += inner + 'actual: ' + ac + '\n';
}
}
if (res.at) {
output += inner + 'at: ' + res.at + '\n';
}

var actualStack = res.actual && (typeof res.actual === 'object' || typeof res.actual === 'function') ? res.actual.stack : undefined;
var errorStack = res.error && res.error.stack;
var stack = defined(actualStack, errorStack);
if (stack) {
var lines = $split(String(stack), '\n');
output += inner + 'stack: |-\n';
for (var i = 0; i < lines.length; i++) {
output += inner + ' ' + lines[i] + '\n';
}
}

output += outer + '...\n';
return output;
}

function Results() {
if (!(this instanceof Results)) { return new Results(); }
this.count = 0;
Expand All @@ -36,6 +103,8 @@ function Results() {
this._isRunning = false;
}

inherits(Results, EventEmitter);

Results.prototype.createStream = function (opts) {
if (!opts) { opts = {}; }
var self = this;
Expand Down Expand Up @@ -160,71 +229,4 @@ Results.prototype.close = function () {
self._stream.queue(null);
};

function encodeResult(res, count) {
var output = '';
output += (res.ok ? 'ok ' : 'not ok ') + count;
output += res.name ? ' ' + coalesceWhiteSpaces(res.name) : '';

if (res.skip) {
output += ' # SKIP' + (typeof res.skip === 'string' ? ' ' + coalesceWhiteSpaces(res.skip) : '');
} else if (res.todo) {
output += ' # TODO' + (typeof res.todo === 'string' ? ' ' + coalesceWhiteSpaces(res.todo) : '');
}

output += '\n';
if (res.ok) { return output; }

var outer = ' ';
var inner = outer + ' ';
output += outer + '---\n';
output += inner + 'operator: ' + res.operator + '\n';

if (has(res, 'expected') || has(res, 'actual')) {
var ex = inspect(res.expected, { depth: res.objectPrintDepth });
var ac = inspect(res.actual, { depth: res.objectPrintDepth });

if (Math.max(ex.length, ac.length) > 65 || invalidYaml(ex) || invalidYaml(ac)) {
output += inner + 'expected: |-\n' + inner + ' ' + ex + '\n';
output += inner + 'actual: |-\n' + inner + ' ' + ac + '\n';
} else {
output += inner + 'expected: ' + ex + '\n';
output += inner + 'actual: ' + ac + '\n';
}
}
if (res.at) {
output += inner + 'at: ' + res.at + '\n';
}

var actualStack = res.actual && (typeof res.actual === 'object' || typeof res.actual === 'function') ? res.actual.stack : undefined;
var errorStack = res.error && res.error.stack;
var stack = defined(actualStack, errorStack);
if (stack) {
var lines = $split(String(stack), '\n');
output += inner + 'stack: |-\n';
for (var i = 0; i < lines.length; i++) {
output += inner + ' ' + lines[i] + '\n';
}
}

output += outer + '...\n';
return output;
}

function getNextTest(results) {
if (!results._only) {
return $shift(results.tests);
}

do {
var t = $shift(results.tests);
if (t && results._only === t) {
return t;
}
} while (results.tests.length !== 0);

return void undefined;
}

function invalidYaml(str) {
return $exec(yamlIndicators, str) !== null;
}
module.exports = Results;

0 comments on commit f8a8a7f

Please sign in to comment.