Skip to content

Commit 63b8750

Browse files
robhoganfacebook-github-bot
authored andcommitted
Use arrow functions, avoid self = this, var => const/let
Summary: Modernises this file a bit further by: - Preferring `const` (where possible) or `let` over `var`. - Preferring arrow functions (which don't have a `this` binding) in callbacks and utilities to obviate the need to capture the class instance as `self`. Reviewed By: huntie Differential Revision: D66600171 fbshipit-source-id: 0cb24c1918eaf25802d5acb1d7a4238cd38e2418
1 parent b987a81 commit 63b8750

File tree

1 file changed

+69
-75
lines changed

1 file changed

+69
-75
lines changed

watchman/node/index.js

Lines changed: 69 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -9,24 +9,23 @@
99

1010
'use strict';
1111

12-
var net = require('net');
13-
var EE = require('events').EventEmitter;
14-
var childProcess = require('child_process');
15-
var bser = require('bser');
12+
const bser = require('bser');
13+
const childProcess = require('child_process');
14+
const {EventEmitter} = require('events');
15+
const net = require('net');
1616

1717
// We'll emit the responses to these when they get sent down to us
18-
var unilateralTags = ['subscription', 'log'];
18+
const unilateralTags = ['subscription', 'log'];
1919

2020
/**
2121
* @param options An object with the following optional keys:
2222
* * 'watchmanBinaryPath' (string) Absolute path to the watchman binary.
2323
* If not provided, the Client locates the binary using the PATH specified
2424
* by the node child_process's default env.
2525
*/
26-
class Client extends EE {
26+
class Client extends EventEmitter {
2727
constructor(options) {
2828
super();
29-
const self = this;
3029

3130
this.watchmanBinaryPath = 'watchman';
3231
if (options && options.watchmanBinaryPath) {
@@ -52,11 +51,11 @@ class Client extends EE {
5251
}
5352

5453
cancelCommands(why) {
55-
var error = new Error(why);
54+
const error = new Error(why);
5655

5756
// Steal all pending commands before we start cancellation, in
5857
// case something decides to schedule more commands
59-
var cmds = this.commands;
58+
const cmds = this.commands;
6059
this.commands = [];
6160

6261
if (this.currentCommand) {
@@ -65,37 +64,35 @@ class Client extends EE {
6564
}
6665

6766
// Synthesize an error condition for any commands that were queued
68-
cmds.forEach(function (cmd) {
67+
cmds.forEach(cmd => {
6968
cmd.cb(error);
7069
});
7170
}
7271

7372
connect() {
74-
var self = this;
75-
76-
function makeSock(sockname) {
73+
const makeSock = sockname => {
7774
// bunser will decode the watchman BSER protocol for us
78-
self.bunser = new bser.BunserBuf();
75+
this.bunser = new bser.BunserBuf();
7976
// For each decoded line:
80-
self.bunser.on('value', function (obj) {
77+
this.bunser.on('value', obj => {
8178
// Figure out if this is a unliteral response or if it is the
8279
// response portion of a request-response sequence. At the time
8380
// of writing, there are only two possible unilateral responses.
84-
var unilateral = false;
85-
for (var i = 0; i < unilateralTags.length; i++) {
86-
var tag = unilateralTags[i];
81+
let unilateral = false;
82+
for (let i = 0; i < unilateralTags.length; i++) {
83+
const tag = unilateralTags[i];
8784
if (tag in obj) {
8885
unilateral = tag;
8986
}
9087
}
9188

9289
if (unilateral) {
93-
self.emit(unilateral, obj);
94-
} else if (self.currentCommand) {
95-
var cmd = self.currentCommand;
96-
self.currentCommand = null;
90+
this.emit(unilateral, obj);
91+
} else if (this.currentCommand) {
92+
const cmd = this.currentCommand;
93+
this.currentCommand = null;
9794
if ('error' in obj) {
98-
var error = new Error(obj.error);
95+
const error = new Error(obj.error);
9996
error.watchmanResponse = obj;
10097
cmd.cb(error);
10198
} else {
@@ -104,34 +101,34 @@ class Client extends EE {
104101
}
105102

106103
// See if we can dispatch the next queued command, if any
107-
self.sendNextCommand();
104+
this.sendNextCommand();
108105
});
109-
self.bunser.on('error', function (err) {
110-
self.emit('error', err);
106+
this.bunser.on('error', err => {
107+
this.emit('error', err);
111108
});
112109

113-
self.socket = net.createConnection(sockname);
114-
self.socket.on('connect', function () {
115-
self.connecting = false;
116-
self.emit('connect');
117-
self.sendNextCommand();
110+
this.socket = net.createConnection(sockname);
111+
this.socket.on('connect', () => {
112+
this.connecting = false;
113+
this.emit('connect');
114+
this.sendNextCommand();
118115
});
119-
self.socket.on('error', function (err) {
120-
self.connecting = false;
121-
self.emit('error', err);
116+
this.socket.on('error', err => {
117+
this.connecting = false;
118+
this.emit('error', err);
122119
});
123-
self.socket.on('data', function (buf) {
124-
if (self.bunser) {
125-
self.bunser.append(buf);
120+
this.socket.on('data', buf => {
121+
if (this.bunser) {
122+
this.bunser.append(buf);
126123
}
127124
});
128-
self.socket.on('end', function () {
129-
self.socket = null;
130-
self.bunser = null;
131-
self.cancelCommands('The watchman connection was closed');
132-
self.emit('end');
125+
this.socket.on('end', () => {
126+
this.socket = null;
127+
this.bunser = null;
128+
this.cancelCommands('The watchman connection was closed');
129+
this.emit('end');
133130
});
134-
}
131+
};
135132

136133
// triggers will export the sock path to the environment.
137134
// If we're invoked in such a way, we can simply pick up the
@@ -145,16 +142,16 @@ class Client extends EE {
145142
// We need to ask the client binary where to find it.
146143
// This will cause the service to start for us if it isn't
147144
// already running.
148-
var args = ['--no-pretty', 'get-sockname'];
145+
const args = ['--no-pretty', 'get-sockname'];
149146

150147
// We use the more elaborate spawn rather than exec because there
151148
// are some error cases on Windows where process spawning can hang.
152149
// It is desirable to pipe stderr directly to stderr live so that
153150
// we can discover the problem.
154-
var proc = null;
155-
var spawnFailed = false;
151+
let proc = null;
152+
let spawnFailed = false;
156153

157-
function spawnError(error) {
154+
const spawnError = error => {
158155
if (spawnFailed) {
159156
// For ENOENT, proc 'close' will also trigger with a negative code,
160157
// let's suppress that second error.
@@ -172,8 +169,8 @@ class Client extends EE {
172169
'for installation instructions';
173170
}
174171
console.error('Watchman: ', error.message);
175-
self.emit('error', error);
176-
}
172+
this.emit('error', error);
173+
};
177174

178175
try {
179176
proc = childProcess.spawn(this.watchmanBinaryPath, args, {
@@ -185,25 +182,25 @@ class Client extends EE {
185182
return;
186183
}
187184

188-
var stdout = [];
189-
var stderr = [];
190-
proc.stdout.on('data', function (data) {
185+
const stdout = [];
186+
const stderr = [];
187+
proc.stdout.on('data', data => {
191188
stdout.push(data);
192189
});
193-
proc.stderr.on('data', function (data) {
190+
proc.stderr.on('data', data => {
194191
data = data.toString('utf8');
195192
stderr.push(data);
196193
console.error(data);
197194
});
198-
proc.on('error', function (error) {
195+
proc.on('error', error => {
199196
spawnError(error);
200197
});
201198

202-
proc.on('close', function (code, signal) {
199+
proc.on('close', (code, signal) => {
203200
if (code !== 0) {
204201
spawnError(
205202
new Error(
206-
self.watchmanBinaryPath +
203+
this.watchmanBinaryPath +
207204
' ' +
208205
args.join(' ') +
209206
' returned with exit code=' +
@@ -217,23 +214,21 @@ class Client extends EE {
217214
return;
218215
}
219216
try {
220-
var obj = JSON.parse(stdout.join(''));
217+
const obj = JSON.parse(stdout.join(''));
221218
if ('error' in obj) {
222-
var error = new Error(obj.error);
219+
const error = new Error(obj.error);
223220
error.watchmanResponse = obj;
224-
self.emit('error', error);
221+
this.emit('error', error);
225222
return;
226223
}
227224
makeSock(obj.sockname);
228225
} catch (e) {
229-
self.emit('error', e);
226+
this.emit('error', e);
230227
}
231228
});
232229
}
233230

234-
command(args, done) {
235-
done = done || function () {};
236-
231+
command(args, done = () => {}) {
237232
// Queue up the command
238233
this.commands.push({cmd: args, cb: done});
239234

@@ -254,12 +249,12 @@ class Client extends EE {
254249
// This is a helper that we expose for testing purposes
255250
_synthesizeCapabilityCheck(resp, optional, required) {
256251
resp.capabilities = {};
257-
var version = resp.version;
258-
optional.forEach(function (name) {
252+
const version = resp.version;
253+
optional.forEach(name => {
259254
resp.capabilities[name] = have_cap(version, name);
260255
});
261-
required.forEach(function (name) {
262-
var have = have_cap(version, name);
256+
required.forEach(name => {
257+
const have = have_cap(version, name);
263258
resp.capabilities[name] = have;
264259
if (!have) {
265260
resp.error =
@@ -272,9 +267,8 @@ class Client extends EE {
272267
}
273268

274269
capabilityCheck(caps, done) {
275-
var optional = caps.optional || [];
276-
var required = caps.required || [];
277-
var self = this;
270+
const optional = caps.optional || [];
271+
const required = caps.required || [];
278272
this.command(
279273
[
280274
'version',
@@ -283,15 +277,15 @@ class Client extends EE {
283277
required: required,
284278
},
285279
],
286-
function (error, resp) {
280+
(error, resp) => {
287281
if (error) {
288282
done(error);
289283
return;
290284
}
291285
if (!('capabilities' in resp)) {
292286
// Server doesn't support capabilities, so we need to
293287
// synthesize the results based on the version
294-
resp = self._synthesizeCapabilityCheck(resp, optional, required);
288+
resp = this._synthesizeCapabilityCheck(resp, optional, required);
295289
if (resp.error) {
296290
error = new Error(resp.error);
297291
error.watchmanResponse = resp;
@@ -315,7 +309,7 @@ class Client extends EE {
315309
}
316310
}
317311

318-
var cap_versions = {
312+
const cap_versions = {
319313
'cmd-watch-del-all': '3.1.1',
320314
'cmd-watch-project': '3.1',
321315
relative_root: '3.3',
@@ -328,8 +322,8 @@ var cap_versions = {
328322
function vers_compare(a, b) {
329323
a = a.split('.');
330324
b = b.split('.');
331-
for (var i = 0; i < 3; i++) {
332-
var d = parseInt(a[i] || '0') - parseInt(b[i] || '0');
325+
for (let i = 0; i < 3; i++) {
326+
const d = parseInt(a[i] || '0') - parseInt(b[i] || '0');
333327
if (d != 0) {
334328
return d;
335329
}

0 commit comments

Comments
 (0)