Skip to content

Commit 55512ec

Browse files
authored
lints (#1061)
* Enable and fix recommended lints * Enable and fix two more lints
1 parent 61a6636 commit 55512ec

31 files changed

+79
-71
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
# 2.2.1-dev
2+
13
# 2.2.0
24

35
* Fix analyzer dependency constraint (#1051).

analysis_options.yaml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
include: package:pedantic/analysis_options.yaml
1+
include: package:lints/recommended.yaml
22
linter:
33
rules:
4+
- avoid_dynamic_calls
5+
- directives_ordering
46
- prefer_equal_for_default_values
57
- prefer_generic_function_type_aliases
68
- slash_for_doc_comments

benchmark/benchmark.dart

Lines changed: 7 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,11 @@ library dart_style.benchmark.benchmark;
66

77
import 'dart:io';
88

9-
import 'package:path/path.dart' as p;
10-
119
import 'package:dart_style/dart_style.dart';
10+
import 'package:path/path.dart' as p;
1211

13-
const NUM_TRIALS = 100;
14-
const FORMATS_PER_TRIAL = 30;
12+
const _numTrials = 100;
13+
const _formatsPerTrial = 30;
1514

1615
/// Note, these files use ".txt" because while they can be *parsed* correctly,
1716
/// they don't resolve without error. That's OK because the formatter doesn't
@@ -24,17 +23,17 @@ void main(List<String> args) {
2423

2524
// Run the benchmark several times. This ensures the VM is warmed up and lets
2625
// us see how much variance there is.
27-
for (var i = 0; i <= NUM_TRIALS; i++) {
26+
for (var i = 0; i <= _numTrials; i++) {
2827
var start = DateTime.now();
2928

3029
// For a single benchmark, format the source multiple times.
31-
var result;
32-
for (var j = 0; j < FORMATS_PER_TRIAL; j++) {
30+
String? result;
31+
for (var j = 0; j < _formatsPerTrial; j++) {
3332
result = formatSource();
3433
}
3534

3635
var elapsed =
37-
DateTime.now().difference(start).inMilliseconds / FORMATS_PER_TRIAL;
36+
DateTime.now().difference(start).inMilliseconds / _formatsPerTrial;
3837

3938
// Keep track of the best run so far.
4039
if (elapsed >= best) continue;

bin/format.dart

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,7 @@ void main(List<String> args) async {
3535
return;
3636
}
3737

38-
if (argResults['verbose'] && !argResults['help']) {
38+
if (argResults['verbose'] && !(argResults['help'] as bool)) {
3939
usageError(parser, 'Can only use --verbose with --help.');
4040
}
4141

@@ -52,7 +52,7 @@ void main(List<String> args) async {
5252
}
5353

5454
void checkForReporterCollision(String chosen, String other) {
55-
if (!argResults[other]) return;
55+
if (!(argResults[other] as bool)) return;
5656

5757
usageError(parser, 'Cannot use --$chosen and --$other at the same time.');
5858
}

example/format.dart

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,9 @@
44
import 'dart:io';
55
import 'dart:mirrors';
66

7-
import 'package:path/path.dart' as p;
8-
97
import 'package:dart_style/dart_style.dart';
108
import 'package:dart_style/src/debug.dart' as debug;
9+
import 'package:path/path.dart' as p;
1110

1211
void main(List<String> args) {
1312
// Enable debugging so you can see some of the formatter's internal state.
@@ -33,7 +32,7 @@ void runFormatter(String source, int pageWidth,
3332
try {
3433
var formatter = DartFormatter(pageWidth: pageWidth);
3534

36-
var result;
35+
String result;
3736
if (isCompilationUnit) {
3837
result = formatter.format(source);
3938
} else {

lib/dart_style.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,5 @@
33
// BSD-style license that can be found in the LICENSE file.
44
export 'src/dart_formatter.dart';
55
export 'src/exceptions.dart';
6-
export 'src/style_fix.dart';
76
export 'src/source_code.dart';
7+
export 'src/style_fix.dart';

lib/src/argument_list_visitor.dart

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -64,8 +64,8 @@ class ArgumentListVisitor {
6464
Token rightParenthesis,
6565
List<Expression> arguments) {
6666
// Look for a single contiguous range of block function arguments.
67-
var functionsStart;
68-
var functionsEnd;
67+
int? functionsStart;
68+
int? functionsEnd;
6969

7070
for (var i = 0; i < arguments.length; i++) {
7171
var argument = arguments[i];
@@ -104,7 +104,7 @@ class ArgumentListVisitor {
104104
// another: argument);
105105
if (functionsStart != null &&
106106
arguments[0] is NamedExpression &&
107-
(functionsStart > 0 || functionsEnd < arguments.length)) {
107+
(functionsStart > 0 || functionsEnd! < arguments.length)) {
108108
functionsStart = null;
109109
}
110110

@@ -125,7 +125,7 @@ class ArgumentListVisitor {
125125
return false;
126126
}
127127

128-
for (var i = 0; i < functionsStart; i++) {
128+
for (var i = 0; i < functionsStart!; i++) {
129129
var argument = arguments[i];
130130
if (argument is! NamedExpression) continue;
131131

@@ -135,7 +135,7 @@ class ArgumentListVisitor {
135135
}
136136
}
137137

138-
for (var i = functionsEnd; i < arguments.length; i++) {
138+
for (var i = functionsEnd!; i < arguments.length; i++) {
139139
if (isArrow(arguments[i] as NamedExpression)) {
140140
functionsStart = null;
141141
break;
@@ -153,7 +153,7 @@ class ArgumentListVisitor {
153153
// functions in the middle.
154154
var argumentsBefore = arguments.take(functionsStart).toList();
155155
var functions = arguments.sublist(functionsStart, functionsEnd);
156-
var argumentsAfter = arguments.skip(functionsEnd).toList();
156+
var argumentsAfter = arguments.skip(functionsEnd!).toList();
157157

158158
return ArgumentListVisitor._(
159159
visitor,

lib/src/chunk_builder.dart

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -312,7 +312,7 @@ class ChunkBuilder {
312312

313313
// Make sure there is at least one newline after a line comment and allow
314314
// one or two after a block comment that has nothing after it.
315-
var linesAfter;
315+
int linesAfter;
316316
if (i < comments.length - 1) {
317317
linesAfter = comments[i + 1].linesBefore;
318318
} else {
@@ -432,6 +432,8 @@ class ChunkBuilder {
432432
_pendingWhitespace = Whitespace.newline;
433433
}
434434
break;
435+
default:
436+
break;
435437
}
436438
}
437439

@@ -670,8 +672,8 @@ class ChunkBuilder {
670672
var result = writer.writeLines(_formatter.indent,
671673
isCompilationUnit: _source.isCompilationUnit);
672674

673-
var selectionStart;
674-
var selectionLength;
675+
int? selectionStart;
676+
int? selectionLength;
675677
if (_source.selectionStart != null) {
676678
selectionStart = result.selectionStart;
677679
var selectionEnd = result.selectionEnd;
@@ -734,6 +736,8 @@ class ChunkBuilder {
734736
// We should have pinned these down before getting here.
735737
assert(false);
736738
break;
739+
default:
740+
break;
737741
}
738742

739743
_pendingWhitespace = Whitespace.none;
@@ -928,7 +932,7 @@ class ChunkBuilder {
928932
void _hardenRules() {
929933
if (_hardSplitRules.isEmpty) return;
930934

931-
void walkConstraints(rule) {
935+
void walkConstraints(Rule rule) {
932936
rule.harden();
933937

934938
// Follow this rule's constraints, recursively.

lib/src/cli/format_command.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class FormatCommand extends Command<int> {
7878
}
7979

8080
// Can't use --verbose with anything but --help.
81-
if (argResults['verbose'] && !argResults['help']) {
81+
if (argResults['verbose'] && !(argResults['help'] as bool)) {
8282
usageException('Can only use --verbose with --help.');
8383
}
8484

lib/src/cli/options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ void defineOptions(ArgParser parser,
125125
}
126126

127127
List<int>? parseSelection(ArgResults argResults, String optionName) {
128-
var option = argResults[optionName];
128+
var option = argResults[optionName] as String?;
129129
if (option == null) return null;
130130

131131
// Can only preserve a selection when parsing from stdin.

0 commit comments

Comments
 (0)