Skip to content
This repository was archived by the owner on Jul 16, 2023. It is now read-only.

Commit 3130ac9

Browse files
committed
feat: add allow-initialized option to avoid-late-keyword
1 parent 52c597b commit 3130ac9

File tree

8 files changed

+60
-4
lines changed

8 files changed

+60
-4
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
* feat: exclude `.freezed.dart` files by default.
77
* fix: handle try and switch statements for [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously)
88
* chore: restrict `analyzer` version to `>=5.1.0 <5.4.0`.
9+
* feat: add `allow-initialized` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).
910

1011
## 5.4.0
1112

lib/src/analyzers/lint_analyzer/rules/rules_list/avoid_late_keyword/avoid_late_keyword_rule.dart

+6-2
Original file line numberDiff line numberDiff line change
@@ -11,15 +11,19 @@ import '../../../models/severity.dart';
1111
import '../../models/common_rule.dart';
1212
import '../../rule_utils.dart';
1313

14+
part 'config_parser.dart';
1415
part 'visitor.dart';
1516

1617
class AvoidLateKeywordRule extends CommonRule {
1718
static const String ruleId = 'avoid-late-keyword';
1819

1920
static const _warning = "Avoid using 'late' keyword.";
2021

22+
final bool _allowInitialized;
23+
2124
AvoidLateKeywordRule([Map<String, Object> config = const {}])
22-
: super(
25+
: _allowInitialized = _ConfigParser.parseAllowInitialized(config),
26+
super(
2327
id: ruleId,
2428
severity: readSeverity(config, Severity.warning),
2529
excludes: readExcludes(config),
@@ -28,7 +32,7 @@ class AvoidLateKeywordRule extends CommonRule {
2832

2933
@override
3034
Iterable<Issue> check(InternalResolvedUnitResult source) {
31-
final visitor = _Visitor();
35+
final visitor = _Visitor(_allowInitialized);
3236

3337
source.unit.visitChildren(visitor);
3438

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
part of 'avoid_late_keyword_rule.dart';
2+
3+
class _ConfigParser {
4+
static const _allowInitializedConfig = 'allow-initialized';
5+
6+
static bool parseAllowInitialized(Map<String, Object> config) =>
7+
config[_allowInitializedConfig] as bool? ?? false;
8+
}
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,25 @@
11
part of 'avoid_late_keyword_rule.dart';
22

33
class _Visitor extends RecursiveAstVisitor<void> {
4+
final bool allowInitialized;
5+
46
final _declarations = <AstNode>[];
57

68
Iterable<AstNode> get declarations => _declarations;
79

10+
// ignore: avoid_positional_boolean_parameters
11+
_Visitor(this.allowInitialized);
12+
813
@override
914
void visitVariableDeclaration(VariableDeclaration node) {
1015
super.visitVariableDeclaration(node);
1116

1217
if (node.isLate && node.parent != null) {
1318
final parent = node.parent;
1419

15-
_declarations.add(parent ?? node);
20+
if (!(allowInitialized && node.initializer != null)) {
21+
_declarations.add(parent ?? node);
22+
}
1623
}
1724
}
1825
}

test/src/analyzers/lint_analyzer/rules/rules_list/avoid_late_keyword/avoid_late_keyword_rule_test.dart

+22
Original file line numberDiff line numberDiff line change
@@ -45,5 +45,27 @@ void main() {
4545
],
4646
);
4747
});
48+
49+
test('reports about found issues with config', () async {
50+
final unit = await RuleTestHelper.resolveFromFile(_examplePath);
51+
final issues =
52+
AvoidLateKeywordRule({'allow-initialized': true}).check(unit);
53+
54+
RuleTestHelper.verifyIssues(
55+
issues: issues,
56+
startLines: [8, 17, 23],
57+
startColumns: [3, 5, 1],
58+
locationTexts: [
59+
'late String uninitializedField',
60+
'late String uninitializedVariable',
61+
'late String topLevelUninitializedVariable',
62+
],
63+
messages: [
64+
"Avoid using 'late' keyword.",
65+
"Avoid using 'late' keyword.",
66+
"Avoid using 'late' keyword.",
67+
],
68+
);
69+
});
4870
});
4971
}

website/docs/rules/common/avoid-late-keyword.mdx

+13
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,19 @@ Use this rule if you want to avoid unexpected runtime exceptions.
1212

1313
:::
1414

15+
Use `allow-initialized` configuration (default is `false`), if you want to allow initialized late variable declarations.
16+
17+
### ⚙️ Config example {#config-example}
18+
19+
```yaml
20+
dart_code_metrics:
21+
...
22+
rules:
23+
...
24+
- avoid-late-keyword:
25+
allow-initialized: true
26+
```
27+
1528
### Example {#example}
1629
1730
**❌ Bad:**

website/docs/rules/index.mdx

+1
Original file line numberDiff line numberDiff line change
@@ -112,6 +112,7 @@ Rules are grouped by category to help you understand their purpose. Each rule ha
112112
type="common"
113113
severity="warning"
114114
version="3.2.0"
115+
hasConfig
115116
>
116117
Warns when a field or variable is declared with a <code>late</code> keyword.
117118
</RuleEntry>

website/docs/rules/intl/prefer-provide-intl-description.mdx

+1-1
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ import RuleDetails from '@site/src/components/RuleDetails';
22

33
<RuleDetails version="5.5.0" severity="warning" />
44

5-
Warns when the <code>Intl.message()</code>, <code>Intl.plural()</code>, <code>Intl.gender()</code> or <code>Intl.select()</code> methods are invoked without a description.
5+
Warns when the `Intl.message()`, `Intl.plural()`, `Intl.gender()` or `Intl.select()` methods are invoked without a description.
66

77
To make the translator's job easier, provide a description of the message usage.
88

0 commit comments

Comments
 (0)