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

fix: Intl.NumberFormat locale RangeError related to Simulation Details #24068

Merged
merged 2 commits into from
Apr 17, 2024

Conversation

dbrans
Copy link
Contributor

@dbrans dbrans commented Apr 16, 2024

Description

Problem: The locale returned by getCurrentLocale is not meant to be used with the Intl API.

To fix this, this PR will:

  • Rename getCurrentLocale => getLocaleNotSafeForIntl to alert developers in the future.
  • Add getIntlLocale which converts the locale returned from getLocaleNotSafeForIntl into a BCP 47 Language Tag which can be used with the Intl API.
  • Add tests to ensure that getIntlLocale works with all our locales going forward.

Open in GitHub Codespaces

Related issues

Fixes: #24067

Manual testing steps

  1. Go to this page...

Screenshots/Recordings

Before

After

Pre-merge author checklist

  • I’ve followed MetaMask Coding Standards.
  • I've completed the PR template to the best of my ability
  • I’ve included tests if applicable
  • I’ve documented my code using JSDoc format if applicable
  • I’ve applied the right labels on the PR (see labeling guidelines). Not required for external contributors.

Pre-merge reviewer checklist

  • I've manually tested the PR (e.g. pull and build branch, run the app, test code being changed).
  • I confirm that this PR addresses all acceptance criteria described in the ticket it closes and includes the necessary testing evidence such as recordings and or screenshots.

Copy link
Contributor

CLA Signature Action: All authors have signed the CLA. You may need to manually re-run the blocking PR check if it doesn't pass in a few minutes.

@dbrans dbrans added the team-confirmations-system DEPRECATED: please use "team-confirmations" label instead label Apr 16, 2024
@dbrans dbrans force-pushed the dbrans/locale-fix branch from 6592878 to ca65bb1 Compare April 16, 2024 22:40
@dbrans dbrans marked this pull request as ready for review April 16, 2024 22:43
@dbrans dbrans requested a review from a team as a code owner April 16, 2024 22:43
@dbrans dbrans force-pushed the dbrans/locale-fix branch from ca65bb1 to 56b2b72 Compare April 16, 2024 22:51
* @returns {string} one of the codes in file://./../../../app/_locales/index.json.
* These codes are not safe to use with the Intl API.
*/
export const getLocaleNotSafeForIntl = (state) =>
Copy link
Member

Choose a reason for hiding this comment

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

For the sake of minimising refactor and the PR diff, is this really needed if SimulationDetails was the first usage of Intl.NumberFormat with one of these locales, and there was no issues previously?

Would the above warning in the JSDoc, plus the presence of the getIntlLocale method be a sufficient deterrent?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

export const getCurrentLocale = (state) => state.localeMessages.currentLocale;
/**
* @param state
* @returns {string} one of the codes in file://./../../../app/_locales/index.json.
Copy link
Member

Choose a reason for hiding this comment

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

Should we be explicit in both these method's JSDoc that it's the user's selected locale that is returned?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Done.

});

describe('getIntlLocale', () => {
it.each(locales)('can convert locale "%s" to BCP 47 format', (locale) => {
Copy link
Member

Choose a reason for hiding this comment

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

Love this test! But if this is the happy path, should we also have a unit test to verify we throw if a bad locale is in the state?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Good call. Done.

Copy link

codecov bot commented Apr 17, 2024

Codecov Report

Attention: Patch coverage is 42.85714% with 4 lines in your changes are missing coverage. Please review.

Project coverage is 67.58%. Comparing base (6428b33) to head (ba587ce).
Report is 38 commits behind head on develop.

Files Patch % Lines
ui/ducks/locale/locale.js 33.33% 4 Missing ⚠️
Additional details and impacted files
@@             Coverage Diff             @@
##           develop   #24068      +/-   ##
===========================================
+ Coverage    67.56%   67.58%   +0.02%     
===========================================
  Files         1246     1247       +1     
  Lines        48888    48938      +50     
  Branches     12746    12775      +29     
===========================================
+ Hits         33031    33073      +42     
- Misses       15857    15865       +8     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@dbrans dbrans merged commit 15fc955 into develop Apr 17, 2024
67 of 69 checks passed
@dbrans dbrans deleted the dbrans/locale-fix branch April 17, 2024 16:47
@github-actions github-actions bot locked and limited conversation to collaborators Apr 17, 2024
@metamaskbot metamaskbot added the release-11.16.0 Issue or pull request that will be included in release 11.16.0 label Apr 17, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
release-11.16.0 Issue or pull request that will be included in release 11.16.0 team-confirmations-system DEPRECATED: please use "team-confirmations" label instead
Projects
None yet
Development

Successfully merging this pull request may close these issues.

[Bug]: Simulation Details reports NumberFormat RangeError for some locales
4 participants