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 19, 2022
1 parent 65df5a4 commit 87deb68
Show file tree
Hide file tree
Showing 9 changed files with 212 additions and 203 deletions.
7 changes: 6 additions & 1 deletion .eslintrc
Expand Up @@ -21,7 +21,6 @@
"no-negated-condition": "off",
"no-param-reassign": "warn",
"no-underscore-dangle": "warn",
"no-use-before-define": "warn",
"object-curly-newline": "off",
"operator-linebreak": ["error", "before"],
"sort-keys": "warn",
Expand Down Expand Up @@ -121,5 +120,11 @@
"camelcase": "off",
},
},
{
"files": ["lib/default_stream.js"],
"rules": {
"no-use-before-define": "warn",
},
}
],
}
111 changes: 57 additions & 54 deletions index.js
Expand Up @@ -13,6 +13,16 @@ var canExit = typeof process !== 'undefined' && process

module.exports = (function () {
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); }
return harness;
}

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

return lazyLoad;

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

function createExitHarness(conf) {
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 stream = harness.createStream({ objectMode: conf.objectMode });
var es = stream.pipe(conf.stream || createDefaultStream());
if (canEmitExit) {
// eslint-disable-next-line no-unused-vars
es.on('error', function (err) { harness._exitCode = 1; });
}

var ended = false;
stream.on('end', function () { ended = true; });

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

process.on('exit', function (code) {
// let the process exit cleanly.
if (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.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;

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

return test;
}

function createExitHarness(conf) {
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 stream = harness.createStream({ objectMode: conf.objectMode });
var es = stream.pipe(conf.stream || createDefaultStream());
if (canEmitExit) {
// eslint-disable-next-line no-unused-vars
es.on('error', function (err) { harness._exitCode = 1; });
}

var ended = false;
stream.on('end', function () { ended = true; });

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

process.on('exit', function (code) {
// let the process exit cleanly.
if (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.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 @@ -11,13 +11,80 @@ var has = require('has');
var $exec = callBound('RegExp.prototype.exec');
var yamlIndicators = /:|-|\?/;
var nextTick = typeof setImmediate !== 'undefined' ? setImmediate : process.nextTick;
module.exports = Results;
inherits(Results, EventEmitter);

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

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 = String(stack).split('\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 results.tests.shift();
}

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

return void undefined;
}

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

inherits(Results, EventEmitter);

Results.prototype.createStream = function (opts) {
if (!opts) { opts = {}; }
var self = this;
Expand Down Expand Up @@ -155,71 +224,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 = String(stack).split('\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 results.tests.shift();
}

do {
var t = results.tests.shift();
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;
34 changes: 17 additions & 17 deletions lib/test.js
Expand Up @@ -16,16 +16,12 @@ var toLowerCase = callBound('String.prototype.toLowerCase');
var $exec = callBound('RegExp.prototype.exec');
var objectToString = callBound('Object.prototype.toString');

module.exports = Test;

var nextTick = typeof setImmediate !== 'undefined'
? setImmediate
: process.nextTick;
var safeSetTimeout = setTimeout;
var safeClearTimeout = clearTimeout;

inherits(Test, EventEmitter);

// eslint-disable-next-line no-unused-vars
var getTestArgs = function (name_, opts_, cb_) {
var name = '(anonymous)';
Expand Down Expand Up @@ -94,6 +90,8 @@ function Test(name_, opts_, cb_) {
}
}

inherits(Test, EventEmitter);

Test.prototype.run = function run() {
this.emit('prerun');
if (!this._cb || this._skip) {
Expand Down Expand Up @@ -183,6 +181,19 @@ Test.prototype._end = function (err) {
return;
}

function completeEnd() {
if (!self.ended) { self.emit('end'); }
var pendingAsserts = self._pendingAsserts();
if (!self._planError && self._plan !== undefined && pendingAsserts) {
self._planError = true;
self.fail('plan != count', {
expected: self._plan,
actual: self.assertCount
});
}
self.ended = true;
}

function next(i) {
if (i === self._teardown.length) {
completeEnd();
Expand Down Expand Up @@ -211,19 +222,6 @@ Test.prototype._end = function (err) {
} else {
completeEnd();
}

function completeEnd() {
if (!self.ended) { self.emit('end'); }
var pendingAsserts = self._pendingAsserts();
if (!self._planError && self._plan !== undefined && pendingAsserts) {
self._planError = true;
self.fail('plan != count', {
expected: self._plan,
actual: self.assertCount
});
}
self.ended = true;
}
};

Test.prototype._exit = function () {
Expand Down Expand Up @@ -665,4 +663,6 @@ Test.skip = function (name_, _opts, _cb) {
return new Test(args.name, args.opts, args.cb);
};

module.exports = Test;

// vim: set softtabstop=4 shiftwidth=4:

0 comments on commit 87deb68

Please sign in to comment.