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

lib: refactor to use optional chaining #38697

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
2 changes: 1 addition & 1 deletion lib/_http_agent.js
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ Agent.prototype.addRequest = function addRequest(req, options, port/* legacy */,
delete this.freeSockets[name];
}

const freeLen = freeSockets ? freeSockets.length : 0;
const freeLen = freeSockets?.length ?? 0;
const sockLen = freeLen + this.sockets[name].length;

if (socket) {
Expand Down
8 changes: 4 additions & 4 deletions lib/_http_outgoing.js
Original file line number Diff line number Diff line change
Expand Up @@ -159,19 +159,19 @@ ObjectDefineProperty(OutgoingMessage.prototype, 'writableObjectMode', {

ObjectDefineProperty(OutgoingMessage.prototype, 'writableLength', {
get() {
return this.outputSize + (this.socket ? this.socket.writableLength : 0);
return this.outputSize + (this.socket?.writableLength ?? 0);
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableHighWaterMark', {
get() {
return this.socket ? this.socket.writableHighWaterMark : HIGH_WATER_MARK;
return this.socket?.writableHighWaterMark ?? HIGH_WATER_MARK;
}
});

ObjectDefineProperty(OutgoingMessage.prototype, 'writableCorked', {
get() {
const corked = this.socket ? this.socket.writableCorked : 0;
const corked = this.socket?.writableCorked ?? 0;
return corked + this[kCorked];
}
});
Expand Down Expand Up @@ -709,7 +709,7 @@ OutgoingMessage.prototype.write = function write(chunk, encoding, callback) {
};

function onError(msg, err, callback) {
const triggerAsyncId = msg.socket ? msg.socket[async_id_symbol] : undefined;
const triggerAsyncId = msg.socket?.[async_id_symbol] ?? undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const triggerAsyncId = msg.socket?.[async_id_symbol] ?? undefined;
const triggerAsyncId = msg.socket?.[async_id_symbol];

defaultTriggerAsyncIdScope(triggerAsyncId,
process.nextTick,
emitErrorNt,
Expand Down
2 changes: 1 addition & 1 deletion lib/_tls_wrap.js
Original file line number Diff line number Diff line change
Expand Up @@ -511,7 +511,7 @@ function TLSSocket(socket, opts) {

ReflectApply(net.Socket, this, [{
handle: this._wrapHandle(wrap),
allowHalfOpen: socket ? socket.allowHalfOpen : tlsOptions.allowHalfOpen,
allowHalfOpen: socket?.allowHalfOpen ?? tlsOptions.allowHalfOpen,
pauseOnCreate: tlsOptions.pauseOnConnect,
manualStart: true,
highWaterMark: tlsOptions.highWaterMark,
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/child_process.js
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ class Control extends EventEmitter {
}

get fd() {
return this.#channel ? this.#channel.fd : undefined;
return this.#channel?.fd ?? undefined;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return this.#channel?.fd ?? undefined;
return this.#channel?.fd;

}
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/fs/promises.js
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ class FileHandle extends EventEmitterMixin(JSTransferable) {
constructor(filehandle) {
super();
this[kHandle] = filehandle;
this[kFd] = filehandle ? filehandle.fd : -1;
this[kFd] = filehandle?.fd ?? -1;

this[kRefs] = 1;
this[kClosePromise] = null;
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/compat.js
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ const proxySocketHandler = {
if (stream.destroyed)
return false;
const request = stream[kRequest];
return request ? request.readable : stream.readable;
return request?.readable ?? stream.readable;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
return request?.readable ?? stream.readable;
return stream[kRequest]?.readable ?? stream.readable;

case 'setTimeout':
const session = stream.session;
if (session !== undefined)
Expand Down
2 changes: 1 addition & 1 deletion lib/internal/http2/core.js
Original file line number Diff line number Diff line change
Expand Up @@ -199,7 +199,7 @@ function debugStream(id, sessionType, message, ...args) {

function debugStreamObj(stream, message, ...args) {
const session = stream[kSession];
const type = session ? session[kType] : undefined;
const type = session?.kType ?? undefined;
Copy link
Contributor

@aduh95 aduh95 May 16, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const type = session?.kType ?? undefined;
const type = stream[kSession]?.[kType];

debugStream(stream[kID], type, message, ...new SafeArrayIterator(args));
}

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/inspector/inspect_repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -688,7 +688,7 @@ function createRepl(inspector) {
function formatLocation(location) {
if (!location) return '<unknown location>';
const script = knownScripts[location.scriptId];
const scriptUrl = script ? script.url : location.scriptUrl;
const scriptUrl = script?.url ?? location.scriptUrl;
return `${getRelativePath(scriptUrl)}:${location.lineNumber + 1}`;
}
const breaklist = ArrayPrototypeJoin(
Expand Down
6 changes: 3 additions & 3 deletions lib/internal/streams/readable.js
Original file line number Diff line number Diff line change
Expand Up @@ -1200,14 +1200,14 @@ ObjectDefineProperties(Readable.prototype, {
readableObjectMode: {
enumerable: false,
get() {
return this._readableState ? this._readableState.objectMode : false;
return this._readableState?.objectMode ?? false;
}
},

readableEncoding: {
enumerable: false,
get() {
return this._readableState ? this._readableState.encoding : null;
return this._readableState?.encoding ?? null;
}
},

Expand Down Expand Up @@ -1235,7 +1235,7 @@ ObjectDefineProperties(Readable.prototype, {
readableEnded: {
enumerable: false,
get() {
return this._readableState ? this._readableState.endEmitted : false;
return this._readableState?.endEmitted ?? false;
}
},

Expand Down
10 changes: 5 additions & 5 deletions lib/internal/streams/writable.js
Original file line number Diff line number Diff line change
Expand Up @@ -768,7 +768,7 @@ ObjectDefineProperties(Writable.prototype, {

destroyed: {
get() {
return this._writableState ? this._writableState.destroyed : false;
return this._writableState?.destroyed ?? false;
},
set(value) {
// Backward compatibility, the user is explicitly managing destroyed.
Expand Down Expand Up @@ -798,13 +798,13 @@ ObjectDefineProperties(Writable.prototype, {

writableFinished: {
get() {
return this._writableState ? this._writableState.finished : false;
return this._writableState?.finished ?? false;
}
},

writableObjectMode: {
get() {
return this._writableState ? this._writableState.objectMode : false;
return this._writableState?.objectMode ?? false;
}
},

Expand All @@ -816,7 +816,7 @@ ObjectDefineProperties(Writable.prototype, {

writableEnded: {
get() {
return this._writableState ? this._writableState.ending : false;
return this._writableState?.ending ?? false;
}
},

Expand All @@ -836,7 +836,7 @@ ObjectDefineProperties(Writable.prototype, {

writableCorked: {
get() {
return this._writableState ? this._writableState.corked : 0;
return this._writableState?.corked ?? 0;
}
},

Expand Down
2 changes: 1 addition & 1 deletion lib/internal/util.js
Original file line number Diff line number Diff line change
Expand Up @@ -289,7 +289,7 @@ function getConstructorOf(obj) {

function getSystemErrorName(err) {
const entry = uvErrmapGet(err);
return entry ? entry[0] : `Unknown system error ${err}`;
return entry?.[0] ?? `Unknown system error ${err}`;
}

function getSystemErrorMap() {
Expand Down
4 changes: 2 additions & 2 deletions lib/net.js
Original file line number Diff line number Diff line change
Expand Up @@ -698,7 +698,7 @@ function protoGetter(name, callback) {
}

protoGetter('bytesRead', function bytesRead() {
return this._handle ? this._handle.bytesRead : this[kBytesRead];
return this._handle?.bytesRead ?? this[kBytesRead];
});

protoGetter('remoteAddress', function remoteAddress() {
Expand Down Expand Up @@ -785,7 +785,7 @@ Socket.prototype._write = function(data, encoding, cb) {
// Legacy alias. Having this is probably being overly cautious, but it doesn't
// really hurt anyone either. This can probably be removed safely if desired.
protoGetter('_bytesDispatched', function _bytesDispatched() {
return this._handle ? this._handle.bytesWritten : this[kBytesWritten];
return this._handle?.bytesWritten ?? this[kBytesWritten];
});

protoGetter('bytesWritten', function bytesWritten() {
Expand Down
6 changes: 3 additions & 3 deletions lib/repl.js
Original file line number Diff line number Diff line change
Expand Up @@ -1423,7 +1423,7 @@ function complete(line, callback) {
memberGroups.push(filteredOwnPropertyNames(obj));
p = ObjectGetPrototypeOf(obj);
} else {
p = obj.constructor ? obj.constructor.prototype : null;
p = obj.constructor?.prototype ?? null;
}
// Circular refs possible? Let's guard against that.
let sentinel = 5;
Expand Down Expand Up @@ -1557,8 +1557,8 @@ function _memory(cmd) {
// going up is } and )
let dw = StringPrototypeMatch(cmd, /[{(]/g);
let up = StringPrototypeMatch(cmd, /[})]/g);
up = up ? up.length : 0;
dw = dw ? dw.length : 0;
up = up?.length ?? 0;
dw = dw?.length ?? 0;
let depth = dw - up;

if (depth) {
Expand Down