Skip to content

Commit f8a8a7f

Browse files
committed
[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;

0 commit comments

Comments
 (0)