Skip to content
Merged
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
@@ -1,6 +1,7 @@
## 0.3.2

- Added `named_parameters_ordering` rule.
- Upgraded to analyzer: ^8.4.0 and custom_lint_builder: ^0.8.1

## 0.3.1

Expand Down
2 changes: 1 addition & 1 deletion example/pubspec.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ dependencies:
sdk: flutter

dev_dependencies:
custom_lint: ^0.7.1
custom_lint: ^0.8.1
solid_lints:
path: ../
test: ^1.25.14
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ class ExcludedIdentifiersListParameter {

/// Returns whether the target node should be ignored during analysis.
bool shouldIgnore(Declaration node) {
final declarationName = node.declaredFragment?.name2;
final declarationName = node.declaredFragment?.name;

final excludedItem = exclude.firstWhereOrNull(
(e) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addFunctionExpressionInvocation(
Expand Down Expand Up @@ -102,7 +102,7 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
void _checkIdentifier({
required Identifier identifier,
required AstNode node,
required ErrorReporter reporter,
required DiagnosticReporter reporter,
}) {
if (!_isDebugPrintNode(identifier)) {
return;
Expand Down Expand Up @@ -137,7 +137,7 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
/// Handles variable assignment and declaration
void _handleVariableAssignmentDeclaration({
required AstNode node,
required ErrorReporter reporter,
required DiagnosticReporter reporter,
}) {
final rightOperand = _getRightOperand(node.childEntities.toList());

Expand All @@ -159,10 +159,10 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
case PrefixedIdentifier():
final prefix = node.prefix.name;
name = node.name.replaceAll('$prefix.', '');
sourcePath = node.element?.library2?.uri.toString() ?? '';
sourcePath = node.element?.library?.uri.toString() ?? '';
case SimpleIdentifier():
name = node.name;
sourcePath = node.element?.library2?.uri.toString() ?? '';
sourcePath = node.element?.library?.uri.toString() ?? '';

default:
return false;
Expand Down Expand Up @@ -193,10 +193,10 @@ your `debugPrint` call in a `!kReleaseMode` check.""",
final prefix = node.prefix.name;

name = node.name.replaceAll('$prefix.', '');
sourcePath = node.element?.library2?.uri.toString() ?? '';
sourcePath = node.element?.library?.uri.toString() ?? '';
case SimpleIdentifier():
name = node.name;
sourcePath = node.element?.library2?.uri.toString() ?? '';
sourcePath = node.element?.library?.uri.toString() ?? '';
default:
return false;
}
Expand Down
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import 'package:analyzer/error/error.dart' as error;
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
Expand Down Expand Up @@ -39,6 +39,8 @@ class AvoidFinalWithGetterRule extends SolidLintRule {
/// the error whether we use final private fields with getters.
static const lintName = 'avoid_final_with_getter';

final _diagnosticsInfoExpando = Expando<FinalWithGetterInfo>();

AvoidFinalWithGetterRule._(super.config);

/// Creates a new instance of [AvoidFinalWithGetterRule]
Expand All @@ -56,23 +58,21 @@ class AvoidFinalWithGetterRule extends SolidLintRule {
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addCompilationUnit((node) {
final visitor = AvoidFinalWithGetterVisitor();
node.accept(visitor);

for (final element in visitor.getters) {
reporter.atNode(
element.getter,
code,
data: element,
);
final diagnostic = reporter.atNode(element.getter, code);

_diagnosticsInfoExpando[diagnostic] = element;
}
});
}

@override
List<Fix> getFixes() => [_FinalWithGetterFix()];
List<Fix> getFixes() => [_FinalWithGetterFix(_diagnosticsInfoExpando)];
}
Original file line number Diff line number Diff line change
@@ -1,21 +1,25 @@
part of '../avoid_final_with_getter_rule.dart';

class _FinalWithGetterFix extends DartFix {
final Expando<FinalWithGetterInfo> _diagnosticsInfoExpando;

_FinalWithGetterFix(this._diagnosticsInfoExpando);

@override
void run(
CustomLintResolver resolver,
ChangeReporter reporter,
CustomLintContext context,
error.AnalysisError analysisError,
List<error.AnalysisError> others,
Diagnostic diagnostic,
List<Diagnostic> others,
) {
context.registry.addMethodDeclaration((node) {
if (analysisError.sourceRange.intersects(node.sourceRange)) {
final info = analysisError.data as FinalWithGetterInfo?;
if (info == null) return;
if (!diagnostic.sourceRange.intersects(node.sourceRange)) return;

final info = _diagnosticsInfoExpando[diagnostic];
if (info == null) return;

_addReplacement(reporter, info);
}
_addReplacement(reporter, info);
});
}

Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:solid_lints/src/lints/avoid_final_with_getter/visitors/getter_variable_visitor.dart';

/// A visitor that checks for final private fields with getters.
Expand All @@ -17,7 +17,7 @@ class AvoidFinalWithGetterVisitor extends RecursiveAstVisitor<void> {
case MethodDeclaration(
isGetter: true,
declaredFragment: ExecutableFragment(
element: ExecutableElement2(
element: ExecutableElement(
isAbstract: false,
isPublic: true,
)
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';

/// A visitor that checks the association of the getter with
/// the final private variable
Expand Down Expand Up @@ -31,14 +31,13 @@ class GetterVariableVisitor extends RecursiveAstVisitor<void> {

extension on MethodDeclaration {
int? get getterReferenceId {
if (body is! ExpressionFunctionBody) return null;

final expression = (body as ExpressionFunctionBody).expression;
if (expression is SimpleIdentifier) {
final element = expression.element;
if (element is PropertyAccessorElement2) {
return element.variable3?.id;
}
if (body
case ExpressionFunctionBody(
expression: SimpleIdentifier(
element: PropertyAccessorElement(:final variable)
)
)) {
return variable.id;
}

return null;
Expand Down
4 changes: 2 additions & 2 deletions lib/src/lints/avoid_global_state/avoid_global_state_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ class AvoidGlobalStateRule extends SolidLintRule {
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addTopLevelVariableDeclaration(
Expand All @@ -77,7 +77,7 @@ class AvoidGlobalStateRule extends SolidLintRule {
extension on VariableDeclaration {
bool get isMutable => !isFinal && !isConst;

bool get isPrivate => declaredElement2?.isPrivate ?? false;
bool get isPrivate => declaredFragment?.element.isPrivate ?? false;

bool get isPublicMutable => isMutable && !isPrivate;
}
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addVariableDeclaration((node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ class AvoidNonNullAssertionRule extends SolidLintRule {
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addPostfixExpression((node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AvoidReturningWidgetsRule
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addDeclaration((node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/src/common/visitors/select_expression_identifiers_visitor.dart';
Expand Down Expand Up @@ -58,7 +58,7 @@ Rewrite the variable evaluation into return statement instead.""",
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addReturnStatement(
Expand All @@ -74,14 +74,14 @@ Rewrite the variable evaluation into return statement instead.""",

void _checkReturnStatement({
required ReturnStatement statement,
required ErrorReporter reporter,
required DiagnosticReporter reporter,
required CustomLintContext context,
}) {
final expr = statement.expression;
if (expr is! SimpleIdentifier) return;

final element = expr.element;
if (element is! LocalVariableElement2) return;
if (element is! LocalVariableElement) return;
final returnVariableElement = element;

if (!returnVariableElement.isFinal && !returnVariableElement.isConst) {
Expand Down Expand Up @@ -125,17 +125,14 @@ Rewrite the variable evaluation into return statement instead.""",

bool _isSimpleIdentifierImmutable(SimpleIdentifier identifier) {
switch (identifier.element) {
case final VariableElement2 variable:
case final VariableElement variable:
return variable.isFinal || variable.isConst;

case ClassElement2 _:
case ClassElement _:
return true;

case final GetterElement getter:
if (getter.variable3 case final PropertyInducingElement2 property
when property.isFinal || property.isConst) {
return true;
}
case GetterElement(:final PropertyInducingElement variable):
return variable.isFinal || variable.isConst;
}

return false;
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/element2.dart';
import 'package:analyzer/dart/element/element.dart';
import 'package:collection/collection.dart';

/// This visitor is searches all uses of a single local variable,
Expand All @@ -9,10 +9,10 @@ class AvoidUnnecessaryReturnVariableVisitor extends RecursiveAstVisitor<void> {
/// The problem expects that exactly 1 mention of return variable.
/// VariableDeclarationStatement doesn't count when visiting SimpleIdentifier.
/// Any other amount of variable mentions implies that it is used somewhere
/// except return, so its existance is justified.
/// except return, so its existence is justified.
static const _badStatementCount = 1;

final LocalVariableElement2 _returnVariableElement;
final LocalVariableElement _returnVariableElement;

bool _foundTokensBetweenDeclarationAndReturn = false;
VariableDeclaration? _variableDeclaration;
Expand Down Expand Up @@ -57,7 +57,7 @@ class AvoidUnnecessaryReturnVariableVisitor extends RecursiveAstVisitor<void> {

bool _collectVariableDeclaration(VariableDeclarationStatement node) {
final targetVariable = node.variables.variables.firstWhereOrNull(
(v) => v.declaredElement2?.id == _returnVariableElement.id,
(v) => v.declaredFragment?.element.id == _returnVariableElement.id,
);
if (targetVariable == null) return false;

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -77,7 +77,7 @@ class AvoidUnnecessarySetStateRule extends SolidLintRule {
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addClassDeclaration((node) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/error.dart' as error;
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/error/listener.dart';
import 'package:analyzer/source/source_range.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
Expand Down Expand Up @@ -72,7 +72,7 @@ class AvoidUnnecessaryTypeAssertions extends SolidLintRule {
@override
void run(
CustomLintResolver resolver,
ErrorReporter reporter,
DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addIsExpression((node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class _UnnecessaryTypeAssertionsFix extends DartFix {
CustomLintResolver resolver,
ChangeReporter reporter,
CustomLintContext context,
error.AnalysisError analysisError,
List<error.AnalysisError> others,
Diagnostic analysisError,
List<Diagnostic> others,
) {
context.registry.addIsExpression((node) {
if (analysisError.sourceRange.intersects(node.sourceRange)) {
Expand Down
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/error/error.dart' as error;
import 'package:analyzer/diagnostic/diagnostic.dart';
import 'package:analyzer/error/listener.dart' as error_listener;
import 'package:analyzer/source/source_range.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';
Expand Down Expand Up @@ -33,7 +33,7 @@ class AvoidUnnecessaryTypeCastsRule extends SolidLintRule {
@override
void run(
CustomLintResolver resolver,
error_listener.ErrorReporter reporter,
error_listener.DiagnosticReporter reporter,
CustomLintContext context,
) {
context.registry.addAsExpression((node) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,8 @@ class _UnnecessaryTypeCastsFix extends DartFix {
CustomLintResolver resolver,
ChangeReporter reporter,
CustomLintContext context,
error.AnalysisError analysisError,
List<error.AnalysisError> others,
Diagnostic analysisError,
List<Diagnostic> others,
) {
context.registry.addAsExpression((node) {
if (analysisError.sourceRange.intersects(node.sourceRange)) {
Expand Down
Loading