feat(linter): implement max-statements rule#17005
Closed
amondnet wants to merge 3 commits intooxc-project:mainfrom
Closed
feat(linter): implement max-statements rule#17005amondnet wants to merge 3 commits intooxc-project:mainfrom
amondnet wants to merge 3 commits intooxc-project:mainfrom
Conversation
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
Contributor
There was a problem hiding this comment.
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 theignoreTopLevelFunctionsoption 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.
- 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
Contributor
|
fixed by #15804 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Implements the ESLint
max-statementsrule that enforces a maximum number of statements allowed in function blocks.Features
maxoption (default: 10)ignoreTopLevelFunctionsoption with correct ESLint behavior:Implementation Details
Uses
run_once()instead ofrun()to handle theignoreTopLevelFunctionsoption, which requires knowing about all function violations before deciding what to report.Part of #479
Test Plan
just fmt