Skip to content

fix: align no-constant-binary-expression with ESLint implementation#553

Merged
chenjiahan merged 1 commit intomainfrom
fix/no-constant-binary-expression-20260320
Mar 20, 2026
Merged

fix: align no-constant-binary-expression with ESLint implementation#553
chenjiahan merged 1 commit intomainfrom
fix/no-constant-binary-expression-20260320

Conversation

@fansenze
Copy link
Copy Markdown
Contributor

@fansenze fansenze commented Mar 20, 2026

Summary

Fix false positives in no-constant-binary-expression rule (closes #552) and fully align the implementation with ESLint's original rule behavior.

Root cause: isConstant() unconditionally returned true for !expr, causing normal short-circuit patterns like !module || !module[pluginName] to be falsely flagged.

Changes:

  • Rewrite isConstant with inBooleanPosition parameter for context-aware checking
  • Fix isStaticBoolean(!expr) to recursively check operand constancy instead of returning true
  • Fix isAlwaysNew to only flag built-in constructors (via TypeChecker scope analysis), not all new X()
  • Fix hasConstantNullishness with nonNullish parameter; correct ?? recursive logic (only check right side non-nullish); cover all binary operators including comparison operators
  • Restructure main rule logic: ===/!== uses single-side alwaysNew, ==/!= uses bothAlwaysNew
  • Add missing AST node handlers: typeof, delete, void, class expressions, template expressions, sequence/comma expressions, assignment expressions, BigInt literals, spread elements, logical assignments
  • Add scope analysis via isGlobalBuiltin() using TypeChecker + IsSymbolFromDefaultLibrary, with fallback for function declaration merging
  • Fix getBooleanValue to not strip quotes/backticks (node.Text() already returns parsed content without delimiters)
  • Fix rule definition: use rule.Rule{} instead of rule.CreateRule() (core ESLint rule should not have @typescript-eslint/ prefix)
  • Add comprehensive Go tests (47 valid + 85 invalid) and JS integration tests (87 cases with snapshots)

Related Links

Checklist

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

@gemini-code-assist
Copy link
Copy Markdown

Summary of Changes

Hello, 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 significantly enhances the no-constant-binary-expression linting rule by overhauling its core logic and expanding its AST analysis capabilities. The primary goal was to eliminate false positives and achieve full parity with the ESLint version of the rule, ensuring more reliable detection of redundant or unchanging binary expressions in codebases. This change improves the accuracy and utility of the linter, helping developers write cleaner and more efficient JavaScript.

Highlights

  • Rule Logic Alignment: The no-constant-binary-expression rule has been refactored to fully align its behavior with the original ESLint implementation, addressing previous false positives.
  • Enhanced Constant Detection: The isConstant helper function was rewritten to include an inBooleanPosition parameter, allowing for context-aware checking and more accurate identification of constant expressions.
  • Improved isStaticBoolean and isAlwaysNew: The isStaticBoolean function now recursively checks operand constancy for negated expressions, and isAlwaysNew was refined to only flag built-in constructors, preventing incorrect reports for user-defined constructors.
  • Comprehensive AST Node Handling: Support for various AST nodes, including typeof, delete, void, class expressions, template expressions, sequence/comma expressions, assignment expressions, BigInt literals, spread elements, and logical assignments, was added to improve the rule's accuracy.
  • Scope Analysis Integration: New scope analysis capabilities were introduced using isGlobalBuiltin() and TypeChecker to correctly identify global built-ins and handle function declaration merging, preventing issues with shadowed built-in functions.
  • Updated Rule Definition and Testing: The rule definition was updated to use rule.Rule{} directly, and extensive Go tests (127 sub-tests) and JS integration tests (81 cases with snapshots) were added to ensure robustness and correctness.
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.

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 is an excellent and thorough pull request that significantly improves the no-constant-binary-expression rule by aligning it with ESLint's implementation. The refactoring of the core logic is substantial, making it more robust and context-aware, particularly with the introduction of type-checker-based scope analysis to handle built-ins correctly. The addition of comprehensive unit and integration tests is a fantastic step towards ensuring correctness and preventing regressions. I've found one potential issue with how delete expressions are handled, which I've detailed in a specific comment. Overall, this is a very high-quality contribution.

Rewrite the rule to fix false positives on short-circuit patterns like
`!module || !module[pluginName]` and fully align with ESLint's original
`no-constant-binary-expression` rule behavior.

Key changes:
- Add `inBooleanPosition` parameter to `isConstant` for context-aware checking
- Fix `isStaticBoolean(!expr)` to recursively check operand constancy
- Fix `isAlwaysNew` to only flag built-in constructors via TypeChecker scope analysis
- Fix `hasConstantNullishness` with `nonNullish` parameter and correct `??` recursion
- Restructure main rule logic: `===`/`!==` uses single-side alwaysNew, `==`/`!=` uses bothAlwaysNew
- Add missing AST node handlers: typeof, delete, void, class, template, sequence, assignment expressions
- Add scope analysis via `isGlobalBuiltin` with TypeChecker + IsSymbolFromDefaultLibrary
- Fix `getBooleanValue` to not strip quotes (node.Text() already returns parsed content)
- Fix `hasConstantNullishness` to return true for all binary operators including comparisons
- Fix rule definition to use `rule.Rule{}` instead of `rule.CreateRule()` (core ESLint rule, no plugin prefix)
- Add comprehensive Go tests (47 valid + 85 invalid) and JS integration tests (87 cases with snapshots)
@fansenze fansenze force-pushed the fix/no-constant-binary-expression-20260320 branch from 5655c8d to 661ffa7 Compare March 20, 2026 06:54
@chenjiahan chenjiahan merged commit f4ca6a7 into main Mar 20, 2026
15 checks passed
@chenjiahan chenjiahan deleted the fix/no-constant-binary-expression-20260320 branch March 20, 2026 07:10
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.

[Bug]: no-constant-binary-expression reports valid short-circuit checks

2 participants