Skip to content

Commit

Permalink
Avoid-late ignored_types
Browse files Browse the repository at this point in the history
  • Loading branch information
maxxlab committed Nov 16, 2023
1 parent 13b5e61 commit 83629fe
Show file tree
Hide file tree
Showing 5 changed files with 48 additions and 4 deletions.
11 changes: 10 additions & 1 deletion lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart
Original file line number Diff line number Diff line change
Expand Up @@ -44,10 +44,19 @@ class AvoidLateKeywordRule extends SolidLintRule<AvoidLateKeywordParameters> {
final isLateDeclaration = node.declaredElement?.isLate ?? false;
if (!isLateDeclaration) return false;

final ignoredTypes = _hasIgnoredType(node);
if (ignoredTypes) return false;

final allowInitialized = config.parameters.allowInitialized;
if (!allowInitialized) return true; // all late`s are linted
if (!allowInitialized) return true;

final hasInitializer = node.initializer != null;
return !hasInitializer;
}

bool _hasIgnoredType(VariableDeclaration node) =>
config.parameters.ignoredTypes.contains(
// ignore: deprecated_member_use
node.declaredElement2?.type.getDisplayString(withNullability: false),
);
}
15 changes: 15 additions & 0 deletions lib/lints/avoid_late_keyword/models/_config_parser.dart
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
part of 'avoid_late_keyword_parameters.dart';

class _ConfigParser {
static const _allowInitializedConfig = 'allow_initialized';
static const _ignoredTypesConfig = 'ignored_types';

static bool parseAllowInitialized(Map<String, Object?> config) =>
config[_allowInitializedConfig] as bool? ?? false;

static Iterable<String> parseIgnoredTypes(Map<String, Object?> config) =>
config.containsKey(_ignoredTypesConfig) &&
config[_ignoredTypesConfig] is Iterable
? List<String>.from(config[_ignoredTypesConfig] as Iterable)
: <String>['AnimationController'];
}
Original file line number Diff line number Diff line change
@@ -1,17 +1,24 @@
part '_config_parser.dart';

/// A data model class that represents the "avoid late keyword" input
/// parameters.
class AvoidLateKeywordParameters {
/// Maximum number of parameters
/// Allow to dynamically initialize
final bool allowInitialized;

/// Types that would be ignored by avoid-late rule
final Iterable<String> ignoredTypes;

/// Constructor for [AvoidLateKeywordParameters] model
const AvoidLateKeywordParameters({
this.allowInitialized = false,
this.allowInitialized = false,
this.ignoredTypes = const [],
});

/// Method for creating from json data
factory AvoidLateKeywordParameters.fromJson(Map<String, Object?> json) =>
AvoidLateKeywordParameters(
allowInitialized: json['allow_initialized'] as bool?,
allowInitialized: _ConfigParser.parseAllowInitialized(json),
ignoredTypes: _ConfigParser.parseIgnoredTypes(json),
);
}
3 changes: 3 additions & 0 deletions lint_test/analysis_options.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,9 @@ custom_lint:
- avoid_non_null_assertion
- avoid_late_keyword:
allow_initialized: true
ignored_types:
- ColorTween
- AnimationController
- avoid_global_state
- avoid_returning_widgets
- avoid_unnecessary_setstate
Expand Down
10 changes: 10 additions & 0 deletions lint_test/avoid_late_keyword_test.dart
Original file line number Diff line number Diff line change
@@ -1,16 +1,26 @@
// ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name
// ignore_for_file: avoid_global_state

import 'package:flutter/material.dart';

/// Check "late" keyword fail
///
/// `avoid_late_keyword`
class AvoidLateKeyword {
late final ColorTween colorTween;

late final AnimationController controller1;

late final field1 = 'string';

/// expect_lint: avoid_late_keyword
late String field2;

void test() {
late final ColorTween colorTween;

late final AnimationController controller2;

late final field3 = 'string';

/// expect_lint: avoid_late_keyword
Expand Down

0 comments on commit 83629fe

Please sign in to comment.