Skip to content

Commit 87e527d

Browse files
authored
Remove support for fixes and --fix. (#1564)
The tools that come with the Dart SDK provide two ways to apply automated changes to code: `dart format --fix` and `dart fix`. The former is older and used to be faster. But it can only apply a few fixes and hasn't been maintained in many years. The `dart fix` command is actively maintained, can apply all of the fixes that `dart format --fix` could apply and many many more. In order to avoid duplicate engineering effort, we decided to consolidate on `dart fix` as the one way to make automated changes that go beyond the simple formatting and style changes that `dart format` applies. The ability to apply fixes is also removed from the `DartFormatter()` library API.
1 parent 8afa44a commit 87e527d

28 files changed

+104
-1600
lines changed

CHANGELOG.md

Lines changed: 33 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,38 @@
11
## 3.0.0-wip
22

3-
* Make the language version parameter to `DartFormatter()` mandatory.
3+
* **Remove support for fixes and `--fix`.** The tools that come with the Dart
4+
SDK provide two ways to apply automated changes to code: `dart format --fix`
5+
and `dart fix`. The former is older and used to be faster. But it can only
6+
apply a few fixes and hasn't been maintained in many years. The `dart fix`
7+
command is actively maintained, can apply all of the fixes that
8+
`dart format --fix` could apply and many many more.
9+
10+
In order to avoid duplicate engineering effort, we decided to consolidate on
11+
`dart fix` as the one way to make automated changes that go beyond the simple
12+
formatting and style changes that `dart format` applies.
13+
14+
The ability to apply fixes is also removed from the `DartFormatter()` library
15+
API.
16+
17+
* **Make the language version parameter to `DartFormatter()` mandatory.** This
18+
way, the formatter always knows what language version the input is intended
19+
to be treated as. Note that a `// @dart=` language version comment if present
20+
overrides the specified language version. You can think of the version passed
21+
to the `DartFormatter()` constructor as a "default" language version which
22+
the file's contents may then override.
23+
24+
If you don't particularly care about the version of what you're formatting,
25+
you can pass in `DartFormatter.latestLanguageVersion` to unconditionally get
26+
the latest language version that the formatter supports. Note that doing so
27+
means you will also implicitly opt into the new tall style when that style
28+
becomes available.
29+
30+
This change only affects the library API. When using the formatter from the
31+
command line, you can use `--language-version=` to specify a language version
32+
or pass `--language-version=latest` to use the latest supported version. If
33+
omitted, the formatter will look up the surrounding directories for a package
34+
config file and infer the language version for the package from that, similar
35+
to how other Dart tools behave like `dart analyze` and `dart run`.
436

537
## 2.3.7
638

README.md

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -32,29 +32,6 @@ if (tag == 'style' ||
3232
The formatter will never break your code—you can safely invoke it
3333
automatically from build and presubmit scripts.
3434

35-
## Style fixes
36-
37-
The formatter can also apply non-whitespace changes to make your code
38-
consistently idiomatic. You must opt into these by passing either `--fix` which
39-
applies all style fixes, or any of the `--fix-`-prefixed flags to apply specific
40-
fixes.
41-
42-
For example, running with `--fix-named-default-separator` changes this:
43-
44-
```dart
45-
greet(String name, {String title: "Captain"}) {
46-
print("Greetings, $title $name!");
47-
}
48-
```
49-
50-
into:
51-
52-
```dart
53-
greet(String name, {String title = "Captain"}) {
54-
print("Greetings, $title $name!");
55-
}
56-
```
57-
5835
## Using the formatter
5936

6037
The formatter is part of the unified [`dart`][] developer tool included in the

bin/format.dart

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import 'package:dart_style/src/cli/output.dart';
1010
import 'package:dart_style/src/cli/show.dart';
1111
import 'package:dart_style/src/cli/summary.dart';
1212
import 'package:dart_style/src/io.dart';
13-
import 'package:dart_style/src/short/style_fix.dart';
1413

1514
void main(List<String> args) async {
1615
var parser = ArgParser(allowTrailingOptions: true);
@@ -108,18 +107,6 @@ void main(List<String> args) async {
108107

109108
var followLinks = argResults['follow-links'] as bool;
110109

111-
var fixes = <StyleFix>[];
112-
if (argResults['fix'] as bool) fixes.addAll(StyleFix.all);
113-
for (var fix in StyleFix.all) {
114-
if (argResults['fix-${fix.name}'] as bool) {
115-
if (argResults['fix'] as bool) {
116-
usageError(parser, '--fix-${fix.name} is redundant with --fix.');
117-
}
118-
119-
fixes.add(fix);
120-
}
121-
}
122-
123110
if (argResults.wasParsed('stdin-name') && argResults.rest.isNotEmpty) {
124111
usageError(parser, 'Cannot pass --stdin-name when not reading from stdin.');
125112
}
@@ -128,7 +115,6 @@ void main(List<String> args) async {
128115
indent: indent,
129116
pageWidth: pageWidth,
130117
followLinks: followLinks,
131-
fixes: fixes,
132118
show: show,
133119
output: output,
134120
summary: summary,

example/format.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,6 @@ Future<void> _runTest(String path, int line,
7676
languageVersion: formatTest.languageVersion,
7777
pageWidth: testFile.pageWidth,
7878
indent: formatTest.leadingIndent,
79-
fixes: formatTest.fixes,
8079
experimentFlags: tall
8180
? const ['inline-class', tallStyleExperimentFlag]
8281
: const ['inline-class']);

lib/dart_style.dart

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,5 +3,4 @@
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/short/style_fix.dart';
76
export 'src/source_code.dart';

lib/src/cli/format_command.dart

Lines changed: 0 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import 'package:pub_semver/pub_semver.dart';
88

99
import '../dart_formatter.dart';
1010
import '../io.dart';
11-
import '../short/style_fix.dart';
1211
import 'formatter_options.dart';
1312
import 'options.dart';
1413
import 'output.dart';
@@ -116,18 +115,6 @@ class FormatCommand extends Command<int> {
116115
'"${argResults['indent']}".');
117116
}
118117

119-
var fixes = <StyleFix>[];
120-
if (argResults['fix'] as bool) fixes.addAll(StyleFix.all);
121-
for (var fix in StyleFix.all) {
122-
if (argResults['fix-${fix.name}'] as bool) {
123-
if (argResults['fix'] as bool) {
124-
usageException('--fix-${fix.name} is redundant with --fix.');
125-
}
126-
127-
fixes.add(fix);
128-
}
129-
}
130-
131118
List<int>? selection;
132119
try {
133120
selection = parseSelection(argResults, 'selection');
@@ -160,7 +147,6 @@ class FormatCommand extends Command<int> {
160147
indent: indent,
161148
pageWidth: pageWidth,
162149
followLinks: followLinks,
163-
fixes: fixes,
164150
show: show,
165151
output: output,
166152
summary: summary,

lib/src/cli/formatter_options.dart

Lines changed: 1 addition & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ import 'dart:io';
66

77
import 'package:pub_semver/pub_semver.dart';
88

9-
import '../short/style_fix.dart';
109
import '../source_code.dart';
1110
import 'output.dart';
1211
import 'show.dart';
@@ -31,9 +30,6 @@ class FormatterOptions {
3130
/// Whether symlinks should be traversed when formatting a directory.
3231
final bool followLinks;
3332

34-
/// The style fixes to apply while formatting.
35-
final List<StyleFix> fixes;
36-
3733
/// Which affected files should be shown.
3834
final Show show;
3935

@@ -55,14 +51,12 @@ class FormatterOptions {
5551
this.indent = 0,
5652
this.pageWidth = 80,
5753
this.followLinks = false,
58-
Iterable<StyleFix>? fixes,
5954
this.show = Show.changed,
6055
this.output = Output.write,
6156
this.summary = Summary.none,
6257
this.setExitIfChanged = false,
6358
List<String>? experimentFlags})
64-
: fixes = [...?fixes],
65-
experimentFlags = [...?experimentFlags];
59+
: experimentFlags = [...?experimentFlags];
6660

6761
/// Called when [file] is about to be formatted.
6862
///

lib/src/cli/options.dart

Lines changed: 0 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
// BSD-style license that can be found in the LICENSE file.
44
import 'package:args/args.dart';
55

6-
import '../short/style_fix.dart';
7-
86
void defineOptions(ArgParser parser,
97
{bool oldCli = false, bool verbose = false}) {
108
if (oldCli) {
@@ -78,15 +76,6 @@ void defineOptions(ArgParser parser,
7876
negatable: false,
7977
help: 'Return exit code 1 if there are any formatting changes.');
8078

81-
if (verbose) parser.addSeparator('Non-whitespace fixes (off by default):');
82-
parser.addFlag('fix',
83-
negatable: false, help: 'Apply all style fixes.', hide: !verbose);
84-
85-
for (var fix in StyleFix.all) {
86-
parser.addFlag('fix-${fix.name}',
87-
negatable: false, help: fix.description, hide: !verbose);
88-
}
89-
9079
if (verbose) parser.addSeparator('Other options:');
9180

9281
parser.addOption('line-length',

lib/src/dart_formatter.dart

Lines changed: 1 addition & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@ import 'constants.dart';
1818
import 'exceptions.dart';
1919
import 'front_end/ast_node_visitor.dart';
2020
import 'short/source_visitor.dart';
21-
import 'short/style_fix.dart';
2221
import 'source_code.dart';
2322
import 'string_compare.dart' as string_compare;
2423

@@ -46,8 +45,6 @@ class DartFormatter {
4645
/// The number of characters of indentation to prefix the output lines with.
4746
final int indent;
4847

49-
final Set<StyleFix> fixes;
50-
5148
/// Flags to enable experimental language features.
5249
///
5350
/// See dart.dev/go/experiments for details.
@@ -61,18 +58,14 @@ class DartFormatter {
6158
///
6259
/// If [indent] is given, that many levels of indentation will be prefixed
6360
/// before each resulting line in the output.
64-
///
65-
/// While formatting, also applies any of the given [fixes].
6661
DartFormatter(
6762
{required this.languageVersion,
6863
this.lineEnding,
6964
int? pageWidth,
7065
int? indent,
71-
Iterable<StyleFix>? fixes,
7266
List<String>? experimentFlags})
7367
: pageWidth = pageWidth ?? 80,
7468
indent = indent ?? 0,
75-
fixes = {...?fixes},
7669
experimentFlags = [...?experimentFlags];
7770

7871
/// Formats the given [source] string containing an entire Dart compilation
@@ -193,8 +186,7 @@ class DartFormatter {
193186
}
194187

195188
// Sanity check that only whitespace was changed if that's all we expect.
196-
if (fixes.isEmpty &&
197-
!string_compare.equalIgnoringWhitespace(source.text, output.text)) {
189+
if (!string_compare.equalIgnoringWhitespace(source.text, output.text)) {
198190
throw UnexpectedOutputException(source.text, output.text);
199191
}
200192

lib/src/io.dart

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,6 @@ Future<void> formatStdin(
3434
options.languageVersion ?? DartFormatter.latestLanguageVersion,
3535
indent: options.indent,
3636
pageWidth: options.pageWidth,
37-
fixes: options.fixes,
3837
experimentFlags: options.experimentFlags);
3938
try {
4039
options.beforeFile(null, name);
@@ -181,7 +180,6 @@ Future<bool> _processFile(
181180
languageVersion: languageVersion ?? DartFormatter.latestLanguageVersion,
182181
indent: options.indent,
183182
pageWidth: options.pageWidth,
184-
fixes: options.fixes,
185183
experimentFlags: options.experimentFlags);
186184

187185
try {

0 commit comments

Comments
 (0)