Skip to content

Commit 6249194

Browse files
authored
Update to and fix latest lints (#1335)
1 parent 7ae652e commit 6249194

24 files changed

+102
-134
lines changed

analysis_options.yaml

Lines changed: 5 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
1-
include: package:lints/recommended.yaml
2-
linter:
3-
rules:
4-
- avoid_dynamic_calls
5-
- directives_ordering
6-
- unawaited_futures
1+
include: package:dart_flutter_team_lints/analysis_options.yaml
2+
3+
analyzer:
4+
errors:
5+
comment_references: ignore

benchmark/benchmark.dart

Lines changed: 9 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -13,8 +13,8 @@ const _formatsPerTrial = 30;
1313
/// Note, these files use ".txt" because while they can be *parsed* correctly,
1414
/// they don't resolve without error. That's OK because the formatter doesn't
1515
/// care about that.
16-
final source = loadFile('before.dart.txt');
17-
final expected = loadFile('after.dart.txt');
16+
final source = _loadFile('before.dart.txt');
17+
final expected = _loadFile('after.dart.txt');
1818

1919
void main(List<String> args) {
2020
var best = 99999999.0;
@@ -27,7 +27,7 @@ void main(List<String> args) {
2727
// For a single benchmark, format the source multiple times.
2828
String? result;
2929
for (var j = 0; j < _formatsPerTrial; j++) {
30-
result = formatSource();
30+
result = _formatSource();
3131
}
3232

3333
var elapsed =
@@ -47,32 +47,23 @@ void main(List<String> args) {
4747
// Don't print the first run. It's always terrible since the VM hasn't
4848
// warmed up yet.
4949
if (i == 0) continue;
50-
printResult("Run ${padLeft('#$i', 3)}", elapsed);
50+
_printResult("Run ${'#$i'.padLeft(3)}", elapsed);
5151
}
5252

53-
printResult('Best ', best);
53+
_printResult('Best ', best);
5454
}
5555

56-
String loadFile(String name) {
56+
String _loadFile(String name) {
5757
var path = p.join(p.dirname(p.fromUri(Platform.script)), name);
5858
return File(path).readAsStringSync();
5959
}
6060

61-
void printResult(String label, double time) {
62-
print('$label: ${padLeft(time.toStringAsFixed(2), 4)}ms '
61+
void _printResult(String label, double time) {
62+
print('$label: ${time.toStringAsFixed(2).padLeft(4)}ms '
6363
"${'=' * ((time * 5).toInt())}");
6464
}
6565

66-
String padLeft(input, int length) {
67-
var result = input.toString();
68-
if (result.length < length) {
69-
result = ' ' * (length - result.length) + result;
70-
}
71-
72-
return result;
73-
}
74-
75-
String formatSource() {
66+
String _formatSource() {
7667
var formatter = DartFormatter();
7768
return formatter.format(source);
7869
}

bin/format.dart

Lines changed: 17 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -25,17 +25,17 @@ void main(List<String> args) async {
2525
usageError(parser, err.message);
2626
}
2727

28-
if (argResults['help']) {
28+
if (argResults['help'] as bool) {
2929
printUsage(parser);
3030
return;
3131
}
3232

33-
if (argResults['version']) {
33+
if (argResults['version'] as bool) {
3434
print(dartStyleVersion);
3535
return;
3636
}
3737

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

@@ -46,7 +46,7 @@ void main(List<String> args) async {
4646
usageError(parser, exception.message);
4747
}
4848

49-
if (argResults['dry-run'] && argResults['overwrite']) {
49+
if (argResults['dry-run'] as bool && argResults['overwrite'] as bool) {
5050
usageError(
5151
parser, 'Cannot use --dry-run and --overwrite at the same time.');
5252
}
@@ -61,13 +61,13 @@ void main(List<String> args) async {
6161
var summary = Summary.none;
6262
var output = Output.show;
6363
var setExitIfChanged = false;
64-
if (argResults['dry-run']) {
64+
if (argResults['dry-run'] as bool) {
6565
checkForReporterCollision('dry-run', 'overwrite');
6666
checkForReporterCollision('dry-run', 'machine');
6767

6868
show = Show.dryRun;
6969
output = Output.none;
70-
} else if (argResults['overwrite']) {
70+
} else if (argResults['overwrite'] as bool) {
7171
checkForReporterCollision('overwrite', 'machine');
7272

7373
if (argResults.rest.isEmpty) {
@@ -77,17 +77,17 @@ void main(List<String> args) async {
7777

7878
show = Show.overwrite;
7979
output = Output.write;
80-
} else if (argResults['machine']) {
80+
} else if (argResults['machine'] as bool) {
8181
output = Output.json;
8282
}
8383

84-
if (argResults['profile']) summary = Summary.profile();
84+
if (argResults['profile'] as bool) summary = Summary.profile();
8585

86-
setExitIfChanged = argResults['set-exit-if-changed'];
86+
setExitIfChanged = argResults['set-exit-if-changed'] as bool;
8787

8888
int pageWidth;
8989
try {
90-
pageWidth = int.parse(argResults['line-length']);
90+
pageWidth = int.parse(argResults['line-length'] as String);
9191
} on FormatException catch (_) {
9292
usageError(
9393
parser,
@@ -97,22 +97,22 @@ void main(List<String> args) async {
9797

9898
int indent;
9999
try {
100-
indent = int.parse(argResults['indent']);
101-
if (indent < 0 || indent.toInt() != indent) throw FormatException();
100+
indent = int.parse(argResults['indent'] as String);
101+
if (indent < 0 || indent.toInt() != indent) throw const FormatException();
102102
} on FormatException catch (_) {
103103
usageError(
104104
parser,
105105
'--indent must be a non-negative integer, was '
106106
'"${argResults['indent']}".');
107107
}
108108

109-
var followLinks = argResults['follow-links'];
109+
var followLinks = argResults['follow-links'] as bool;
110110

111111
var fixes = <StyleFix>[];
112-
if (argResults['fix']) fixes.addAll(StyleFix.all);
112+
if (argResults['fix'] as bool) fixes.addAll(StyleFix.all);
113113
for (var fix in StyleFix.all) {
114-
if (argResults['fix-${fix.name}']) {
115-
if (argResults['fix']) {
114+
if (argResults['fix-${fix.name}'] as bool) {
115+
if (argResults['fix'] as bool) {
116116
usageError(parser, '--fix-${fix.name} is redundant with --fix.');
117117
}
118118

@@ -133,7 +133,7 @@ void main(List<String> args) async {
133133
output: output,
134134
summary: summary,
135135
setExitIfChanged: setExitIfChanged,
136-
experimentFlags: argResults['enable-experiment']);
136+
experimentFlags: argResults['enable-experiment'] as List<String>);
137137

138138
if (argResults.rest.isEmpty) {
139139
await formatStdin(options, selection, argResults['stdin-name'] as String);

example/format.dart

Lines changed: 8 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
// Copyright (c) 2015, the Dart project authors. Please see the AUTHORS file
22
// for details. All rights reserved. Use of this source code is governed by a
33
// BSD-style license that can be found in the LICENSE file.
4+
45
import 'package:dart_style/dart_style.dart';
56
import 'package:dart_style/src/constants.dart';
67
import 'package:dart_style/src/debug.dart' as debug;
@@ -16,48 +17,17 @@ void main(List<String> args) {
1617
debug.tracePieceBuilder = true;
1718
debug.traceSolver = true;
1819

19-
runTest('selection/selection.stmt', 2);
20-
}
21-
22-
void formatStmt(String source, {required bool tall, int pageWidth = 80}) {
23-
runFormatter(source, pageWidth, tall: tall, isCompilationUnit: false);
24-
}
25-
26-
void formatUnit(String source, {required bool tall, int pageWidth = 80}) {
27-
runFormatter(source, pageWidth, tall: tall, isCompilationUnit: true);
28-
}
29-
30-
void runFormatter(String source, int pageWidth,
31-
{required bool tall, required bool isCompilationUnit}) {
32-
try {
33-
var formatter = DartFormatter(
34-
pageWidth: pageWidth,
35-
experimentFlags: [if (tall) tallStyleExperimentFlag]);
36-
37-
String result;
38-
if (isCompilationUnit) {
39-
result = formatter.format(source);
40-
} else {
41-
result = formatter.formatStatement(source);
42-
}
43-
44-
drawRuler('before', pageWidth);
45-
print(source);
46-
drawRuler('after', pageWidth);
47-
print(result);
48-
} on FormatterException catch (error) {
49-
print(error.message());
50-
}
20+
_runTest('selection/selection.stmt', 2);
5121
}
5222

53-
void drawRuler(String label, int width) {
23+
void _drawRuler(String label, int width) {
5424
var padding = ' ' * (width - label.length - 1);
5525
print('$label:$padding|');
5626
}
5727

5828
/// Runs the formatter test starting on [line] at [path] inside the "test"
5929
/// directory.
60-
Future<void> runTest(String path, int line,
30+
Future<void> _runTest(String path, int line,
6131
{int pageWidth = 40, bool tall = true}) async {
6232
var testFile = await TestFile.read(path);
6333
var formatTest = testFile.tests.firstWhere((test) => test.line == line);
@@ -81,16 +51,16 @@ Future<void> runTest(String path, int line,
8151
var expectedText = formatTest.output.textWithSelectionMarkers;
8252

8353
print('$path ${formatTest.description}');
84-
drawRuler('before', pageWidth);
54+
_drawRuler('before', pageWidth);
8555
print(formatTest.input.textWithSelectionMarkers);
8656
if (actualText == expectedText) {
87-
drawRuler('result', pageWidth);
57+
_drawRuler('result', pageWidth);
8858
print(actualText);
8959
} else {
9060
print('FAIL');
91-
drawRuler('expected', pageWidth);
61+
_drawRuler('expected', pageWidth);
9262
print(expectedText);
93-
drawRuler('actual', pageWidth);
63+
_drawRuler('actual', pageWidth);
9464
print(actualText);
9565
}
9666
}

lib/src/argument_list_visitor.dart

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -96,10 +96,9 @@ class ArgumentListVisitor {
9696
this._allArguments,
9797
this._arguments,
9898
this._functions,
99-
this._argumentsAfterFunctions) {
100-
assert(_functions == null || _argumentsAfterFunctions != null,
101-
'If _functions is passed, _argumentsAfterFunctions must be too.');
102-
}
99+
this._argumentsAfterFunctions)
100+
: assert(_functions == null || _argumentsAfterFunctions != null,
101+
'If _functions is passed, _argumentsAfterFunctions must be too.');
103102

104103
/// Builds chunks for the argument list.
105104
void visit() {

lib/src/cli/format_command.dart

Lines changed: 8 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ class FormatCommand extends Command<int> {
3232
Future<int> run() async {
3333
var argResults = this.argResults!;
3434

35-
if (argResults['version']) {
35+
if (argResults['version'] as bool) {
3636
print(dartStyleVersion);
3737
return 0;
3838
}
@@ -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'] as bool)) {
81+
if (argResults['verbose'] as bool && !(argResults['help'] as bool)) {
8282
usageException('Can only use --verbose with --help.');
8383
}
8484

@@ -87,11 +87,11 @@ class FormatCommand extends Command<int> {
8787
usageException('Cannot print a summary with JSON output.');
8888
}
8989

90-
var pageWidth = int.tryParse(argResults['line-length']) ??
90+
var pageWidth = int.tryParse(argResults['line-length'] as String) ??
9191
usageException('--line-length must be an integer, was '
9292
'"${argResults['line-length']}".');
9393

94-
var indent = int.tryParse(argResults['indent']) ??
94+
var indent = int.tryParse(argResults['indent'] as String) ??
9595
usageException('--indent must be an integer, was '
9696
'"${argResults['indent']}".');
9797

@@ -101,10 +101,10 @@ class FormatCommand extends Command<int> {
101101
}
102102

103103
var fixes = <StyleFix>[];
104-
if (argResults['fix']) fixes.addAll(StyleFix.all);
104+
if (argResults['fix'] as bool) fixes.addAll(StyleFix.all);
105105
for (var fix in StyleFix.all) {
106-
if (argResults['fix-${fix.name}']) {
107-
if (argResults['fix']) {
106+
if (argResults['fix-${fix.name}'] as bool) {
107+
if (argResults['fix'] as bool) {
108108
usageException('--fix-${fix.name} is redundant with --fix.');
109109
}
110110

@@ -119,7 +119,7 @@ class FormatCommand extends Command<int> {
119119
usageException(exception.message);
120120
}
121121

122-
var followLinks = argResults['follow-links'];
122+
var followLinks = argResults['follow-links'] as bool;
123123
var setExitIfChanged = argResults['set-exit-if-changed'] as bool;
124124

125125
var experimentFlags = argResults['enable-experiment'] as List<String>;

lib/src/cli/options.dart

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -145,7 +145,7 @@ List<int>? parseSelection(ArgResults argResults, String optionName) {
145145
try {
146146
var coordinates = option.split(':');
147147
if (coordinates.length != 2) {
148-
throw FormatException(
148+
throw const FormatException(
149149
'Selection should be a colon-separated pair of integers, "123:45".');
150150
}
151151

lib/src/cli/output.dart

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -46,11 +46,11 @@ enum Output {
4646
break;
4747

4848
case Output.json:
49-
// TODO(rnystrom): Put an empty selection in here to remain compatible with
50-
// the old formatter. Since there's no way to pass a selection on the
51-
// command line, this will never be used, which is why it's hard-coded to
52-
// -1, -1. If we add support for passing in a selection, put the real
53-
// result here.
49+
// TODO(rnystrom): Put an empty selection in here to remain compatible
50+
// with the old formatter. Since there's no way to pass a selection on
51+
// the command line, this will never be used, which is why it's
52+
// hard-coded to -1, -1. If we add support for passing in a selection,
53+
// put the real result here.
5454
print(jsonEncode({
5555
'path': path,
5656
'source': result.text,

lib/src/dart_formatter.dart

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ class DartFormatter {
7171
///
7272
/// If [uri] is given, it is a [String] or [Uri] used to identify the file
7373
/// being formatted in error messages.
74-
String format(String source, {uri}) {
74+
String format(String source, {Object? uri}) {
7575
if (uri == null) {
7676
// Do nothing.
7777
} else if (uri is Uri) {
@@ -82,7 +82,8 @@ class DartFormatter {
8282
throw ArgumentError('uri must be `null`, a Uri, or a String.');
8383
}
8484

85-
return formatSource(SourceCode(source, uri: uri, isCompilationUnit: true))
85+
return formatSource(
86+
SourceCode(source, uri: uri as String?, isCompilationUnit: true))
8687
.text;
8788
}
8889

0 commit comments

Comments
 (0)