From 83629fed22e39ae99ea39abc0e2950098935d245 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Thu, 16 Nov 2023 13:30:59 +0200 Subject: [PATCH] Avoid-late ignored_types --- .../avoid_late_keyword_rule.dart | 11 ++++++++++- .../avoid_late_keyword/models/_config_parser.dart | 15 +++++++++++++++ .../models/avoid_late_keyword_parameters.dart | 13 ++++++++++--- lint_test/analysis_options.yaml | 3 +++ lint_test/avoid_late_keyword_test.dart | 10 ++++++++++ 5 files changed, 48 insertions(+), 4 deletions(-) create mode 100644 lib/lints/avoid_late_keyword/models/_config_parser.dart diff --git a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart index efd58b20..51c9ed01 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -44,10 +44,19 @@ class AvoidLateKeywordRule extends SolidLintRule { 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), + ); } diff --git a/lib/lints/avoid_late_keyword/models/_config_parser.dart b/lib/lints/avoid_late_keyword/models/_config_parser.dart new file mode 100644 index 00000000..aca2b0c0 --- /dev/null +++ b/lib/lints/avoid_late_keyword/models/_config_parser.dart @@ -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 config) => + config[_allowInitializedConfig] as bool? ?? false; + + static Iterable parseIgnoredTypes(Map config) => + config.containsKey(_ignoredTypesConfig) && + config[_ignoredTypesConfig] is Iterable + ? List.from(config[_ignoredTypesConfig] as Iterable) + : ['AnimationController']; +} diff --git a/lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart b/lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart index 767d9d67..edadb80a 100644 --- a/lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart +++ b/lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart @@ -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 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 json) => AvoidLateKeywordParameters( - allowInitialized: json['allow_initialized'] as bool?, + allowInitialized: _ConfigParser.parseAllowInitialized(json), + ignoredTypes: _ConfigParser.parseIgnoredTypes(json), ); } diff --git a/lint_test/analysis_options.yaml b/lint_test/analysis_options.yaml index 9c591dff..b91cff3c 100644 --- a/lint_test/analysis_options.yaml +++ b/lint_test/analysis_options.yaml @@ -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 diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 5a01967e..36a686db 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -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