Skip to content

Commit b4fa868

Browse files
authored
Get rid of a backlog of potentially suboptimal coding practices in dartdoc (#2830)
* switch to lints/core.yaml * recommended * presubmit cleanup * oops, recommended on presubmit too
1 parent c45302c commit b4fa868

Some content is hidden

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

54 files changed

+221
-224
lines changed

analysis_options.yaml

+16-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Change analysis_options.yaml and analysis_options_presubmit.yaml
22
# together.
3-
include: package:pedantic/analysis_options.1.11.0.yaml
3+
include: package:lints/recommended.yaml
44

55
analyzer:
66
errors:
@@ -19,45 +19,19 @@ analyzer:
1919
- 'testing/test_package_export_error/**'
2020
linter:
2121
rules:
22-
- always_declare_return_types
23-
- avoid_dynamic_calls
24-
- avoid_single_cascade_in_expression_statements
25-
- avoid_unused_constructor_parameters
26-
- annotate_overrides
27-
- avoid_init_to_null
28-
- directives_ordering
29-
- no_adjacent_strings_in_list
30-
- package_api_docs
31-
- prefer_final_fields
32-
- prefer_initializing_formals
33-
- prefer_void_to_null
34-
- slash_for_doc_comments
35-
- type_annotate_public_apis
36-
# - unnecessary_brace_in_string_interps
22+
always_declare_return_types: true
23+
annotate_overrides: true
24+
avoid_dynamic_calls: true
25+
avoid_single_cascade_in_expression_statements: true
26+
avoid_unused_constructor_parameters: true
27+
avoid_init_to_null: true
28+
directives_ordering: true
29+
no_adjacent_strings_in_list: true
30+
package_api_docs: true
31+
prefer_final_fields: true
32+
prefer_initializing_formals: true
33+
prefer_void_to_null: true
34+
slash_for_doc_comments: true
35+
type_annotate_public_apis: true
3736
# Work in progress canonical score lints
38-
- avoid_empty_else
39-
- avoid_relative_lib_imports
40-
- avoid_shadowing_type_parameters
41-
- await_only_futures
42-
- camel_case_extensions
43-
- camel_case_types
44-
- curly_braces_in_flow_control_structures
45-
- empty_catches
46-
- file_names
47-
- hash_and_equals
48-
- iterable_contains_unrelated_type
49-
- list_remove_unrelated_type
50-
- no_duplicate_case_values
51-
# - non_constant_identifier_names
52-
- package_prefixed_library_names
53-
- prefer_generic_function_type_aliases
54-
- prefer_is_empty
55-
- prefer_is_not_empty
56-
- prefer_iterable_whereType
57-
- prefer_typing_uninitialized_variables
58-
- provide_deprecation_message
59-
- unawaited_futures
60-
- unnecessary_overrides
61-
- unrelated_type_equality_checks
62-
- valid_regexps
63-
- void_checks
37+
unawaited_futures: true

analysis_options_presubmit.yaml

+16-42
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
# Change analysis_options.yaml and analysis_options_presubmit.yaml
22
# together.
3-
include: package:pedantic/analysis_options.1.11.0.yaml
3+
include: package:lints/recommended.yaml
44

55
analyzer:
66
errors:
@@ -22,45 +22,19 @@ analyzer:
2222
- 'testing/test_package_export_error/**'
2323
linter:
2424
rules:
25-
- always_declare_return_types
26-
- avoid_dynamic_calls
27-
- avoid_single_cascade_in_expression_statements
28-
- avoid_unused_constructor_parameters
29-
- annotate_overrides
30-
- avoid_init_to_null
31-
- directives_ordering
32-
- no_adjacent_strings_in_list
33-
- package_api_docs
34-
- prefer_final_fields
35-
- prefer_initializing_formals
36-
- prefer_void_to_null
37-
- slash_for_doc_comments
38-
- type_annotate_public_apis
39-
# - unnecessary_brace_in_string_interps
25+
always_declare_return_types: true
26+
annotate_overrides: true
27+
avoid_dynamic_calls: true
28+
avoid_single_cascade_in_expression_statements: true
29+
avoid_unused_constructor_parameters: true
30+
avoid_init_to_null: true
31+
directives_ordering: true
32+
no_adjacent_strings_in_list: true
33+
package_api_docs: true
34+
prefer_final_fields: true
35+
prefer_initializing_formals: true
36+
prefer_void_to_null: true
37+
slash_for_doc_comments: true
38+
type_annotate_public_apis: true
4039
# Work in progress canonical score lints
41-
- avoid_empty_else
42-
- avoid_relative_lib_imports
43-
- avoid_shadowing_type_parameters
44-
- await_only_futures
45-
- camel_case_extensions
46-
- camel_case_types
47-
- curly_braces_in_flow_control_structures
48-
- empty_catches
49-
- file_names
50-
- hash_and_equals
51-
- iterable_contains_unrelated_type
52-
- list_remove_unrelated_type
53-
- no_duplicate_case_values
54-
# - non_constant_identifier_names
55-
- package_prefixed_library_names
56-
- prefer_generic_function_type_aliases
57-
- prefer_is_empty
58-
- prefer_is_not_empty
59-
- prefer_iterable_whereType
60-
- prefer_typing_uninitialized_variables
61-
- provide_deprecation_message
62-
- unawaited_futures
63-
- unnecessary_overrides
64-
- unrelated_type_equality_checks
65-
- valid_regexps
66-
- void_checks
40+
unawaited_futures: true

lib/dartdoc.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -370,7 +370,7 @@ class Dartdoc {
370370
var indexPath = path.joinAll([origin, 'index.html']);
371371
var file = config.resourceProvider.getFile(fullPath);
372372
if (!file.exists) {
373-
return null;
373+
return;
374374
}
375375
var decoder = JsonDecoder();
376376
List<Object> jsonData = decoder.convert(file.readAsStringSync());
@@ -415,7 +415,7 @@ class Dartdoc {
415415
// Remove so that we properly count that the file doesn't exist for
416416
// the orphan check.
417417
visited.remove(fullPath);
418-
return null;
418+
return;
419419
}
420420
visited.add(fullPath);
421421
var stringLinks = stringLinksAndHref.item1;

lib/src/comment_references/parser.dart

+3-4
Original file line numberDiff line numberDiff line change
@@ -131,13 +131,12 @@ class CommentReferenceParser {
131131
var typeVariablesResult = _parseTypeVariables();
132132
if (typeVariablesResult.type == _TypeVariablesResultType.endOfFile) {
133133
break;
134-
} else if (typeVariablesResult.type ==
135-
_TypeVariablesResultType.notTypeVariables) {
136-
// Do nothing, _index has not moved.
137-
;
138134
} else if (typeVariablesResult.type ==
139135
_TypeVariablesResultType.parsedTypeVariables) {
140136
children.add(typeVariablesResult.node);
137+
} else {
138+
assert(typeVariablesResult.type ==
139+
_TypeVariablesResultType.notTypeVariables);
141140
}
142141
}
143142
if (_atEnd || _thisChar != $dot) {

lib/src/dartdoc_options.dart

+11-5
Original file line numberDiff line numberDiff line change
@@ -556,7 +556,9 @@ abstract class DartdocOption<T extends Object> {
556556
/// Apply the function [visit] to [this] and all children.
557557
void traverse(void Function(DartdocOption option) visit) {
558558
visit(this);
559-
_children.values.forEach((d) => d.traverse(visit));
559+
for (var value in _children.values) {
560+
value.traverse(visit);
561+
}
560562
}
561563
}
562564

@@ -731,7 +733,7 @@ class DartdocOptionSet extends DartdocOption<void> {
731733

732734
/// [DartdocOptionSet] always has the null value.
733735
@override
734-
void valueAt(Folder dir) => null;
736+
void valueAt(Folder dir) {}
735737

736738
/// Since we have no value, [_onMissing] does nothing.
737739
@override
@@ -742,7 +744,9 @@ class DartdocOptionSet extends DartdocOption<void> {
742744
/// configuration object.
743745
@override
744746
void traverse(void Function(DartdocOption option) visitor) {
745-
_children.values.forEach((d) => d.traverse(visitor));
747+
for (var value in _children.values) {
748+
value.traverse(visitor);
749+
}
746750
}
747751
}
748752

@@ -1033,7 +1037,9 @@ abstract class _DartdocFileOption<T> implements DartdocOption<T> {
10331037
}
10341038
}
10351039
}
1036-
canonicalPaths.forEach((p) => _yamlAtCanonicalPathCache[p] = yamlData);
1040+
for (var canonicalPath in canonicalPaths) {
1041+
_yamlAtCanonicalPathCache[canonicalPath] = yamlData;
1042+
}
10371043
return yamlData;
10381044
}
10391045
}
@@ -1137,7 +1143,7 @@ abstract class _DartdocArgOption<T> implements DartdocOption<T> {
11371143
/// 'something-bar-over-the-hill' (with default skip).
11381144
/// This allows argument names to reflect nested structure.
11391145
static String _keysToArgName(Iterable<String> keys, [int skip = 1]) {
1140-
var argName = "${keys.skip(skip).join('-')}";
1146+
var argName = keys.skip(skip).join('-');
11411147
argName = argName.replaceAll('_', '-');
11421148
// Do not consume the lowercase character after the uppercase one, to handle
11431149
// two character words.

lib/src/experiment_options.dart

+1
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@
99
library dartdoc.experiment_options;
1010

1111
import 'package:analyzer/file_system/file_system.dart';
12+
// ignore: implementation_imports
1213
import 'package:analyzer/src/dart/analysis/experiments.dart';
1314
import 'package:dartdoc/src/dartdoc_options.dart';
1415

lib/src/generator/empty_generator.dart

+4-4
Original file line numberDiff line numberDiff line change
@@ -11,10 +11,10 @@ import 'package:dartdoc/src/model_utils.dart';
1111
/// it were.
1212
class EmptyGenerator extends Generator {
1313
@override
14-
Future<void> generate(PackageGraph _packageGraph, FileWriter writer) {
15-
logProgress(_packageGraph.defaultPackage.documentationAsHtml);
16-
for (var package in {_packageGraph.defaultPackage}
17-
..addAll(_packageGraph.localPackages)) {
14+
Future<void> generate(PackageGraph packageGraph, FileWriter writer) {
15+
logProgress(packageGraph.defaultPackage.documentationAsHtml);
16+
for (var package in {packageGraph.defaultPackage}
17+
..addAll(packageGraph.localPackages)) {
1818
for (var category in filterNonDocumented(package.categories)) {
1919
logProgress(category.documentationAsHtml);
2020
}

lib/src/generator/html_generator.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class HtmlGeneratorBackend extends DartdocGeneratorBackend {
5959
}
6060

6161
Future<void> _copyResources(FileWriter writer) async {
62-
for (var resourcePath in resources.resource_names) {
62+
for (var resourcePath in resources.resourceNames) {
6363
if (!resourcePath.startsWith(_dartdocResourcePrefix)) {
6464
throw StateError('Resource paths must start with '
6565
'$_dartdocResourcePrefix, encountered $resourcePath');

lib/src/generator/html_resources.g.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
// WARNING: This file is auto-generated. Do not taunt.
1+
// WARNING: This file is auto-generated. Do not edit.
22

3-
const List<String> resource_names = [
3+
const List<String> resourceNames = [
44
'package:dartdoc/resources/favicon.png',
55
'package:dartdoc/resources/github.css',
66
'package:dartdoc/resources/highlight.pack.js',

lib/src/generator/templates.aot_renderers_for_html.dart

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// the variable is not used; generally when the section is checking if a
88
// non-bool, non-Iterable field is non-null.
99
// ignore_for_file: unused_local_variable
10+
// ignore_for_file: non_constant_identifier_names, unnecessary_string_escapes
1011

1112
import 'dart:convert' as _i19;
1213

lib/src/generator/templates.aot_renderers_for_md.dart

+1
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
// the variable is not used; generally when the section is checking if a
88
// non-bool, non-Iterable field is non-null.
99
// ignore_for_file: unused_local_variable
10+
// ignore_for_file: non_constant_identifier_names, unnecessary_string_escapes
1011

1112
import 'dart:convert' as _i19;
1213

lib/src/generator/templates.runtime_renderers.dart

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
// files in the tool/mustachio/ directory.
55

66
// ignore_for_file: camel_case_types, deprecated_member_use_from_same_package
7+
// ignore_for_file: non_constant_identifier_names, unnecessary_string_escapes
78
// ignore_for_file: unused_import
89
import 'package:dartdoc/src/element_type.dart';
910
import 'package:dartdoc/src/generator/template_data.dart';

lib/src/io_utils.dart

+4-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,10 @@ import 'dart:io' as io;
1212

1313
import 'package:analyzer/file_system/file_system.dart';
1414
import 'package:analyzer/file_system/physical_file_system.dart';
15-
import 'package:analyzer/src/generated/sdk.dart';
16-
import 'package:analyzer/src/test_utilities/mock_sdk.dart';
15+
// ignore: implementation_imports
16+
import 'package:analyzer/src/generated/sdk.dart' show SdkLibrary;
17+
// ignore: implementation_imports
18+
import 'package:analyzer/src/test_utilities/mock_sdk.dart' show MockSdkLibrary;
1719
import 'package:path/path.dart' as path show Context;
1820

1921
Encoding utf8AllowMalformed = Utf8Codec(allowMalformed: true);

lib/src/logging.dart

+1-1
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,7 @@ void startLogging(LoggingContext config) {
117117
// the backspace to occur for stderr as well.
118118
stderr.write('${ansi.backspace} ${ansi.backspace}');
119119
}
120-
stderr.writeln('$message');
120+
stderr.writeln(message);
121121
}
122122
writingProgress = false;
123123
});

lib/src/markdown_processor.dart

+2-2
Original file line numberDiff line numberDiff line change
@@ -284,9 +284,9 @@ void showWarningsForGenericsOutsideSquareBracketsBlocks(
284284
PackageWarningMode.ignore) {
285285
for (var position in findFreeHangingGenericsPositions(text)) {
286286
var priorContext =
287-
'${text.substring(max(position - maxPriorContext, 0), position)}';
287+
text.substring(max(position - maxPriorContext, 0), position);
288288
var postContext =
289-
'${text.substring(position, min(position + maxPostContext, text.length))}';
289+
text.substring(position, min(position + maxPostContext, text.length));
290290
priorContext = priorContext.replaceAll(allBeforeFirstNewline, '');
291291
postContext = postContext.replaceAll(allAfterLastNewline, '');
292292
var errorMessage = '$priorContext$postContext';

lib/src/model/accessor.dart

+1
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
import 'package:analyzer/dart/element/element.dart';
66
import 'package:analyzer/source/line_info.dart';
7+
// ignore: implementation_imports
78
import 'package:analyzer/src/dart/element/member.dart' show ExecutableMember;
89
import 'package:dartdoc/src/element_type.dart';
910
import 'package:dartdoc/src/model/comment_referable.dart';

lib/src/model/comment_referable.dart

+5-4
Original file line numberDiff line numberDiff line change
@@ -70,10 +70,11 @@ extension CommentReferableEntryGenerators on Iterable<CommentReferable> {
7070
extension CommentReferableEntryBuilder on Map<String, CommentReferable> {
7171
/// Like [Map.putIfAbsent] except works on an iterable of entries.
7272
void addEntriesIfAbsent(
73-
Iterable<MapEntry<String, CommentReferable>> entries) =>
74-
entries.forEach((e) {
75-
if (!containsKey(e.key)) this[e.key] = e.value;
76-
});
73+
Iterable<MapEntry<String, CommentReferable>> entries) {
74+
for (var entry in entries) {
75+
if (!containsKey(entry.key)) this[entry.key] = entry.value;
76+
}
77+
}
7778
}
7879

7980
/// Support comment reference lookups on a Nameable object.

lib/src/model/documentation_comment.dart

+3-3
Original file line numberDiff line numberDiff line change
@@ -393,7 +393,7 @@ mixin DocumentationComment
393393

394394
/// Matches YouTube IDs from supported YouTube URLs.
395395
static final _validYouTubeUrlPattern =
396-
RegExp('https://www\.youtube\.com/watch\\?v=([^&]+)\$');
396+
RegExp(r'https://www\.youtube\.com/watch\?v=([^&]+)$');
397397

398398
/// An argument parser used in [_injectYouTube] to parse a `{@youtube}`
399399
/// directive.
@@ -736,14 +736,14 @@ mixin DocumentationComment
736736
firstOfPair.add(results[i]);
737737
}
738738
}
739-
firstOfPair.forEach((element) {
739+
for (var element in firstOfPair) {
740740
final result = element.group(2).trim();
741741
if (result.isEmpty) {
742742
warn(PackageWarning.missingCodeBlockLanguage,
743743
message:
744744
'A fenced code block in Markdown should have a language specified');
745745
}
746-
});
746+
}
747747
}
748748

749749
/// Returns the documentation for this literal element unless

lib/src/model/field.dart

+1-3
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44

55
import 'package:analyzer/dart/element/element.dart';
6-
import 'package:analyzer/src/dart/element/element.dart';
76
import 'package:dartdoc/src/model/feature.dart';
87
import 'package:dartdoc/src/model/model.dart';
98
import 'package:dartdoc/src/render/source_code_renderer.dart';
@@ -82,8 +81,7 @@ class Field extends ModelElement
8281
/// Returns true if the FieldElement is covariant, or if the first parameter
8382
/// for the setter is covariant.
8483
@override
85-
bool get isCovariant =>
86-
setter?.isCovariant == true || (field as FieldElementImpl).isCovariant;
84+
bool get isCovariant => setter?.isCovariant == true || field.isCovariant;
8785

8886
@override
8987
bool get isFinal {

0 commit comments

Comments
 (0)