Note: you can find the full documentation on the website
Configuration | Rules | Metrics | Anti-patterns
Dart Code Metrics is a static analysis tool that helps you analyse and improve your code quality.
- Reports code metrics
- Provides additional rules for the dart analyzer
- Checks for anti-patterns
- Checks unused
*.dart
files - Checks unused l10n
- Can be used as CLI, analyzer plugin or library
- See CHANGELOG.md for major/breaking updates, and releases for a detailed version history.
- To contribute, please read CONTRIBUTING.md first.
- Please open an issue if anything is missing or unclear in this documentation.
$ dart pub add --dev dart_code_metrics
# or for a Flutter package
$ flutter pub add --dev dart_code_metrics
OR
add it manually to pubspec.yaml
dev_dependencies:
dart_code_metrics: ^4.6.0
and then run
$ dart pub get
# or for a Flutter package
$ flutter pub get
Add configuration to analysis_options.yaml
dart_code_metrics:
anti-patterns:
- long-method
- long-parameter-list
metrics:
cyclomatic-complexity: 20
maximum-nesting-level: 5
number-of-parameters: 4
source-lines-of-code: 50
metrics-exclude:
- test/**
rules:
- newline-before-return
- no-boolean-literal-compare
- no-empty-block
- prefer-trailing-comma
- prefer-conditional-expressions
- no-equal-then-else
Reload IDE to allow the analyzer to discover the plugin config.
You can read more about the configuration on the website.
Dart Code Metrics can be used as a plugin for the Dart analyzer
package providing additional rules. All issues produced by rules or anti-patterns will be highlighted in IDE.
Rules that marked with a has auto-fix
badge have auto-fixes available through the IDE context menu. VS Code example:
The package can be used as CLI and supports multiple commands:
Command | Example of use | Short description |
---|---|---|
analyze | dart run dart_code_metrics:metrics analyze lib | Reports code metrics, rules and anti-patterns violations. |
check-unused-files | dart run dart_code_metrics:metrics check-unused-files lib | Checks unused *.dart files. |
check-unused-l10n | dart run dart_code_metrics:metrics check-unused-l10n lib | Check unused localization in *.dart files. |
For additional help on any of the commands, enter dart run dart_code_metrics:metrics help <command>
Note: if you're setting up Dart Code Metrics for multi-package repository, check out this website section.
Reports code metrics, rules and anti-patterns violations. To execute the command, run
$ dart run dart_code_metrics:metrics analyze lib
# or for a Flutter package
$ flutter pub run dart_code_metrics:metrics analyze lib
It will produce a result in one of the format:
- Console
- GitHub
- Codeclimate
- HTML
- JSON
Console report example:
Checks unused *.dart
files. To execute the command, run
$ dart run dart_code_metrics:metrics check-unused-files lib
# or for a Flutter package
$ flutter pub run dart_code_metrics:metrics check-unused-files lib
It will produce a result in one of the format:
- Console
- JSON
Console report example:
Checks unused Dart class members, that encapsulates the app’s localized values.
An example of such class:
class ClassWithLocalization {
String get title {
return Intl.message(
'Hello World',
name: 'title',
desc: 'Title for the Demo application',
locale: localeName,
);
}
}
To execute the command, run
$ dart run dart_code_metrics:metrics check-unused-l10n lib
# or for a Flutter package
$ flutter pub run dart_code_metrics:metrics check-unused-l10n lib
It will produce a result in one of the format:
- Console
- JSON
Console report example:
Please read the following guide if the plugin is not working as you'd expect it to work.
If you are interested in contributing, please check out the contribution guidelines. Feedback and contributions are welcome!
Please feel free to ask any questions about this tool. Join our community chat on Telegram. We speak both English and Russian.