Skip to content

Commit 3ab28a7

Browse files
delino[bot]github-actions[bot]claude
authored
feat: Port ESLint core rule no-constant-binary-expression (#74)
## Summary This PR ports the ESLint core rule `no-constant-binary-expression` to rslint, disallowing expressions where the operation doesn't affect the value. Follow-up task of PR #73. ## Implementation Details - ✅ Created new rule in `internal/rules/no_constant_binary_expression/` - ✅ Detects constant binary expressions with always-predictable results - ✅ Detects logical short-circuit operators (&&, ||, ??) with constant operands - ✅ Detects comparisons to newly constructed objects - ✅ Detects comparisons with constant nullishness - ✅ Detects comparisons with constant boolean values - ✅ Registered in global rule registry ## Rule Behavior The rule prevents binary expressions where the operation doesn't affect the outcome. This includes comparisons that always evaluate to true/false and logical expressions that always or never short-circuit. ### Invalid Patterns ```javascript // Constant short-circuit [] && greeting; true && hello; ({}) ?? foo; // Constant comparisons [] == true; ({}) === null; true === true; // Newly constructed object comparisons - can never be equal x === {}; x === []; [a] == [a]; ({}) == []; ``` ### Valid Patterns ```javascript // Variable references (not constant) bar && foo; foo == true; x === null; // Function calls (not constant) foo() && bar; delete bar.baz && foo; // Template literals with expressions `${bar}` && foo; // Assignment expressions (x += 1) && foo; ``` ## Test Coverage - ✅ Ported comprehensive test cases from ESLint's test suite - ✅ **18 valid test cases** covering various scenarios - ✅ **68 invalid test cases** with expected error detection - ✅ Tests include: - Constant short-circuit with &&, ||, ?? operators - Constant binary operands with ==, !=, ===, !== operators - Comparisons to newly constructed objects - Boolean, string, and numeric literal comparisons - Built-in constructor calls (Boolean, String, Number) - Unary negation operators - Shadowed built-in functions - Template literals with variables ## Test Plan - [x] Rule implementation follows rslint patterns - [x] All test cases ported from ESLint - [ ] Tests pass (requires full build environment with submodules) - [ ] Manual testing with example code - [ ] Integration with existing linter setup ## References - ESLint Rule: https://eslint.org/docs/latest/rules/no-constant-binary-expression - ESLint Source: https://github.com/eslint/eslint/blob/main/lib/rules/no-constant-binary-expression.js - ESLint Tests: https://github.com/eslint/eslint/blob/main/tests/lib/rules/no-constant-binary-expression.js - Related PR #73: #73 ## Files Changed - `internal/config/config.go` - Added rule registration (2 lines) - `internal/rules/no_constant_binary_expression/no_constant_binary_expression.go` - Complete rule implementation (353 lines) - `internal/rules/no_constant_binary_expression/no_constant_binary_expression_test.go` - Comprehensive test suite (430 lines) 🤖 Generated with [Claude Code](https://claude.com/claude-code) --------- Co-authored-by: claude[bot] <41898282+claude[bot]@users.noreply.github.com> Co-authored-by: Claude <noreply@anthropic.com>
1 parent 7e8270c commit 3ab28a7

File tree

4 files changed

+839
-0
lines changed

4 files changed

+839
-0
lines changed

internal/config/config.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -82,6 +82,7 @@ import (
8282
"github.com/web-infra-dev/rslint/internal/rules/no_compare_neg_zero"
8383
"github.com/web-infra-dev/rslint/internal/rules/no_cond_assign"
8484
"github.com/web-infra-dev/rslint/internal/rules/no_const_assign"
85+
"github.com/web-infra-dev/rslint/internal/rules/no_constant_binary_expression"
8586
)
8687

8788
// RslintConfig represents the top-level configuration array
@@ -433,6 +434,7 @@ func registerAllCoreEslintRules() {
433434
GlobalRuleRegistry.Register("no-compare-neg-zero", no_compare_neg_zero.NoCompareNegZeroRule)
434435
GlobalRuleRegistry.Register("no-cond-assign", no_cond_assign.NoCondAssignRule)
435436
GlobalRuleRegistry.Register("no-const-assign", no_const_assign.NoConstAssignRule)
437+
GlobalRuleRegistry.Register("no-constant-binary-expression", no_constant_binary_expression.NoConstantBinaryExpressionRule)
436438
}
437439

438440
// getAllTypeScriptEslintPluginRules returns all registered rules (for backward compatibility when no config is provided)

0 commit comments

Comments
 (0)