Skip to content

Commit

Permalink
chore: Add DisposeConfig lint rule to ensure proper disposal of Confi…
Browse files Browse the repository at this point in the history
…g instances
  • Loading branch information
naipaka committed Dec 22, 2023
1 parent 421041b commit a4fb9a6
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions packages/flutterfire_lint/lib/src/lints/dispose_config.dart
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ import 'package:analyzer/dart/element/type.dart';
import 'package:analyzer/error/listener.dart';
import 'package:custom_lint_builder/custom_lint_builder.dart';

/// LintRule to ensure that Config instances are disposed.
class DisposeConfig extends DartLintRule {
const DisposeConfig() : super(code: _code);

Expand All @@ -13,12 +14,14 @@ class DisposeConfig extends DartLintRule {
"'Config' was created.",
);

bool isConfig(DartType type) => const TypeChecker.fromName(
/// Returns true if the given [type] is a Config of flutterfire_configurator.
bool _isConfig(DartType type) => const TypeChecker.fromName(
'Config',
packageName: 'flutterfire_configurator',
).isExactlyType(type);

Iterable<MethodInvocation> traverseMethodInvocations(AstNode node) {
/// Returns all method invocations in the given [node].
Iterable<MethodInvocation> _traverseMethodInvocations(AstNode node) {
final methodInvocations = <MethodInvocation>[];
void visitNode(AstNode node) {
if (node is MethodInvocation) {
Expand All @@ -37,7 +40,8 @@ class DisposeConfig extends DartLintRule {
return methodInvocations;
}

Iterable<PrefixedIdentifier> traversePrefixedIdentifiers(AstNode node) {
/// Returns all prefixed identifiers in the given [node].
Iterable<PrefixedIdentifier> _traversePrefixedIdentifiers(AstNode node) {
final prefixedIdentifiers = <PrefixedIdentifier>[];
void visitNode(AstNode node) {
if (node is PrefixedIdentifier) {
Expand All @@ -61,7 +65,7 @@ class DisposeConfig extends DartLintRule {
if (targetType == null) {
return;
}
if (!isConfig(targetType)) {
if (!_isConfig(targetType)) {
return;
}

Expand All @@ -70,7 +74,7 @@ class DisposeConfig extends DartLintRule {
return;
}

final methodInvocations = traverseMethodInvocations(compilationUnit);
final methodInvocations = _traverseMethodInvocations(compilationUnit);
for (final field in node.fields.variables) {
var isDisposed = false;
for (final methodInvocation in methodInvocations) {
Expand Down Expand Up @@ -99,7 +103,7 @@ class DisposeConfig extends DartLintRule {
continue;
}

if (!isConfig(variableType)) {
if (!_isConfig(variableType)) {
continue;
}

Expand All @@ -110,7 +114,7 @@ class DisposeConfig extends DartLintRule {

var isDisposed = false;

final methodInvocations = traverseMethodInvocations(function);
final methodInvocations = _traverseMethodInvocations(function);
for (final methodInvocation in methodInvocations) {
final target = methodInvocation.realTarget;
if (target is! SimpleIdentifier) {
Expand All @@ -123,7 +127,7 @@ class DisposeConfig extends DartLintRule {
}
}

final prefixIdentifiers = traversePrefixedIdentifiers(function);
final prefixIdentifiers = _traversePrefixedIdentifiers(function);
for (final prefixedIdentifier in prefixIdentifiers) {
final prefix = prefixedIdentifier.prefix;
final identifier = prefixedIdentifier.identifier;
Expand Down

0 comments on commit a4fb9a6

Please sign in to comment.