Skip to content

Commit 6e3a8be

Browse files
LekoMylesBorins
authored andcommitted
test: replace function with arrow function
1. Among the list of Code and Learn, I solved the unfinished task of replacing function with arrow function: nodejs/code-and-learn#72 (comment) 2. Replace arrow function with shorter property syntax Arrow function makes `this` lexical scope. But toString expects evaluate `this` in runtime. 3. Replace this with null makeBlock does not need `this`. update `this` with `null` to clarify the intent. PR-URL: #17345 Reviewed-By: Anna Henningsen <[email protected]> Reviewed-By: Jon Moss <[email protected]> Reviewed-By: Yosuke Furukawa <[email protected]>
1 parent b719b77 commit 6e3a8be

5 files changed

+40
-40
lines changed

test/parallel/test-assert.js

+9-9
Original file line numberDiff line numberDiff line change
@@ -26,8 +26,8 @@ const a = assert;
2626

2727
function makeBlock(f) {
2828
const args = Array.prototype.slice.call(arguments, 1);
29-
return function() {
30-
return f.apply(this, args);
29+
return () => {
30+
return f.apply(null, args);
3131
};
3232
}
3333

@@ -183,7 +183,7 @@ assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2));
183183

184184
// having an identical prototype property
185185
const nbRoot = {
186-
toString: function() { return `${this.first} ${this.last}`; }
186+
toString() { return `${this.first} ${this.last}`; }
187187
};
188188

189189
function nameBuilder(first, last) {
@@ -458,10 +458,10 @@ assert.throws(makeBlock(thrower, TypeError));
458458
'a.doesNotThrow is not catching type matching errors');
459459
}
460460

461-
assert.throws(function() { assert.ifError(new Error('test error')); },
461+
assert.throws(() => { assert.ifError(new Error('test error')); },
462462
/^Error: test error$/);
463-
assert.doesNotThrow(function() { assert.ifError(null); });
464-
assert.doesNotThrow(function() { assert.ifError(); });
463+
assert.doesNotThrow(() => { assert.ifError(null); });
464+
assert.doesNotThrow(() => { assert.ifError(); });
465465

466466
assert.throws(() => {
467467
assert.doesNotThrow(makeBlock(thrower, Error), 'user message');
@@ -501,7 +501,7 @@ assert.throws(() => {
501501
let threw = false;
502502
try {
503503
assert.throws(
504-
function() {
504+
() => {
505505
throw ({}); // eslint-disable-line no-throw-literal
506506
},
507507
Array
@@ -516,7 +516,7 @@ assert.throws(() => {
516516
a.throws(makeBlock(thrower, TypeError), /\[object Object\]/);
517517

518518
// use a fn to validate error object
519-
a.throws(makeBlock(thrower, TypeError), function(err) {
519+
a.throws(makeBlock(thrower, TypeError), (err) => {
520520
if ((err instanceof TypeError) && /\[object Object\]/.test(err)) {
521521
return true;
522522
}
@@ -619,7 +619,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
619619
let threw = false;
620620
try {
621621
// eslint-disable-next-line no-restricted-syntax
622-
assert.throws(function() {
622+
assert.throws(() => {
623623
assert.ifError(null);
624624
});
625625
} catch (e) {

test/parallel/test-domain-top-level-error-handler-clears-stack.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@ const domain = require('domain');
99
*/
1010
const d = domain.create();
1111

12-
d.on('error', common.mustCall(function() {
13-
process.nextTick(function() {
12+
d.on('error', common.mustCall(() => {
13+
process.nextTick(() => {
1414
// Scheduling a callback with process.nextTick will enter a _new_ domain,
1515
// and the callback will be called after the domain that handled the error
1616
// was exited. So there should be only one domain on the domains stack if
@@ -29,6 +29,6 @@ d.on('error', common.mustCall(function() {
2929
});
3030
}));
3131

32-
d.run(function() {
32+
d.run(() => {
3333
throw new Error('Error from domain');
3434
});

test/parallel/test-querystring.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -129,7 +129,7 @@ const qsWeirdObjects = [
129129
[{ regexp: /./g }, 'regexp=', { 'regexp': '' }],
130130
// eslint-disable-next-line no-unescaped-regexp-dot
131131
[{ regexp: new RegExp('.', 'g') }, 'regexp=', { 'regexp': '' }],
132-
[{ fn: function() {} }, 'fn=', { 'fn': '' }],
132+
[{ fn: () => {} }, 'fn=', { 'fn': '' }],
133133
[{ fn: new Function('') }, 'fn=', { 'fn': '' }],
134134
[{ math: Math }, 'math=', { 'math': '' }],
135135
[{ e: extendedFunction }, 'e=', { 'e': '' }],
@@ -192,7 +192,7 @@ function check(actual, expected, input) {
192192
`Expected keys: ${inspect(expectedKeys)}`;
193193
}
194194
assert.deepStrictEqual(actualKeys, expectedKeys, msg);
195-
expectedKeys.forEach(function(key) {
195+
expectedKeys.forEach((key) => {
196196
if (typeof input === 'string') {
197197
msg = `Input: ${inspect(input)}\n` +
198198
`Key: ${inspect(key)}\n` +
@@ -206,21 +206,21 @@ function check(actual, expected, input) {
206206
}
207207

208208
// test that the canonical qs is parsed properly.
209-
qsTestCases.forEach(function(testCase) {
209+
qsTestCases.forEach((testCase) => {
210210
check(qs.parse(testCase[0]), testCase[2], testCase[0]);
211211
});
212212

213213
// test that the colon test cases can do the same
214-
qsColonTestCases.forEach(function(testCase) {
214+
qsColonTestCases.forEach((testCase) => {
215215
check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]);
216216
});
217217

218218
// test the weird objects, that they get parsed properly
219-
qsWeirdObjects.forEach(function(testCase) {
219+
qsWeirdObjects.forEach((testCase) => {
220220
check(qs.parse(testCase[1]), testCase[2], testCase[1]);
221221
});
222222

223-
qsNoMungeTestCases.forEach(function(testCase) {
223+
qsNoMungeTestCases.forEach((testCase) => {
224224
assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '='));
225225
});
226226

@@ -258,15 +258,15 @@ qsNoMungeTestCases.forEach(function(testCase) {
258258
// now test stringifying
259259

260260
// basic
261-
qsTestCases.forEach(function(testCase) {
261+
qsTestCases.forEach((testCase) => {
262262
assert.strictEqual(testCase[1], qs.stringify(testCase[2]));
263263
});
264264

265-
qsColonTestCases.forEach(function(testCase) {
265+
qsColonTestCases.forEach((testCase) => {
266266
assert.strictEqual(testCase[1], qs.stringify(testCase[2], ';', ':'));
267267
});
268268

269-
qsWeirdObjects.forEach(function(testCase) {
269+
qsWeirdObjects.forEach((testCase) => {
270270
assert.strictEqual(testCase[1], qs.stringify(testCase[0]));
271271
});
272272

@@ -300,7 +300,7 @@ assert.strictEqual('foo=', qs.stringify({ foo: Infinity }));
300300
assert.strictEqual(f, 'a=b&q=x%3Dy%26y%3Dz');
301301
}
302302

303-
assert.doesNotThrow(function() {
303+
assert.doesNotThrow(() => {
304304
qs.parse(undefined);
305305
});
306306

@@ -432,15 +432,15 @@ check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' });
432432
}
433433

434434
// Test QueryString.unescapeBuffer
435-
qsUnescapeTestCases.forEach(function(testCase) {
435+
qsUnescapeTestCases.forEach((testCase) => {
436436
assert.strictEqual(qs.unescape(testCase[0]), testCase[1]);
437437
assert.strictEqual(qs.unescapeBuffer(testCase[0]).toString(), testCase[1]);
438438
});
439439

440440
// test overriding .unescape
441441
{
442442
const prevUnescape = qs.unescape;
443-
qs.unescape = function(str) {
443+
qs.unescape = (str) => {
444444
return str.replace(/o/g, '_');
445445
};
446446
check(

test/parallel/test-writeint.js

+12-12
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,10 @@ function test8(clazz) {
4141
assert.strictEqual(0xfb, buffer[1]);
4242

4343
/* Make sure we handle truncation correctly */
44-
assert.throws(function() {
44+
assert.throws(() => {
4545
buffer.writeInt8(0xabc, 0);
4646
}, errorOutOfBounds);
47-
assert.throws(function() {
47+
assert.throws(() => {
4848
buffer.writeInt8(0xabc, 0);
4949
}, errorOutOfBounds);
5050

@@ -54,10 +54,10 @@ function test8(clazz) {
5454

5555
assert.strictEqual(0x7f, buffer[0]);
5656
assert.strictEqual(0x80, buffer[1]);
57-
assert.throws(function() {
57+
assert.throws(() => {
5858
buffer.writeInt8(0x7f + 1, 0);
5959
}, errorOutOfBounds);
60-
assert.throws(function() {
60+
assert.throws(() => {
6161
buffer.writeInt8(-0x80 - 1, 0);
6262
}, errorOutOfBounds);
6363
}
@@ -94,10 +94,10 @@ function test16(clazz) {
9494
assert.strictEqual(0xff, buffer[1]);
9595
assert.strictEqual(0x80, buffer[2]);
9696
assert.strictEqual(0x00, buffer[3]);
97-
assert.throws(function() {
97+
assert.throws(() => {
9898
buffer.writeInt16BE(0x7fff + 1, 0);
9999
}, errorOutOfBounds);
100-
assert.throws(function() {
100+
assert.throws(() => {
101101
buffer.writeInt16BE(-0x8000 - 1, 0);
102102
}, errorOutOfBounds);
103103

@@ -107,10 +107,10 @@ function test16(clazz) {
107107
assert.strictEqual(0x7f, buffer[1]);
108108
assert.strictEqual(0x00, buffer[2]);
109109
assert.strictEqual(0x80, buffer[3]);
110-
assert.throws(function() {
110+
assert.throws(() => {
111111
buffer.writeInt16LE(0x7fff + 1, 0);
112112
}, errorOutOfBounds);
113-
assert.throws(function() {
113+
assert.throws(() => {
114114
buffer.writeInt16LE(-0x8000 - 1, 0);
115115
}, errorOutOfBounds);
116116
}
@@ -163,10 +163,10 @@ function test32(clazz) {
163163
assert.strictEqual(0x00, buffer[5]);
164164
assert.strictEqual(0x00, buffer[6]);
165165
assert.strictEqual(0x00, buffer[7]);
166-
assert.throws(function() {
166+
assert.throws(() => {
167167
buffer.writeInt32BE(0x7fffffff + 1, 0);
168168
}, errorOutOfBounds);
169-
assert.throws(function() {
169+
assert.throws(() => {
170170
buffer.writeInt32BE(-0x80000000 - 1, 0);
171171
}, errorOutOfBounds);
172172

@@ -180,10 +180,10 @@ function test32(clazz) {
180180
assert.strictEqual(0x00, buffer[5]);
181181
assert.strictEqual(0x00, buffer[6]);
182182
assert.strictEqual(0x80, buffer[7]);
183-
assert.throws(function() {
183+
assert.throws(() => {
184184
buffer.writeInt32LE(0x7fffffff + 1, 0);
185185
}, errorOutOfBounds);
186-
assert.throws(function() {
186+
assert.throws(() => {
187187
buffer.writeInt32LE(-0x80000000 - 1, 0);
188188
}, errorOutOfBounds);
189189
}

test/parallel/test-zerolengthbufferbug.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,20 @@
44
const common = require('../common');
55
const http = require('http');
66

7-
const server = http.createServer(function(req, res) {
7+
const server = http.createServer((req, res) => {
88
const buffer = Buffer.alloc(0);
99
// FIXME: WTF gjslint want this?
1010
res.writeHead(200, { 'Content-Type': 'text/html',
1111
'Content-Length': buffer.length });
1212
res.end(buffer);
1313
});
1414

15-
server.listen(0, common.mustCall(function() {
16-
http.get({ port: this.address().port }, common.mustCall(function(res) {
15+
server.listen(0, common.mustCall(() => {
16+
http.get({ port: server.address().port }, common.mustCall((res) => {
1717

1818
res.on('data', common.mustNotCall());
1919

20-
res.on('end', function(d) {
20+
res.on('end', (d) => {
2121
server.close();
2222
});
2323
}));

0 commit comments

Comments
 (0)