Commit 3ab28a7
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- internal
- config
- rules/no_constant_binary_expression
- scripts
4 files changed
+839
-0
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
82 | 82 | | |
83 | 83 | | |
84 | 84 | | |
| 85 | + | |
85 | 86 | | |
86 | 87 | | |
87 | 88 | | |
| |||
433 | 434 | | |
434 | 435 | | |
435 | 436 | | |
| 437 | + | |
436 | 438 | | |
437 | 439 | | |
438 | 440 | | |
| |||
0 commit comments