Skip to content

Commit

Permalink
- Added excludeNames param for function_lines_of_code lint (#141)
Browse files Browse the repository at this point in the history
- Added `excludeNames` param for `function_lines_of_code` lint
- Refactored tests for function_lines_of_code
  • Loading branch information
DerhachevAndrii authored Mar 15, 2024
1 parent 8ed3f80 commit 9adae1f
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 191 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
- ignores methods that override ones that return widget (build() for example)
- no longer allows returning widgets from methods/functions named build
- Fixed unexpected avoid_unnecessary_type_assertions
- Added `excludeNames` param for `function_lines_of_code` lint

## 0.1.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
/// rules:
/// - function_lines_of_code:
/// max_lines: 100
/// excludeNames:
/// - "Build"
/// ```
class FunctionLinesOfCodeMetric
extends SolidLintRule<FunctionLinesOfCodeParameters> {
Expand Down Expand Up @@ -49,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 @@ -57,6 +60,12 @@ class FunctionLinesOfCodeMetric
ErrorReporter reporter,
AstNode node,
) {
final functionName = _getFunctionName(node);
if (functionName != null &&
config.parameters.excludeNames.contains(functionName)) {
return;
}

final visitor = FunctionLinesOfCodeVisitor(resolver.lineInfo);
node.visitChildren(visitor);

Expand All @@ -75,4 +84,16 @@ class FunctionLinesOfCodeMetric
);
}
}

String? _getFunctionName(AstNode node) {
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;
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,22 @@ class FunctionLinesOfCodeParameters {
/// exceeding this limit triggers a warning.
final int maxLines;

/// Function names to be excluded from the rule check
final List<String> excludeNames;

static const _defaultMaxLines = 200;

/// Constructor for [FunctionLinesOfCodeParameters] model
const FunctionLinesOfCodeParameters({
required this.maxLines,
required this.excludeNames,
});

/// Method for creating from json data
factory FunctionLinesOfCodeParameters.fromJson(Map<String, Object?> json) =>
FunctionLinesOfCodeParameters(
maxLines: json['max_lines'] as int? ?? _defaultMaxLines,
excludeNames:
List<String>.from(json['excludeNames'] as Iterable? ?? []),
);
}
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,57 @@
class ClassWithLongMethods {
int notLongMethod() {
var i = 0;
i++;
i++;
i++;
return i;
}

// 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;
}
}

int notLongFunction() {
var i = 0;
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 9adae1f

Please sign in to comment.