Skip to content
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

Excluded methods named 'copyWith' from `number_of_parameter… #142

Closed
Show file tree
Hide file tree
Changes from all 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_using_debug_print` rule
- Fixed an issue with no_magic_number lint
- Excluded methods/functions named 'copyWith' from `number_of_parameters` lint

## 0.1.4

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';

/// A number of parameters metric which checks whether we didn't exceed
/// the maximum allowed number of parameters for a function, method or
/// constructor.
/// constructor. Methods named 'copyWith' are excluded.
///
/// ### Example:
///
Expand Down Expand Up @@ -35,6 +35,27 @@ import 'package:solid_lints/src/models/solid_lint_rule.dart';
/// void method(a, b) {} // OK
/// }
/// ```
///
/// ### Allowed
/// ```dart
/// UserDto copyWith({
/// String? email,
/// String? firstName,
/// String? id,
/// String? imageUrl,
/// String? lastName,
/// String? phone,
/// }) {
/// return UserDto(
/// email: email ?? this.email,
/// firstName: firstName ?? this.firstName,
/// id: id ?? this.id,
/// imageUrl: imageUrl ?? this.imageUrl,
/// lastName: lastName ?? this.lastName,
/// phone: phone ?? this.phone,
/// );
/// }
/// ```
class NumberOfParametersMetric
extends SolidLintRule<NumberOfParametersParameters> {
/// The [LintCode] of this lint rule that represents the error if number of
Expand All @@ -58,13 +79,20 @@ class NumberOfParametersMetric
return NumberOfParametersMetric._(rule);
}

bool _hasAllowedName(String name) {
return name == 'copyWith';
}

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addDeclaration((node) {
if (node is! MethodDeclaration && node is! FunctionDeclaration) {
return;
}
final parameters = switch (node) {
(final MethodDeclaration node) =>
node.parameters?.parameters.length ?? 0,
Expand All @@ -73,11 +101,21 @@ class NumberOfParametersMetric
_ => 0,
};

if (parameters > config.parameters.maxParameters) {
reporter.reportErrorForOffset(
final isMethod = node is MethodDeclaration;

final declaredElement = node.declaredElement;
if (declaredElement == null) {
return;
}
final shouldInclude = switch (isMethod) {
true => !_hasAllowedName(node.declaredElement?.name ?? ''),
false => true,
};

if (parameters > config.parameters.maxParameters && shouldInclude) {
reporter.reportErrorForElement(
code,
node.firstTokenAfterCommentAndMetadata.offset,
node.end,
declaredElement,
);
}
});
Expand Down
65 changes: 64 additions & 1 deletion lint_test/number_of_parameters_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,70 @@
// ignore_for_file: public_member_api_docs, sort_constructors_first
// ignore_for_file: prefer_match_file_name
/// Check number of parameters fail
///
/// `number_of_parameters: max_parameters`
/// expect_lint: number_of_parameters

// expect_lint: number_of_parameters
String numberOfParameters(String a, String b, String c) {
return a + b + c;
}

// expect_lint: number_of_parameters
String copyWith(String a, String b, String c) {
return a + b + c;
}

class UserDto {
final String email;
final String firstName;
final String id;
final String imageUrl;
final String lastName;

final String phone;
const UserDto({
required this.id,
required this.firstName,
required this.lastName,
required this.imageUrl,
required this.email,
required this.phone,
});

UserDto copyWith({
String? email,
String? firstName,
String? id,
String? imageUrl,
String? lastName,
String? phone,
}) {
return UserDto(
email: email ?? this.email,
firstName: firstName ?? this.firstName,
id: id ?? this.id,
imageUrl: imageUrl ?? this.imageUrl,
lastName: lastName ?? this.lastName,
phone: phone ?? this.phone,
);
}

// expect_lint: number_of_parameters
UserDto test({
String? email,
String? firstName,
String? id,
String? imageUrl,
String? lastName,
String? phone,
}) {
return UserDto(
email: email ?? this.email,
firstName: firstName ?? this.firstName,
id: id ?? this.id,
imageUrl: imageUrl ?? this.imageUrl,
lastName: lastName ?? this.lastName,
phone: phone ?? this.phone,
);
}
}
Loading