Skip to content

Commit

Permalink
- Fixed function_lines_of_code for top level fumctions
Browse files Browse the repository at this point in the history
- Refactored tests for function_lines_of_code
  • Loading branch information
DerhachevAndrii committed Mar 13, 2024
1 parent d3a304f commit a5d7936
Show file tree
Hide file tree
Showing 4 changed files with 66 additions and 193 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ class FunctionLinesOfCodeMetric
void checkNode(AstNode node) => _checkNode(resolver, reporter, node);

context.registry.addMethodDeclaration(checkNode);
context.registry.addFunctionDeclaration(checkNode);
context.registry.addFunctionExpression(checkNode);
}

Expand All @@ -59,8 +60,19 @@ class FunctionLinesOfCodeMetric
ErrorReporter reporter,
AstNode node,
) {
if (node is MethodDeclaration &&
config.parameters.excludeNames.contains(node.name.lexeme)) {
final name = () {
if (node is FunctionDeclaration) {
return node.name.lexeme;
} else if (node is MethodDeclaration) {
return node.name.lexeme;
} else if (node is FunctionExpression) {
return node.declaredElement?.name;
} else {
return null;
}
}();

if (name != null && config.parameters.excludeNames.contains(name)) {
return;
}

Expand Down
11 changes: 11 additions & 0 deletions lint_test/function_lines_of_code_test/analysis_options.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
analyzer:
plugins:
- ../custom_lint

custom_lint:
rules:
- function_lines_of_code:
max_lines: 5
excludeNames:
- "longFunctionExcluded"
- "longMethodExcluded"
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
class ClassWithLongMethods {
// expect_lint: function_lines_of_code
int longMethod() {
var i = 0;
i++;
i++;
i++;
i++;
return i;
}

// Excluded by excludeNames
int longMethodExcluded() {
var i = 0;
i++;
i++;
i++;
i++;
return i;
}
}

// expect_lint: function_lines_of_code
int longFunction() {
var i = 0;
i++;
i++;
i++;
i++;
return i;
}

// Excluded by excludeNames
int longFunctionExcluded() {
var i = 0;
i++;
i++;
i++;
i++;
return i;
}
191 changes: 0 additions & 191 deletions lint_test/lines_of_code_test.dart

This file was deleted.

0 comments on commit a5d7936

Please sign in to comment.