Skip to content

Commit e30af7b

Browse files
Lxxyxtargos
authored andcommitted
fs: refactor to use optional chaining
PR-URL: #36524 Reviewed-By: Antoine du Hamel <duhamelantoine1995@gmail.com> Reviewed-By: James M Snell <jasnell@gmail.com> Reviewed-By: Trivikram Kamat <trivikr.dev@gmail.com>
1 parent e3c5adc commit e30af7b

File tree

5 files changed

+15
-15
lines changed

5 files changed

+15
-15
lines changed

lib/fs.js

+8-8
Original file line numberDiff line numberDiff line change
@@ -877,7 +877,7 @@ function rmdir(path, options, callback) {
877877
callback = makeCallback(callback);
878878
path = pathModule.toNamespacedPath(getValidatedPath(path));
879879

880-
if (options && options.recursive) {
880+
if (options?.recursive) {
881881
validateRmOptions(
882882
path,
883883
{ ...options, force: true },
@@ -901,7 +901,7 @@ function rmdir(path, options, callback) {
901901
function rmdirSync(path, options) {
902902
path = getValidatedPath(path);
903903

904-
if (options && options.recursive) {
904+
if (options?.recursive) {
905905
options = validateRmOptionsSync(path, { ...options, force: true }, true);
906906
lazyLoadRimraf();
907907
return rimrafSync(pathModule.toNamespacedPath(path), options);
@@ -1087,7 +1087,7 @@ function stat(path, options = { bigint: false }, callback) {
10871087
function hasNoEntryError(ctx) {
10881088
if (ctx.errno) {
10891089
const uvErr = uvErrmapGet(ctx.errno);
1090-
return uvErr && uvErr[0] === 'ENOENT';
1090+
return uvErr?.[0] === 'ENOENT';
10911091
}
10921092

10931093
if (ctx.error) {
@@ -1711,7 +1711,7 @@ function realpathSync(p, options) {
17111711
p = pathModule.resolve(p);
17121712

17131713
const cache = options[realpathCacheKey];
1714-
const maybeCachedResult = cache && cache.get(p);
1714+
const maybeCachedResult = cache?.get(p);
17151715
if (maybeCachedResult) {
17161716
return maybeCachedResult;
17171717
}
@@ -1760,7 +1760,7 @@ function realpathSync(p, options) {
17601760
}
17611761

17621762
// Continue if not a symlink, break if a pipe/socket
1763-
if (knownHard[base] || (cache && cache.get(base) === base)) {
1763+
if (knownHard[base] || cache?.get(base) === base) {
17641764
if (isFileType(statValues, S_IFIFO) ||
17651765
isFileType(statValues, S_IFSOCK)) {
17661766
break;
@@ -1769,7 +1769,7 @@ function realpathSync(p, options) {
17691769
}
17701770

17711771
let resolvedLink;
1772-
const maybeCachedResolved = cache && cache.get(base);
1772+
const maybeCachedResolved = cache?.get(base);
17731773
if (maybeCachedResolved) {
17741774
resolvedLink = maybeCachedResolved;
17751775
} else {
@@ -1783,7 +1783,7 @@ function realpathSync(p, options) {
17831783

17841784
if (!isFileType(stats, S_IFLNK)) {
17851785
knownHard[base] = true;
1786-
if (cache) cache.set(base, base);
1786+
cache?.set(base, base);
17871787
continue;
17881788
}
17891789

@@ -1828,7 +1828,7 @@ function realpathSync(p, options) {
18281828
}
18291829
}
18301830

1831-
if (cache) cache.set(original, p);
1831+
cache?.set(original, p);
18321832
return encodeRealpathResult(p, options);
18331833
}
18341834

lib/internal/fs/promises.js

+4-4
Original file line numberDiff line numberDiff line change
@@ -291,14 +291,14 @@ async function writeFileHandle(filehandle, data, signal) {
291291
}
292292

293293
async function readFileHandle(filehandle, options) {
294-
const signal = options && options.signal;
294+
const signal = options?.signal;
295295

296-
if (signal && signal.aborted) {
296+
if (signal?.aborted) {
297297
throw lazyDOMException('The operation was aborted', 'AbortError');
298298
}
299299
const statFields = await binding.fstat(filehandle.fd, false, kUsePromises);
300300

301-
if (signal && signal.aborted) {
301+
if (signal?.aborted) {
302302
throw lazyDOMException('The operation was aborted', 'AbortError');
303303
}
304304

@@ -318,7 +318,7 @@ async function readFileHandle(filehandle, options) {
318318
MathMin(size, kReadFileMaxChunkSize);
319319
let endOfFile = false;
320320
do {
321-
if (signal && signal.aborted) {
321+
if (signal?.aborted) {
322322
throw lazyDOMException('The operation was aborted', 'AbortError');
323323
}
324324
const buf = Buffer.alloc(chunkSize);

lib/internal/fs/read_file_context.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -94,7 +94,7 @@ class ReadFileContext {
9494
let offset;
9595
let length;
9696

97-
if (this.signal && this.signal.aborted) {
97+
if (this.signal?.aborted) {
9898
return this.close(
9999
lazyDOMException('The operation was aborted', 'AbortError')
100100
);

lib/internal/fs/rimraf.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -189,7 +189,7 @@ function rimrafSync(path, options) {
189189

190190
try {
191191
// SunOS lets the root user unlink directories.
192-
if (stats !== undefined && stats.isDirectory())
192+
if (stats?.isDirectory())
193193
_rmdirSync(path, options, null);
194194
else
195195
_unlinkSync(path, options);

lib/internal/fs/watchers.js

+1-1
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ StatWatcher.prototype[kFSStatWatcherAddOrCleanRef] = function(operate) {
137137
// Clean up all
138138
this[KFSStatWatcherMaxRefCount] = 0;
139139
this[KFSStatWatcherRefCount] = 0;
140-
this._handle && this._handle.unref();
140+
this._handle?.unref();
141141
}
142142
};
143143

0 commit comments

Comments
 (0)