Skip to content

Commit 1849094

Browse files
committed
Refactor source code to use eslint with airbnb legacy rules.
1 parent ecced93 commit 1849094

File tree

6 files changed

+352
-283
lines changed

6 files changed

+352
-283
lines changed

.eslintrc

Lines changed: 28 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,28 @@
1+
{
2+
"extends": "airbnb/legacy",
3+
"env": {
4+
"node": true,
5+
"mocha": true
6+
},
7+
"rules": {
8+
// disabled - disagree with airbnb
9+
"func-names": [0],
10+
"space-before-function-paren": [0],
11+
"padded-blocks": [0],
12+
"consistent-return": [0],
13+
14+
// Disabled but may want to refactor code eventually
15+
"no-proto": [1],
16+
"no-shadow": [1],
17+
18+
// IMHO, more sensible overrides to existing airbnb error definitions
19+
"max-len": [2, 100, 4, {"ignoreComments": true, "ignoreUrls": true}],
20+
"comma-dangle": [2, "never"],
21+
"no-multi-spaces": [2, { "exceptions": { "VariableDeclarator": true, "Property": true } }],
22+
"no-unused-expressions": [2, { "allowShortCircuit": true, "allowTernary": true }],
23+
"no-use-before-define": [2, "nofunc"],
24+
"no-param-reassign": [2, {"props": false}],
25+
"no-cond-assign": [2, "except-parens"],
26+
"no-return-assign": [2, "except-parens"]
27+
}
28+
}

lib/agent.js

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,10 @@
33
* Module dependencies.
44
*/
55

6-
var Agent = require('superagent').agent
7-
, methods = require('methods')
8-
, http = require('http')
9-
, Test = require('./test');
6+
var Agent = require('superagent').agent;
7+
var methods = require('methods');
8+
var http = require('http');
9+
var Test = require('./test');
1010

1111
/**
1212
* Expose `Agent`.
@@ -22,9 +22,9 @@ module.exports = TestAgent;
2222
* @api public
2323
*/
2424

25-
function TestAgent(app, options){
25+
function TestAgent(app, options) {
2626
if (!(this instanceof TestAgent)) return new TestAgent(app, options);
27-
if ('function' == typeof app) app = http.createServer(app);
27+
if (typeof app === 'function') app = http.createServer(app); // eslint-disable-line no-param-reassign
2828
if (options) this._ca = options.ca;
2929
Agent.call(this);
3030
this.app = app;
@@ -37,8 +37,8 @@ function TestAgent(app, options){
3737
TestAgent.prototype.__proto__ = Agent.prototype;
3838

3939
// override HTTP verb methods
40-
methods.forEach(function(method){
41-
TestAgent.prototype[method] = function(url, fn){
40+
methods.forEach(function(method) {
41+
TestAgent.prototype[method] = function(url, fn) { // eslint-disable-line no-unused-vars
4242
var req = new Test(this.app, method.toUpperCase(), url);
4343
req.ca(this._ca);
4444

lib/test.js

Lines changed: 54 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -2,12 +2,12 @@
22
* Module dependencies.
33
*/
44

5-
var request = require('superagent')
6-
, util = require('util')
7-
, http = require('http')
8-
, https = require('https')
9-
, assert = require('assert')
10-
, Request = request.Request;
5+
var request = require('superagent');
6+
var util = require('util');
7+
var http = require('http');
8+
var https = require('https');
9+
var assert = require('assert');
10+
var Request = request.Request;
1111

1212
/**
1313
* Expose `Test`.
@@ -31,7 +31,7 @@ function Test(app, method, path) {
3131
this.buffer();
3232
this.app = app;
3333
this._asserts = [];
34-
this.url = 'string' == typeof app
34+
this.url = typeof app === 'string'
3535
? app + path
3636
: this.serverAddress(app, path);
3737
}
@@ -51,11 +51,14 @@ Test.prototype.__proto__ = Request.prototype;
5151
* @api private
5252
*/
5353

54-
Test.prototype.serverAddress = function(app, path){
54+
Test.prototype.serverAddress = function(app, path) {
5555
var addr = app.address();
56+
var port;
57+
var protocol;
58+
5659
if (!addr) this._server = app.listen(0);
57-
var port = app.address().port;
58-
var protocol = app instanceof https.Server ? 'https' : 'http';
60+
port = app.address().port;
61+
protocol = app instanceof https.Server ? 'https' : 'http';
5962
return protocol + '://127.0.0.1:' + port + path;
6063
};
6164

@@ -75,27 +78,28 @@ Test.prototype.serverAddress = function(app, path){
7578
* @api public
7679
*/
7780

78-
Test.prototype.expect = function(a, b, c){
81+
Test.prototype.expect = function(a, b, c) {
7982
// callback
80-
if ('function' == typeof a) {
83+
if (typeof a === 'function') {
8184
this._asserts.push(a);
8285
return this;
8386
}
84-
if ('function' == typeof b) this.end(b);
85-
if ('function' == typeof c) this.end(c);
87+
if (typeof b === 'function') this.end(b);
88+
if (typeof c === 'function') this.end(c);
8689

8790
// status
88-
if ('number' == typeof a) {
91+
if (typeof a === 'number') {
8992
this._asserts.push(this._assertStatus.bind(this, a));
9093
// body
91-
if ('function' != typeof b && arguments.length > 1)
94+
if (typeof b !== 'function' && arguments.length > 1) {
9295
this._asserts.push(this._assertBody.bind(this, b));
96+
}
9397
return this;
9498
}
9599

96100
// header field
97-
if ('string' == typeof b || 'number' == typeof b || b instanceof RegExp) {
98-
this._asserts.push(this._assertHeader.bind(this, {name: ''+a, value: b}));
101+
if (typeof b === 'string' || typeof b === 'number' || b instanceof RegExp) {
102+
this._asserts.push(this._assertHeader.bind(this, { name: '' + a, value: b }));
99103
return this;
100104
}
101105

@@ -113,17 +117,17 @@ Test.prototype.expect = function(a, b, c){
113117
* @api public
114118
*/
115119

116-
Test.prototype.end = function(fn){
120+
Test.prototype.end = function(fn) {
117121
var self = this;
118122
var server = this._server;
119123
var end = Request.prototype.end;
120124

121-
end.call(this, function(err, res){
125+
end.call(this, function(err, res) {
122126
if (server && server._handle) return server.close(assert);
123127

124128
assert();
125129

126-
function assert(){
130+
function assert() {
127131
self.assert(err, res, fn);
128132
}
129133
});
@@ -140,18 +144,19 @@ Test.prototype.end = function(fn){
140144
* @api private
141145
*/
142146

143-
Test.prototype.assert = function(resError, res, fn){
147+
Test.prototype.assert = function(resError, res, fn) {
144148
var error;
149+
var i;
145150

146151
// asserts
147-
for (var i = 0; i < this._asserts.length && !error; ++i) {
152+
for (i = 0; i < this._asserts.length && !error; ++i) {
148153
error = this._assertFunction(this._asserts[i], res);
149154
}
150155

151156
// set unexpected superagent error if no other error has occurred.
152-
if (!error && resError instanceof Error &&
153-
(!res || resError.status !== res.status))
157+
if (!error && resError instanceof Error && (!res || resError.status !== res.status)) {
154158
error = resError;
159+
}
155160

156161
fn.call(this, error || null, res);
157162
};
@@ -167,20 +172,23 @@ Test.prototype.assert = function(resError, res, fn){
167172

168173
Test.prototype._assertBody = function(body, res) {
169174
var isregexp = body instanceof RegExp;
175+
var a;
176+
var b;
177+
170178
// parsed
171-
if ('object' == typeof body && !isregexp) {
179+
if (typeof body === 'object' && !isregexp) {
172180
try {
173181
assert.deepEqual(body, res.body);
174182
} catch (err) {
175-
var a = util.inspect(body);
176-
var b = util.inspect(res.body);
183+
a = util.inspect(body);
184+
b = util.inspect(res.body);
177185
return error('expected ' + a + ' response body, got ' + b, body, res.body);
178186
}
179187
} else {
180188
// string
181189
if (body !== res.text) {
182-
var a = util.inspect(body);
183-
var b = util.inspect(res.text);
190+
a = util.inspect(body);
191+
b = util.inspect(res.text);
184192

185193
// regexp
186194
if (isregexp) {
@@ -206,11 +214,19 @@ Test.prototype._assertBody = function(body, res) {
206214
Test.prototype._assertHeader = function(header, res) {
207215
var field = header.name;
208216
var actual = res.header[field.toLowerCase()];
209-
if (null == actual) return new Error('expected "' + field + '" header field');
210217
var fieldExpected = header.value;
211-
if (fieldExpected == actual) return;
218+
219+
if (typeof actual === 'undefined') return new Error('expected "' + field + '" header field');
220+
if (fieldExpected == actual) return; // eslint-disable-line eqeqeq
221+
/* if ((actual instanceof Array && actual.toString() === fieldExpected) ||
222+
fieldExpected === actual) {
223+
return;
224+
} */
212225
if (fieldExpected instanceof RegExp) {
213-
if (!fieldExpected.test(actual)) return new Error('expected "' + field + '" matching ' + fieldExpected + ', got "' + actual + '"');
226+
if (!fieldExpected.test(actual)) {
227+
return new Error('expected "' + field + '" matching ' +
228+
fieldExpected + ', got "' + actual + '"');
229+
}
214230
} else {
215231
return new Error('expected "' + field + '" of "' + fieldExpected + '", got "' + actual + '"');
216232
}
@@ -226,9 +242,11 @@ Test.prototype._assertHeader = function(header, res) {
226242
*/
227243

228244
Test.prototype._assertStatus = function(status, res) {
245+
var a;
246+
var b;
229247
if (res.status !== status) {
230-
var a = http.STATUS_CODES[status];
231-
var b = http.STATUS_CODES[res.status];
248+
a = http.STATUS_CODES[status];
249+
b = http.STATUS_CODES[res.status];
232250
return new Error('expected ' + status + ' "' + a + '", got ' + res.status + ' "' + b + '"');
233251
}
234252
};
@@ -245,7 +263,7 @@ Test.prototype._assertFunction = function(check, res) {
245263
var err;
246264
try {
247265
err = check(res);
248-
} catch(e) {
266+
} catch (e) {
249267
err = e;
250268
}
251269
if (err instanceof Error) return err;

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
"main": "index.js",
66
"scripts": {
77
"pretest": "npm install",
8-
"test": "mocha --require should --reporter spec --check-leaks"
8+
"test": "eslint lib/** test/** && mocha --require should --reporter spec --check-leaks"
99
},
1010
"dependencies": {
1111
"superagent": "^1.7.2",
@@ -14,6 +14,8 @@
1414
"devDependencies": {
1515
"body-parser": "~1.13.2",
1616
"cookie-parser": "~1.3.5",
17+
"eslint": "^2.5.3",
18+
"eslint-config-airbnb": "^6.2.0",
1719
"express": "~4.12.4",
1820
"mocha": "~2.2.5",
1921
"should": "~7.0.2"

test/.eslintrc

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"rules": {
3+
// errors - disabled for chai test support
4+
"no-unused-expressions": [0],
5+
// allow function args for superagent
6+
"no-unused-vars": [2, {"args": "none"}]
7+
}
8+
}

0 commit comments

Comments
 (0)