From da4b8543b0c8de538eee6c928153d6bee59043d0 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Wed, 15 Nov 2023 15:51:57 +0200 Subject: [PATCH 01/23] Fix avoid-late if initialized --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 4 ++++ lint_test/avoid_late_keyword_test.dart | 6 ++++-- 2 files changed, 8 insertions(+), 2 deletions(-) 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 4e30bbf7..3e15bb36 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -32,6 +32,10 @@ class AvoidLateKeywordRule extends SolidLintRule { ) { context.registry.addVariableDeclaration((node) { if (node.declaredElement?.isLate ?? false) { + if (node.initializer != null) { + // Ignore late variables that are initialized right away + return; + } reporter.reportErrorForNode(code, node); } }); diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 105f1607..73717bf7 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -5,17 +5,19 @@ /// /// `avoid_late_keyword` class AvoidLateKeyword { - /// expect_lint: avoid_late_keyword late final field1 = 'string'; /// expect_lint: avoid_late_keyword late String field2; + + void test() { - /// expect_lint: avoid_late_keyword late final field3 = 'string'; /// expect_lint: avoid_late_keyword late String field4; + + late String field5 = 'string'; } } From f5f5bf2ef3bf1153df51b468ca859ba771bd6380 Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:14:47 +0200 Subject: [PATCH 02/23] Update lint_test/avoid_late_keyword_test.dart Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- lint_test/avoid_late_keyword_test.dart | 2 -- 1 file changed, 2 deletions(-) diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 73717bf7..296aa758 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -17,7 +17,5 @@ class AvoidLateKeyword { /// expect_lint: avoid_late_keyword late String field4; - - late String field5 = 'string'; } } From e34f82273dd1462653be679a4c6221a85f291c8c Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Wed, 15 Nov 2023 16:19:10 +0200 Subject: [PATCH 03/23] Apply suggestions from code review Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 8 +++----- lint_test/avoid_late_keyword_test.dart | 2 -- 2 files changed, 3 insertions(+), 7 deletions(-) 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 3e15bb36..1d83daf0 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -31,11 +31,9 @@ class AvoidLateKeywordRule extends SolidLintRule { CustomLintContext context, ) { context.registry.addVariableDeclaration((node) { - if (node.declaredElement?.isLate ?? false) { - if (node.initializer != null) { - // Ignore late variables that are initialized right away - return; - } + final isLateDeclaration = node.declaredElement?.isLate ?? false; + final hasInitializer = node.initializer != null; + if (isLateDeclaration && !hasInitializer) { reporter.reportErrorForNode(code, node); } }); diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 296aa758..5a01967e 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -10,8 +10,6 @@ class AvoidLateKeyword { /// expect_lint: avoid_late_keyword late String field2; - - void test() { late final field3 = 'string'; From 2633fc9e9a78e07476a257e1145163665c651eff Mon Sep 17 00:00:00 2001 From: maxxlab Date: Wed, 15 Nov 2023 17:40:58 +0200 Subject: [PATCH 04/23] Custom avoid-late --- .../avoid_late_keyword_rule.dart | 10 ++++++++-- .../models/avoid_late_keyword_parameters.dart | 20 +++++++++++++++++++ lint_test/analysis_options.yaml | 3 ++- 3 files changed, 30 insertions(+), 3 deletions(-) create mode 100644 lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.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 1d83daf0..4753d930 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -1,10 +1,11 @@ import 'package:analyzer/error/listener.dart'; 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'; /// A `late` keyword rule which forbids using it to avoid runtime exceptions. -class AvoidLateKeywordRule extends SolidLintRule { +class AvoidLateKeywordRule extends SolidLintRule { /// The [LintCode] of this lint rule that represents /// the error whether we use `late` keyword. static const lintName = 'avoid_late_keyword'; @@ -17,6 +18,7 @@ class AvoidLateKeywordRule extends SolidLintRule { final rule = RuleConfig( configs: configs, name: lintName, + paramsParser: AvoidLateKeywordParameters.fromJson, problemMessage: (_) => 'Avoid using the "late" keyword. ' 'It may result in runtime exceptions.', ); @@ -31,10 +33,14 @@ class AvoidLateKeywordRule extends SolidLintRule { CustomLintContext context, ) { context.registry.addVariableDeclaration((node) { + final initializedDynamically = config.parameters.initializedDynamically; final isLateDeclaration = node.declaredElement?.isLate ?? false; final hasInitializer = node.initializer != null; - if (isLateDeclaration && !hasInitializer) { + + if ((!initializedDynamically || !hasInitializer) && isLateDeclaration) { reporter.reportErrorForNode(code, node); + } else if (initializedDynamically && hasInitializer) { + return; } }); } 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 new file mode 100644 index 00000000..5475f939 --- /dev/null +++ b/lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart @@ -0,0 +1,20 @@ +/// A data model class that represents the "avoid late keyword" input +/// parameters. +class AvoidLateKeywordParameters { + /// Maximum number of parameters + final bool initializedDynamically; + + static const _defaulInitializedDynamically = false; + + /// Constructor for [AvoidLateKeywordParameters] model + const AvoidLateKeywordParameters({ + required this.initializedDynamically, + }); + + /// Method for creating from json data + factory AvoidLateKeywordParameters.fromJson(Map json) => + AvoidLateKeywordParameters( + initializedDynamically: json['allow-initialized'] as bool? ?? + _defaulInitializedDynamically, + ); +} diff --git a/lint_test/analysis_options.yaml b/lint_test/analysis_options.yaml index d7f934e4..f4b47f5d 100644 --- a/lint_test/analysis_options.yaml +++ b/lint_test/analysis_options.yaml @@ -11,7 +11,8 @@ custom_lint: - function_lines_of_code: max_lines: 50 - avoid_non_null_assertion - - avoid_late_keyword + - avoid_late_keyword: + allow-initialized: true - avoid_global_state - avoid_returning_widgets - avoid_unnecessary_setstate From f5a4a81f3f57d7082b80a4c95be6de5d18d8cdf4 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Wed, 15 Nov 2023 17:54:44 +0200 Subject: [PATCH 05/23] Fix naming --- .../avoid_late_keyword/avoid_late_keyword_rule.dart | 2 +- .../models/avoid_late_keyword_parameters.dart | 10 +++++----- 2 files changed, 6 insertions(+), 6 deletions(-) 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 4753d930..62a2d99f 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -33,7 +33,7 @@ class AvoidLateKeywordRule extends SolidLintRule { CustomLintContext context, ) { context.registry.addVariableDeclaration((node) { - final initializedDynamically = config.parameters.initializedDynamically; + final initializedDynamically = config.parameters.allowInitialized; final isLateDeclaration = node.declaredElement?.isLate ?? false; final hasInitializer = node.initializer != null; 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 5475f939..5cbd6227 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 @@ -2,19 +2,19 @@ /// parameters. class AvoidLateKeywordParameters { /// Maximum number of parameters - final bool initializedDynamically; + final bool allowInitialized; - static const _defaulInitializedDynamically = false; + static const _defaultAllowInitialized = false; /// Constructor for [AvoidLateKeywordParameters] model const AvoidLateKeywordParameters({ - required this.initializedDynamically, + required this.allowInitialized, }); /// Method for creating from json data factory AvoidLateKeywordParameters.fromJson(Map json) => AvoidLateKeywordParameters( - initializedDynamically: json['allow-initialized'] as bool? ?? - _defaulInitializedDynamically, + allowInitialized: json['allow-initialized'] as bool? ?? + _defaultAllowInitialized, ); } From 78eef078065aa4270f793095c447e57a2b111546 Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Wed, 15 Nov 2023 18:08:53 +0200 Subject: [PATCH 06/23] Apply suggestions from code review Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- lint_test/analysis_options.yaml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lint_test/analysis_options.yaml b/lint_test/analysis_options.yaml index f4b47f5d..9c591dff 100644 --- a/lint_test/analysis_options.yaml +++ b/lint_test/analysis_options.yaml @@ -12,7 +12,7 @@ custom_lint: max_lines: 50 - avoid_non_null_assertion - avoid_late_keyword: - allow-initialized: true + allow_initialized: true - avoid_global_state - avoid_returning_widgets - avoid_unnecessary_setstate From 8aeeeb2cc6b48f83ea95b5ba954acb50e76662e5 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Wed, 15 Nov 2023 19:18:03 +0200 Subject: [PATCH 07/23] Avoid late simplified --- .../avoid_late_keyword_rule.dart | 20 ++++++++++++------- .../models/avoid_late_keyword_parameters.dart | 2 +- 2 files changed, 14 insertions(+), 8 deletions(-) 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 62a2d99f..efd58b20 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -1,3 +1,4 @@ +import 'package:analyzer/dart/ast/ast.dart'; import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; import 'package:solid_lints/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart'; @@ -33,15 +34,20 @@ class AvoidLateKeywordRule extends SolidLintRule { CustomLintContext context, ) { context.registry.addVariableDeclaration((node) { - final initializedDynamically = config.parameters.allowInitialized; - final isLateDeclaration = node.declaredElement?.isLate ?? false; - final hasInitializer = node.initializer != null; - - if ((!initializedDynamically || !hasInitializer) && isLateDeclaration) { + if (_shouldLint(node)) { reporter.reportErrorForNode(code, node); - } else if (initializedDynamically && hasInitializer) { - return; } }); } + + bool _shouldLint(VariableDeclaration node) { + final isLateDeclaration = node.declaredElement?.isLate ?? false; + if (!isLateDeclaration) return false; + + final allowInitialized = config.parameters.allowInitialized; + if (!allowInitialized) return true; // all late`s are linted + + final hasInitializer = node.initializer != null; + return !hasInitializer; + } } 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 5cbd6227..213a5f4f 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 @@ -14,7 +14,7 @@ class AvoidLateKeywordParameters { /// Method for creating from json data factory AvoidLateKeywordParameters.fromJson(Map json) => AvoidLateKeywordParameters( - allowInitialized: json['allow-initialized'] as bool? ?? + allowInitialized: json['allow_initialized'] as bool? ?? _defaultAllowInitialized, ); } From 13b5e61b8d005dac6e145318d56d940fa7c45eb0 Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Wed, 15 Nov 2023 19:29:48 +0200 Subject: [PATCH 08/23] Update lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- .../models/avoid_late_keyword_parameters.dart | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) 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 213a5f4f..767d9d67 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 @@ -4,17 +4,14 @@ class AvoidLateKeywordParameters { /// Maximum number of parameters final bool allowInitialized; - static const _defaultAllowInitialized = false; - /// Constructor for [AvoidLateKeywordParameters] model const AvoidLateKeywordParameters({ - required this.allowInitialized, + this.allowInitialized = false, }); /// Method for creating from json data factory AvoidLateKeywordParameters.fromJson(Map json) => AvoidLateKeywordParameters( - allowInitialized: json['allow_initialized'] as bool? ?? - _defaultAllowInitialized, + allowInitialized: json['allow_initialized'] as bool?, ); } From 83629fed22e39ae99ea39abc0e2950098935d245 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Thu, 16 Nov 2023 13:30:59 +0200 Subject: [PATCH 09/23] 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 From 13a6d16b829438c3a2f734c9e7498934a78a0ee5 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Thu, 16 Nov 2023 14:36:18 +0200 Subject: [PATCH 10/23] Avoid-late ignored_types formatted --- lint_test/avoid_late_keyword_test.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 36a686db..0650d777 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -18,9 +18,9 @@ class AvoidLateKeyword { void test() { late final ColorTween colorTween; - + late final AnimationController controller2; - + late final field3 = 'string'; /// expect_lint: avoid_late_keyword From 56abdb6289f05fdf8fff7955adac723ac4ae46aa Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Thu, 16 Nov 2023 14:47:20 +0200 Subject: [PATCH 11/23] Update lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- .../models/avoid_late_keyword_parameters.dart | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) 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 edadb80a..7a0538c7 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 @@ -3,7 +3,12 @@ part '_config_parser.dart'; /// A data model class that represents the "avoid late keyword" input /// parameters. class AvoidLateKeywordParameters { - /// Allow to dynamically initialize + /// Allow immediately initialised late variables. + /// + /// ```dart + /// late var ok = 0; // ok when allowInitialized == true + /// late var notOk; // initialized elsewhere, not allowed + /// ``` final bool allowInitialized; /// Types that would be ignored by avoid-late rule From 6315ecafb6e0c09d1c39b8588bbdcb4a308db8cc Mon Sep 17 00:00:00 2001 From: maxxlab Date: Thu, 16 Nov 2023 15:51:31 +0200 Subject: [PATCH 12/23] Avoid-late ignored_types fix --- .../avoid_late_keyword_rule.dart | 4 ++-- .../avoid_late_keyword/models/_config_parser.dart | 15 --------------- .../models/avoid_late_keyword_parameters.dart | 9 +++++---- 3 files changed, 7 insertions(+), 21 deletions(-) delete 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 51c9ed01..c658a749 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -1,4 +1,5 @@ import 'package:analyzer/dart/ast/ast.dart'; +import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; import 'package:solid_lints/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart'; @@ -56,7 +57,6 @@ class AvoidLateKeywordRule extends SolidLintRule { bool _hasIgnoredType(VariableDeclaration node) => config.parameters.ignoredTypes.contains( - // ignore: deprecated_member_use - node.declaredElement2?.type.getDisplayString(withNullability: false), + (node.parent! as VariableDeclarationList).type?.toSource() ?? '', ); } diff --git a/lib/lints/avoid_late_keyword/models/_config_parser.dart b/lib/lints/avoid_late_keyword/models/_config_parser.dart deleted file mode 100644 index aca2b0c0..00000000 --- a/lib/lints/avoid_late_keyword/models/_config_parser.dart +++ /dev/null @@ -1,15 +0,0 @@ -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 edadb80a..d7b87299 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,5 +1,3 @@ -part '_config_parser.dart'; - /// A data model class that represents the "avoid late keyword" input /// parameters. class AvoidLateKeywordParameters { @@ -18,7 +16,10 @@ class AvoidLateKeywordParameters { /// Method for creating from json data factory AvoidLateKeywordParameters.fromJson(Map json) => AvoidLateKeywordParameters( - allowInitialized: _ConfigParser.parseAllowInitialized(json), - ignoredTypes: _ConfigParser.parseIgnoredTypes(json), + allowInitialized: json['allow_initialized'] as bool? ?? false, + ignoredTypes: json.containsKey('ignored_types') && + json['ignored_types'] is Iterable + ? List.from(json['ignored_types'] as Iterable) + : ['AnimationController'], ); } From dbd6bdfe300d2f0e7b9a9dcc94fa8d746bbc9ca4 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Thu, 16 Nov 2023 15:54:15 +0200 Subject: [PATCH 13/23] Avoid-late ignored_types Fix --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 1 - 1 file changed, 1 deletion(-) 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 c658a749..ae6e87ee 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -1,5 +1,4 @@ import 'package:analyzer/dart/ast/ast.dart'; -import 'package:analyzer/dart/element/type.dart'; import 'package:analyzer/error/listener.dart'; import 'package:custom_lint_builder/custom_lint_builder.dart'; import 'package:solid_lints/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart'; From d2158719c88f294749bc6ae23d0f2f25175ecbf3 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Thu, 16 Nov 2023 16:45:28 +0200 Subject: [PATCH 14/23] Avoid-late allow_initialized testcases --- .../analysis_options.yaml | 8 +++++ ...d_late_keyword_allow_initialized_test.dart | 34 +++++++++++++++++++ .../pubspec.yaml | 14 ++++++++ lint_test/avoid_late_keyword_test.dart | 1 + 4 files changed, 57 insertions(+) create mode 100644 lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml create mode 100644 lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart create mode 100644 lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml b/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml new file mode 100644 index 00000000..45cf91a8 --- /dev/null +++ b/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml @@ -0,0 +1,8 @@ +analyzer: + plugins: + - ../custom_lint + +custom_lint: + rules: + - avoid_late_keyword: + allow_initialized: false diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart b/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart new file mode 100644 index 00000000..56c354c1 --- /dev/null +++ b/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart @@ -0,0 +1,34 @@ +// 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` +/// allow_initialized option disabled +class AvoidLateKeyword { + /// expect_lint: avoid_late_keyword + late final ColorTween colorTween; + + late final AnimationController controller1; + + /// expect_lint: avoid_late_keyword + late final field1 = 'string'; + + /// expect_lint: avoid_late_keyword + late String field2; + + void test() { + /// expect_lint: avoid_late_keyword + late final ColorTween colorTween; + + late final AnimationController controller2; + + /// expect_lint: avoid_late_keyword + late final field3 = 'string'; + + /// expect_lint: avoid_late_keyword + late String field4; + } +} diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml b/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml new file mode 100644 index 00000000..99676bde --- /dev/null +++ b/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml @@ -0,0 +1,14 @@ +name: avoid_late_keyword_allow_initialized_test +publish_to: none + +environment: + sdk: '>=3.0.0 <4.0.0' + +dependencies: + flutter: + sdk: flutter + +dev_dependencies: + solid_lints: + path: ../../ + test: ^1.20.1 diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 0650d777..98653cfb 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -6,6 +6,7 @@ import 'package:flutter/material.dart'; /// Check "late" keyword fail /// /// `avoid_late_keyword` +/// allow_initialized option enabled class AvoidLateKeyword { late final ColorTween colorTween; From f639d919c2f6f344deb551b73db728472cf2449e Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:44:57 +0200 Subject: [PATCH 15/23] Update lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml | 1 - 1 file changed, 1 deletion(-) diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml b/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml index 99676bde..96763356 100644 --- a/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml +++ b/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml @@ -11,4 +11,3 @@ dependencies: dev_dependencies: solid_lints: path: ../../ - test: ^1.20.1 From 850cfaace78c05da4d3e8e9916aa2c9e37fbcb4c Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Thu, 16 Nov 2023 18:45:45 +0200 Subject: [PATCH 16/23] Update lib/lints/avoid_late_keyword/models/avoid_late_keyword_parameters.dart Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- .../models/avoid_late_keyword_parameters.dart | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) 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 4877300b..82313cec 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 @@ -22,9 +22,6 @@ class AvoidLateKeywordParameters { factory AvoidLateKeywordParameters.fromJson(Map json) => AvoidLateKeywordParameters( allowInitialized: json['allow_initialized'] as bool? ?? false, - ignoredTypes: json.containsKey('ignored_types') && - json['ignored_types'] is Iterable - ? List.from(json['ignored_types'] as Iterable) - : ['AnimationController'], + ignoredTypes: json['ignored_types'] as List?, ); } From b426d4996cf7bb26803092b2db89afb18dd736fc Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Thu, 16 Nov 2023 19:15:10 +0200 Subject: [PATCH 17/23] Allow subclasses for avoid-late whitelist --- .../avoid_late_keyword_rule.dart | 17 +++++++++++++---- .../models/avoid_late_keyword_parameters.dart | 5 +++-- lib/utils/types_utils.dart | 8 ++++++++ lint_test/avoid_late_keyword_test.dart | 13 ++++++++++++- 4 files changed, 36 insertions(+), 7 deletions(-) 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 ae6e87ee..226bfa5a 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -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 { @@ -54,8 +55,16 @@ class AvoidLateKeywordRule extends SolidLintRule { 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.subtypes] + .map((t) => t.getDisplayString(withNullability: false)) + .toSet(); + + final ignoredTypes = config.parameters.ignoredTypes.toSet(); + + return checkedTypes.intersection(ignoredTypes).isNotEmpty; + } } 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 82313cec..6051edd7 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 @@ -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 @@ -22,6 +22,7 @@ class AvoidLateKeywordParameters { factory AvoidLateKeywordParameters.fromJson(Map json) => AvoidLateKeywordParameters( allowInitialized: json['allow_initialized'] as bool? ?? false, - ignoredTypes: json['ignored_types'] as List?, + ignoredTypes: + List.from(json['ignored_types'] as Iterable? ?? []), ); } diff --git a/lib/utils/types_utils.dart b/lib/utils/types_utils.dart index d73ce35f..7f1c2133 100644 --- a/lib/utils/types_utils.dart +++ b/lib/utils/types_utils.dart @@ -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 get subtypes { + final element = this.element; + return element is InterfaceElement ? element.allSupertypes : []; + } +} + bool hasWidgetType(DartType type) => (isWidgetOrSubclass(type) || _isIterable(type) || diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 98653cfb..0063dd75 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -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 /// @@ -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 From b023b8cae168c79c894f0b2a434070ec6121c6c9 Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Thu, 16 Nov 2023 19:20:29 +0200 Subject: [PATCH 18/23] Fix naming --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 2 +- lib/utils/types_utils.dart | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) 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 226bfa5a..5f74af75 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -59,7 +59,7 @@ class AvoidLateKeywordRule extends SolidLintRule { final variableType = node.declaredElement?.type; if (variableType == null) return false; - final checkedTypes = [variableType, ...variableType.subtypes] + final checkedTypes = [variableType, ...variableType.supertypes] .map((t) => t.getDisplayString(withNullability: false)) .toSet(); diff --git a/lib/utils/types_utils.dart b/lib/utils/types_utils.dart index 7f1c2133..9d42947c 100644 --- a/lib/utils/types_utils.dart +++ b/lib/utils/types_utils.dart @@ -28,7 +28,7 @@ import 'package:analyzer/dart/element/type.dart'; import 'package:collection/collection.dart'; extension Subtypes on DartType { - Iterable get subtypes { + Iterable get supertypes { final element = this.element; return element is InterfaceElement ? element.allSupertypes : []; } From 662dc49c098132a6ca749558b7be4a260d0ec047 Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Thu, 16 Nov 2023 19:29:10 +0200 Subject: [PATCH 19/23] Short-circuit of there's no ignored types --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) 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 5f74af75..edaad1ed 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -59,12 +59,13 @@ class AvoidLateKeywordRule extends SolidLintRule { final variableType = node.declaredElement?.type; if (variableType == null) return false; + final ignoredTypes = config.parameters.ignoredTypes.toSet(); + if (ignoredTypes.isEmpty) 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; } } From 41fe9abe8c5a33afdfe1b640d5916f2054358b49 Mon Sep 17 00:00:00 2001 From: Yurii Prykhodko Date: Thu, 16 Nov 2023 19:30:00 +0200 Subject: [PATCH 20/23] Short-circuit earlier --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) 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 edaad1ed..52e1b2bc 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -56,12 +56,12 @@ class AvoidLateKeywordRule extends SolidLintRule { } bool _hasIgnoredType(VariableDeclaration node) { - final variableType = node.declaredElement?.type; - if (variableType == null) return false; - final ignoredTypes = config.parameters.ignoredTypes.toSet(); if (ignoredTypes.isEmpty) return false; + final variableType = node.declaredElement?.type; + if (variableType == null) return false; + final checkedTypes = [variableType, ...variableType.supertypes] .map((t) => t.getDisplayString(withNullability: false)) .toSet(); From fbf2270aac36fe2bd362e8782a558d610d939718 Mon Sep 17 00:00:00 2001 From: maxxlab <42914015+maxxlab@users.noreply.github.com> Date: Fri, 17 Nov 2023 11:00:20 +0200 Subject: [PATCH 21/23] Update lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart Co-authored-by: Yurii Prykhodko <144313329+yurii-prykhodko-solid@users.noreply.github.com> --- lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) 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 52e1b2bc..a41fc2a2 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -45,8 +45,8 @@ class AvoidLateKeywordRule extends SolidLintRule { final isLateDeclaration = node.declaredElement?.isLate ?? false; if (!isLateDeclaration) return false; - final ignoredTypes = _hasIgnoredType(node); - if (ignoredTypes) return false; + final hasIgnoredType = _hasIgnoredType(node); + if (hasIgnoredType) return false; final allowInitialized = config.parameters.allowInitialized; if (!allowInitialized) return true; From 203755065484d02f7fd00545d0deb8fae90422f7 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Fri, 17 Nov 2023 11:02:19 +0200 Subject: [PATCH 22/23] Avoid-late ignored_types tests --- .../avoid_late_keyword/avoid_late_keyword_rule.dart | 2 +- .../analysis_options.yaml | 2 ++ .../avoid_late_keyword_allow_initialized_test.dart | 10 +++++++--- .../pubspec.yaml | 2 -- lint_test/avoid_late_keyword_test.dart | 8 ++++---- 5 files changed, 14 insertions(+), 10 deletions(-) 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 52e1b2bc..29c2e92f 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -54,7 +54,7 @@ class AvoidLateKeywordRule extends SolidLintRule { final hasInitializer = node.initializer != null; return !hasInitializer; } - + bool _hasIgnoredType(VariableDeclaration node) { final ignoredTypes = config.parameters.ignoredTypes.toSet(); if (ignoredTypes.isEmpty) return false; diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml b/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml index 45cf91a8..7f92efe8 100644 --- a/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml +++ b/lint_test/avoid_late_keyword_allow_initialized_test/analysis_options.yaml @@ -6,3 +6,5 @@ custom_lint: rules: - avoid_late_keyword: allow_initialized: false + ignored_types: + - Animation diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart b/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart index 56c354c1..0cfeedea 100644 --- a/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart +++ b/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart @@ -1,8 +1,6 @@ // 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` @@ -22,7 +20,7 @@ class AvoidLateKeyword { void test() { /// expect_lint: avoid_late_keyword late final ColorTween colorTween; - + late final AnimationController controller2; /// expect_lint: avoid_late_keyword @@ -32,3 +30,9 @@ class AvoidLateKeyword { late String field4; } } + +abstract class Animation {} + +class AnimationController implements Animation {} + +class ColorTween {} \ No newline at end of file diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml b/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml index 96763356..3fd63997 100644 --- a/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml +++ b/lint_test/avoid_late_keyword_allow_initialized_test/pubspec.yaml @@ -5,8 +5,6 @@ environment: sdk: '>=3.0.0 <4.0.0' dependencies: - flutter: - sdk: flutter dev_dependencies: solid_lints: diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 0063dd75..5228faa1 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -20,14 +20,14 @@ class AvoidLateKeyword { late final SubAnimationController controller2; - /// expect_lint: avoid_late_keyword - late final NotAllowed na; - late final field1 = 'string'; - /// expect_lint: avoid_late_keyword + /// expect_lint: avoid_late_keyword late String field2; + /// expect_lint: avoid_late_keyword + late final NotAllowed na; + void test() { late final ColorTween colorTween; From bb69baa313d7c8d4c423e844d0706c27dd9564d9 Mon Sep 17 00:00:00 2001 From: maxxlab Date: Fri, 17 Nov 2023 12:12:55 +0200 Subject: [PATCH 23/23] Avoid-late add testcases --- .../avoid_late_keyword_rule.dart | 2 +- ...d_late_keyword_allow_initialized_test.dart | 62 +++++++++++++++---- lint_test/avoid_late_keyword_test.dart | 37 ++++++++--- 3 files changed, 81 insertions(+), 20 deletions(-) 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 3f5ec6eb..a41fc2a2 100644 --- a/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart +++ b/lib/lints/avoid_late_keyword/avoid_late_keyword_rule.dart @@ -54,7 +54,7 @@ class AvoidLateKeywordRule extends SolidLintRule { final hasInitializer = node.initializer != null; return !hasInitializer; } - + bool _hasIgnoredType(VariableDeclaration node) { final ignoredTypes = config.parameters.ignoredTypes.toSet(); if (ignoredTypes.isEmpty) return false; diff --git a/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart b/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart index 0cfeedea..04dbfe1d 100644 --- a/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart +++ b/lint_test/avoid_late_keyword_allow_initialized_test/avoid_late_keyword_allow_initialized_test.dart @@ -1,13 +1,33 @@ // ignore_for_file: prefer_const_declarations, unused_local_variable, prefer_match_file_name // ignore_for_file: avoid_global_state +abstract class Animation {} + +class AnimationController implements Animation {} + +class SubAnimationController extends AnimationController {} + +class ColorTween {} + /// Check "late" keyword fail /// /// `avoid_late_keyword` /// allow_initialized option disabled class AvoidLateKeyword { + late final Animation animation1; + + late final animation2 = AnimationController(); + + late final animation3 = SubAnimationController(); + + /// expect_lint: avoid_late_keyword + late final ColorTween colorTween1; + + /// expect_lint: avoid_late_keyword + late final colorTween2 = ColorTween(); + /// expect_lint: avoid_late_keyword - late final ColorTween colorTween; + late final colorTween3 = colorTween2; late final AnimationController controller1; @@ -15,24 +35,42 @@ class AvoidLateKeyword { late final field1 = 'string'; /// expect_lint: avoid_late_keyword - late String field2; + late final String field2; + + /// expect_lint: avoid_late_keyword + late final String field3 = 'string'; + + /// expect_lint: avoid_late_keyword + late final field4; void test() { + late final Animation animation1; + + late final animation2 = AnimationController(); + + late final animation3 = SubAnimationController(); + /// expect_lint: avoid_late_keyword - late final ColorTween colorTween; - - late final AnimationController controller2; + late final ColorTween colorTween1; /// expect_lint: avoid_late_keyword - late final field3 = 'string'; + late final colorTween2 = ColorTween(); /// expect_lint: avoid_late_keyword - late String field4; - } -} + late final colorTween3 = colorTween2; -abstract class Animation {} + late final AnimationController controller1; -class AnimationController implements Animation {} + /// expect_lint: avoid_late_keyword + late final local1 = 'string'; + + /// expect_lint: avoid_late_keyword + late final String local2; + + /// expect_lint: avoid_late_keyword + late final String local4 = 'string'; -class ColorTween {} \ No newline at end of file + /// expect_lint: avoid_late_keyword + late final local3; + } +} diff --git a/lint_test/avoid_late_keyword_test.dart b/lint_test/avoid_late_keyword_test.dart index 5228faa1..67ce76e3 100644 --- a/lint_test/avoid_late_keyword_test.dart +++ b/lint_test/avoid_late_keyword_test.dart @@ -20,22 +20,45 @@ class AvoidLateKeyword { late final SubAnimationController controller2; + late final controller3 = AnimationController(); + + late final controller4 = SubAnimationController(); + late final field1 = 'string'; - /// expect_lint: avoid_late_keyword - late String field2; + /// expect_lint: avoid_late_keyword + late final String field2; + + /// expect_lint: avoid_late_keyword + late final field3; /// expect_lint: avoid_late_keyword - late final NotAllowed na; - + late final NotAllowed na1; + + late final na2 = NotAllowed(); + void test() { late final ColorTween colorTween; - late final AnimationController controller2; + late final AnimationController controller1; + + late final SubAnimationController controller2; - late final field3 = 'string'; + late final controller3 = AnimationController(); + + late final controller4 = SubAnimationController(); + + late final local1 = 'string'; + + /// expect_lint: avoid_late_keyword + late final String local2; /// expect_lint: avoid_late_keyword - late String field4; + late final local3; + + /// expect_lint: avoid_late_keyword + late final NotAllowed na1; + + late final na2 = NotAllowed(); } }