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

Commit 3d92124

Browse files
authored
chore: activate new lint rules (#689)
* chore: activate new linter rules * chore: activate new lint rules. * chore: disable some lint rules for dart 2.14 * chore: update tests
1 parent 0edaad7 commit 3d92124

File tree

42 files changed

+224
-185
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

42 files changed

+224
-185
lines changed

.github/workflows/package_analyze.yaml

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -127,7 +127,13 @@ jobs:
127127

128128
- name: Disable unsupported rules
129129
run: |
130-
sed -i'.original' 's/- unnecessary_constructor_name/# - unnecessary_constructor_name/g' analysis_options.yaml
130+
sed -i'.original' 's/- avoid_final_parameters/# - avoid_final_parameters/g' analysis_options.yaml
131+
sed -i'.original' 's/- conditional_uri_does_not_exist/# - conditional_uri_does_not_exist/g' analysis_options.yaml
132+
sed -i'.original' 's/- no_leading_underscores_for_library_prefixes/# - no_leading_underscores_for_library_prefixes/g' analysis_options.yaml
133+
sed -i'.original' 's/- no_leading_underscores_for_local_identifiers/# - no_leading_underscores_for_local_identifiers/g' analysis_options.yaml
134+
sed -i'.original' 's/- secure_pubspec_urls/# - secure_pubspec_urls/g' analysis_options.yaml
135+
sed -i'.original' 's/- unnecessary_constructor_name/# - unnecessary_constructor_name/g' analysis_options.yaml
136+
sed -i'.original' 's/- unnecessary_late/# - unnecessary_late/g' analysis_options.yaml
131137
132138
- name: Check compatibility
133139
run: |

CHANGELOG.md

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,13 @@
11
# Changelog
22

3+
## Unreleased
4+
5+
* chore: activate new lint rules.
6+
37
## 4.11.0-dev.1
48

59
* fix: move byte store out of driver creation to reuse it between multiple plugins.
6-
* fix: add `monorepo` flag for `check-unused-code` command.
10+
* fix: add `monorepo` flag for `check-unused-files` command.
711
* fix: ignore a class usage inside `State<T>` for `check-unused-code` command.
812
* fix: correctly handle variables declaration for `check-unused-code` command.
913
* feat: add static code diagnostics `avoid-dynamic`, `prefer-async-await`.

analysis_options.yaml

Lines changed: 23 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,30 @@ dart_code_metrics:
4545
- test/resources/*
4646
- test/*
4747
rules:
48+
- avoid-dynamic:
49+
exclude:
50+
- test/**
4851
- avoid-global-state
49-
- avoid-unused-parameters
52+
- avoid-late-keyword:
53+
exclude:
54+
- test/**
55+
- avoid-missing-enum-constant-in-map
5056
- avoid-unnecessary-type-assertions
5157
- avoid-unnecessary-type-casts
52-
- avoid-global-state
58+
- avoid-unused-parameters
5359
- binary-expression-operand-order
5460
- double-literal-format
5561
- newline-before-return
5662
- no-boolean-literal-compare
5763
- no-empty-block
5864
- no-equal-then-else
5965
- no-object-declaration
66+
- prefer-async-await
6067
- prefer-conditional-expressions
68+
- prefer-correct-identifier-length:
69+
exceptions:
70+
- id
71+
- ok
6172
- prefer-correct-type-name
6273
- prefer-first
6374
- prefer-last
@@ -83,6 +94,7 @@ linter:
8394
- avoid_equals_and_hash_code_on_mutable_classes
8495
- avoid_escaping_inner_quotes
8596
- avoid_field_initializers_in_const_classes
97+
- avoid_final_parameters
8698
- avoid_implementing_value_types
8799
- avoid_js_rounded_ints
88100
- avoid_multiple_declarations_per_line
@@ -106,6 +118,7 @@ linter:
106118
# - cast_nullable_to_non_nullable
107119
- close_sinks
108120
- comment_references
121+
- conditional_uri_does_not_exist
109122
- depend_on_referenced_packages
110123
- deprecated_consistency
111124
- diagnostic_describe_all_properties
@@ -122,6 +135,8 @@ linter:
122135
- missing_whitespace_between_adjacent_strings
123136
# - no_adjacent_strings_in_list
124137
- no_default_cases
138+
- no_leading_underscores_for_library_prefixes
139+
- no_leading_underscores_for_local_identifiers
125140
- no_logic_in_create_state
126141
- no_runtimeType_toString
127142
- noop_primitive_operations
@@ -153,7 +168,9 @@ linter:
153168
- prefer_single_quotes
154169
# - public_member_api_docs
155170
# - require_trailing_commas
156-
- sized_box_for_whitespace
171+
- secure_pubspec_urls
172+
# - sized_box_for_whitespace
173+
# - sized_box_shrink_expand
157174
# - sort_child_properties_last
158175
# - sort_constructors_first
159176
- sort_pub_dependencies
@@ -167,14 +184,16 @@ linter:
167184
- unnecessary_constructor_name
168185
# - unnecessary_final
169186
- unnecessary_lambdas
187+
- unnecessary_late
170188
- unnecessary_null_aware_assignments
171189
- unnecessary_null_checks
172190
- unnecessary_nullable_for_final_variable_declarations
173191
- unnecessary_parenthesis
174192
- unnecessary_raw_strings
175193
- unnecessary_statements
176194
- unsafe_html
177-
- use_build_context_synchronously
195+
# - use_build_context_synchronously
196+
# - use_decorated_box
178197
# - use_full_hex_values_for_flutter_colors
179198
- use_if_null_to_convert_nulls_to_bools
180199
- use_is_even_rather_than_modulo

lib/src/analyzers/lint_analyzer/lint_config.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -23,17 +23,17 @@ class LintConfig {
2323

2424
/// Creates the config from analysis [options].
2525
factory LintConfig.fromAnalysisOptions(AnalysisOptions options) {
26-
const _rootKey = 'dart_code_metrics';
26+
const rootKey = 'dart_code_metrics';
2727

2828
return LintConfig(
2929
excludePatterns: options.readIterableOfString(['analyzer', 'exclude']),
3030
excludeForMetricsPatterns:
31-
options.readIterableOfString([_rootKey, 'metrics-exclude']),
32-
metrics: options.readMap([_rootKey, 'metrics']),
33-
rules: options.readMapOfMap([_rootKey, 'rules']),
31+
options.readIterableOfString([rootKey, 'metrics-exclude']),
32+
metrics: options.readMap([rootKey, 'metrics']),
33+
rules: options.readMapOfMap([rootKey, 'rules']),
3434
excludeForRulesPatterns:
35-
options.readIterableOfString([_rootKey, 'rules-exclude']),
36-
antiPatterns: options.readMapOfMap([_rootKey, 'anti-patterns']),
35+
options.readIterableOfString([rootKey, 'rules-exclude']),
36+
antiPatterns: options.readMapOfMap([rootKey, 'anti-patterns']),
3737
);
3838
}
3939

lib/src/analyzers/lint_analyzer/metrics/metric_utils.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -82,11 +82,11 @@ bool isReportLevel(MetricValueLevel level) =>
8282

8383
/// Returns user friendly string representations of [type].
8484
String userFriendlyType(Type type) {
85-
const _impl = 'Impl';
85+
const impl = 'Impl';
8686

8787
final typeName = type.toString();
8888

89-
return typeName.endsWith(_impl)
90-
? typeName.substring(0, typeName.length - _impl.length)
89+
return typeName.endsWith(impl)
90+
? typeName.substring(0, typeName.length - impl.length)
9191
: typeName;
9292
}

lib/src/analyzers/lint_analyzer/metrics/metrics_factory.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -39,15 +39,15 @@ Iterable<Metric> getMetrics({
3939
Iterable<String> patternsDependencies = const [],
4040
EntityType? measuredType,
4141
}) {
42-
final _metrics = _implementedMetrics.keys.map(
42+
final metrics = _implementedMetrics.keys.map(
4343
(id) => _implementedMetrics[id]!(
4444
!patternsDependencies.contains(id) ? config : {},
4545
),
4646
);
4747

4848
return measuredType != null
49-
? _metrics
49+
? metrics
5050
.where((metric) => metric.documentation.measuredType == measuredType)
5151
.toList()
52-
: _metrics;
52+
: metrics;
5353
}

lib/src/analyzers/lint_analyzer/metrics/metrics_list/cyclomatic_complexity/cyclomatic_complexity_metric.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,15 +70,16 @@ class CyclomaticComplexityMetric extends FunctionMetric<int> {
7070
InternalResolvedUnitResult source,
7171
) =>
7272
complexityEntities.map((entity) {
73-
late String message;
73+
String? message;
74+
7475
if (entity is AstNode) {
7576
message = userFriendlyType(entity.runtimeType).camelCaseToText();
7677
} else if (entity is Token) {
7778
message = 'Operator ${entity.lexeme}';
7879
}
7980

8081
return ContextMessage(
81-
message: '${message.capitalize()} increases complexity',
82+
message: '${message?.capitalize()} increases complexity',
8283
location: nodeLocation(node: entity, source: source),
8384
);
8485
}).toList()

lib/src/analyzers/lint_analyzer/metrics/metrics_list/weight_of_class_metric.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -89,13 +89,13 @@ class WeightOfClassMetric extends ClassMetric<double> {
8989
!Identifier.isPrivateName(function.name);
9090

9191
bool _isFunctionalMethod(ScopedFunctionDeclaration function) {
92-
const _nonFunctionalTypes = {
92+
const nonFunctionalTypes = {
9393
FunctionType.constructor,
9494
FunctionType.setter,
9595
FunctionType.getter,
9696
};
9797

98-
return !_nonFunctionalTypes.contains(function.type);
98+
return !nonFunctionalTypes.contains(function.type);
9999
}
100100

101101
Iterable<ContextMessage> _context(

lib/src/analyzers/lint_analyzer/reporters/reporters_list/html/lint_html_reporter.dart

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -333,40 +333,41 @@ class LintHtmlReporter extends HtmlReporter<LintFileReport,
333333

334334
final linesIndices = Element.tag('td')
335335
..classes.add('metrics-source-code__number-lines');
336-
for (var i = 1; i <= sourceFileLines.length; ++i) {
336+
for (var lineIndex = 1; lineIndex <= sourceFileLines.length; ++lineIndex) {
337337
linesIndices
338-
..append(Element.tag('a')..attributes['name'] = 'L$i')
338+
..append(Element.tag('a')..attributes['name'] = 'L$lineIndex')
339339
..append(Element.tag('a')
340340
..classes.add('metrics-source-code__number')
341-
..attributes['href'] = '#L$i'
342-
..text = '$i')
341+
..attributes['href'] = '#L$lineIndex'
342+
..text = '$lineIndex')
343343
..append(Element.tag('br'));
344344
}
345345

346346
final cyclomaticValues = Element.tag('td')
347347
..classes.add('metrics-source-code__complexity');
348-
for (var i = 1; i <= sourceFileLines.length; ++i) {
348+
for (var lineIndex = 1; lineIndex <= sourceFileLines.length; ++lineIndex) {
349349
final complexityValueElement = Element.tag('div')
350350
..classes.add('metrics-source-code__text');
351351

352352
final classReport = record.classes.entries.firstWhereOrNull((report) =>
353-
report.value.location.start.line <= i &&
354-
report.value.location.end.line >= i);
355-
if (classReport != null && classReport.value.location.start.line == i) {
353+
report.value.location.start.line <= lineIndex &&
354+
report.value.location.end.line >= lineIndex);
355+
if (classReport != null &&
356+
classReport.value.location.start.line == lineIndex) {
356357
complexityValueElement
357358
..classes.add('metrics-source-code__text--with-icon')
358359
..append(renderComplexityIcon(classReport.value, classReport.key));
359360
}
360361

361362
final functionReport = record.functions.entries.firstWhereOrNull(
362363
(report) =>
363-
report.value.location.start.line <= i &&
364-
report.value.location.end.line >= i,
364+
report.value.location.start.line <= lineIndex &&
365+
report.value.location.end.line >= lineIndex,
365366
);
366367

367368
var line = '';
368369
if (functionReport != null) {
369-
if (functionReport.value.location.start.line == i) {
370+
if (functionReport.value.location.start.line == lineIndex) {
370371
complexityValueElement
371372
..classes.add('metrics-source-code__text--with-icon')
372373
..append(
@@ -377,7 +378,7 @@ class LintHtmlReporter extends HtmlReporter<LintFileReport,
377378
final lineWithComplexityIncrement = functionReport.value
378379
.metric(CyclomaticComplexityMetric.metricId)
379380
?.context
380-
.where((element) => element.location.start.line == i)
381+
.where((element) => element.location.start.line == lineIndex)
381382
.length ??
382383
0;
383384

@@ -403,7 +404,9 @@ class LintHtmlReporter extends HtmlReporter<LintFileReport,
403404
final debt = record.file
404405
.metric(TechnicalDebtMetric.metricId)
405406
?.context
406-
.firstWhereOrNull((context) => context.location.start.line == i)
407+
.firstWhereOrNull(
408+
(context) => context.location.start.line == lineIndex,
409+
)
407410
?.message;
408411
if (debt != null) {
409412
line += debt;
@@ -414,8 +417,9 @@ class LintHtmlReporter extends HtmlReporter<LintFileReport,
414417
complexityValueElement.text = line.replaceAll(' ', '&nbsp;');
415418
}
416419

417-
final architecturalIssues = record.antiPatternCases
418-
.firstWhereOrNull((element) => element.location.start.line == i);
420+
final architecturalIssues = record.antiPatternCases.firstWhereOrNull(
421+
(element) => element.location.start.line == lineIndex,
422+
);
419423

420424
if (architecturalIssues != null) {
421425
final issueIcon = Element.tag('div')

lib/src/analyzers/lint_analyzer/rules/rules_list/always_remove_listener/always_remove_listener_rule.dart

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,11 @@ class AlwaysRemoveListenerRule extends FlutterRule {
3232

3333
@override
3434
Iterable<Issue> check(InternalResolvedUnitResult source) {
35-
final _visitor = _Visitor();
35+
final visitor = _Visitor();
3636

37-
source.unit.visitChildren(_visitor);
37+
source.unit.visitChildren(visitor);
3838

39-
return _visitor.missingInvocations
39+
return visitor.missingInvocations
4040
.map((invocation) => createIssue(
4141
rule: this,
4242
location: nodeLocation(

0 commit comments

Comments
 (0)