Skip to content

feat: port rule no-loss-of-precision#455

Merged
fansenze merged 1 commit intomainfrom
feat/port-rule-no_loss_of_precision-20260203
Feb 5, 2026
Merged

feat: port rule no-loss-of-precision#455
fansenze merged 1 commit intomainfrom
feat/port-rule-no_loss_of_precision-20260203

Conversation

@fansenze
Copy link
Copy Markdown
Contributor

@fansenze fansenze commented Feb 3, 2026

Summary

Port the ESLint no-loss-of-precision rule to rslint.

This rule disallows literal numbers that lose precision when converted to JavaScript Number (IEEE 754 64-bit floating-point). Supports base-10 integers/decimals, scientific notation, binary (0b), octal (0o), hexadecimal (0x), and ES2021 numeric separators.

Also adds the core ESLint rule JS test infrastructure under packages/rslint-test-tools/tests/eslint/.

Related Links

Checklist

  • Tests updated (or not required).
  • Documentation updated (or not required).

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello @fansenze, I'm Gemini Code Assist1! 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 introduces a crucial new linting rule to prevent silent numerical precision loss in JavaScript code. By flagging literal numbers that cannot be accurately represented by JavaScript's standard Number type, it helps developers write more robust and predictable code. The PR also includes a necessary correction to how rule options are processed, improving the overall reliability of the linter's configuration system.

Highlights

  • New Linter Rule: no-loss-of-precision: The ESLint no-loss-of-precision rule has been ported to rslint. This rule identifies and disallows literal numbers that would lose precision when converted to a JavaScript Number (IEEE 754 64-bit floating-point).
  • Rule Registration: The new rule is registered under both its core ESLint name (no-loss-of-precision) and its TypeScript ESLint equivalent (@typescript-eslint/no-loss-of-precision).
  • Bug Fix in Rule Option Handling: A bug in cmd/rslint/api.go was fixed, ensuring that rule options are correctly matched against the registered rule name rather than the internal rule name.
  • Comprehensive Implementation and Testing: The rule includes detailed logic for detecting precision loss across various number formats (decimal, binary, octal, hexadecimal) and is accompanied by extensive unit and snapshot tests.
Changelog
  • cmd/rslint/api.go
    • Corrected rule option matching logic to use the registered rule name instead of the internal rule name when filtering rules.
  • internal/config/config.go
    • Imported the new no_loss_of_precision rule package.
    • Registered the no-loss-of-precision rule for both core ESLint and @typescript-eslint contexts.
  • internal/rules/no_loss_of_precision/no_loss_of_precision.go
    • Added the full implementation for the no-loss-of-precision rule, including functions to detect precision loss in base-10 and non-base-10 numeric literals.
    • Utilizes math/big for arbitrary precision arithmetic to accurately compare original and JavaScript-represented number values.
  • internal/rules/no_loss_of_precision/no_loss_of_precision.md
    • Added comprehensive documentation for the no-loss-of-precision rule, detailing its purpose, providing examples of correct and incorrect usage, and explaining when to use or disable it.
  • internal/rules/no_loss_of_precision/no_loss_of_precision_test.go
    • Added extensive unit tests for the no-loss-of-precision rule, covering various valid and invalid cases for integers, decimals, scientific notation, and different number bases (binary, octal, hexadecimal), including those with numeric separators.
  • packages/rslint-test-tools/rstest.config.mts
    • Enabled the no-loss-of-precision.test.ts file in the test configuration to include its tests in the overall test suite.
  • packages/rslint-test-tools/tests/typescript-eslint/rules/snapshots/no-loss-of-precision.test.ts.snap
    • Added snapshot tests for the no-loss-of-precision rule, capturing expected diagnostic output for specific invalid code examples.
Activity
  • The author, fansenze, initiated this pull request to introduce a new linter feature.
  • The pull request includes the complete implementation of the no-loss-of-precision rule, along with its documentation and a thorough test suite.
  • A bug in the rule option matching logic was identified and addressed as part of this change.
  • The author has confirmed that all Go unit tests, JS tests, TypeScript typechecks, and linting checks pass, indicating a well-tested and stable addition.
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 👍 and 👎 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.

Footnotes

  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.

Copy link
Copy Markdown

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

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

Code Review

This pull request successfully ports the no-loss-of-precision ESLint rule to rslint, enhancing the linter's ability to detect potential precision issues in numeric literals. The implementation appears thorough, covering various number formats including binary, octal, hexadecimal, and scientific notation. A critical bug in cmd/rslint/api.go related to rule option matching has also been addressed, ensuring that rule configurations are correctly applied. The addition of comprehensive test cases further validates the rule's functionality.

@fansenze fansenze force-pushed the feat/port-rule-no_loss_of_precision-20260203 branch 8 times, most recently from 52f2866 to ff6dc85 Compare February 4, 2026 03:15
@fansenze fansenze requested a review from hardfist February 4, 2026 03:37
@fansenze fansenze changed the title feat(linter): port rule no-loss-of-precision feat: port rule no-loss-of-precision Feb 4, 2026
@fansenze fansenze force-pushed the feat/port-rule-no_loss_of_precision-20260203 branch 2 times, most recently from 80f9ca5 to ac125fc Compare February 4, 2026 03:43
@fansenze fansenze force-pushed the feat/port-rule-no_loss_of_precision-20260203 branch from ac125fc to 76cd5e8 Compare February 4, 2026 09:05
@fansenze fansenze merged commit c3aa16f into main Feb 5, 2026
14 checks passed
@fansenze fansenze deleted the feat/port-rule-no_loss_of_precision-20260203 branch February 5, 2026 05:43
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.

2 participants