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

misc: use codemod for optional chaining #13503

Merged
merged 9 commits into from
Dec 20, 2021
Merged

misc: use codemod for optional chaining #13503

merged 9 commits into from
Dec 20, 2021

Conversation

connorjclark
Copy link
Collaborator

I used this tool: https://github.com/villesau/optional-chaining-codemod

First commit is just running the tool on our code, no manual changes except reverting changes to compiled test files. Subsequent commits may be done for some touchups.

Misses a few files, because of villesau/optional-chaining-codemod#48

@connorjclark connorjclark requested a review from a team as a code owner December 15, 2021 21:38
@connorjclark connorjclark requested review from adamraine and removed request for a team December 15, 2021 21:38
@connorjclark
Copy link
Collaborator Author

any interest in an eslint rule too?

@brendankenny
Copy link
Member

A quick scan of this doesn't appear to have anything I wouldn't write myself, but for all these Node version bumps I'm always more in favor of letting code update in its own time. Unless there's a big benefit or it came up in an eng sync and everyone's generally excited about it for whatever reason, it ends up in but_why.gif land for me.

It takes someone's review time and the author's time to make really sure there weren't any mistakes, and all the code being replaced honestly looks just fine. Maybe in a year or two it'll be anathema to be so liberal with our &&s (like if Promise-based code like this showed up in a PR today), but right now there's really not anything wrong with the existing working code.

But again, there's nothing here I think I wouldn't prefer and is definitely a nice cleanup in a few places. If @adamraine's excited about it that seems fine.

Copy link
Member

@adamraine adamraine left a comment

Choose a reason for hiding this comment

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

If @adamraine's excited about it that seems fine.

I am actually excited about this. I remember the long && conditions made some of these files confusing when I first started working on Lighthouse. I think these changes will help anyone in that same situation.

speedIndexTs: speedIndex && speedIndex.timestamp,
totalBlockingTime: totalBlockingTime && totalBlockingTime.timing,
maxPotentialFID: maxPotentialFID && maxPotentialFID.timing,
firstContentfulPaint: firstContentfulPaint?.timing,
Copy link
Member

Choose a reason for hiding this comment

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

👍

@@ -65,35 +65,31 @@ class TraceElements extends FRGatherer {
* @return {number | undefined}
*/
static getNodeIDFromTraceEvent(event) {
return event && event.args &&
event.args.data && event.args.data.nodeId;
return event?.args?.data?.nodeId;
Copy link
Member

Choose a reason for hiding this comment

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

These functions seem kinda obsolete now.

@brendankenny
Copy link
Member

If @adamraine's excited about it that seems fine.

I am actually excited about this. I remember the long && conditions made some of these files confusing when I first started working on Lighthouse. I think these changes will help anyone in that same situation.

SG.

I actually dislike the &&-existence-check pattern because it mixes control flow into types (typescript helps prevent errors with them but you still end up into these unnecessary type superpositions). ?. is basically equivalent so really should be used sparingly, but I already find myself reaching for it much more often than I do && because it's so nice and compact. Oh well :)

@brendankenny
Copy link
Member

any interest in an eslint rule too?

thinking about this since, this seems like it might be a good idea just to avoid having to suggest it in code reviews. I can't think of a good reason for it ever to be a toss-up between the old && and optional chaining.

Is there a good rule anywhere? There's prefer-optional-chain but it's slightly stricter than what this codemod is doing (and not sure what to make of the false positive note).

@connorjclark
Copy link
Collaborator Author

connorjclark commented Dec 20, 2021

Here is the output from that lint plugin on this PR.

/Users/cjamcl/src/lighthouse/build/build-report-components.js
  87:19  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/flow-report/src/common.tsx
  96:7  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-cli/test/smokehouse/frontends/lib.js
  34:26  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  40:5   error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/audits/byte-efficiency/uses-long-cache-ttl.js
  114:9  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/audits/script-treemap-data.js
  86:21  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/audits/seo/is-crawlable.js
  116:9  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/computed/critical-request-chains.js
  58:9  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/config/config.js
  334:20  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  427:7   error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/gather/gatherers/dobetterweb/tags-blocking-first-paint.js
  140:22  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/lib/asset-saver.js
  22:18  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/lib/dependency-graph/network-node.js
  50:12  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/lib/lh-error.js
  138:18  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/scripts/compare-runs.js
  310:11  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/scripts/gcp-collection/fleet-create-directories.js
  35:19  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/scripts/internal-analysis/download-issues.js
  112:25  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/test/audits/dobetterweb/no-vulnerable-libraries-test.js
  113:37  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/lighthouse-core/test/lib/tracehouse/main-thread-tasks-test.js
  215:23  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/report/renderer/performance-category-renderer.js
  373:15  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/report/renderer/report-ui-features.js
   98:28  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  106:7   error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  249:20  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  286:7   error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/treemap/test/treemap-test-pptr.js
  51:7   error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  98:13  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

/Users/cjamcl/src/lighthouse/viewer/test/viewer-test-pptr.js
   84:7   error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain
  161:30  error  Prefer using an optional chain expression instead, as it's more concise and easier to read  @typescript-eslint/prefer-optional-chain

I only checked a few, but they were all real, just for some reason missed by the codemod. We can followup in a new PR.

@devtools-bot devtools-bot deleted the optional-codemod branch December 20, 2021 21:36
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants