From e30af7be3387a9709d8871f3785ddd10db54ea10 Mon Sep 17 00:00:00 2001 From: ZiJian Liu Date: Tue, 15 Dec 2020 19:24:10 +0800 Subject: [PATCH] fs: refactor to use optional chaining PR-URL: https://github.com/nodejs/node/pull/36524 Reviewed-By: Antoine du Hamel Reviewed-By: James M Snell Reviewed-By: Trivikram Kamat --- lib/fs.js | 16 ++++++++-------- lib/internal/fs/promises.js | 8 ++++---- lib/internal/fs/read_file_context.js | 2 +- lib/internal/fs/rimraf.js | 2 +- lib/internal/fs/watchers.js | 2 +- 5 files changed, 15 insertions(+), 15 deletions(-) diff --git a/lib/fs.js b/lib/fs.js index 39f35371393f4a..93e875311e08a6 100644 --- a/lib/fs.js +++ b/lib/fs.js @@ -877,7 +877,7 @@ function rmdir(path, options, callback) { callback = makeCallback(callback); path = pathModule.toNamespacedPath(getValidatedPath(path)); - if (options && options.recursive) { + if (options?.recursive) { validateRmOptions( path, { ...options, force: true }, @@ -901,7 +901,7 @@ function rmdir(path, options, callback) { function rmdirSync(path, options) { path = getValidatedPath(path); - if (options && options.recursive) { + if (options?.recursive) { options = validateRmOptionsSync(path, { ...options, force: true }, true); lazyLoadRimraf(); return rimrafSync(pathModule.toNamespacedPath(path), options); @@ -1087,7 +1087,7 @@ function stat(path, options = { bigint: false }, callback) { function hasNoEntryError(ctx) { if (ctx.errno) { const uvErr = uvErrmapGet(ctx.errno); - return uvErr && uvErr[0] === 'ENOENT'; + return uvErr?.[0] === 'ENOENT'; } if (ctx.error) { @@ -1711,7 +1711,7 @@ function realpathSync(p, options) { p = pathModule.resolve(p); const cache = options[realpathCacheKey]; - const maybeCachedResult = cache && cache.get(p); + const maybeCachedResult = cache?.get(p); if (maybeCachedResult) { return maybeCachedResult; } @@ -1760,7 +1760,7 @@ function realpathSync(p, options) { } // Continue if not a symlink, break if a pipe/socket - if (knownHard[base] || (cache && cache.get(base) === base)) { + if (knownHard[base] || cache?.get(base) === base) { if (isFileType(statValues, S_IFIFO) || isFileType(statValues, S_IFSOCK)) { break; @@ -1769,7 +1769,7 @@ function realpathSync(p, options) { } let resolvedLink; - const maybeCachedResolved = cache && cache.get(base); + const maybeCachedResolved = cache?.get(base); if (maybeCachedResolved) { resolvedLink = maybeCachedResolved; } else { @@ -1783,7 +1783,7 @@ function realpathSync(p, options) { if (!isFileType(stats, S_IFLNK)) { knownHard[base] = true; - if (cache) cache.set(base, base); + cache?.set(base, base); continue; } @@ -1828,7 +1828,7 @@ function realpathSync(p, options) { } } - if (cache) cache.set(original, p); + cache?.set(original, p); return encodeRealpathResult(p, options); } diff --git a/lib/internal/fs/promises.js b/lib/internal/fs/promises.js index 2f4856e04427c4..885df198a0629b 100644 --- a/lib/internal/fs/promises.js +++ b/lib/internal/fs/promises.js @@ -291,14 +291,14 @@ async function writeFileHandle(filehandle, data, signal) { } async function readFileHandle(filehandle, options) { - const signal = options && options.signal; + const signal = options?.signal; - if (signal && signal.aborted) { + if (signal?.aborted) { throw lazyDOMException('The operation was aborted', 'AbortError'); } const statFields = await binding.fstat(filehandle.fd, false, kUsePromises); - if (signal && signal.aborted) { + if (signal?.aborted) { throw lazyDOMException('The operation was aborted', 'AbortError'); } @@ -318,7 +318,7 @@ async function readFileHandle(filehandle, options) { MathMin(size, kReadFileMaxChunkSize); let endOfFile = false; do { - if (signal && signal.aborted) { + if (signal?.aborted) { throw lazyDOMException('The operation was aborted', 'AbortError'); } const buf = Buffer.alloc(chunkSize); diff --git a/lib/internal/fs/read_file_context.js b/lib/internal/fs/read_file_context.js index 10ba44e9c67bfc..61f25f75cb9af9 100644 --- a/lib/internal/fs/read_file_context.js +++ b/lib/internal/fs/read_file_context.js @@ -94,7 +94,7 @@ class ReadFileContext { let offset; let length; - if (this.signal && this.signal.aborted) { + if (this.signal?.aborted) { return this.close( lazyDOMException('The operation was aborted', 'AbortError') ); diff --git a/lib/internal/fs/rimraf.js b/lib/internal/fs/rimraf.js index b0260be9ad1b51..758cdcb1aa3548 100644 --- a/lib/internal/fs/rimraf.js +++ b/lib/internal/fs/rimraf.js @@ -189,7 +189,7 @@ function rimrafSync(path, options) { try { // SunOS lets the root user unlink directories. - if (stats !== undefined && stats.isDirectory()) + if (stats?.isDirectory()) _rmdirSync(path, options, null); else _unlinkSync(path, options); diff --git a/lib/internal/fs/watchers.js b/lib/internal/fs/watchers.js index 43c6a8f9a938fa..6791fb12d838d9 100644 --- a/lib/internal/fs/watchers.js +++ b/lib/internal/fs/watchers.js @@ -137,7 +137,7 @@ StatWatcher.prototype[kFSStatWatcherAddOrCleanRef] = function(operate) { // Clean up all this[KFSStatWatcherMaxRefCount] = 0; this[KFSStatWatcherRefCount] = 0; - this._handle && this._handle.unref(); + this._handle?.unref(); } };