This repository has been archived by the owner on Jul 16, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 258
/
prefer_iterable_of_rule.dart
52 lines (43 loc) · 1.68 KB
/
prefer_iterable_of_rule.dart
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// ignore_for_file: public_member_api_docs
import 'package:analyzer/dart/ast/ast.dart';
import 'package:analyzer/dart/ast/visitor.dart';
import 'package:analyzer/dart/element/nullability_suffix.dart';
import 'package:analyzer/dart/element/type.dart';
import 'package:collection/collection.dart';
import '../../../../../utils/dart_types_utils.dart';
import '../../../../../utils/node_utils.dart';
import '../../../lint_utils.dart';
import '../../../models/internal_resolved_unit_result.dart';
import '../../../models/issue.dart';
import '../../../models/replacement.dart';
import '../../../models/severity.dart';
import '../../models/common_rule.dart';
import '../../rule_utils.dart';
part 'visitor.dart';
class PreferIterableOfRule extends CommonRule {
static const ruleId = 'prefer-iterable-of';
static const _warningMessage = 'Prefer using .of';
static const _replaceComment = "Replace with 'of'.";
PreferIterableOfRule([Map<String, Object> config = const {}])
: super(
id: ruleId,
severity: readSeverity(config, Severity.warning),
excludes: readExcludes(config),
);
@override
Iterable<Issue> check(InternalResolvedUnitResult source) {
final visitor = _Visitor();
source.unit.visitChildren(visitor);
return visitor.expressions
.map((expression) => createIssue(
rule: this,
location: nodeLocation(node: expression, source: source),
message: _warningMessage,
replacement: Replacement(
comment: _replaceComment,
replacement: expression.toString().replaceAll('.from', '.of'),
),
))
.toList(growable: false);
}
}