|
1 | 1 | ---
|
2 | 2 | title: dart format
|
3 | 3 | description: Command-line tool for formatting Dart source code.
|
4 |
| -toc: false |
5 | 4 | ---
|
6 | 5 |
|
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. |
12 | 11 |
|
13 | 12 | {% render 'tools/dart-tool-note.md' %}
|
14 | 13 |
|
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: |
18 | 26 |
|
19 | 27 | ```console
|
20 | 28 | $ dart format .
|
21 | 29 | ```
|
22 | 30 |
|
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, |
26 | 36 | plus one Dart file under the `bin` directory:
|
27 | 37 |
|
28 | 38 | ```console
|
29 | 39 | $ dart format lib bin/updater.dart
|
30 | 40 | ```
|
31 | 41 |
|
32 |
| -:::warning Notice |
| 42 | +### Prevent overwriting Dart files |
| 43 | + |
33 | 44 | By default, `dart format` **overwrites** the Dart files.
|
34 |
| -::: |
35 | 45 |
|
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`. |
40 | 49 |
|
41 | 50 | ```console
|
42 | 51 | $ dart format -o show bin/my_app.dart
|
43 | 52 | ```
|
44 | 53 |
|
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, |
47 | 57 | 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. |
50 | 64 |
|
51 | 65 | ```console
|
52 | 66 | $ dart format -o none --set-exit-if-changed bin/my_app.dart
|
53 | 67 | ```
|
54 | 68 |
|
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 |
58 | 70 |
|
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 |
62 | 78 |
|
63 |
| -:::tip |
64 | 79 | To avoid making changes that might be unsafe,
|
65 | 80 | `dart format` only affects whitespace.
|
66 | 81 |
|
67 | 82 | There's a lot more to writing readable and
|
68 | 83 | consistent code than just whitespace, though.
|
69 | 84 | To learn more about best practices for writing and styling Dart code,
|
70 | 85 | 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 | +``` |
72 | 96 |
|
73 | 97 | [Dart style guide]: /effective-dart/style
|
| 98 | +[dart_style]: {{site.pub-pkg}}/dart_style |
| 99 | +[dart-guidelines]: /effective-dart/style#formatting |
0 commit comments