refactor: replace unsafe Number constructor with type-safe alternatives #7777
Motivation
Remove usage of Number() constructor which is unsafe because:
- It accepts
anyas parameter, allowing invalid types without TypeScript errors - Silently produces
NaNwhen given invalid input - Makes code prone to runtime errors
Description
Added
-
bigintToNumber()utility function in@lodestar/utils- Validates bigint is within safe integer range
- Throws descriptive errors instead of returning NaN
- Provides compile-time type safety
Modified
- Replaced
Number(bigint)withbigintToNumber(bigint) - Replaced
Number(string)withparseInt(string, 10) - Fixed code formatting across multiple files
Closes #issue_number #7771
Steps to test or reproduce
git checkout fix-number-constructor-clean
yarn install
yarn build
yarn lint
Summary of Changes
Hello @Beutife, I'm Gemini Code Assist[^1]! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!
This pull request refactors the codebase to eliminate the unsafe usage of the Number() constructor, which can lead to silent NaN values and runtime errors due to its loose type acceptance. By introducing a new bigintToNumber() utility function and consistently using parseInt() for string conversions, the changes significantly improve type safety, error handling, and overall code robustness, making the system more predictable and less prone to unexpected numerical issues.
Highlights
-
New Utility Function: Introduced
bigintToNumber()in@lodestar/utilsfor safe conversion ofbiginttonumber, including validation for safe integer range and error throwing for invalid conversions. -
Type-Safe BigInt Conversion: Replaced direct
Number(bigint)calls with the newbigintToNumber()utility across the codebase to enhance type safety and prevent silentNaNissues. -
Type-Safe String Conversion: Replaced
Number(string)calls withparseInt(string, 10)to ensure explicit base conversion and safer handling of string-to-number parsing. - Code Formatting: Applied consistent code formatting across multiple files, improving readability and maintainability.
Using Gemini Code Assist
The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.
Invoking Gemini
You can request assistance from Gemini at any point by creating a comment using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands on the current page.
| Feature | Command | Description |
|---|---|---|
| Code Review | /gemini review |
Performs a code review for the current pull request in its current state. |
| Pull Request Summary | /gemini summary |
Provides a summary of the current pull request in its current state. |
| Comment | @gemini-code-assist | Responds in comments when explicitly tagged, both in pull request comments and review comments. |
| Help | /gemini help |
Displays a list of available commands. |
Customization
To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.
Limitations & Feedback
Gemini Code Assist may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with :thumbsup: and :thumbsdown: on @gemini-code-assist comments. If you're interested in giving your feedback about your experience with Gemini Code Assist for Github and other Google products, sign up here.
You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.
[^1]: Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.
Codecov Report
:x: Patch coverage is 45.58824% with 37 lines in your changes missing coverage. Please review.
:white_check_mark: Project coverage is 51.91%. Comparing base (13fb933) to head (137f8a5).
:warning: Report is 55 commits behind head on unstable.
Additional details and impacted files
@@ Coverage Diff @@
## unstable #8584 +/- ##
=========================================
Coverage 51.90% 51.91%
=========================================
Files 848 849 +1
Lines 65797 65817 +20
Branches 4795 4797 +2
=========================================
+ Hits 34153 34166 +13
- Misses 31575 31582 +7
Partials 69 69
:rocket: New features to boost your workflow:
- :snowflake: Test Analytics: Detect flaky tests, report on failures, and find test suite problems.
- :package: JS Bundle Analysis: Save yourself from yourself by tracking and limiting bundle sizes in JS merges.
CI is failing, putting as draft
Hi @nflaig , I’ve been trying to resolve the issue with the failing test, but I noticed that whenever I run yarn install, several packages get installed automatically. I think the same thing happened the first time I committed.
After running yarn install, I’m able to successfully run yarn build, yarn lint:fix, and yarn test , everything works fine locally. However, running yarn install also modifies a lot of files, resulting in several uncommitted changes.
I’m not sure what’s causing this or what the best next step is to ensure the tests don’t fail again. Could you please help me figure out how to handle this? I’ve been stuck on it for a while.
yarn install also modifies a lot of files, resulting in several uncommitted changes.
yarn install shouldn't modify any files, it just installs packages in the node_modules folder, make sure you have the right yarn version installed, it should be version 1, see https://classic.yarnpkg.com/lang/en/.
yarn install also modifies a lot of files, resulting in several uncommitted changes.
yarn installshouldn't modify any files, it just installs packages in thenode_modulesfolder, make sure you have the right yarn version installed, it should be version 1, see https://classic.yarnpkg.com/lang/en/.
Ok, I'll look through it, thanks so much for your guidance, i appreciate
closing stale
Hi @wemeetagain ,I would love to continue work on this, I'd been offline for a while .
@Beutife I would look to fix the problems addressed earlier and then opening a new PR only when you've addressed issues that don't modify content you're not wanting to actually commit. Only submit when you're actually ready to minimize back and forth. We don't have the capacity to do continuous reviews.