Skip to content

Commit a27df5d

Browse files
Lekogibfahn
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 a6c7030 commit a27df5d

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');
@@ -473,7 +473,7 @@ assert.throws(() => {
473473
let threw = false;
474474
try {
475475
assert.throws(
476-
function() {
476+
() => {
477477
throw ({}); // eslint-disable-line no-throw-literal
478478
},
479479
Array
@@ -488,7 +488,7 @@ assert.throws(() => {
488488
a.throws(makeBlock(thrower, TypeError), /\[object Object\]/);
489489

490490
// use a fn to validate error object
491-
a.throws(makeBlock(thrower, TypeError), function(err) {
491+
a.throws(makeBlock(thrower, TypeError), (err) => {
492492
if ((err instanceof TypeError) && /\[object Object\]/.test(err)) {
493493
return true;
494494
}
@@ -591,7 +591,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity },
591591
let threw = false;
592592
try {
593593
// eslint-disable-next-line no-restricted-syntax
594-
assert.throws(function() {
594+
assert.throws(() => {
595595
assert.ifError(null);
596596
});
597597
} 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': '' }],
@@ -189,7 +189,7 @@ function check(actual, expected, input) {
189189
`Expected keys: ${inspect(expectedKeys)}`;
190190
}
191191
assert.deepStrictEqual(actualKeys, expectedKeys, msg);
192-
expectedKeys.forEach(function(key) {
192+
expectedKeys.forEach((key) => {
193193
if (typeof input === 'string') {
194194
msg = `Input: ${inspect(input)}\n` +
195195
`Key: ${inspect(key)}\n` +
@@ -203,21 +203,21 @@ function check(actual, expected, input) {
203203
}
204204

205205
// test that the canonical qs is parsed properly.
206-
qsTestCases.forEach(function(testCase) {
206+
qsTestCases.forEach((testCase) => {
207207
check(qs.parse(testCase[0]), testCase[2], testCase[0]);
208208
});
209209

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

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

220-
qsNoMungeTestCases.forEach(function(testCase) {
220+
qsNoMungeTestCases.forEach((testCase) => {
221221
assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '='));
222222
});
223223

@@ -255,15 +255,15 @@ qsNoMungeTestCases.forEach(function(testCase) {
255255
// now test stringifying
256256

257257
// basic
258-
qsTestCases.forEach(function(testCase) {
258+
qsTestCases.forEach((testCase) => {
259259
assert.strictEqual(testCase[1], qs.stringify(testCase[2]));
260260
});
261261

262-
qsColonTestCases.forEach(function(testCase) {
262+
qsColonTestCases.forEach((testCase) => {
263263
assert.strictEqual(testCase[1], qs.stringify(testCase[2], ';', ':'));
264264
});
265265

266-
qsWeirdObjects.forEach(function(testCase) {
266+
qsWeirdObjects.forEach((testCase) => {
267267
assert.strictEqual(testCase[1], qs.stringify(testCase[0]));
268268
});
269269

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

295-
assert.doesNotThrow(function() {
295+
assert.doesNotThrow(() => {
296296
qs.parse(undefined);
297297
});
298298

@@ -423,15 +423,15 @@ check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' });
423423
}
424424

425425
// Test QueryString.unescapeBuffer
426-
qsUnescapeTestCases.forEach(function(testCase) {
426+
qsUnescapeTestCases.forEach((testCase) => {
427427
assert.strictEqual(qs.unescape(testCase[0]), testCase[1]);
428428
assert.strictEqual(qs.unescapeBuffer(testCase[0]).toString(), testCase[1]);
429429
});
430430

431431
// test overriding .unescape
432432
{
433433
const prevUnescape = qs.unescape;
434-
qs.unescape = function(str) {
434+
qs.unescape = (str) => {
435435
return str.replace(/o/g, '_');
436436
};
437437
check(

test/parallel/test-writeint.js

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

3939
/* Make sure we handle truncation correctly */
40-
assert.throws(function() {
40+
assert.throws(() => {
4141
buffer.writeInt8(0xabc, 0);
4242
}, errorOutOfBounds);
43-
assert.throws(function() {
43+
assert.throws(() => {
4444
buffer.writeInt8(0xabc, 0);
4545
}, errorOutOfBounds);
4646

@@ -50,10 +50,10 @@ function test8(clazz) {
5050

5151
assert.strictEqual(0x7f, buffer[0]);
5252
assert.strictEqual(0x80, buffer[1]);
53-
assert.throws(function() {
53+
assert.throws(() => {
5454
buffer.writeInt8(0x7f + 1, 0);
5555
}, errorOutOfBounds);
56-
assert.throws(function() {
56+
assert.throws(() => {
5757
buffer.writeInt8(-0x80 - 1, 0);
5858
}, errorOutOfBounds);
5959
}
@@ -90,10 +90,10 @@ function test16(clazz) {
9090
assert.strictEqual(0xff, buffer[1]);
9191
assert.strictEqual(0x80, buffer[2]);
9292
assert.strictEqual(0x00, buffer[3]);
93-
assert.throws(function() {
93+
assert.throws(() => {
9494
buffer.writeInt16BE(0x7fff + 1, 0);
9595
}, errorOutOfBounds);
96-
assert.throws(function() {
96+
assert.throws(() => {
9797
buffer.writeInt16BE(-0x8000 - 1, 0);
9898
}, errorOutOfBounds);
9999

@@ -103,10 +103,10 @@ function test16(clazz) {
103103
assert.strictEqual(0x7f, buffer[1]);
104104
assert.strictEqual(0x00, buffer[2]);
105105
assert.strictEqual(0x80, buffer[3]);
106-
assert.throws(function() {
106+
assert.throws(() => {
107107
buffer.writeInt16LE(0x7fff + 1, 0);
108108
}, errorOutOfBounds);
109-
assert.throws(function() {
109+
assert.throws(() => {
110110
buffer.writeInt16LE(-0x8000 - 1, 0);
111111
}, errorOutOfBounds);
112112
}
@@ -159,10 +159,10 @@ function test32(clazz) {
159159
assert.strictEqual(0x00, buffer[5]);
160160
assert.strictEqual(0x00, buffer[6]);
161161
assert.strictEqual(0x00, buffer[7]);
162-
assert.throws(function() {
162+
assert.throws(() => {
163163
buffer.writeInt32BE(0x7fffffff + 1, 0);
164164
}, errorOutOfBounds);
165-
assert.throws(function() {
165+
assert.throws(() => {
166166
buffer.writeInt32BE(-0x80000000 - 1, 0);
167167
}, errorOutOfBounds);
168168

@@ -176,10 +176,10 @@ function test32(clazz) {
176176
assert.strictEqual(0x00, buffer[5]);
177177
assert.strictEqual(0x00, buffer[6]);
178178
assert.strictEqual(0x80, buffer[7]);
179-
assert.throws(function() {
179+
assert.throws(() => {
180180
buffer.writeInt32LE(0x7fffffff + 1, 0);
181181
}, errorOutOfBounds);
182-
assert.throws(function() {
182+
assert.throws(() => {
183183
buffer.writeInt32LE(-0x80000000 - 1, 0);
184184
}, errorOutOfBounds);
185185
}

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)