Skip to content
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

- Added `avoid_debug_print` rule
- Fixed an issue with no_magic_number lint
- 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,22 @@ class FunctionLinesOfCodeMetric
ErrorReporter reporter,
AstNode node,
) {
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;
}
}();

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Please, extract this into a separate private method or just inline it into current method. Do not use inline function declaration since it hurts readability.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


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

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

Expand Down
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;

/// Method names to be excluded from the rule check

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Suggested change
/// Method names to be excluded from the rule check
/// Function names to be excluded from the rule check

Does it make sense to be consistent with all the rest documentation/naming?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done

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"

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Thanks for changing the file name to a proper one!

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;
}
}
Comment on lines +27 to +29

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

Would be great to also test that it does not report false positives for both - methods and functions.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

done


// 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.