Skip to content

Commit

Permalink
Merge pull request #1 from yurii-prykhodko-solid/ignore-late-subclasses
Browse files Browse the repository at this point in the history
Allow subclasses for avoid-late whitelist
  • Loading branch information
maxxlab authored Nov 16, 2023
2 parents 850cfaa + b023b8c commit b4fe907
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 7 deletions.
17 changes: 13 additions & 4 deletions lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import 'package:custom_lint_builder/custom_lint_builder.dart';
import 'package:solid_lints/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart';
import 'package:solid_lints/models/rule_config.dart';
import 'package:solid_lints/models/solid_lint_rule.dart';
import 'package:solid_lints/utils/types_utils.dart';

/// A `late` keyword rule which forbids using it to avoid runtime exceptions.
class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
Expand Down Expand Up @@ -54,8 +55,16 @@ class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
return !hasInitializer;
}

bool _hasIgnoredType(VariableDeclaration node) =>
config.parameters.ignoredTypes.contains(
(node.parent! as VariableDeclarationList).type?.toSource() ?? '',
);
bool _hasIgnoredType(VariableDeclaration node) {
final variableType = node.declaredElement?.type;
if (variableType == null) return false;

final checkedTypes = [variableType, ...variableType.supertypes]
.map((t) => t.getDisplayString(withNullability: false))
.toSet();

final ignoredTypes = config.parameters.ignoredTypes.toSet();

return checkedTypes.intersection(ignoredTypes).isNotEmpty;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
/// parameters.
class AvoidLateKeywordParameters {
/// Allow immediately initialised late variables.
///
///
/// ```dart
/// late var ok = 0; // ok when allowInitialized == true
/// late var notOk; // initialized elsewhere, not allowed
Expand All @@ -22,6 +22,7 @@ class AvoidLateKeywordParameters {
factory AvoidLateKeywordParameters.fromJson(Map<String, Object?> json) =>
AvoidLateKeywordParameters(
allowInitialized: json['allow_initialized'] as bool? ?? false,
ignoredTypes: json['ignored_types'] as List<String>?,
ignoredTypes:
List<String>.from(json['ignored_types'] as Iterable? ?? []),
);
}
8 changes: 8 additions & 0 deletions lib/utils/types_utils.dart
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,18 @@
// SOFTWARE.
// ignore_for_file: public_member_api_docs

import 'package:analyzer/dart/element/element.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';

extension Subtypes on DartType {
Iterable<DartType> get supertypes {
final element = this.element;
return element is InterfaceElement ? element.allSupertypes : [];
}
}

bool hasWidgetType(DartType type) =>
(isWidgetOrSubclass(type) ||
_isIterable(type) ||
Expand Down
13 changes: 12 additions & 1 deletion lint_test/avoid_late_keyword_test.dart
Original file line number Diff line number Diff line change
@@ -1,7 +1,13 @@
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name
// ignore_for_file: avoid_global_state

import 'package:flutter/material.dart';
class ColorTween {}

class AnimationController {}

class SubAnimationController extends AnimationController {}

class NotAllowed {}

/// Check "late" keyword fail
///
Expand All @@ -12,6 +18,11 @@ class AvoidLateKeyword {

late final AnimationController controller1;

late final SubAnimationController controller2;

/// expect_lint: avoid_late_keyword
late final NotAllowed na;

late final field1 = 'string';

/// expect_lint: avoid_late_keyword
Expand Down

0 comments on commit b4fe907

Please sign in to comment.