Skip to content

Commit 7aeb24e

Browse files
Hixieamantoux
authored andcommitted
Add a way to opt a file out of Dart formatting (flutter#4292)
1 parent 622cd7f commit 7aeb24e

File tree

4 files changed

+61
-2
lines changed

4 files changed

+61
-2
lines changed

script/tool/CHANGELOG.md

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,8 @@
1-
## NEXT
1+
## 0.7.0
22

33
- `native-test` now supports `--linux` for unit tests.
4+
- Formatting now skips Dart files that contain a line that exactly
5+
matches the string `// This file is hand-formatted.`.
46

57
## 0.6.0+1
68

script/tool/lib/src/format_command.dart

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,27 @@ class FormatCommand extends PluginCommand {
206206

207207
final String fromPath = relativeTo.path;
208208

209+
// Dart files are allowed to have a pragma to disable auto-formatting. This
210+
// was added because Hixie hurts when dealing with what dartfmt does to
211+
// artisanally-formatted Dart, while Stuart gets really frustrated when
212+
// dealing with PRs from newer contributors who don't know how to make Dart
213+
// readable. After much discussion, it was decided that files in the plugins
214+
// and packages repos that really benefit from hand-formatting (e.g. files
215+
// with large blobs of hex literals) could be opted-out of the requirement
216+
// that they be autoformatted, so long as the code's owner was willing to
217+
// bear the cost of this during code reviews.
218+
// In the event that code ownership moves to someone who does not hold the
219+
// same views as the original owner, the pragma can be removed and the file
220+
// auto-formatted.
221+
const String handFormattedExtension = '.dart';
222+
const String handFormattedPragma = '// This file is hand-formatted.';
223+
209224
return files
225+
.where((File file) {
226+
// See comment above near [handFormattedPragma].
227+
return path.extension(file.path) != handFormattedExtension ||
228+
!file.readAsLinesSync().contains(handFormattedPragma);
229+
})
210230
.map((File file) => path.relative(file.path, from: fromPath))
211231
.where((String path) =>
212232
// Ignore files in build/ directories (e.g., headers of frameworks)

script/tool/pubspec.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
name: flutter_plugin_tools
22
description: Productivity utils for flutter/plugins and flutter/packages
33
repository: https://github.com/flutter/plugins/tree/master/script/tool
4-
version: 0.6.0+1
4+
version: 0.7.0
55

66
dependencies:
77
args: ^2.1.0

script/tool/test/format_command_test.dart

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import 'package:args/command_runner.dart';
88
import 'package:file/file.dart';
99
import 'package:file/memory.dart';
1010
import 'package:flutter_plugin_tools/src/common/core.dart';
11+
import 'package:flutter_plugin_tools/src/common/file_utils.dart';
1112
import 'package:flutter_plugin_tools/src/format_command.dart';
1213
import 'package:path/path.dart' as p;
1314
import 'package:test/test.dart';
@@ -106,6 +107,42 @@ void main() {
106107
]));
107108
});
108109

110+
test('does not format .dart files with pragma', () async {
111+
const List<String> formattedFiles = <String>[
112+
'lib/a.dart',
113+
'lib/src/b.dart',
114+
'lib/src/c.dart',
115+
];
116+
const String unformattedFile = 'lib/src/d.dart';
117+
final Directory pluginDir = createFakePlugin(
118+
'a_plugin',
119+
packagesDir,
120+
extraFiles: <String>[
121+
...formattedFiles,
122+
unformattedFile,
123+
],
124+
);
125+
126+
final p.Context posixContext = p.posix;
127+
childFileWithSubcomponents(pluginDir, posixContext.split(unformattedFile))
128+
.writeAsStringSync(
129+
'// copyright bla bla\n// This file is hand-formatted.\ncode...');
130+
131+
await runCapturingPrint(runner, <String>['format']);
132+
133+
expect(
134+
processRunner.recordedCalls,
135+
orderedEquals(<ProcessCall>[
136+
ProcessCall(
137+
getFlutterCommand(mockPlatform),
138+
<String>[
139+
'format',
140+
..._getPackagesDirRelativePaths(pluginDir, formattedFiles)
141+
],
142+
packagesDir.path),
143+
]));
144+
});
145+
109146
test('fails if flutter format fails', () async {
110147
const List<String> files = <String>[
111148
'lib/a.dart',

0 commit comments

Comments
 (0)