A command-line tool that reformats } else { (and optionally } catch) constructs so that the else/catch keyword appears on its own line, separated from the closing brace.
Before:
if (condition) {
doSomething();
} else {
doOther();
}After:
if (condition) {
doSomething();
}
else {
doOther();
}elsefix automatically detects whether the file uses spaces or tabs and preserves the existing indentation style.
- Dart SDK ^3.10.7
dart run bin/elsefix.dart <filename> [flags]dart compile exe bin/elsefix.dart
mv bin/elsefix.exe elsefixThen place the resulting elsefix binary somewhere on your $PATH.
elsefix [<filename>|-] [flags]
| Flag | Short | Description |
|---|---|---|
--stdin |
- |
Read from stdin instead of a file |
--stdout |
-s |
Print results to stdout instead of editing in place |
--include-catch |
-c |
Also reformat } catch blocks |
--interactive |
-i |
Review each change individually before applying |
--help |
-h |
Print the help menu |
Edit a file in place:
elsefix myfile.dartPreview changes without modifying the file:
elsefix myfile.dart --stdoutAlso fix } catch blocks:
elsefix myfile.dart --include-catchRead from stdin and write to stdout (useful in pipelines):
cat myfile.dart | elsefix -Interactive mode — review each change one at a time:
elsefix myfile.dart --interactiveIn interactive mode, a colored diff is shown for each match and you are prompted to respond:
3 | if (condition) {
4 | doSomething();
- 5 | } else {
+ 5 | }
+ 6 | else {
7 | doOther();
[y]es / [n]o / [A]ccept all / [q]uit:
| Key | Action |
|---|---|
y |
Accept this change |
n |
Skip this change |
A |
Accept this and all remaining changes |
q |
Quit; leave remaining lines unchanged |
Note:
--interactivecannot be combined with--stdin.
- elsefix is smart about string literals — it will not reformat
elseorcatchthat appears inside a quoted string. - When editing in place (default), the file is overwritten with the reformatted content.
- When using
--stdinor--stdout, the result is printed to stdout.