úterý 20. ledna 2015

dart_style: How to make your Dart code even better

Recently I noticed a package dart_style which helps you have your code according to the Dart Style Guide.

It is a formatter/linter for Dart code which you can run on desired files from the command line. It then outputs formatted code directly to the command line or you can let overwrite existing files with formatted code.

Update: dart_style package is a new Dart formatter dartfmt!

How to install?

We need to activate globally the package via the command in command line:

pub global activate dart_style

I personally use it directly from Github to have always last (dev) version via the command: 

pub global activate --source git https://github.com/dart-lang/dart_style.git

How to use?

And now the magic begins with the command dartfmt. For example:

dartfmt index.dart

This outputs formatted index.dart in the console. But usually I want to directly format all my files in the folder and overwrite my existing files with formatted code. The parameter -w does this trick for me:

dartfmt -w lib

This command goes through all the .dart files in the lib folder and tries to format them. In my case the output may look like this:

Formatting directory lib:
Unchanged controller.dart
Unchanged model\user.dart
Formatted view\app_view.dart
Formatted view\menu_view.dart

And that's it! Your code should be now nicely formatted. And it is possible to use the formatter not only via the command line but also programmatically.

Dart style formatter is now one of my favourite tools used during Dart development and it helps me to have my code even better.

What do you think about Dart style formatter? Have you tried it?

Žádné komentáře: