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

no-default-id-values rule #137

Merged
merged 4 commits into from
Dec 11, 2024

Conversation

fabioh8010
Copy link
Contributor

@fabioh8010 fabioh8010 commented Nov 22, 2024

This PR implements a new ESLint rule to detect and restricts usage of default number/string IDs in E/App. It's intended to be used on .eslintrc.changed.js, that will only run for the changes files of a PR.

This ESLint rule does a simple string match check to look for ID defaults in E/App, while it is not an optimal solution it should be enough for us to detect these situations.

Issue: Expensify/App#50360

Screenshot 2024-11-28 at 09 01 32 Screenshot 2024-11-28 at 09 01 47

Tests

  1. Checkout this branch
  2. Go to E/App repo and run this command to link your eslint-config-expensify repo to E/App: npx link publish <path-to-eslint-repo>/
  3. Add the rulesdir/no-default-id-values rule to .eslintrc.changed.js and set it to error.
  4. Make any change in a file that has plenty of examples e.g. src/libs/ReportUtils.ts, and commit that file.
  5. Run npm run lint-changed, assert the errors were reported.

Copy link
Contributor

github-actions bot commented Nov 22, 2024

All contributors have signed the CLA ✍️ ✅
Posted by the CLA Assistant Lite bot.

@fabioh8010 fabioh8010 changed the title [WIP] no-default-values rule [WIP] no-default-id-values rule Nov 28, 2024
@fabioh8010
Copy link
Contributor Author

I have read the CLA Document and I hereby sign the CLA

CLABotify added a commit to Expensify/CLA that referenced this pull request Nov 28, 2024
@fabioh8010 fabioh8010 changed the title [WIP] no-default-id-values rule no-default-id-values rule Nov 28, 2024
@fabioh8010 fabioh8010 marked this pull request as ready for review November 28, 2024 15:30
@fabioh8010
Copy link
Contributor Author

Opened PR to review cc @neil-marcellini @iwiznia @paultsimura

@fabioh8010
Copy link
Contributor Author

Should we catch assignments of default values too? e.g.

let currentUserAccountID = -1;
let transactionID = '-1';

@paultsimura
Copy link

Should we catch assignments of default values too? e.g.

let currentUserAccountID = -1;
let transactionID = '-1';

IMO, this is not a common case in the App, so we shouldn't worry about it. However, I'll leave the decision to @iwiznia or @neil-marcellini.

@paultsimura
Copy link

I'll treat the 👍 from Ionatan as approval for #137 (comment).
Reviewing the PR soon...

@tgolen
Copy link
Contributor

tgolen commented Dec 2, 2024

@paultsimura and @fabioh8010 I believe this was correctly spotted, but the original issue isn't too clear. Empty strings are also being disallowed for defaults with this, right? I think I saw @fabioh8010 correctly spot that the danger of empty strings is subscribing to the entire collection.

@fabioh8010
Copy link
Contributor Author

@paultsimura and @fabioh8010 I believe this was correctly spotted, but the original issue isn't too clear. Empty strings are also being disallowed for defaults with this, right? I think I saw @fabioh8010 correctly spot that the danger of empty strings is subscribing to the entire collection.

That is correct, and this rule will catch these situations too 👍

Copy link

@paultsimura paultsimura left a comment

Choose a reason for hiding this comment

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

Looks good and tests well ✔️

My only concern is the repeated "see more" link, but it'll be cluttering only until the first code cleanup. Later it'll be only useful.

image

@paultsimura
Copy link

@iwiznia, @neil-marcellini, @tgolen, please proceed with your review.

@fabioh8010
Copy link
Contributor Author

@iwiznia, @neil-marcellini, @tgolen could you have a look here? Thanks!

Copy link
Contributor

@iwiznia iwiznia left a comment

Choose a reason for hiding this comment

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

This looks good, but hard to tell if it will generate a lot of false positives, can you make this new rule run in App and see if it does?


const disallowedNumberDefaults = [
'ID ?? -1',
'id ?? -1',
Copy link
Contributor

Choose a reason for hiding this comment

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

Rather than having all case-sensitive options, can we just make the check be cause-insensitive?

Copy link
Contributor

Choose a reason for hiding this comment

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

@fabioh8010 bump on this question

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 would like to keep them all separated for all, just in case we discover that some string match is being problematic, but it happens to be case sensitive. Is that ok for you?

Copy link
Contributor

Choose a reason for hiding this comment

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

OK, yeah. That's fine. Thanks for explaining and documenting the analysis you did!

@fabioh8010
Copy link
Contributor Author

@iwiznia I analysed all search results by string match and here are the possible false-positives for each one:

Number IDs
* ID ?? -1 (201 matches)
  * src/components/ReportActionItem/IssueCardMessage.tsx 1
  * src/libs/ReportActionsUtils.ts 1
  * src/libs/actions/Card.ts 2
  * src/libs/TransactionUtils/index.ts 1
  * src/pages/home/report/ContextMenu/BaseReportActionContextMenu.tsx 1
  * src/pages/settings/Wallet/Card/BaseGetPhysicalCard.tsx 1
* id ?? -1 (6 matches)
  * .github/libs/GithubUtils.ts 1
* ID ?? 0 (32 matches)
  * src/libs/PolicyUtils.ts 1
  * src/libs/actions/OnyxUpdateManager/utils/DeferredOnyxUpdates.ts 1
  * src/libs/Middleware/SaveResponseInOnyx.ts 2
* id ?? 0 (0 matches)
  * -
* ID || -1 (14 matches)
  * -
* id || -1 (0 matches)
  * -
* ID || 0 (3 matches)
  * src/libs/actions/User.ts 2
* id || 0 (0 matches)
  * -
* ID : -1 (9 matches)
  * -
* id : -1 (0 matches)
  * -
* ID : 0 (1 match)
  * -
* id : 0 (0 matches)
  * -

String IDs
*  ?? '-1' (754 matches)
   * .github/actions/javascript/getAndroidRolloutPercentage/getAndroidRolloutPercentage.ts 1
   * src/libs/DistanceRequestUtils.ts 1
   * src/libs/OptionsListUtils.ts 1
   * src/libs/PolicyUtils.ts 1
   * src/libs/WorkflowUtils.ts 2
   * src/libs/actions/Card.ts 2
   * src/libs/actions/CompanyCards.ts 1
   * src/libs/actions/TaxRate.ts 1
   * src/pages/ReportDetailsPage.tsx 2
   * src/pages/workspace/WorkspacePageWithSections.tsx 1
* ID ?? '' (183 matches)
  * src/components/MoneyRequestConfirmationList.tsx 2
  * src/components/TextPicker/TextSelectorModal.tsx 1
  * src/libs/actions/TaxRate.ts 1
  * src/libs/actions/Policy/DistanceRate.ts 4
  * src/libs/TransactionUtils/index.ts 1
  * src/pages/ReimbursementAccount/BusinessInfo/substeps/TaxIdBusiness.tsx 1
  * src/pages/settings/Wallet/PaymentMethodList.tsx 1
  * src/pages/workspace/accounting/netsuite/NetSuiteSubsidiarySelector.tsx 1
  * src/pages/workspace/distanceRates/CreateDistanceRatePage.tsx 1
  * src/pages/workspace/distanceRates/PolicyDistanceRatesPage.tsx 5
  * src/pages/workspace/distanceRates/PolicyDistanceRatesSettingsPage.tsx 1
  * src/pages/workspace/distanceRates/PolicyDistanceRateTaxRateEditPage.tsx 1
  * src/pages/workspace/perDiem/ImportedPerDiemPage.tsx 1
  * src/pages/workspace/perDiem/WorkspacePerDiemPage.tsx 2
  * src/pages/workspace/perDiem/WorkspacePerDiemSettingsPage.tsx 1
  * src/pages/workspace/taxes/WorkspaceTaxesSettingsWorkspaceCurrency.tsx 1
* id ?? '' (17 matches)
  * src/libs/Pusher/pusher.ts 1
* ID ?? '0' (8 matches)
  * -
* id ?? '0' (0 matches)
  * -
*  || '-1' (20 matches)
   * -
* ID || '' (2 matches)
  * -
* id || '' (0 matches)
  * -
* ID || '0' (0 matches)
  * -
* id || '0' (0 matches)
  * -
*  : '-1' (38 matches)
  * -
*  : '0' (1 match)
  * -

Most of these possible false-positives are based on my own doubts if that ID could be of an entity or Onyx data, so the real number can be smaller. Anyway, I think the results here show us that we have few ones compared to the total of results of each string match.

@tgolen tgolen merged commit f9e1076 into Expensify:main Dec 11, 2024
3 checks passed
@os-botify
Copy link
Contributor

os-botify bot commented Dec 11, 2024

🚀 Published to npm in 2.0.75 🎉

@iwiznia
Copy link
Contributor

iwiznia commented Dec 11, 2024

here are the possible false-positives for each one:

So if we update the config in App, all PRs will start failing on those files?

@fabioh8010
Copy link
Contributor Author

fabioh8010 commented Dec 12, 2024

here are the possible false-positives for each one:

So if we update the config in App, all PRs will start failing on those files?

The PRs will fail if they have changed files that have one of the matches described in the rule, it could be any .ts/.tsx file.

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.

5 participants