Skip to content
This repository was archived by the owner on Feb 22, 2023. It is now read-only.

Commit 0e63075

Browse files
committed
Add a way to opt a file out of Dart formatting
1 parent 5afbfe9 commit 0e63075

File tree

3 files changed

+60
-0
lines changed

3 files changed

+60
-0
lines changed

script/tool/CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,8 @@
1212
`--packages` flag used by most other packages.
1313
- **Breaking change** Passing both `--run-on-changed-packages` and `--packages`
1414
is now an error; previously it the former would be ignored.
15+
- Formatting now skips files that contain a line that exactly
16+
matches the string `// This file is hand-formatted.`.
1517

1618
## 0.5.0
1719

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/test/format_command_test.dart

Lines changed: 38 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';
@@ -85,6 +86,7 @@ void main() {
8586
'lib/src/b.dart',
8687
'lib/src/c.dart',
8788
];
89+
const String fileWithOptOut = 'lib/src/d.dart';
8890
final Directory pluginDir = createFakePlugin(
8991
'a_plugin',
9092
packagesDir,
@@ -106,6 +108,42 @@ void main() {
106108
]));
107109
});
108110

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

0 commit comments

Comments
 (0)