Skip to content

Commit 7e6a956

Browse files
TrottMylesBorins
authored andcommitted
test,lib,doc: use function declarations
Replace function expressions with function declarations in preparation for a lint rule requiring function declarations. PR-URL: #12711 Backport-PR-URL: #13774 Reviewed-By: Vse Mozhet Byt <[email protected]> Reviewed-By: Gibson Fahnestock <[email protected]>
1 parent 6afa5fe commit 7e6a956

39 files changed

+130
-133
lines changed

doc/api/util.md

+1-1
Original file line numberDiff line numberDiff line change
@@ -557,7 +557,7 @@ Returns `true` if the given `object` is a `Function`. Otherwise, returns
557557
const util = require('util');
558558

559559
function Foo() {}
560-
const Bar = function() {};
560+
const Bar = () => {};
561561

562562
util.isFunction({});
563563
// Returns: false

lib/_http_client.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -606,7 +606,7 @@ ClientRequest.prototype._deferToConnect = function(method, arguments_, cb) {
606606
cb();
607607
}
608608

609-
var onSocket = function() {
609+
var onSocket = () => {
610610
if (self.socket.writable) {
611611
callSocketMethod();
612612
} else {

lib/_tls_legacy.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -425,9 +425,9 @@ CryptoStream.prototype.destroySoon = function(err) {
425425
// was written on this side was read from the other side.
426426
var self = this;
427427
var waiting = 1;
428-
var finish = function() {
428+
function finish() {
429429
if (--waiting === 0) self.destroy();
430-
};
430+
}
431431
this._opposite.once('end', finish);
432432
if (!this._finished) {
433433
this.once('finish', finish);

lib/crypto.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -581,11 +581,11 @@ function pbkdf2(password, salt, iterations, keylen, digest, callback) {
581581
// at this point, we need to handle encodings.
582582
var encoding = exports.DEFAULT_ENCODING;
583583
if (callback) {
584-
var next = function(er, ret) {
584+
function next(er, ret) {
585585
if (ret)
586586
ret = ret.toString(encoding);
587587
callback(er, ret);
588-
};
588+
}
589589
binding.PBKDF2(password, salt, iterations, keylen, digest, next);
590590
} else {
591591
var ret = binding.PBKDF2(password, salt, iterations, keylen, digest);

test/addons/make-callback/test.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,9 +57,9 @@ const forward = vm.runInNewContext(`
5757
})
5858
`);
5959
// Runs in outer context.
60-
const endpoint = function($Object) {
60+
function endpoint($Object) {
6161
if (Object === $Object)
6262
throw new Error('bad');
6363
return Object;
64-
};
64+
}
6565
assert.strictEqual(Object, makeCallback(process, forward, endpoint));

test/inspector/inspector-helper.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -120,7 +120,7 @@ function timeout(message, multiplicator) {
120120
TIMEOUT * (multiplicator || 1));
121121
}
122122

123-
const TestSession = function(socket, harness) {
123+
function TestSession(socket, harness) {
124124
this.mainScriptPath = harness.mainScriptPath;
125125
this.mainScriptId = null;
126126

@@ -145,7 +145,7 @@ const TestSession = function(socket, harness) {
145145
buffer = buffer.slice(consumed);
146146
} while (consumed);
147147
}).on('close', () => assert(this.expectClose_, 'Socket closed prematurely'));
148-
};
148+
}
149149

150150
TestSession.prototype.scriptUrlForId = function(id) {
151151
return this.scripts_[id];
@@ -302,7 +302,7 @@ TestSession.prototype.testHttpResponse = function(path, check) {
302302
};
303303

304304

305-
const Harness = function(port, childProcess) {
305+
function Harness(port, childProcess) {
306306
this.port = port;
307307
this.mainScriptPath = mainScript;
308308
this.stderrFilters_ = [];
@@ -327,7 +327,7 @@ const Harness = function(port, childProcess) {
327327
this.returnCode_ = code;
328328
this.running_ = false;
329329
});
330-
};
330+
}
331331

332332
Harness.prototype.addStderrFilter = function(regexp, callback) {
333333
this.stderrFilters_.push((message) => {

test/parallel/test-assert.js

+2-1
Original file line numberDiff line numberDiff line change
@@ -428,7 +428,7 @@ try {
428428

429429
AnotherErrorType = class extends Error {};
430430

431-
const functionThatThrows = function() {
431+
const functionThatThrows = () => {
432432
throw new AnotherErrorType('foo');
433433
};
434434

@@ -471,6 +471,7 @@ a.throws(makeBlock(a.deepEqual, args, []));
471471

472472
// more checking that arguments objects are handled correctly
473473
{
474+
// eslint-disable-next-line func-style
474475
const returnArguments = function() { return arguments; };
475476

476477
const someArgs = returnArguments('a');

test/parallel/test-child-process-fork-dgram.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ if (process.argv[2] === 'child') {
5757
});
5858
});
5959

60-
const sendMessages = function() {
60+
function sendMessages() {
6161
const serverPort = parentServer.address().port;
6262

6363
const timer = setInterval(function() {
@@ -81,7 +81,7 @@ if (process.argv[2] === 'child') {
8181
);
8282
}
8383
}, 1);
84-
};
84+
}
8585

8686
parentServer.bind(0, '127.0.0.1');
8787

test/parallel/test-child-process-fork-net.js

+6-6
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ if (process.argv[2] === 'child') {
6666
}));
6767

6868
// send net.Server to child and test by connecting
69-
const testServer = function(callback) {
69+
function testServer(callback) {
7070

7171
// destroy server execute callback when done
7272
const progress = new ProgressTracker(2, function() {
@@ -95,7 +95,7 @@ if (process.argv[2] === 'child') {
9595
server.listen(0);
9696

9797
// handle client messages
98-
const messageHandlers = function(msg) {
98+
function messageHandlers(msg) {
9999

100100
if (msg.what === 'listening') {
101101
// make connections
@@ -117,13 +117,13 @@ if (process.argv[2] === 'child') {
117117
child.removeListener('message', messageHandlers);
118118
callback();
119119
}
120-
};
120+
}
121121

122122
child.on('message', messageHandlers);
123-
};
123+
}
124124

125125
// send net.Socket to child
126-
const testSocket = function(callback) {
126+
function testSocket(callback) {
127127

128128
// create a new server and connect to it,
129129
// but the socket will be handled by the child
@@ -158,7 +158,7 @@ if (process.argv[2] === 'child') {
158158
server.close();
159159
});
160160
});
161-
};
161+
}
162162

163163
// create server and send it to child
164164
let serverSuccess = false;

test/parallel/test-child-process-fork-net2.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ if (process.argv[2] === 'child') {
123123

124124
server.listen(0, '127.0.0.1');
125125

126-
let closeServer = function() {
126+
function closeServer() {
127127
server.close();
128128

129129
setTimeout(function() {
@@ -132,7 +132,7 @@ if (process.argv[2] === 'child') {
132132
child2.send('close');
133133
child3.disconnect();
134134
}, 200);
135-
};
135+
}
136136

137137
process.on('exit', function() {
138138
assert.strictEqual(disconnected, count);

test/parallel/test-child-process-spawn-typeerror.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ assert.throws(function() {
5757
// Argument types for combinatorics
5858
const a = [];
5959
const o = {};
60-
const c = function c() {};
60+
function c() {}
6161
const s = 'string';
6262
const u = undefined;
6363
const n = null;

test/parallel/test-cluster-disconnect.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ if (cluster.isWorker) {
1717
const servers = 2;
1818

1919
// test a single TCP server
20-
const testConnection = function(port, cb) {
20+
const testConnection = (port, cb) => {
2121
const socket = net.connect(port, '127.0.0.1', () => {
2222
// buffer result
2323
let result = '';
@@ -31,7 +31,7 @@ if (cluster.isWorker) {
3131
};
3232

3333
// test both servers created in the cluster
34-
const testCluster = function(cb) {
34+
const testCluster = (cb) => {
3535
let done = 0;
3636

3737
for (let i = 0; i < servers; i++) {
@@ -46,7 +46,7 @@ if (cluster.isWorker) {
4646
};
4747

4848
// start two workers and execute callback when both is listening
49-
const startCluster = function(cb) {
49+
const startCluster = (cb) => {
5050
const workers = 8;
5151
let online = 0;
5252

@@ -60,7 +60,7 @@ if (cluster.isWorker) {
6060
}
6161
};
6262

63-
const test = function(again) {
63+
const test = (again) => {
6464
//1. start cluster
6565
startCluster(common.mustCall(() => {
6666
//2. test cluster

test/parallel/test-cluster-master-error.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,7 @@ if (cluster.isWorker) {
8080
// Check that the cluster died accidentally (non-zero exit code)
8181
masterExited = !!code;
8282

83-
const pollWorkers = function() {
83+
const pollWorkers = () => {
8484
// When master is dead all workers should be dead too
8585
let alive = false;
8686
workers.forEach((pid) => alive = common.isAlive(pid));

test/parallel/test-cluster-master-kill.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -47,7 +47,7 @@ if (cluster.isWorker) {
4747
assert.strictEqual(code, 0);
4848

4949
// check worker process status
50-
const pollWorker = function() {
50+
const pollWorker = () => {
5151
alive = common.isAlive(pid);
5252
if (alive) {
5353
setTimeout(pollWorker, 50);

test/parallel/test-cluster-message.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ if (cluster.isWorker) {
5959

6060

6161
let client;
62-
const check = function(type, result) {
62+
const check = (type, result) => {
6363
checks[type].receive = true;
6464
checks[type].correct = result;
6565
console.error('check', checks);

test/parallel/test-cluster-worker-exit.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ if (cluster.isWorker) {
8282
}
8383
}));
8484

85-
const finish_test = function() {
85+
const finish_test = () => {
8686
try {
8787
checkResults(expected_results, results);
8888
} catch (exc) {

test/parallel/test-console-not-call-toString.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
require('../common');
33
const assert = require('assert');
44

5-
const func = function() {};
5+
function func() {}
66
let toStringCalled = false;
77
func.toString = function() {
88
toStringCalled = true;

test/parallel/test-debugger-pid.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ const interfacer = spawn(process.execPath, ['debug', '-p', '655555']);
1111
console.error(process.execPath, 'debug', '-p', '655555');
1212
interfacer.stdout.setEncoding('utf-8');
1313
interfacer.stderr.setEncoding('utf-8');
14-
const onData = function(data) {
14+
const onData = (data) => {
1515
data = (buffer + data).split('\n');
1616
buffer = data.pop();
1717
data.forEach(function(line) {

test/parallel/test-event-emitter-add-listeners.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,8 @@ const EventEmitter = require('events');
4747
}
4848

4949
{
50-
const listen1 = function listen1() {};
51-
const listen2 = function listen2() {};
50+
const listen1 = () => {};
51+
const listen2 = () => {};
5252
const ee = new EventEmitter();
5353

5454
ee.once('newListener', function() {

test/parallel/test-event-emitter-once.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ e.emit('hello', 'a', 'b');
1212
e.emit('hello', 'a', 'b');
1313
e.emit('hello', 'a', 'b');
1414

15-
const remove = function() {
15+
function remove() {
1616
common.fail('once->foo should not be emitted');
17-
};
17+
}
1818

1919
e.once('foo', remove);
2020
e.removeListener('foo', remove);

test/parallel/test-fs-access.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -7,19 +7,19 @@ const doesNotExist = path.join(common.tmpDir, '__this_should_not_exist');
77
const readOnlyFile = path.join(common.tmpDir, 'read_only_file');
88
const readWriteFile = path.join(common.tmpDir, 'read_write_file');
99

10-
const removeFile = function(file) {
10+
const removeFile = (file) => {
1111
try {
1212
fs.unlinkSync(file);
1313
} catch (err) {
1414
// Ignore error
1515
}
1616
};
1717

18-
const createFileWithPerms = function(file, mode) {
18+
function createFileWithPerms(file, mode) {
1919
removeFile(file);
2020
fs.writeFileSync(file, '');
2121
fs.chmodSync(file, mode);
22-
};
22+
}
2323

2424
common.refreshTmpDir();
2525
createFileWithPerms(readOnlyFile, 0o444);

test/parallel/test-fs-link.js

+3-3
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@ const srcPath = path.join(common.tmpDir, 'hardlink-target.txt');
1111
const dstPath = path.join(common.tmpDir, 'link1.js');
1212
fs.writeFileSync(srcPath, 'hello world');
1313

14-
const callback = function(err) {
15-
if (err) throw err;
14+
function callback(err) {
15+
assert.ifError(err);
1616
const dstContent = fs.readFileSync(dstPath, 'utf8');
1717
assert.strictEqual('hello world', dstContent);
18-
};
18+
}
1919

2020
fs.link(srcPath, dstPath, common.mustCall(callback));
2121

test/parallel/test-fs-read-stream-fd-leak.js

+2-2
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ function testLeak(endFn, callback) {
2929
let i = 0;
3030
let check = 0;
3131

32-
const checkFunction = function() {
32+
function checkFunction() {
3333
if (openCount !== 0 && check < totalCheck) {
3434
check++;
3535
setTimeout(checkFunction, 100);
@@ -44,7 +44,7 @@ function testLeak(endFn, callback) {
4444

4545
openCount = 0;
4646
callback && setTimeout(callback, 100);
47-
};
47+
}
4848

4949
setInterval(function() {
5050
const s = fs.createReadStream(emptyTxt);

0 commit comments

Comments
 (0)