From 537433435ee6c8aa0ffa5a97a9ff1e74a432f7c0 Mon Sep 17 00:00:00 2001 From: sagirk Date: Mon, 19 Nov 2018 15:39:20 +0530 Subject: [PATCH 1/3] test: refactor test-http-write-empty-string to use arrow functions In `test/parallel/test-http-write-empty-string.js`, callbacks use anonymous closure functions. It is safe to replace them with arrow functions since these callbacks don't contain references to `this`, `super` or `arguments`. This results in shorter functions. --- test/parallel/test-http-write-empty-string.js | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/test/parallel/test-http-write-empty-string.js b/test/parallel/test-http-write-empty-string.js index d4ab070667ef6d..590f331a4f277b 100644 --- a/test/parallel/test-http-write-empty-string.js +++ b/test/parallel/test-http-write-empty-string.js @@ -25,7 +25,7 @@ const assert = require('assert'); const http = require('http'); -const server = http.createServer(function(request, response) { +const server = http.createServer((request, response) => { console.log(`responding to ${request.url}`); response.writeHead(200, { 'Content-Type': 'text/plain' }); @@ -38,16 +38,16 @@ const server = http.createServer(function(request, response) { this.close(); }); -server.listen(0, common.mustCall(function() { - http.get({ port: this.address().port }, common.mustCall(function(res) { +server.listen(0, common.mustCall(() => { + http.get({ port: this.address().port }, common.mustCall((res) => { let response = ''; assert.strictEqual(res.statusCode, 200); res.setEncoding('ascii'); - res.on('data', function(chunk) { + res.on('data', (chunk) => { response += chunk; }); - res.on('end', common.mustCall(function() { + res.on('end', common.mustCall(() => { assert.strictEqual(response, '1\n2\n3\n'); })); })); From bffdf1d60a222a596f0dc95984ace0a677bd397a Mon Sep 17 00:00:00 2001 From: sagirk Date: Tue, 20 Nov 2018 09:19:42 +0530 Subject: [PATCH 2/3] test: replace `this` with the object that it refers to In `test/parallel/test-http-write-empty-string.js`, the callback passed to `http.createServer` contains a reference to `this`. Since the object referenced by `this` doesn't pre-exist, revert the arrow function to an anonymous closure function as a callback. Similarly, the callback passed to `server.listen` too contains a reference to `this`. However, in this case, `this` resolves to a pre-existing object `server`. Therefore, it is safe to use an arrow function as a callback as long as `this` is replaced with `server`. --- test/parallel/test-http-write-empty-string.js | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/test/parallel/test-http-write-empty-string.js b/test/parallel/test-http-write-empty-string.js index 590f331a4f277b..a004de9fc2f028 100644 --- a/test/parallel/test-http-write-empty-string.js +++ b/test/parallel/test-http-write-empty-string.js @@ -25,7 +25,7 @@ const assert = require('assert'); const http = require('http'); -const server = http.createServer((request, response) => { +const server = http.createServer(function (request, response) { console.log(`responding to ${request.url}`); response.writeHead(200, { 'Content-Type': 'text/plain' }); @@ -39,7 +39,7 @@ const server = http.createServer((request, response) => { }); server.listen(0, common.mustCall(() => { - http.get({ port: this.address().port }, common.mustCall((res) => { + http.get({ port: server.address().port }, common.mustCall((res) => { let response = ''; assert.strictEqual(res.statusCode, 200); From 8673f8022768fdc69f0069937dea0cd29fbe3d3a Mon Sep 17 00:00:00 2001 From: sagirk Date: Tue, 20 Nov 2018 09:42:47 +0530 Subject: [PATCH 3/3] test: remove space before function parentheses Fix ESLint complaint: "28:42 Unexpected space before function parentheses space-before-function-paren". --- test/parallel/test-http-write-empty-string.js | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/parallel/test-http-write-empty-string.js b/test/parallel/test-http-write-empty-string.js index a004de9fc2f028..88eff08f766699 100644 --- a/test/parallel/test-http-write-empty-string.js +++ b/test/parallel/test-http-write-empty-string.js @@ -25,7 +25,7 @@ const assert = require('assert'); const http = require('http'); -const server = http.createServer(function (request, response) { +const server = http.createServer(function(request, response) { console.log(`responding to ${request.url}`); response.writeHead(200, { 'Content-Type': 'text/plain' });