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

Start using eslint-plugin-deprecation to find deprecated code #3275

Merged
merged 8 commits into from
Jan 26, 2024

Conversation

norascheuch
Copy link
Contributor

Adds the plugin and resolves existing issues. In two cases the deprecated methods couldn't be easily replaced.

Checklist

  • CHANGELOG.md has been updated to incorporate all user visible changes made by this pull request.
  • Issues have been created for any UI or other user-facing changes made by this pull request.
  • [Maintainers only] If this pull request makes user-facing changes that require documentation changes, open a corresponding docs pull request in the github/codeql repo and add the ready-for-doc-review label there.

@norascheuch norascheuch requested review from a team as code owners January 25, 2024 08:26

const manyScannedRepos = Array.from({ length: 1000 }, (_, i) => {
const mockedScannedRepo = createMockScannedRepo();
const randomInt = uniqueNumbers.pop();
const uniqueId =
randomInt === undefined ? Math.random() * 8000 + 1001 : randomInt;
Copy link
Contributor Author

@norascheuch norascheuch Jan 25, 2024

Choose a reason for hiding this comment

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

Since array.pop() returns a number OR undefined I needed to add an alternative for the undefined case. This is a best effort for a random number bigger than 1000 without using a library to create it. This case should never happen since the uniqueNumbers array is never empty.

Copy link
Contributor

Choose a reason for hiding this comment

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

I'm not a huge fan of having code that's not intended to execute, and that branch isn't just throwing an error. I have a few potential solutions to make things a little cleaner:

Throw an error instead

I'd be happy if we threw an error if pop ever returns undefined. As you say it shouldn't ever happen, so if it does then the code needs to be updated rather than using a fallback strategy.

Iterate over uniqueNumbers

Instead of doing Array.from({ length: 1000 }, (_, i) => to initiate the loop, do uniqueNumbers.map(repoId => so that we remove the need to call pop.

Just use Math.random() and don't precompute a random list

The repo ids don't have to go from 0-999. They just need to be unique, and even then it's only the storybook so not too critical. If we use big enough numbers, e.g. Math.floor(Math.random() * 1000000000000) (AFAIK the max safe int in javascript is 9007199254740991), then the chance of getting duplicates in our 1000 repo ids becomes very low.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think I prefer the error, as you say it should never happen and if it does we should look at it!

id: uniqueId,
fullName: `octodemo/${uniqueId}`,
// We need to ensure the ID is unique for React keys
id: parseInt(nanoid()),
Copy link
Contributor

Choose a reason for hiding this comment

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

Is parseInt a left over from before? nanoid() gives you back a string, not a number.

Copy link
Contributor Author

@norascheuch norascheuch Jan 25, 2024

Choose a reason for hiding this comment

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

Yes, that's why I have to convert it to a number for the id. Nanoid has no method that returns a number, however by giving it the custom alphabet on line 132 we can parse the string back to a number.

Copy link
Contributor

Choose a reason for hiding this comment

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

Ok I'm now following a bit more what we're trying to do 😂 I didn't realise this is an id for a repo (which is expected to be a number). Although I'm still unclear why we aren't just using Math.random() 🤔

I think what you have is fine and I think it'll work because there are enough characters to avoid collisions. This is also storybook code so it's not terrible if things break 😬

Do we want to skip 0 since you could end up with a number of 0123? parseInt still works but it doesn't skip the 0, it just assumes the number is octal rather than decimal so can be a bit confusing.

Copy link
Contributor

@charisk charisk left a comment

Choose a reason for hiding this comment

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

LGTM, thanks for cleaning up!

@norascheuch norascheuch merged commit 1f24cd1 into main Jan 26, 2024
15 checks passed
@norascheuch norascheuch deleted the nora/add-eslint-deprecation branch January 26, 2024 10:46
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants