-
-
Notifications
You must be signed in to change notification settings - Fork 727
feat(linter): implement rule max-statements #15804
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Conversation
How to use the Graphite Merge QueueAdd either label to this PR to merge it via the merge queue:
You must have a Graphite account in order to use the merge queue. Sign up using this link. An organization admin has enabled the Graphite Merge Queue in this repository. Please do not merge from GitHub as this will restart CI on PRs being processed by the merge queue. |
| fn is_top_level_function(function_span: Span, ctx: &LintContext) -> bool { | ||
| let mut top_level_functions_finder = | ||
| TopLevelFunctionFinder { function_depth: 0, top_level_functions: vec![] }; | ||
| top_level_functions_finder.visit_program(ctx.nodes().program()); | ||
|
|
||
| // If there are several top-level functions, it means that the actual top-level function is the module (i.e., the file) itself. | ||
| // If there is only one top-level function, it should be ignored. | ||
| if top_level_functions_finder.top_level_functions.len() > 1 { | ||
| return false; | ||
| } | ||
| match top_level_functions_finder.top_level_functions.pop() { | ||
| Some(top_level_function) => top_level_function.span() == function_span, | ||
| None => false, | ||
| } | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The is_top_level_function implementation appears to have a logical issue. According to the test cases, all functions defined at the module level should be considered top-level functions, but the current implementation returns false if there are multiple top-level functions.
The condition on lines 330-332 that checks for multiple functions should be removed. Instead, the function should simply check if the given function's span matches any of the spans in the top_level_functions list.
This would better align with the expected behavior demonstrated in the test cases where all module-level functions are treated as top-level functions when ignoreTopLevelFunctions is enabled.
| fn is_top_level_function(function_span: Span, ctx: &LintContext) -> bool { | |
| let mut top_level_functions_finder = | |
| TopLevelFunctionFinder { function_depth: 0, top_level_functions: vec![] }; | |
| top_level_functions_finder.visit_program(ctx.nodes().program()); | |
| // If there are several top-level functions, it means that the actual top-level function is the module (i.e., the file) itself. | |
| // If there is only one top-level function, it should be ignored. | |
| if top_level_functions_finder.top_level_functions.len() > 1 { | |
| return false; | |
| } | |
| match top_level_functions_finder.top_level_functions.pop() { | |
| Some(top_level_function) => top_level_function.span() == function_span, | |
| None => false, | |
| } | |
| } | |
| fn is_top_level_function(function_span: Span, ctx: &LintContext) -> bool { | |
| let mut top_level_functions_finder = | |
| TopLevelFunctionFinder { function_depth: 0, top_level_functions: vec![] }; | |
| top_level_functions_finder.visit_program(ctx.nodes().program()); | |
| // Check if the given function is one of the top-level functions | |
| top_level_functions_finder.top_level_functions.iter().any(|func| func.span() == function_span) | |
| } | |
Spotted by Graphite Agent
Is this helpful? React 👍 or 👎 to let us know.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't agree to this. The suggestion would even break unit tests.
My understanding is as I stated in the comment in line 324:
"If there are several top-level functions, it means that the actual top-level function is the module (i.e., the file) itself. If there is only one top-level function, it should be ignored."
CodSpeed Performance ReportMerging #15804 will not alter performanceComparing Summary
Footnotes
|
961e317 to
3efd152
Compare
|
CI / Test Linux fails with error @camc314 Unfortunately, I don't know how to fix this one. |
Add ESLint rule
max-statements.