Skip to content

Commit d725865

Browse files
committed
fs: minor refactoring
1. Removed a few unnecessary variables to reduce LoC 2. Removed redundant `var` definitions of variables in same function 3. Refactored variables which are defined inside a block and used outside as well 4. Refactored effect-less code
1 parent 8eafbfe commit d725865

File tree

1 file changed

+24
-22
lines changed

1 file changed

+24
-22
lines changed

lib/fs.js

Lines changed: 24 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -266,27 +266,25 @@ function ReadFileContext(callback, encoding) {
266266
}
267267

268268
ReadFileContext.prototype.read = function() {
269-
var fd = this.fd;
270-
var size = this.size;
271269
var buffer;
272270
var offset;
273271
var length;
274272

275-
if (size === 0) {
273+
if (this.size === 0) {
276274
buffer = this.buffer = new SlowBuffer(kReadFileBufferLength);
277275
offset = 0;
278276
length = kReadFileBufferLength;
279277
} else {
280278
buffer = this.buffer;
281279
offset = this.pos;
282-
length = size - this.pos;
280+
length = this.size - this.pos;
283281
}
284282

285283
var req = new FSReqWrap();
286284
req.oncomplete = readFileAfterRead;
287285
req.context = this;
288286

289-
binding.read(fd, buffer, offset, length, -1, req);
287+
binding.read(this.fd, buffer, offset, length, -1, req);
290288
};
291289

292290
ReadFileContext.prototype.close = function(err) {
@@ -301,8 +299,7 @@ function readFileAfterOpen(err, fd) {
301299
var context = this.context;
302300

303301
if (err) {
304-
var callback = context.callback;
305-
callback(err);
302+
context.callback(err);
306303
return;
307304
}
308305

@@ -416,7 +413,7 @@ fs.readFileSync = function(path, options) {
416413
if (size === 0) {
417414
buffers = [];
418415
} else {
419-
var threw = true;
416+
threw = true;
420417
try {
421418
buffer = new Buffer(size);
422419
threw = false;
@@ -426,16 +423,18 @@ fs.readFileSync = function(path, options) {
426423
}
427424

428425
var done = false;
426+
var bytesRead;
427+
429428
while (!done) {
430-
var threw = true;
429+
threw = true;
431430
try {
432431
if (size !== 0) {
433-
var bytesRead = fs.readSync(fd, buffer, pos, size - pos);
432+
bytesRead = fs.readSync(fd, buffer, pos, size - pos);
434433
} else {
435434
// the kernel lies about many files.
436435
// Go ahead and try to read some bytes.
437436
buffer = new Buffer(8192);
438-
var bytesRead = fs.readSync(fd, buffer, 0, 8192);
437+
bytesRead = fs.readSync(fd, buffer, 0, 8192);
439438
if (bytesRead) {
440439
buffers.push(buffer.slice(0, bytesRead));
441440
}
@@ -584,10 +583,12 @@ fs.read = function(fd, buffer, offset, length, position, callback) {
584583

585584
fs.readSync = function(fd, buffer, offset, length, position) {
586585
var legacy = false;
586+
var encoding;
587+
587588
if (!(buffer instanceof Buffer)) {
588589
// legacy string interface (fd, length, position, encoding, callback)
589590
legacy = true;
590-
var encoding = arguments[3];
591+
encoding = arguments[3];
591592

592593
assertEncoding(encoding);
593594

@@ -622,14 +623,14 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
622623
callback(err, written || 0, buffer);
623624
}
624625

626+
var req = new FSReqWrap();
625627
if (buffer instanceof Buffer) {
626628
// if no position is passed then assume null
627629
if (typeof position === 'function') {
628630
callback = position;
629631
position = null;
630632
}
631633
callback = maybeCallback(callback);
632-
var req = new FSReqWrap();
633634
req.oncomplete = strWrapper;
634635
return binding.writeBuffer(fd, buffer, offset, length, position, req);
635636
}
@@ -646,7 +647,6 @@ fs.write = function(fd, buffer, offset, length, position, callback) {
646647
length = 'utf8';
647648
}
648649
callback = maybeCallback(position);
649-
var req = new FSReqWrap();
650650
req.oncomplete = bufWrapper;
651651
return binding.writeString(fd, buffer, offset, length, req);
652652
};
@@ -720,8 +720,10 @@ fs.truncateSync = function(path, len) {
720720
}
721721
// allow error to be thrown, but still close fd.
722722
var fd = fs.openSync(path, 'r+');
723+
var ret;
724+
723725
try {
724-
var ret = fs.ftruncateSync(fd, len);
726+
ret = fs.ftruncateSync(fd, len);
725727
} finally {
726728
fs.closeSync(fd);
727729
}
@@ -876,7 +878,7 @@ function preprocessSymlinkDestination(path, type, linkPath) {
876878

877879
fs.symlink = function(destination, path, type_, callback) {
878880
var type = (typeof type_ === 'string' ? type_ : null);
879-
var callback = makeCallback(arguments[arguments.length - 1]);
881+
callback = makeCallback(arguments[arguments.length - 1]);
880882

881883
if (!nullCheck(destination, callback)) return;
882884
if (!nullCheck(path, callback)) return;
@@ -967,9 +969,9 @@ if (constants.hasOwnProperty('O_SYMLINK')) {
967969

968970
// prefer to return the chmod error, if one occurs,
969971
// but still try to close, and report closing errors if they occur.
970-
var err, err2;
972+
var err, err2, ret;
971973
try {
972-
var ret = fs.fchmodSync(fd, mode);
974+
ret = fs.fchmodSync(fd, mode);
973975
} catch (er) {
974976
err = er;
975977
}
@@ -1112,7 +1114,7 @@ function writeAll(fd, buffer, offset, length, position, callback) {
11121114
}
11131115

11141116
fs.writeFile = function(path, data, options, callback) {
1115-
var callback = maybeCallback(arguments[arguments.length - 1]);
1117+
callback = maybeCallback(arguments[arguments.length - 1]);
11161118

11171119
if (!options || typeof options === 'function') {
11181120
options = { encoding: 'utf8', mode: 0o666, flag: 'w' };
@@ -1625,8 +1627,8 @@ function ReadStream(path, options) {
16251627
this.flags = options.flags === undefined ? 'r' : options.flags;
16261628
this.mode = options.mode === undefined ? 0o666 : options.mode;
16271629

1628-
this.start = options.start === undefined ? undefined : options.start;
1629-
this.end = options.end === undefined ? undefined : options.end;
1630+
this.start = options.start;
1631+
this.end = options.end;
16301632
this.autoClose = options.autoClose === undefined ? true : options.autoClose;
16311633
this.pos = undefined;
16321634

@@ -1788,7 +1790,7 @@ function WriteStream(path, options) {
17881790
this.flags = options.flags === undefined ? 'w' : options.flags;
17891791
this.mode = options.mode === undefined ? 0o666 : options.mode;
17901792

1791-
this.start = options.start === undefined ? undefined : options.start;
1793+
this.start = options.start;
17921794
this.pos = undefined;
17931795
this.bytesWritten = 0;
17941796

0 commit comments

Comments
 (0)