Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fs: refactor to use optional chaining #36524

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 8 additions & 8 deletions lib/fs.js
Original file line number Diff line number Diff line change
Expand Up @@ -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 },
Expand All @@ -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);
Expand Down Expand Up @@ -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) {
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand All @@ -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 {
Expand All @@ -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;
}

Expand Down Expand Up @@ -1828,7 +1828,7 @@ function realpathSync(p, options) {
}
}

if (cache) cache.set(original, p);
cache?.set(original, p);
return encodeRealpathResult(p, options);
}

Expand Down
8 changes: 4 additions & 4 deletions lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -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');
}

Expand All @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/read_file_context.js
Original file line number Diff line number Diff line change
Expand Up @@ -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')
);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/rimraf.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/watchers.js
Original file line number Diff line number Diff line change
Expand Up @@ -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();
}
};

Expand Down