Skip to content

Commit 947dc5b

Browse files
Refactor dart format (#5845)
Fixes #4440 --------- Co-authored-by: Marya <111139605+MaryaBelanger@users.noreply.github.com>
1 parent 3ac4e6a commit 947dc5b

File tree

1 file changed

+56
-30
lines changed

1 file changed

+56
-30
lines changed

src/content/tools/dart-format.md

+56-30
Original file line numberDiff line numberDiff line change
@@ -1,73 +1,99 @@
11
---
22
title: dart format
33
description: Command-line tool for formatting Dart source code.
4-
toc: false
54
---
65

7-
Use the `dart format` command to replace the whitespace in your program
8-
with formatting that follows
9-
[Dart guidelines](/effective-dart/style#formatting).
10-
This is the same formatting that you can get
11-
when using an IDE or editor that has Dart support.
6+
To update your code to follow the
7+
[Dart formatting guidelines][dart-guidelines],
8+
use the `dart format` command.
9+
This formatting follows what you get
10+
when using an IDE or editor with Dart support.
1211

1312
{% render 'tools/dart-tool-note.md' %}
1413

15-
Provide a list of files or directories to the `dart format` command.
16-
For example, here's how to format all the Dart files
17-
in or under the current directory:
14+
## Specify files to format
15+
16+
To reformat one or more Dart files,
17+
provide a list of paths to the desired files or directories.
18+
19+
### Specify one path
20+
21+
Provide the path to one file or directory.
22+
If you specify a directory, `dart format` affects only the files in the
23+
immediate directory; it doesn't recurse through subdirectories.
24+
25+
**Example:** To format all the Dart files in or under the current directory:
1826

1927
```console
2028
$ dart format .
2129
```
2230

23-
To specify multiple files or directories,
24-
use a space-delimited list.
25-
The following command formats all Dart files under the `lib` directory,
31+
### Specify multiple paths
32+
33+
To specify multiple files or directories, use a space-delimited list.
34+
35+
**Example:** To format all Dart files under the `lib` directory,
2636
plus one Dart file under the `bin` directory:
2737

2838
```console
2939
$ dart format lib bin/updater.dart
3040
```
3141

32-
:::warning Notice
42+
### Prevent overwriting Dart files
43+
3344
By default, `dart format` **overwrites** the Dart files.
34-
:::
3545

36-
If you don't want to overwrite the files,
37-
add the `--output` or `-o` flag.
38-
Use `-o show` or `-o json` to get the contents of the formatted files,
39-
or `-o none` to see only which files would change.
46+
* To not overwrite the files, add the `--output` or `-o` flag.
47+
* To get the contents of the formatted files, add `-o show` or `-o json`.
48+
* To see only which files _would_ change, add `-o none`.
4049

4150
```console
4251
$ dart format -o show bin/my_app.dart
4352
```
4453

45-
To make the command have an exit code of `1`
46-
if any formatting changes occur,
54+
## Notify when changes occur
55+
56+
To make `dart format` return an exit code when formatting changes occur,
4757
add the `--set-exit-if-changed` flag.
48-
This exit code is often used with continuous integration (CI)
49-
to indicate that a check should fail.
58+
59+
* If changes occur, the `dart format` command returns an exit code of `1`.
60+
* If changes don't occur, the `dart format` command returns an exit code of `0`.
61+
62+
Use exit codes with continuous integration (CI) systems
63+
so they can trigger another action in response to the exit code.
5064

5165
```console
5266
$ dart format -o none --set-exit-if-changed bin/my_app.dart
5367
```
5468

55-
For information on additional command-line options,
56-
use the `dart help` command or see the documentation for the
57-
[dart_style package.]({{site.pub-pkg}}/dart_style)
69+
## Use trailing commas
5870

59-
```console
60-
$ dart help format
61-
```
71+
Use optional trailing commas for better automatic formatting.
72+
Add a trailing comma at the end of parameter lists in functions, methods,
73+
and constructors.
74+
This helps the formatter insert the appropriate amount of line breaks for
75+
Dart-style code.
76+
77+
## Affects whitespace only
6278

63-
:::tip
6479
To avoid making changes that might be unsafe,
6580
`dart format` only affects whitespace.
6681

6782
There's a lot more to writing readable and
6883
consistent code than just whitespace, though.
6984
To learn more about best practices for writing and styling Dart code,
7085
check out the [Dart style guide][].
71-
:::
86+
87+
## Learn more
88+
89+
To learn about additional command-line options,
90+
use the `dart help` command or see the documentation for the
91+
[dart_style package][dart_style]
92+
93+
```console
94+
$ dart help format
95+
```
7296

7397
[Dart style guide]: /effective-dart/style
98+
[dart_style]: {{site.pub-pkg}}/dart_style
99+
[dart-guidelines]: /effective-dart/style#formatting

0 commit comments

Comments
 (0)