Skip to content

lib: refactor to use optional chaining - #38697

Closed
pd4d10 wants to merge 1 commit into
nodejs:mainfrom
pd4d10:patch-lib-optional
Closed

lib: refactor to use optional chaining#38697
pd4d10 wants to merge 1 commit into
nodejs:mainfrom
pd4d10:patch-lib-optional

Conversation

@pd4d10

@pd4d10pd4d10 commented May 16, 2021

Copy link
Copy Markdown
Contributor

This PR migrates expressions such as a ? a.b : c to a?.b ?? c

Codemod script:
https://github.com/pd4d10/nodejs-codemod/blob/main/src/optional-chaining.ts

Also see:
#38609

This PR migrates expressions such as `a ? a.b : c` to `a?.b ?? c`
Codemod script:
https://github.com/pd4d10/nodejs-codemod/blob/main/src/optional-chaining.ts
@aduh95

Copy link
Copy Markdown
Contributor

I'm not sure we want to do that after #38245...
In any case this should be split into several PRs, it's less of a burden to review smaller PRs and we benchmark it more efficiently.


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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
returnthis.#channel?.fd??undefined;
returnthis.#channel?.fd;

return false;
const request = stream[kRequest];
return request ? request.readable : stream.readable;
return request?.readable ?? stream.readable;

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Suggested change
returnrequest?.readable??stream.readable;
returnstream[kRequest]?.readable??stream.readable;

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

@aduh95aduh95May 16, 2021

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

Comment threadlib/_http_outgoing.js

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
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

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

@pd4d10

Copy link
Copy Markdown
ContributorAuthor

I'm not sure we want to do that after #38245...

OK. Given this information, I guess we should hold it until the performance issues solved.

@MifrillMifrill left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Those changes look great, and I see the potential performance issue was also taken into consideration, that's awesome, however would like to raise a hand and ask if the chaining is slower than the ternary operator.

📝 CC @ronag#50337 (comment)

@aduh95aduh95 added the stalled Issues and PRs that are stalled. label May 5, 2024
@github-actions

Copy link
Copy Markdown
Contributor

This issue/PR was marked as stalled, it will be automatically closed in 30 days. If it should remain open, please leave a comment explaining why it should remain open.

@github-actions

Copy link
Copy Markdown
Contributor

Closing this because it has stalled. Feel free to reopen if this issue/PR is still relevant, or to ping the collaborator who labelled it stalled if you have any questions.

Sign up for freeto join this conversation on GitHub. Already have an account? Sign in to comment

Labels

stalledIssues and PRs that are stalled.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants

@pd4d10@aduh95@jasnell@Mifrill