Skip to content

Commit d4fb8e2

Browse files
sigorillaljharb
authored andcommitted
[Tests] Turn on eslint for test files
1 parent 387afd7 commit d4fb8e2

File tree

6 files changed

+72
-60
lines changed

6 files changed

+72
-60
lines changed

.eslintrc

+15-15
Original file line numberDiff line numberDiff line change
@@ -1,19 +1,19 @@
11
{
2-
"root": true,
2+
"root": true,
33

4-
"extends": "@ljharb",
4+
"extends": "@ljharb",
55

6-
"rules": {
7-
"complexity": [2, 25],
8-
"consistent-return": [1],
9-
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
10-
"indent": [2, 4],
11-
"max-params": [2, 11],
12-
"max-statements": [2, 42],
13-
"no-extra-parens": [1],
14-
"no-continue": [1],
15-
"no-magic-numbers": 0,
16-
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
17-
"operator-linebreak": 1
18-
}
6+
"rules": {
7+
"complexity": [2, 25],
8+
"consistent-return": [1],
9+
"id-length": [2, { "min": 1, "max": 25, "properties": "never" }],
10+
"indent": [2, 4],
11+
"max-params": [2, 11],
12+
"max-statements": [2, 42],
13+
"no-extra-parens": [1],
14+
"no-continue": [1],
15+
"no-magic-numbers": 0,
16+
"no-restricted-syntax": [2, "BreakStatement", "DebuggerStatement", "ForInStatement", "LabeledStatement", "WithStatement"],
17+
"operator-linebreak": 1
18+
}
1919
}

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@
4242
"test": "npm run --silent coverage",
4343
"tests-only": "node test",
4444
"readme": "evalmd README.md",
45-
"lint": "eslint lib/*.js text/*.js",
45+
"lint": "eslint lib/*.js test/*.js",
4646
"coverage": "covert test",
4747
"dist": "mkdirp dist && browserify --standalone Qs lib/index.js > dist/qs.js"
4848
},

test/.eslintrc

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
{
2+
"rules": {
3+
"max-lines": 0,
4+
"max-nested-callbacks": [2, 3],
5+
"max-statements": 0,
6+
"no-extend-native": 0,
7+
"sort-keys": 1
8+
}
9+
}

test/index.js

+2
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
'use strict';
2+
13
require('./parse');
24

35
require('./stringify');

test/parse.js

+33-34
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ var iconv = require('iconv-lite');
66

77
test('parse()', function (t) {
88
t.test('parses a simple string', function (st) {
9-
st.deepEqual(qs.parse('0=foo'), { '0': 'foo' });
9+
st.deepEqual(qs.parse('0=foo'), { 0: 'foo' });
1010
st.deepEqual(qs.parse('foo=c++'), { foo: 'c ' });
1111
st.deepEqual(qs.parse('a[>=]=23'), { a: { '>=': '23' } });
1212
st.deepEqual(qs.parse('a[<=>]==23'), { a: { '<=>': '=23' } });
@@ -93,7 +93,7 @@ test('parse()', function (t) {
9393

9494
t.test('limits specific array indices to arrayLimit', function (st) {
9595
st.deepEqual(qs.parse('a[20]=a', { arrayLimit: 20 }), { a: ['a'] });
96-
st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { '21': 'a' } });
96+
st.deepEqual(qs.parse('a[21]=a', { arrayLimit: 20 }), { a: { 21: 'a' } });
9797
st.end();
9898
});
9999

@@ -124,17 +124,17 @@ test('parse()', function (t) {
124124
});
125125

126126
t.test('transforms arrays to objects', function (st) {
127-
st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
128-
st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
129-
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', '0': 'bar' } });
130-
st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { '0': 'bar', bad: 'baz' } });
131-
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
127+
st.deepEqual(qs.parse('foo[0]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
128+
st.deepEqual(qs.parse('foo[bad]=baz&foo[0]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
129+
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar'), { foo: { bad: 'baz', 0: 'bar' } });
130+
st.deepEqual(qs.parse('foo[]=bar&foo[bad]=baz'), { foo: { 0: 'bar', bad: 'baz' } });
131+
st.deepEqual(qs.parse('foo[bad]=baz&foo[]=bar&foo[]=foo'), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
132132
st.deepEqual(qs.parse('foo[0][a]=a&foo[0][b]=b&foo[1][a]=aa&foo[1][b]=bb'), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
133133

134-
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { '0': 'b', c: true, t: 'u' } });
135-
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { '0': 'b', t: 'u', hasOwnProperty: 'c' } });
136-
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { '0': 'b', '1': 'c', x: 'y' } });
137-
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { '0': 'b', hasOwnProperty: 'c', x: 'y' } });
134+
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: false }), { a: { 0: 'b', c: true, t: 'u' } });
135+
st.deepEqual(qs.parse('a[]=b&a[t]=u&a[hasOwnProperty]=c', { allowPrototypes: true }), { a: { 0: 'b', t: 'u', hasOwnProperty: 'c' } });
136+
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: false }), { a: { 0: 'b', 1: 'c', x: 'y' } });
137+
st.deepEqual(qs.parse('a[]=b&a[hasOwnProperty]=c&a[x]=y', { allowPrototypes: true }), { a: { 0: 'b', hasOwnProperty: 'c', x: 'y' } });
138138
st.end();
139139
});
140140

@@ -144,18 +144,18 @@ test('parse()', function (t) {
144144
st.deepEqual(qs.parse('foo[0][0].baz=bar&fool.bad=baz', { allowDots: true }), { foo: [[{ baz: 'bar' }]], fool: { bad: 'baz' } });
145145
st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15'], bar: '2' }] });
146146
st.deepEqual(qs.parse('foo[0].baz[0]=15&foo[0].baz[1]=16&foo[0].bar=2', { allowDots: true }), { foo: [{ baz: ['15', '16'], bar: '2' }] });
147-
st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
148-
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar' } });
149-
st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { '0': 'bar', bad: 'baz' } });
150-
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', '0': 'bar', '1': 'foo' } });
147+
st.deepEqual(qs.parse('foo.bad=baz&foo[0]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
148+
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar' } });
149+
st.deepEqual(qs.parse('foo[]=bar&foo.bad=baz', { allowDots: true }), { foo: { 0: 'bar', bad: 'baz' } });
150+
st.deepEqual(qs.parse('foo.bad=baz&foo[]=bar&foo[]=foo', { allowDots: true }), { foo: { bad: 'baz', 0: 'bar', 1: 'foo' } });
151151
st.deepEqual(qs.parse('foo[0].a=a&foo[0].b=b&foo[1].a=aa&foo[1].b=bb', { allowDots: true }), { foo: [{ a: 'a', b: 'b' }, { a: 'aa', b: 'bb' }] });
152152
st.end();
153153
});
154154

155155
t.deepEqual(qs.parse('a[b]=c&a=d'), { a: { b: 'c', d: true } }, 'can add keys to objects');
156156

157157
t.test('correctly prunes undefined values when converting an array to an object', function (st) {
158-
st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { '2': 'b', '99999999': 'c' } });
158+
st.deepEqual(qs.parse('a[2]=b&a[99999999]=c'), { a: { 2: 'b', 99999999: 'c' } });
159159
st.end();
160160
});
161161

@@ -167,7 +167,7 @@ test('parse()', function (t) {
167167
});
168168

169169
t.test('doesn\'t produce empty keys', function (st) {
170-
st.deepEqual(qs.parse('_r=1&'), { '_r': '1' });
170+
st.deepEqual(qs.parse('_r=1&'), { _r: '1' });
171171
st.end();
172172
});
173173

@@ -238,8 +238,8 @@ test('parse()', function (t) {
238238
});
239239

240240
t.test('continues parsing when no parent is found', function (st) {
241-
st.deepEqual(qs.parse('[]=&a=b'), { '0': '', a: 'b' });
242-
st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { '0': null, a: 'b' });
241+
st.deepEqual(qs.parse('[]=&a=b'), { 0: '', a: 'b' });
242+
st.deepEqual(qs.parse('[]&a=b', { strictNullHandling: true }), { 0: null, a: 'b' });
243243
st.deepEqual(qs.parse('[foo]=bar'), { foo: 'bar' });
244244
st.end();
245245
});
@@ -250,7 +250,9 @@ test('parse()', function (t) {
250250
str = str + '&' + str;
251251
}
252252

253-
st.doesNotThrow(function () { qs.parse(str); });
253+
st.doesNotThrow(function () {
254+
qs.parse(str);
255+
});
254256

255257
st.end();
256258
});
@@ -293,14 +295,14 @@ test('parse()', function (t) {
293295
});
294296

295297
t.test('allows overriding array limit', function (st) {
296-
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { '0': 'b' } });
298+
st.deepEqual(qs.parse('a[0]=b', { arrayLimit: -1 }), { a: { 0: 'b' } });
297299
st.deepEqual(qs.parse('a[-1]=b', { arrayLimit: -1 }), { a: { '-1': 'b' } });
298-
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { '0': 'b', '1': 'c' } });
300+
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { arrayLimit: 0 }), { a: { 0: 'b', 1: 'c' } });
299301
st.end();
300302
});
301303

302304
t.test('allows disabling array parsing', function (st) {
303-
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { '0': 'b', '1': 'c' } });
305+
st.deepEqual(qs.parse('a[0]=b&a[1]=c', { parseArrays: false }), { a: { 0: 'b', 1: 'c' } });
304306
st.end();
305307
});
306308

@@ -344,13 +346,13 @@ test('parse()', function (t) {
344346

345347
t.test('parses an object and not child values', function (st) {
346348
var input = {
347-
'user[name]': { 'pop[bob]': { 'test': 3 } },
349+
'user[name]': { 'pop[bob]': { test: 3 } },
348350
'user[email]': null
349351
};
350352

351353
var expected = {
352354
user: {
353-
name: { 'pop[bob]': { 'test': 3 } },
355+
name: { 'pop[bob]': { test: 3 } },
354356
email: null
355357
}
356358
};
@@ -426,7 +428,7 @@ test('parse()', function (t) {
426428
st.deepEqual(qs.parse(null, { plainObjects: true }), Object.create(null));
427429
var expectedArray = Object.create(null);
428430
expectedArray.a = Object.create(null);
429-
expectedArray.a['0'] = 'b';
431+
expectedArray.a[0] = 'b';
430432
expectedArray.a.c = 'd';
431433
st.deepEqual(qs.parse('a[]=b&a[c]=d', { plainObjects: true }), expectedArray);
432434
st.end();
@@ -435,13 +437,12 @@ test('parse()', function (t) {
435437
t.test('can parse with custom encoding', function (st) {
436438
st.deepEqual(qs.parse('%8c%a7=%91%e5%8d%e3%95%7b', {
437439
decoder: function (str) {
438-
var reg = /\%([0-9A-F]{2})/ig;
440+
var reg = /%([0-9A-F]{2})/ig;
439441
var result = [];
440-
var parts;
441-
var last = 0;
442-
while (parts = reg.exec(str)) {
442+
var parts = reg.exec(str);
443+
while (parts) {
443444
result.push(parseInt(parts[1], 16));
444-
last = parts.index + parts[0].length;
445+
parts = reg.exec(str);
445446
}
446447
return iconv.decode(new Buffer(result), 'shift_jis').toString();
447448
}
@@ -451,9 +452,7 @@ test('parse()', function (t) {
451452

452453
t.test('throws error with wrong decoder', function (st) {
453454
st.throws(function () {
454-
qs.parse({}, {
455-
decoder: 'string'
456-
});
455+
qs.parse({}, { decoder: 'string' });
457456
}, new TypeError('Decoder has to be a function.'));
458457
st.end();
459458
});

test/stringify.js

+12-10
Original file line numberDiff line numberDiff line change
@@ -380,7 +380,7 @@ test('stringify()', function (t) {
380380
var calls = 0;
381381
var obj = { a: 'b', c: 'd', e: { f: new Date(1257894000000) } };
382382
var filterFunc = function (prefix, value) {
383-
calls++;
383+
calls += 1;
384384
if (calls === 1) {
385385
st.equal(prefix, '', 'prefix is empty');
386386
st.equal(value, obj);
@@ -406,14 +406,18 @@ test('stringify()', function (t) {
406406
});
407407

408408
t.test('can sort the keys', function (st) {
409-
var sort = function (a, b) { return a.localeCompare(b); };
409+
var sort = function (a, b) {
410+
return a.localeCompare(b);
411+
};
410412
st.equal(qs.stringify({ a: 'c', z: 'y', b: 'f' }, { sort: sort }), 'a=c&b=f&z=y');
411413
st.equal(qs.stringify({ a: 'c', z: { j: 'a', i: 'b' }, b: 'f' }, { sort: sort }), 'a=c&b=f&z%5Bi%5D=b&z%5Bj%5D=a');
412414
st.end();
413415
});
414416

415417
t.test('can sort the keys at depth 3 or more too', function (st) {
416-
var sort = function (a, b) { return a.localeCompare(b); };
418+
var sort = function (a, b) {
419+
return a.localeCompare(b);
420+
};
417421
st.equal(
418422
qs.stringify(
419423
{ a: 'a', z: { zj: { zjb: 'zjb', zja: 'zja' }, zi: { zib: 'zib', zia: 'zia' } }, b: 'b' },
@@ -432,14 +436,14 @@ test('stringify()', function (t) {
432436
});
433437

434438
t.test('can stringify with custom encoding', function (st) {
435-
st.equal(qs.stringify({ : '大阪府', '': ''}, {
439+
st.equal(qs.stringify({ : '大阪府', '': '' }, {
436440
encoder: function (str) {
437441
if (str.length === 0) {
438442
return '';
439443
}
440444
var buf = iconv.encode(str, 'shiftjis');
441445
var result = [];
442-
for (var i=0; i < buf.length; ++i) {
446+
for (var i = 0; i < buf.length; ++i) {
443447
result.push(buf.readUInt8(i).toString(16));
444448
}
445449
return '%' + result.join('%');
@@ -455,9 +459,7 @@ test('stringify()', function (t) {
455459
st.end();
456460
});
457461

458-
t.test('can use custom encoder for a buffer object', {
459-
skip: typeof Buffer === 'undefined'
460-
}, function (st) {
462+
t.test('can use custom encoder for a buffer object', { skip: typeof Buffer === 'undefined' }, function (st) {
461463
st.equal(qs.stringify({ a: new Buffer([1]) }, {
462464
encoder: function (buffer) {
463465
if (typeof buffer === 'string') {
@@ -505,13 +507,13 @@ test('stringify()', function (t) {
505507

506508
t.test('RFC 1738 spaces serialization', function (st) {
507509
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC1738 }), 'a=b+c');
508-
st.equal(qs.stringify({ "a b": 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
510+
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC1738 }), 'a+b=c+d');
509511
st.end();
510512
});
511513

512514
t.test('RFC 3986 spaces serialization', function (st) {
513515
st.equal(qs.stringify({ a: 'b c' }, { format: qs.formats.RFC3986 }), 'a=b%20c');
514-
st.equal(qs.stringify({ "a b": 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
516+
st.equal(qs.stringify({ 'a b': 'c d' }, { format: qs.formats.RFC3986 }), 'a%20b=c%20d');
515517
st.end();
516518
});
517519

0 commit comments

Comments
 (0)