Skip to content

feat(linter): implement max-statements rule#17005

Closed
amondnet wants to merge 3 commits intooxc-project:mainfrom
amondnet:feat/max-statements-rule
Closed

feat(linter): implement max-statements rule#17005
amondnet wants to merge 3 commits intooxc-project:mainfrom
amondnet:feat/max-statements-rule

Conversation

@amondnet
Copy link
Contributor

@amondnet amondnet commented Dec 17, 2025

Summary

Implements the ESLint max-statements rule that enforces a maximum number of statements allowed in function blocks.

Features

  • ✅ Counts statements in function bodies, including nested blocks (if, for, while, switch, try, etc.)
  • ✅ Supports max option (default: 10)
  • ✅ Supports ignoreTopLevelFunctions option with correct ESLint behavior:
    • Single top-level function with violations is ignored
    • Multiple top-level functions with violations are all reported
  • ✅ Handles class static blocks correctly (functions inside static blocks are not "top-level")
  • ✅ Supports both number and object configuration forms
  • ✅ 20 pass cases and 22 fail cases from ESLint

Implementation Details

Uses run_once() instead of run() to handle the ignoreTopLevelFunctions option, which requires knowing about all function violations before deciding what to report.

Part of #479

Test Plan

  • All 22 ESLint fail cases pass
  • All 20 ESLint pass cases pass
  • No clippy warnings
  • Code formatted with just fmt

Implements the ESLint `max-statements` rule that enforces a maximum
number of statements allowed in function blocks.

Features:
- Counts statements in function bodies (including control flow blocks)
- Supports `max` option (default: 10)
- Supports `ignoreTopLevelFunctions` option
- Handles the ESLint behavior where a single top-level function
  violation is ignored, but multiple are reported

Closes oxc-project#479
Copilot AI review requested due to automatic review settings December 17, 2025 13:41
@amondnet amondnet requested a review from camc314 as a code owner December 17, 2025 13:41
@github-actions github-actions bot added A-linter Area - Linter C-enhancement Category - New feature or request labels Dec 17, 2025
Copy link
Contributor

Copilot AI left a comment

Choose a reason for hiding this comment

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

Pull request overview

This PR implements the ESLint max-statements rule that enforces a maximum number of statements allowed in function blocks. The implementation correctly handles statement counting in nested blocks (if, for, while, switch, try, etc.) and supports both the max option (default: 10) and the ignoreTopLevelFunctions option with ESLint-compliant behavior.

Key changes:

  • Implements statement counting logic that recursively counts statements in nested blocks while excluding nested function bodies
  • Uses run_once() to handle the ignoreTopLevelFunctions option correctly (single top-level violation ignored, multiple reported)
  • Supports both number and object configuration forms with proper parsing

Reviewed changes

Copilot reviewed 3 out of 4 changed files in this pull request and generated 5 comments.

File Description
crates/oxc_linter/src/rules/eslint/max_statements.rs Main implementation with configuration parsing, run_once logic, statement counting, and comprehensive test cases
crates/oxc_linter/src/rules.rs Module declaration for the new rule
crates/oxc_linter/src/generated/rule_runner_impls.rs Generated implementation marking the rule as using RunOnce
crates/oxc_linter/src/snapshots/eslint_max_statements.snap Test snapshots showing diagnostic output for all fail cases

💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.

@camc314 camc314 self-assigned this Dec 18, 2025
- Fix documentation for ignoreTopLevelFunctions option to accurately
  describe ESLint behavior: a single top-level function with violations
  is ignored, but multiple violations are all reported
- Update configuration parsing to use as_number() followed by as_u64()
  pattern to maintain consistency with other max-* rules
- Add Statement import to reduce verbose full-path references
- Add test for deprecated "maximum" property backward compatibility
- Add test for combined config object format
@camc314
Copy link
Contributor

camc314 commented Dec 21, 2025

fixed by #15804

@camc314 camc314 closed this Dec 21, 2025
@amondnet amondnet deleted the feat/max-statements-rule branch December 30, 2025 03:12
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

A-linter Area - Linter C-enhancement Category - New feature or request

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants