Skip to content

Commit f8a8a7f

Browse files
committedSep 18, 2022
[eslint] enforce no-use-before-define
1 parent 298cb80 commit f8a8a7f

12 files changed

+243
-234
lines changed
 

‎.eslintrc

+6-1
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@
2121
"multiline-comment-style": "off",
2222
"no-negated-condition": "off",
2323
"no-underscore-dangle": "warn",
24-
"no-use-before-define": "warn",
2524
"object-curly-newline": "off",
2625
"sort-keys": "warn",
2726
},
@@ -119,5 +118,11 @@
119118
"camelcase": "off",
120119
},
121120
},
121+
{
122+
"files": ["lib/default_stream.js"],
123+
"rules": {
124+
"no-use-before-define": "warn",
125+
},
126+
}
122127
],
123128
}

‎bin/tape

+6-6
Original file line numberDiff line numberDiff line change
@@ -71,12 +71,6 @@ var files = opts._.reduce(function (result, arg) {
7171

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

74-
hasImport().then(function (hasSupport) {
75-
// the nextTick callback gets called outside the promise chain, avoiding
76-
// promises and unhandled rejections when only loading commonjs files
77-
process.nextTick(importFiles, hasSupport);
78-
});
79-
8074
var tape = require('../');
8175

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

94+
hasImport().then(function (hasSupport) {
95+
// the nextTick callback gets called outside the promise chain, avoiding
96+
// promises and unhandled rejections when only loading commonjs files
97+
process.nextTick(importFiles, hasSupport);
98+
});
99+
100100
// vim: ft=javascript

‎index.js

+70-67
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,16 @@ var canExit = typeof process !== 'undefined' && process
1414
module.exports = (function () {
1515
var wait = false;
1616
var harness;
17+
18+
function getHarness(opts) {
19+
if (!opts) { opts = {}; }
20+
opts.autoclose = !canEmitExit;
21+
// this override is here since tests fail via nyc if createHarness is moved upwards
22+
// eslint-disable-next-line no-use-before-define
23+
if (!harness) { harness = createExitHarness(opts, wait); }
24+
return harness;
25+
}
26+
1727
var lazyLoad = function () {
1828
// eslint-disable-next-line no-invalid-this
1929
return getHarness().apply(this, arguments);
@@ -54,75 +64,8 @@ module.exports = (function () {
5464
lazyLoad.getHarness = getHarness;
5565

5666
return lazyLoad;
57-
58-
function getHarness(opts) {
59-
if (!opts) { opts = {}; }
60-
opts.autoclose = !canEmitExit;
61-
if (!harness) { harness = createExitHarness(opts, wait); }
62-
return harness;
63-
}
6467
}());
6568

66-
function createExitHarness(conf, wait) {
67-
var config = conf || {};
68-
var harness = createHarness({
69-
autoclose: defined(config.autoclose, false),
70-
noOnly: defined(conf.noOnly, defined(process.env.NODE_TAPE_NO_ONLY_TEST, false))
71-
});
72-
var running = false;
73-
var ended = false;
74-
75-
if (wait) {
76-
harness.run = run;
77-
} else {
78-
run();
79-
}
80-
81-
if (config.exit === false) { return harness; }
82-
if (!canEmitExit || !canExit) { return harness; }
83-
84-
process.on('exit', function (code) {
85-
// let the process exit cleanly.
86-
if (typeof code === 'number' && code !== 0) {
87-
return;
88-
}
89-
90-
if (!ended) {
91-
var only = harness._results._only;
92-
for (var i = 0; i < harness._tests.length; i++) {
93-
var t = harness._tests[i];
94-
if (!only || t === only) {
95-
t._exit();
96-
}
97-
}
98-
}
99-
harness.close();
100-
101-
process.removeAllListeners('exit'); // necessary for node v0.6
102-
process.exit(code || harness._exitCode); // eslint-disable-line no-process-exit
103-
});
104-
105-
return harness;
106-
107-
function run() {
108-
if (running) { return; }
109-
running = true;
110-
var stream = harness.createStream({ objectMode: config.objectMode });
111-
var es = stream.pipe(config.stream || createDefaultStream());
112-
if (canEmitExit && es) { // in node v0.4, `es` is `undefined`
113-
// TODO: use `err` arg?
114-
// eslint-disable-next-line no-unused-vars
115-
es.on('error', function (err) { harness._exitCode = 1; });
116-
}
117-
stream.on('end', function () { ended = true; });
118-
}
119-
}
120-
121-
module.exports.createHarness = createHarness;
122-
module.exports.Test = Test;
123-
module.exports.test = module.exports; // tap compat
124-
module.exports.test.skip = Test.skip;
125-
12669
function createHarness(conf_) {
12770
var results = createResult();
12871
if (!conf_ || conf_.autoclose !== false) {
@@ -176,3 +119,63 @@ function createHarness(conf_) {
176119

177120
return test;
178121
}
122+
123+
function createExitHarness(conf, wait) {
124+
var config = conf || {};
125+
var harness = createHarness({
126+
autoclose: defined(config.autoclose, false),
127+
noOnly: defined(conf.noOnly, defined(process.env.NODE_TAPE_NO_ONLY_TEST, false))
128+
});
129+
var running = false;
130+
var ended = false;
131+
132+
function run() {
133+
if (running) { return; }
134+
running = true;
135+
var stream = harness.createStream({ objectMode: config.objectMode });
136+
var es = stream.pipe(config.stream || createDefaultStream());
137+
if (canEmitExit && es) { // in node v0.4, `es` is `undefined`
138+
// TODO: use `err` arg?
139+
// eslint-disable-next-line no-unused-vars
140+
es.on('error', function (err) { harness._exitCode = 1; });
141+
}
142+
stream.on('end', function () { ended = true; });
143+
}
144+
145+
if (wait) {
146+
harness.run = run;
147+
} else {
148+
run();
149+
}
150+
151+
if (config.exit === false) { return harness; }
152+
if (!canEmitExit || !canExit) { return harness; }
153+
154+
process.on('exit', function (code) {
155+
// let the process exit cleanly.
156+
if (typeof code === 'number' && code !== 0) {
157+
return;
158+
}
159+
160+
if (!ended) {
161+
var only = harness._results._only;
162+
for (var i = 0; i < harness._tests.length; i++) {
163+
var t = harness._tests[i];
164+
if (!only || t === only) {
165+
t._exit();
166+
}
167+
}
168+
}
169+
harness.close();
170+
171+
process.removeAllListeners('exit'); // necessary for node v0.6
172+
process.exit(code || harness._exitCode); // eslint-disable-line no-process-exit
173+
});
174+
175+
return harness;
176+
}
177+
178+
module.exports.createHarness = createHarness;
179+
module.exports.Test = Test;
180+
module.exports.test = module.exports; // tap compat
181+
module.exports.test.skip = Test.skip;

‎lib/results.js

+72-70
Original file line numberDiff line numberDiff line change
@@ -17,13 +17,80 @@ var yamlIndicators = /:|-|\?/;
1717
var nextTick = typeof setImmediate !== 'undefined'
1818
? setImmediate
1919
: process.nextTick;
20-
module.exports = Results;
21-
inherits(Results, EventEmitter);
2220

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

25+
function getNextTest(results) {
26+
if (!results._only) {
27+
return $shift(results.tests);
28+
}
29+
30+
do {
31+
var t = $shift(results.tests);
32+
if (t && results._only === t) {
33+
return t;
34+
}
35+
} while (results.tests.length !== 0);
36+
37+
return void undefined;
38+
}
39+
40+
function invalidYaml(str) {
41+
return $exec(yamlIndicators, str) !== null;
42+
}
43+
44+
function encodeResult(res, count) {
45+
var output = '';
46+
output += (res.ok ? 'ok ' : 'not ok ') + count;
47+
output += res.name ? ' ' + coalesceWhiteSpaces(res.name) : '';
48+
49+
if (res.skip) {
50+
output += ' # SKIP' + (typeof res.skip === 'string' ? ' ' + coalesceWhiteSpaces(res.skip) : '');
51+
} else if (res.todo) {
52+
output += ' # TODO' + (typeof res.todo === 'string' ? ' ' + coalesceWhiteSpaces(res.todo) : '');
53+
}
54+
55+
output += '\n';
56+
if (res.ok) { return output; }
57+
58+
var outer = ' ';
59+
var inner = outer + ' ';
60+
output += outer + '---\n';
61+
output += inner + 'operator: ' + res.operator + '\n';
62+
63+
if (has(res, 'expected') || has(res, 'actual')) {
64+
var ex = inspect(res.expected, { depth: res.objectPrintDepth });
65+
var ac = inspect(res.actual, { depth: res.objectPrintDepth });
66+
67+
if (Math.max(ex.length, ac.length) > 65 || invalidYaml(ex) || invalidYaml(ac)) {
68+
output += inner + 'expected: |-\n' + inner + ' ' + ex + '\n';
69+
output += inner + 'actual: |-\n' + inner + ' ' + ac + '\n';
70+
} else {
71+
output += inner + 'expected: ' + ex + '\n';
72+
output += inner + 'actual: ' + ac + '\n';
73+
}
74+
}
75+
if (res.at) {
76+
output += inner + 'at: ' + res.at + '\n';
77+
}
78+
79+
var actualStack = res.actual && (typeof res.actual === 'object' || typeof res.actual === 'function') ? res.actual.stack : undefined;
80+
var errorStack = res.error && res.error.stack;
81+
var stack = defined(actualStack, errorStack);
82+
if (stack) {
83+
var lines = $split(String(stack), '\n');
84+
output += inner + 'stack: |-\n';
85+
for (var i = 0; i < lines.length; i++) {
86+
output += inner + ' ' + lines[i] + '\n';
87+
}
88+
}
89+
90+
output += outer + '...\n';
91+
return output;
92+
}
93+
2794
function Results() {
2895
if (!(this instanceof Results)) { return new Results(); }
2996
this.count = 0;
@@ -36,6 +103,8 @@ function Results() {
36103
this._isRunning = false;
37104
}
38105

106+
inherits(Results, EventEmitter);
107+
39108
Results.prototype.createStream = function (opts) {
40109
if (!opts) { opts = {}; }
41110
var self = this;
@@ -160,71 +229,4 @@ Results.prototype.close = function () {
160229
self._stream.queue(null);
161230
};
162231

163-
function encodeResult(res, count) {
164-
var output = '';
165-
output += (res.ok ? 'ok ' : 'not ok ') + count;
166-
output += res.name ? ' ' + coalesceWhiteSpaces(res.name) : '';
167-
168-
if (res.skip) {
169-
output += ' # SKIP' + (typeof res.skip === 'string' ? ' ' + coalesceWhiteSpaces(res.skip) : '');
170-
} else if (res.todo) {
171-
output += ' # TODO' + (typeof res.todo === 'string' ? ' ' + coalesceWhiteSpaces(res.todo) : '');
172-
}
173-
174-
output += '\n';
175-
if (res.ok) { return output; }
176-
177-
var outer = ' ';
178-
var inner = outer + ' ';
179-
output += outer + '---\n';
180-
output += inner + 'operator: ' + res.operator + '\n';
181-
182-
if (has(res, 'expected') || has(res, 'actual')) {
183-
var ex = inspect(res.expected, { depth: res.objectPrintDepth });
184-
var ac = inspect(res.actual, { depth: res.objectPrintDepth });
185-
186-
if (Math.max(ex.length, ac.length) > 65 || invalidYaml(ex) || invalidYaml(ac)) {
187-
output += inner + 'expected: |-\n' + inner + ' ' + ex + '\n';
188-
output += inner + 'actual: |-\n' + inner + ' ' + ac + '\n';
189-
} else {
190-
output += inner + 'expected: ' + ex + '\n';
191-
output += inner + 'actual: ' + ac + '\n';
192-
}
193-
}
194-
if (res.at) {
195-
output += inner + 'at: ' + res.at + '\n';
196-
}
197-
198-
var actualStack = res.actual && (typeof res.actual === 'object' || typeof res.actual === 'function') ? res.actual.stack : undefined;
199-
var errorStack = res.error && res.error.stack;
200-
var stack = defined(actualStack, errorStack);
201-
if (stack) {
202-
var lines = $split(String(stack), '\n');
203-
output += inner + 'stack: |-\n';
204-
for (var i = 0; i < lines.length; i++) {
205-
output += inner + ' ' + lines[i] + '\n';
206-
}
207-
}
208-
209-
output += outer + '...\n';
210-
return output;
211-
}
212-
213-
function getNextTest(results) {
214-
if (!results._only) {
215-
return $shift(results.tests);
216-
}
217-
218-
do {
219-
var t = $shift(results.tests);
220-
if (t && results._only === t) {
221-
return t;
222-
}
223-
} while (results.tests.length !== 0);
224-
225-
return void undefined;
226-
}
227-
228-
function invalidYaml(str) {
229-
return $exec(yamlIndicators, str) !== null;
230-
}
232+
module.exports = Results;

‎lib/test.js

+17-17
Original file line numberDiff line numberDiff line change
@@ -26,16 +26,12 @@ var $strSlice = callBound('String.prototype.slice');
2626
var $push = callBound('Array.prototype.push');
2727
var $shift = callBound('Array.prototype.shift');
2828

29-
module.exports = Test;
30-
3129
var nextTick = typeof setImmediate !== 'undefined'
3230
? setImmediate
3331
: process.nextTick;
3432
var safeSetTimeout = setTimeout;
3533
var safeClearTimeout = clearTimeout;
3634

37-
inherits(Test, EventEmitter);
38-
3935
// eslint-disable-next-line no-unused-vars
4036
var getTestArgs = function (name_, opts_, cb_) {
4137
var name = '(anonymous)';
@@ -104,6 +100,8 @@ function Test(name_, opts_, cb_) {
104100
}
105101
}
106102

103+
inherits(Test, EventEmitter);
104+
107105
Test.prototype.run = function run() {
108106
this.emit('prerun');
109107
if (!this._cb || this._skip) {
@@ -221,6 +219,19 @@ Test.prototype._end = function _end(err) {
221219
return;
222220
}
223221

222+
function completeEnd() {
223+
if (!self.ended) { self.emit('end'); }
224+
var pendingAsserts = self._pendingAsserts();
225+
if (!self._planError && self._plan !== undefined && pendingAsserts) {
226+
self._planError = true;
227+
self.fail('plan != count', {
228+
expected: self._plan,
229+
actual: self.assertCount
230+
});
231+
}
232+
self.ended = true;
233+
}
234+
224235
function next() {
225236
if (self._teardown.length === 0) {
226237
completeEnd();
@@ -244,19 +255,6 @@ Test.prototype._end = function _end(err) {
244255
}
245256

246257
next();
247-
248-
function completeEnd() {
249-
if (!self.ended) { self.emit('end'); }
250-
var pendingAsserts = self._pendingAsserts();
251-
if (!self._planError && self._plan !== undefined && pendingAsserts) {
252-
self._planError = true;
253-
self.fail('plan != count', {
254-
expected: self._plan,
255-
actual: self.assertCount
256-
});
257-
}
258-
self.ended = true;
259-
}
260258
};
261259

262260
Test.prototype._exit = function _exit() {
@@ -780,4 +778,6 @@ Test.skip = function skip(name_, _opts, _cb) {
780778
return new Test(args.name, args.opts, args.cb);
781779
};
782780

781+
module.exports = Test;
782+
783783
// vim: set softtabstop=4 shiftwidth=4:

‎package.json

+3-4
Original file line numberDiff line numberDiff line change
@@ -103,10 +103,9 @@
103103
"assert",
104104
"browser"
105105
],
106-
"author": {
107-
"name": "Jordan Harband",
108-
"email": "ljharb@gmail.com",
109-
"url": "http://ljharb.codes"
106+
"author": "Jordan Harband <ljharb@gmail.com>",
107+
"funding": {
108+
"url": "https://github.com/sponsors/ljharb"
110109
},
111110
"license": "MIT",
112111
"publishConfig": {

‎test/anonymous-fn/test-wrapper.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,13 @@
11
'use strict';
22

3+
function setUp() {
4+
// ... example ...
5+
}
6+
7+
function tearDown() {
8+
// ... example ...
9+
}
10+
311
// Example of wrapper function that would invoke tape
412
module.exports = function (testCase) {
513
return function (t) {
@@ -8,11 +16,3 @@ module.exports = function (testCase) {
816
tearDown();
917
};
1018
};
11-
12-
function setUp() {
13-
// ... example ...
14-
}
15-
16-
function tearDown() {
17-
// ... example ...
18-
}

‎test/async-await/async-bug.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,12 @@ function myCode(arr) {
1111
return sum;
1212
}
1313

14+
function sleep(ms) {
15+
return new Promise((resolve) => {
16+
setTimeout(resolve, ms);
17+
});
18+
}
19+
1420
test('async-error', async function myTest(t) {
1521
await sleep(100);
1622
t.ok(true, 'before throw');
@@ -23,9 +29,3 @@ test('async-error', async function myTest(t) {
2329

2430
t.end();
2531
});
26-
27-
function sleep(ms) {
28-
return new Promise((resolve) => {
29-
setTimeout(resolve, ms);
30-
});
31-
}

‎test/end-as-callback.js

+39-39
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,45 @@ var forEach = require('for-each');
55
var tape = require('../');
66
var concat = require('concat-stream');
77

8+
/**
9+
* extract the stack trace for the failed test.
10+
* this will change dependent on the environment
11+
* so no point hard-coding it in the test assertion
12+
* see: https://git.io/v6hGG for example
13+
* @param String rows - the tap output lines
14+
* @returns String stacktrace - just the error stack part
15+
*/
16+
function getStackTrace(rows) {
17+
var stacktrace = ' ---\n';
18+
var extract = false;
19+
forEach(rows.toString('utf8').split('\n'), function (row) {
20+
if (!extract) {
21+
if (row.indexOf('---') > -1) { // start of stack trace
22+
extract = true;
23+
}
24+
} else if (row.indexOf('...') > -1) { // end of stack trace
25+
extract = false;
26+
stacktrace += ' ...';
27+
} else {
28+
stacktrace += row + '\n';
29+
}
30+
});
31+
// console.log(stacktrace);
32+
return stacktrace;
33+
}
34+
35+
function fakeAsyncTask(name, cb) {
36+
cb(null, 'task' + name);
37+
}
38+
39+
function fakeAsyncWrite(name, cb) {
40+
cb(null);
41+
}
42+
43+
function fakeAsyncWriteFail(name, cb) {
44+
cb(new Error('fail'));
45+
}
46+
847
tap.test('tape assert.end as callback', function (tt) {
948
var test = tape.createHarness({ exit: false });
1049

@@ -46,42 +85,3 @@ tap.test('tape assert.end as callback', function (tt) {
4685
});
4786
});
4887
});
49-
50-
function fakeAsyncTask(name, cb) {
51-
cb(null, 'task' + name);
52-
}
53-
54-
function fakeAsyncWrite(name, cb) {
55-
cb(null);
56-
}
57-
58-
function fakeAsyncWriteFail(name, cb) {
59-
cb(new Error('fail'));
60-
}
61-
62-
/**
63-
* extract the stack trace for the failed test.
64-
* this will change dependent on the environment
65-
* so no point hard-coding it in the test assertion
66-
* see: https://git.io/v6hGG for example
67-
* @param String rows - the tap output lines
68-
* @returns String stacktrace - just the error stack part
69-
*/
70-
function getStackTrace(rows) {
71-
var stacktrace = ' ---\n';
72-
var extract = false;
73-
forEach(rows.toString('utf8').split('\n'), function (row) {
74-
if (!extract) {
75-
if (row.indexOf('---') > -1) { // start of stack trace
76-
extract = true;
77-
}
78-
} else if (row.indexOf('...') > -1) { // end of stack trace
79-
extract = false;
80-
stacktrace += ' ...';
81-
} else {
82-
stacktrace += row + '\n';
83-
}
84-
});
85-
// console.log(stacktrace);
86-
return stacktrace;
87-
}

‎test/import.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,12 @@ var concat = require('concat-stream');
66
var hasDynamicImport = require('has-dynamic-import');
77
var assign = require('object.assign');
88

9+
function tape(args, options) {
10+
var bin = __dirname + '/../bin/tape';
11+
12+
return spawn('node', [bin].concat(args.split(' ')), assign({ cwd: __dirname }, options));
13+
}
14+
915
tap.test('importing mjs files', function (t) {
1016
hasDynamicImport().then(function (hasSupport) {
1117
if (hasSupport) {
@@ -187,9 +193,3 @@ tap.test('errors importing test files', function (t) {
187193
}
188194
});
189195
});
190-
191-
function tape(args, options) {
192-
var bin = __dirname + '/../bin/tape';
193-
194-
return spawn('node', [bin].concat(args.split(' ')), assign({ cwd: __dirname }, options));
195-
}

‎test/require.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,12 @@ var spawn = require('child_process').spawn;
55
var concat = require('concat-stream');
66
var stripFullStack = require('./common').stripFullStack;
77

8+
function tape(args) {
9+
var bin = __dirname + '/../bin/tape';
10+
11+
return spawn('node', [bin].concat(args.split(' ')), { cwd: __dirname });
12+
}
13+
814
tap.test('requiring a single module', function (t) {
915
t.plan(2);
1016

@@ -65,9 +71,3 @@ tap.test('requiring multiple modules', function (t) {
6571
t.equal(code, 0);
6672
});
6773
});
68-
69-
function tape(args) {
70-
var bin = __dirname + '/../bin/tape';
71-
72-
return spawn('node', [bin].concat(args.split(' ')), { cwd: __dirname });
73-
}

‎test/stackTrace.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,10 @@ var common = require('./common');
88

99
var getDiag = common.getDiag;
1010

11+
function stripAt(body) {
12+
return body.replace(/^\s*at:\s+Test.*$\n/m, '');
13+
}
14+
1115
tap.test('preserves stack trace with newlines', function (tt) {
1216
tt.plan(3);
1317

@@ -284,7 +288,3 @@ tap.test('preserves stack trace for failed assertions where actual===falsy', fun
284288
t.equal(false, true, 'false should be true');
285289
});
286290
});
287-
288-
function stripAt(body) {
289-
return body.replace(/^\s*at:\s+Test.*$\n/m, '');
290-
}

0 commit comments

Comments
 (0)
Please sign in to comment.