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

feat: add allow-initialized option to avoid-late-keyword #1145

Merged
merged 1 commit into from
Jan 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
* feat: exclude `.freezed.dart` files by default.
* fix: handle try and switch statements for [`use-setstate-synchronously`](https://dartcodemetrics.dev/docs/rules/flutter/use-setstate-synchronously)
* chore: restrict `analyzer` version to `>=5.1.0 <5.4.0`.
* feat: add `allow-initialized` option to [`avoid-late-keyword`](https://dartcodemetrics.dev/docs/rules/common/avoid-late-keyword).

## 5.4.0

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,19 @@ import '../../../models/severity.dart';
import '../../models/common_rule.dart';
import '../../rule_utils.dart';

part 'config_parser.dart';
part 'visitor.dart';

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

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

final bool _allowInitialized;

AvoidLateKeywordRule([Map<String, Object> config = const {}])
: super(
: _allowInitialized = _ConfigParser.parseAllowInitialized(config),
super(
id: ruleId,
severity: readSeverity(config, Severity.warning),
excludes: readExcludes(config),
Expand All @@ -28,7 +32,7 @@ class AvoidLateKeywordRule extends CommonRule {

@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor();
final visitor = _Visitor(_allowInitialized);

source.unit.visitChildren(visitor);

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
part of 'avoid_late_keyword_rule.dart';

class _ConfigParser {
static const _allowInitializedConfig = 'allow-initialized';

static bool parseAllowInitialized(Map<String, Object> config) =>
config[_allowInitializedConfig] as bool? ?? false;
}
Original file line number Diff line number Diff line change
@@ -1,18 +1,25 @@
part of 'avoid_late_keyword_rule.dart';

class _Visitor extends RecursiveAstVisitor<void> {
final bool allowInitialized;

final _declarations = <AstNode>[];

Iterable<AstNode> get declarations => _declarations;

// ignore: avoid_positional_boolean_parameters
_Visitor(this.allowInitialized);

@override
void visitVariableDeclaration(VariableDeclaration node) {
super.visitVariableDeclaration(node);

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

_declarations.add(parent ?? node);
if (!(allowInitialized && node.initializer != null)) {
_declarations.add(parent ?? node);
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -45,5 +45,27 @@ void main() {
],
);
});

test('reports about found issues with config', () async {
final unit = await RuleTestHelper.resolveFromFile(_examplePath);
final issues =
AvoidLateKeywordRule({'allow-initialized': true}).check(unit);

RuleTestHelper.verifyIssues(
issues: issues,
startLines: [8, 17, 23],
startColumns: [3, 5, 1],
locationTexts: [
'late String uninitializedField',
'late String uninitializedVariable',
'late String topLevelUninitializedVariable',
],
messages: [
"Avoid using 'late' keyword.",
"Avoid using 'late' keyword.",
"Avoid using 'late' keyword.",
],
);
});
});
}
13 changes: 13 additions & 0 deletions website/docs/rules/common/avoid-late-keyword.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,19 @@ Use this rule if you want to avoid unexpected runtime exceptions.

:::

Use `allow-initialized` configuration (default is `false`), if you want to allow initialized late variable declarations.

### ⚙️ Config example {#config-example}

```yaml
dart_code_metrics:
...
rules:
...
- avoid-late-keyword:
allow-initialized: true
```

### Example {#example}

**❌ Bad:**
Expand Down
1 change: 1 addition & 0 deletions website/docs/rules/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,7 @@ Rules are grouped by category to help you understand their purpose. Each rule ha
type="common"
severity="warning"
version="3.2.0"
hasConfig
>
Warns when a field or variable is declared with a <code>late</code> keyword.
</RuleEntry>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ import RuleDetails from '@site/src/components/RuleDetails';

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

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.
Warns when the `Intl.message()`, `Intl.plural()`, `Intl.gender()` or `Intl.select()` methods are invoked without a description.

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

Expand Down