Skip to content

Commit

Permalink
fix pr comments
Browse files Browse the repository at this point in the history
  • Loading branch information
ylaptievwmg authored and vova-beloded-solid committed Aug 31, 2023
1 parent 72132d3 commit c11851b
Show file tree
Hide file tree
Showing 3 changed files with 2 additions and 51 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ import 'package:solid_lints/models/solid_lint_rule.dart';
/// or build methods and when it's called from a sync method that is called
/// inside those methods.
class AvoidUnnecessarySetStateRule extends SolidLintRule {
/// The [LintCode] of this lint rule that represents
/// The lint name of this lint rule that represents
/// the error whether we use setState in inappropriate way.
static const lintName = 'avoid_unnecessary_setstate';

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -31,15 +31,10 @@ class AvoidUnnecessarySetStateMethodVisitor extends RecursiveAstVisitor<void> {
final Iterable<FunctionBody> _bodies;

final _setStateInvocations = <MethodInvocation>[];
final _classMethodsInvocations = <MethodInvocation>[];

/// All setState invocations
Iterable<MethodInvocation> get setStateInvocations => _setStateInvocations;

/// All sync method invocations with setState inside
Iterable<MethodInvocation> get classMethodsInvocations =>
_classMethodsInvocations;

/// Constructor for AvoidUnnecessarySetStateMethodVisitor
AvoidUnnecessarySetStateMethodVisitor(this._classMethodsNames, this._bodies);

Expand All @@ -55,7 +50,7 @@ class AvoidUnnecessarySetStateMethodVisitor extends RecursiveAstVisitor<void> {
} else if (_classMethodsNames.contains(name) &&
notInBody &&
node.realTarget == null) {
_classMethodsInvocations.add(node);
_setStateInvocations.add(node);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,11 +56,6 @@ class AvoidUnnecessarySetStateVisitor extends RecursiveAstVisitor<void> {
final methods = declarations
.where((member) => _checkedMethods.contains(member.name.lexeme))
.toList();
final restMethods = declarations
.where((member) => !_checkedMethods.contains(member.name.lexeme))
.toList();

final visitedRestMethods = <String, bool>{};

for (final method in methods) {
final visitor =
Expand All @@ -69,46 +64,7 @@ class AvoidUnnecessarySetStateVisitor extends RecursiveAstVisitor<void> {

_setStateInvocations.addAll([
...visitor.setStateInvocations,
...visitor.classMethodsInvocations
.where(
(invocation) => _containsSetState(
visitedRestMethods,
classMethodsNames,
bodies,
restMethods.firstWhere(
(method) => method.name.lexeme == invocation.methodName.name,
),
),
)
.toList(),
]);
}
}

bool _containsSetState(
Map<String, bool> visitedRestMethods,
Set<String> classMethodsNames,
Iterable<FunctionBody> bodies,
MethodDeclaration declaration,
) {
final type = declaration.returnType?.type;
if (type != null && (type.isDartAsyncFuture || type.isDartAsyncFutureOr)) {
return false;
}

final name = declaration.name.lexeme;
if (visitedRestMethods.containsKey(name) && visitedRestMethods[name]!) {
return true;
}

final visitor =
AvoidUnnecessarySetStateMethodVisitor(classMethodsNames, bodies);
declaration.visitChildren(visitor);

final hasSetState = visitor.setStateInvocations.isNotEmpty;

visitedRestMethods[name] = hasSetState;

return hasSetState;
}
}

0 comments on commit c11851b

Please sign in to comment.