From 0c15703b181c81e9777c5cfe914b3a65e4a44b75 Mon Sep 17 00:00:00 2001 From: Leko Date: Mon, 27 Nov 2017 17:53:12 +0900 Subject: [PATCH] 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: https://github.com/nodejs/code-and-learn/issues/72#issuecomment-345667395 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: https://github.com/nodejs/node/pull/17345 Reviewed-By: Anna Henningsen Reviewed-By: Jon Moss Reviewed-By: Yosuke Furukawa --- test/parallel/test-assert.js | 18 +++++++------- ...in-top-level-error-handler-clears-stack.js | 6 ++--- test/parallel/test-querystring.js | 24 +++++++++---------- test/parallel/test-writeint.js | 24 +++++++++---------- test/parallel/test-zerolengthbufferbug.js | 8 +++---- 5 files changed, 40 insertions(+), 40 deletions(-) diff --git a/test/parallel/test-assert.js b/test/parallel/test-assert.js index eda477163cd9b5..87422e5ca0665b 100644 --- a/test/parallel/test-assert.js +++ b/test/parallel/test-assert.js @@ -26,8 +26,8 @@ const a = assert; function makeBlock(f) { const args = Array.prototype.slice.call(arguments, 1); - return function() { - return f.apply(this, args); + return () => { + return f.apply(null, args); }; } @@ -183,7 +183,7 @@ assert.doesNotThrow(makeBlock(a.deepEqual, a1, a2)); // having an identical prototype property const nbRoot = { - toString: function() { return `${this.first} ${this.last}`; } + toString() { return `${this.first} ${this.last}`; } }; function nameBuilder(first, last) { @@ -458,10 +458,10 @@ assert.throws(makeBlock(thrower, TypeError)); 'a.doesNotThrow is not catching type matching errors'); } -assert.throws(function() { assert.ifError(new Error('test error')); }, +assert.throws(() => { assert.ifError(new Error('test error')); }, /^Error: test error$/); -assert.doesNotThrow(function() { assert.ifError(null); }); -assert.doesNotThrow(function() { assert.ifError(); }); +assert.doesNotThrow(() => { assert.ifError(null); }); +assert.doesNotThrow(() => { assert.ifError(); }); assert.throws(() => { assert.doesNotThrow(makeBlock(thrower, Error), 'user message'); @@ -501,7 +501,7 @@ assert.throws(() => { let threw = false; try { assert.throws( - function() { + () => { throw ({}); // eslint-disable-line no-throw-literal }, Array @@ -516,7 +516,7 @@ assert.throws(() => { a.throws(makeBlock(thrower, TypeError), /\[object Object\]/); // use a fn to validate error object -a.throws(makeBlock(thrower, TypeError), function(err) { +a.throws(makeBlock(thrower, TypeError), (err) => { if ((err instanceof TypeError) && /\[object Object\]/.test(err)) { return true; } @@ -619,7 +619,7 @@ testAssertionMessage({ a: NaN, b: Infinity, c: -Infinity }, let threw = false; try { // eslint-disable-next-line no-restricted-syntax - assert.throws(function() { + assert.throws(() => { assert.ifError(null); }); } catch (e) { diff --git a/test/parallel/test-domain-top-level-error-handler-clears-stack.js b/test/parallel/test-domain-top-level-error-handler-clears-stack.js index f2095f09b7836c..05d5fca4671826 100644 --- a/test/parallel/test-domain-top-level-error-handler-clears-stack.js +++ b/test/parallel/test-domain-top-level-error-handler-clears-stack.js @@ -9,8 +9,8 @@ const domain = require('domain'); */ const d = domain.create(); -d.on('error', common.mustCall(function() { - process.nextTick(function() { +d.on('error', common.mustCall(() => { + process.nextTick(() => { // Scheduling a callback with process.nextTick will enter a _new_ domain, // and the callback will be called after the domain that handled the error // was exited. So there should be only one domain on the domains stack if @@ -29,6 +29,6 @@ d.on('error', common.mustCall(function() { }); })); -d.run(function() { +d.run(() => { throw new Error('Error from domain'); }); diff --git a/test/parallel/test-querystring.js b/test/parallel/test-querystring.js index 400431fd2cb132..76049584dbe458 100644 --- a/test/parallel/test-querystring.js +++ b/test/parallel/test-querystring.js @@ -129,7 +129,7 @@ const qsWeirdObjects = [ [{ regexp: /./g }, 'regexp=', { 'regexp': '' }], // eslint-disable-next-line no-unescaped-regexp-dot [{ regexp: new RegExp('.', 'g') }, 'regexp=', { 'regexp': '' }], - [{ fn: function() {} }, 'fn=', { 'fn': '' }], + [{ fn: () => {} }, 'fn=', { 'fn': '' }], [{ fn: new Function('') }, 'fn=', { 'fn': '' }], [{ math: Math }, 'math=', { 'math': '' }], [{ e: extendedFunction }, 'e=', { 'e': '' }], @@ -192,7 +192,7 @@ function check(actual, expected, input) { `Expected keys: ${inspect(expectedKeys)}`; } assert.deepStrictEqual(actualKeys, expectedKeys, msg); - expectedKeys.forEach(function(key) { + expectedKeys.forEach((key) => { if (typeof input === 'string') { msg = `Input: ${inspect(input)}\n` + `Key: ${inspect(key)}\n` + @@ -206,21 +206,21 @@ function check(actual, expected, input) { } // test that the canonical qs is parsed properly. -qsTestCases.forEach(function(testCase) { +qsTestCases.forEach((testCase) => { check(qs.parse(testCase[0]), testCase[2], testCase[0]); }); // test that the colon test cases can do the same -qsColonTestCases.forEach(function(testCase) { +qsColonTestCases.forEach((testCase) => { check(qs.parse(testCase[0], ';', ':'), testCase[2], testCase[0]); }); // test the weird objects, that they get parsed properly -qsWeirdObjects.forEach(function(testCase) { +qsWeirdObjects.forEach((testCase) => { check(qs.parse(testCase[1]), testCase[2], testCase[1]); }); -qsNoMungeTestCases.forEach(function(testCase) { +qsNoMungeTestCases.forEach((testCase) => { assert.deepStrictEqual(testCase[0], qs.stringify(testCase[1], '&', '=')); }); @@ -258,15 +258,15 @@ qsNoMungeTestCases.forEach(function(testCase) { // now test stringifying // basic -qsTestCases.forEach(function(testCase) { +qsTestCases.forEach((testCase) => { assert.strictEqual(testCase[1], qs.stringify(testCase[2])); }); -qsColonTestCases.forEach(function(testCase) { +qsColonTestCases.forEach((testCase) => { assert.strictEqual(testCase[1], qs.stringify(testCase[2], ';', ':')); }); -qsWeirdObjects.forEach(function(testCase) { +qsWeirdObjects.forEach((testCase) => { assert.strictEqual(testCase[1], qs.stringify(testCase[0])); }); @@ -300,7 +300,7 @@ assert.strictEqual('foo=', qs.stringify({ foo: Infinity })); assert.strictEqual(f, 'a=b&q=x%3Dy%26y%3Dz'); } -assert.doesNotThrow(function() { +assert.doesNotThrow(() => { qs.parse(undefined); }); @@ -432,7 +432,7 @@ check(qs.parse('%\u0100=%\u0101'), { '%Ā': '%ā' }); } // Test QueryString.unescapeBuffer -qsUnescapeTestCases.forEach(function(testCase) { +qsUnescapeTestCases.forEach((testCase) => { assert.strictEqual(qs.unescape(testCase[0]), testCase[1]); assert.strictEqual(qs.unescapeBuffer(testCase[0]).toString(), testCase[1]); }); @@ -440,7 +440,7 @@ qsUnescapeTestCases.forEach(function(testCase) { // test overriding .unescape { const prevUnescape = qs.unescape; - qs.unescape = function(str) { + qs.unescape = (str) => { return str.replace(/o/g, '_'); }; check( diff --git a/test/parallel/test-writeint.js b/test/parallel/test-writeint.js index 66dc13997e2785..1e0a8e8812ecb2 100644 --- a/test/parallel/test-writeint.js +++ b/test/parallel/test-writeint.js @@ -41,10 +41,10 @@ function test8(clazz) { assert.strictEqual(0xfb, buffer[1]); /* Make sure we handle truncation correctly */ - assert.throws(function() { + assert.throws(() => { buffer.writeInt8(0xabc, 0); }, errorOutOfBounds); - assert.throws(function() { + assert.throws(() => { buffer.writeInt8(0xabc, 0); }, errorOutOfBounds); @@ -54,10 +54,10 @@ function test8(clazz) { assert.strictEqual(0x7f, buffer[0]); assert.strictEqual(0x80, buffer[1]); - assert.throws(function() { + assert.throws(() => { buffer.writeInt8(0x7f + 1, 0); }, errorOutOfBounds); - assert.throws(function() { + assert.throws(() => { buffer.writeInt8(-0x80 - 1, 0); }, errorOutOfBounds); } @@ -94,10 +94,10 @@ function test16(clazz) { assert.strictEqual(0xff, buffer[1]); assert.strictEqual(0x80, buffer[2]); assert.strictEqual(0x00, buffer[3]); - assert.throws(function() { + assert.throws(() => { buffer.writeInt16BE(0x7fff + 1, 0); }, errorOutOfBounds); - assert.throws(function() { + assert.throws(() => { buffer.writeInt16BE(-0x8000 - 1, 0); }, errorOutOfBounds); @@ -107,10 +107,10 @@ function test16(clazz) { assert.strictEqual(0x7f, buffer[1]); assert.strictEqual(0x00, buffer[2]); assert.strictEqual(0x80, buffer[3]); - assert.throws(function() { + assert.throws(() => { buffer.writeInt16LE(0x7fff + 1, 0); }, errorOutOfBounds); - assert.throws(function() { + assert.throws(() => { buffer.writeInt16LE(-0x8000 - 1, 0); }, errorOutOfBounds); } @@ -163,10 +163,10 @@ function test32(clazz) { assert.strictEqual(0x00, buffer[5]); assert.strictEqual(0x00, buffer[6]); assert.strictEqual(0x00, buffer[7]); - assert.throws(function() { + assert.throws(() => { buffer.writeInt32BE(0x7fffffff + 1, 0); }, errorOutOfBounds); - assert.throws(function() { + assert.throws(() => { buffer.writeInt32BE(-0x80000000 - 1, 0); }, errorOutOfBounds); @@ -180,10 +180,10 @@ function test32(clazz) { assert.strictEqual(0x00, buffer[5]); assert.strictEqual(0x00, buffer[6]); assert.strictEqual(0x80, buffer[7]); - assert.throws(function() { + assert.throws(() => { buffer.writeInt32LE(0x7fffffff + 1, 0); }, errorOutOfBounds); - assert.throws(function() { + assert.throws(() => { buffer.writeInt32LE(-0x80000000 - 1, 0); }, errorOutOfBounds); } diff --git a/test/parallel/test-zerolengthbufferbug.js b/test/parallel/test-zerolengthbufferbug.js index 75aaa2f48dd8f3..0e1e976e568818 100644 --- a/test/parallel/test-zerolengthbufferbug.js +++ b/test/parallel/test-zerolengthbufferbug.js @@ -4,7 +4,7 @@ const common = require('../common'); const http = require('http'); -const server = http.createServer(function(req, res) { +const server = http.createServer((req, res) => { const buffer = Buffer.alloc(0); // FIXME: WTF gjslint want this? res.writeHead(200, { 'Content-Type': 'text/html', @@ -12,12 +12,12 @@ const server = http.createServer(function(req, res) { res.end(buffer); }); -server.listen(0, common.mustCall(function() { - http.get({ port: this.address().port }, common.mustCall(function(res) { +server.listen(0, common.mustCall(() => { + http.get({ port: server.address().port }, common.mustCall((res) => { res.on('data', common.mustNotCall()); - res.on('end', function(d) { + res.on('end', (d) => { server.close(); }); }));