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

issue-93. added new consider_making_a_member_privat rule #188

Closed
wants to merge 3 commits into from
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 example/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ custom_lint:
- double_literal_format
- avoid_unnecessary_type_assertions
- avoid_debug_print_in_release
- consider_making_a_member_private
- avoid_using_api:
severity: info
entries:
Expand Down
1 change: 1 addition & 0 deletions lib/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ custom_lint:
- avoid_unused_parameters
- avoid_debug_print_in_release
- avoid_final_with_getter
- consider_making_a_member_private

- cyclomatic_complexity:
max_complexity: 10
Expand Down
2 changes: 2 additions & 0 deletions lib/solid_lints.dart
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import 'package:solid_lints/src/lints/avoid_unnecessary_type_casts/avoid_unneces
import 'package:solid_lints/src/lints/avoid_unrelated_type_assertions/avoid_unrelated_type_assertions_rule.dart';
import 'package:solid_lints/src/lints/avoid_unused_parameters/avoid_unused_parameters_rule.dart';
import 'package:solid_lints/src/lints/avoid_using_api/avoid_using_api_rule.dart';
import 'package:solid_lints/src/lints/consider_making_a_member_private/consider_making_a_member_private_rule.dart';
import 'package:solid_lints/src/lints/cyclomatic_complexity/cyclomatic_complexity_rule.dart';
import 'package:solid_lints/src/lints/double_literal_format/double_literal_format_rule.dart';
import 'package:solid_lints/src/lints/function_lines_of_code/function_lines_of_code_rule.dart';
Expand Down Expand Up @@ -65,6 +66,7 @@ class _SolidLints extends PluginBase {
AvoidDebugPrintInReleaseRule.createRule(configs),
PreferEarlyReturnRule.createRule(configs),
AvoidFinalWithGetterRule.createRule(configs),
ConsiderMakingAMemberPrivateRule.createRule(configs),
];

// Return only enabled rules
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/lints/consider_making_a_member_private/visitor/consider_making_a_member_private_visitor.dart';
import 'package:solid_lints/src/models/rule_config.dart';
import 'package:solid_lints/src/models/solid_lint_rule.dart';

/// Warns about public method, field if its not used outside.
///
/// ### Example:
/// #### BAD:
/// class X {
/// only used internally
/// void x() {
/// ...
/// }
///}
/// #### Good:
/// class X {
/// only used internally
/// void _x() {
/// ...
/// }
///}
/// #### Good:
/// class X {
///
/// void x() {
/// ...
/// }
///}
///
/// class Y {
///
/// final x = X();
///
/// void y(){
/// x.x();
/// }
/// }
class ConsiderMakingAMemberPrivateRule extends SolidLintRule {
/// This lint rule represents
/// the error whether we use public method, field if its not used outside.
static const String lintName = 'consider_making_a_member_private';

ConsiderMakingAMemberPrivateRule._(super.config);

/// Creates a new instance of [ConsiderMakingAMemberPrivateRule]
/// based on the lint configuration.
factory ConsiderMakingAMemberPrivateRule.createRule(
CustomLintConfigs configs,
) {
final rule = RuleConfig(
configs: configs,
name: lintName,
problemMessage: (_) => 'Consider making a member privat',
);

return ConsiderMakingAMemberPrivateRule._(rule);
}

@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
CustomLintContext context,
) {
context.registry.addCompilationUnit((node) {
final visitor = ConsiderMakingAMemberPrivateVisitor(node);
node.visitChildren(visitor);

final unusedMembers = visitor.unusedPublicMembers;
final unusedGlobalVariables = visitor.unusedGlobalVariables;
final unusedGlobalFunctions = visitor.unusedGlobalFunctions;

for (final member in unusedMembers) {
reporter.atNode(member, code);
}
for (final variable in unusedGlobalVariables) {
reporter.atNode(variable, code);
}
for (final function in unusedGlobalFunctions) {
reporter.atNode(function, code);
}
});
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,178 @@
// MIT License
//
// Copyright (c) 2020-2021 Dart Code Checker team
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';

/// AST Visitor for identifying unused public members, global functions,
/// and variables.
class ConsiderMakingAMemberPrivateVisitor extends RecursiveAstVisitor<void> {
final CompilationUnit _node;

final _unusedPublicMembers = <ClassMember>{};
final _unusedGlobalFunctions = <FunctionDeclaration>{};
final _unusedGlobalVariables = <VariableDeclaration>{};

/// Returns unused public members of classes.
Iterable<ClassMember> get unusedPublicMembers => _unusedPublicMembers;

/// Returns unused global functions.
Iterable<FunctionDeclaration> get unusedGlobalFunctions =>
_unusedGlobalFunctions;

/// Returns unused global variables.
Iterable<VariableDeclaration> get unusedGlobalVariables =>
_unusedGlobalVariables;

/// Constructor for [ConsiderMakingAMemberPrivateVisitor]
ConsiderMakingAMemberPrivateVisitor(this._node);

@override
void visitClassDeclaration(ClassDeclaration node) {
// Check for unused public members in a class.
final classMembers = node.members.where((member) {
if (member is MethodDeclaration || member is FieldDeclaration) {
final name = _getMemberName(member);
return name != null && !_isPrivate(name);
}
if (member is ConstructorDeclaration) {
final name = _getMemberName(member);
if (name == null) return true;

return !_isPrivate(name);
}
return false;
}).toList();

for (final member in classMembers) {
final memberName = _getMemberName(member);
if (!_isUsedOutsideClass(memberName, node)) {
_unusedPublicMembers.add(member);
}
}

super.visitClassDeclaration(node);
}

@override
void visitFunctionDeclaration(FunctionDeclaration node) {
final name = node.declaredElement?.name;
if (!_isPrivate(name) && !_isUsedEntity(name)) {
_unusedGlobalFunctions.add(node);
}
super.visitFunctionDeclaration(node);
}

@override
void visitTopLevelVariableDeclaration(TopLevelVariableDeclaration node) {
for (final variable in node.variables.variables) {
final name = variable.declaredElement?.name;
if (!_isPrivate(name) && !_isUsedEntity(name)) {
_unusedGlobalVariables.add(variable);
}
}
super.visitTopLevelVariableDeclaration(node);
}

bool _isPrivate(String? name) => name?.startsWith('_') ?? false;

String? _getMemberName(ClassMember member) {
if (member is MethodDeclaration) return member.declaredElement?.name;
if (member is FieldDeclaration) {
final fields = member.fields.variables;
return fields.isNotEmpty ? fields.first.declaredElement?.name : null;
}
if (member is ConstructorDeclaration) {
return member.declaredElement?.name;
}
return null;
}

bool _isUsedOutsideClass(String? memberName, ClassDeclaration classNode) {
bool isUsedOutside = false;

_node.visitChildren(
_GlobalEntityUsageVisitor(
entityName: memberName!,
onUsageFound: () {
isUsedOutside = true;
},
),
);

if (memberName.isEmpty) return true;

return isUsedOutside;
}

bool _isUsedEntity(String? entityName) {
bool isUsed = false;

if (entityName == null) return false;

_node.visitChildren(
_GlobalEntityUsageVisitor(
entityName: entityName,
onUsageFound: () {
isUsed = true;
},
),
);

return isUsed;
}
}

class _GlobalEntityUsageVisitor extends RecursiveAstVisitor<void> {
final String entityName;
final Function() onUsageFound;

_GlobalEntityUsageVisitor({
required this.entityName,
required this.onUsageFound,
});

@override
void visitSimpleIdentifier(SimpleIdentifier identifier) {
if (identifier.name == entityName) {
onUsageFound();
}
super.visitSimpleIdentifier(identifier);
}

@override
void visitInstanceCreationExpression(InstanceCreationExpression node) {
if (node.constructorName.name?.name == entityName) {
onUsageFound();
}
super.visitInstanceCreationExpression(node);
}

@override
void visitMethodInvocation(MethodInvocation node) {
if (node.target != null && node.target.toString() == entityName) {
onUsageFound();
}
super.visitMethodInvocation(node);
}
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: unused_field
// ignore_for_file: unused_field, consider_making_a_member_private
// ignore_for_file: unused_element
// ignore_for_file: prefer_match_file_name

Expand Down
1 change: 1 addition & 0 deletions lint_test/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -58,3 +58,4 @@ custom_lint:
- prefer_match_file_name
- proper_super_calls
- avoid_final_with_getter
- consider_making_a_member_private
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: unused_local_variable
// ignore_for_file: unused_local_variable, consider_making_a_member_private

import 'package:flutter/foundation.dart' as f;

Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: unused_local_variable
// ignore_for_file: unused_local_variable, consider_making_a_member_private

import 'package:flutter/foundation.dart';

Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_final_with_getter_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: type_annotate_public_apis, prefer_match_file_name, unused_local_variable
// ignore_for_file: type_annotate_public_apis, prefer_match_file_name, unused_local_variable, consider_making_a_member_private

/// Check final private field with getter fail
/// `avoid_final_with_getter`
Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_global_state_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: type_annotate_public_apis, prefer_match_file_name, unused_local_variable
// ignore_for_file: type_annotate_public_apis, prefer_match_file_name, unused_local_variable, consider_making_a_member_private

/// Check global mutable variable fail
/// `avoid_global_state`
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name, consider_making_a_member_private
// ignore_for_file: avoid_global_state

abstract class Animation {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name, consider_making_a_member_private
// ignore_for_file: avoid_global_state

class Subscription<T> {}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name, consider_making_a_member_private
// ignore_for_file: avoid_global_state

class ColorTween {}
Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_non_null_assertion_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: avoid_global_state, prefer_match_file_name
// ignore_for_file: avoid_global_state, prefer_match_file_name, consider_making_a_member_private
// ignore_for_file: member_ordering

/// Check "bang" operator fail
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: unused_element, prefer_match_file_name
// ignore_for_file: unused_element, prefer_match_file_name, consider_making_a_member_private
// ignore_for_file: member_ordering

/// Check returning a widget fail
Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_unnecessary_setstate_test.dart
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// ignore_for_file: member_ordering, prefer_match_file_name
// ignore_for_file: member_ordering, prefer_match_file_name, consider_making_a_member_private

import 'package:flutter/material.dart';

Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_unnecessary_type_assertions_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations, prefer_match_file_name, cyclomatic_complexity, unused_element
// ignore_for_file: prefer_const_declarations, prefer_match_file_name, cyclomatic_complexity, unused_element, consider_making_a_member_private
// ignore_for_file: unnecessary_nullable_for_final_variable_declarations
// ignore_for_file: unnecessary_type_check
// ignore_for_file: unused_local_variable
Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_unnecessary_type_casts_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations
// ignore_for_file: prefer_const_declarations, consider_making_a_member_private
// ignore_for_file: unnecessary_nullable_for_final_variable_declarations
// ignore_for_file: unnecessary_cast
// ignore_for_file: unused_local_variable
Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_unrelated_type_assertions_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations, prefer_match_file_name, unused_element
// ignore_for_file: prefer_const_declarations, prefer_match_file_name, unused_element, consider_making_a_member_private
// ignore_for_file: unnecessary_nullable_for_final_variable_declarations
// ignore_for_file: unused_local_variable

Expand Down
2 changes: 1 addition & 1 deletion lint_test/avoid_unused_parameters_test.dart
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// ignore_for_file: prefer_const_declarations, prefer_match_file_name, avoid_global_state
// ignore_for_file: prefer_const_declarations, prefer_match_file_name, avoid_global_state, consider_making_a_member_private
// ignore_for_file: unnecessary_nullable_for_final_variable_declarations
// ignore_for_file: unused_local_variable
// ignore_for_file: unused_element
Expand Down
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
//ignore_for_file:consider_making_a_member_private
// expect_lint: avoid_global_state
int banned = 5;
Loading
Loading