A static analysis tool that helps you analyse and improve your code quality
Dart Code Metrics
Dart Code Metrics is a static analysis tool that helps you analyse and improve your code quality.
Quick start
Analyzer plugin
A plugin for the Dart analyzer
package providing additional rules from Dart Code Metrics. All issues produced by rules or anti-patterns will be highlighted in IDE.
-
Install package as a dev dependency
$ 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.2.0-dev.1
and then run
$ dart pub get # or for a Flutter package $ flutter pub get
-
Add configuration to
analysis_options.yaml
analyzer: plugins: - dart_code_metrics 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
CLI
The package can be used as a command-line tool.
It will produce a result in one of the supported formats:
- Console
- GitHub
- Codeclimate
- HTML
- JSON
Usage
Install the package as listed in the Analyzer plugin usage example.
If you want the command-line tool to check rules, you should configure rules
entry in the analysis_options.yaml
first.
dart pub run dart_code_metrics:metrics lib
# or for a Flutter package
flutter pub run dart_code_metrics:metrics lib
Multi-package repositories usage
If you use Melos, you can add custom command to melos.yaml
.
metrics:
run: |
melos exec -c 1 --ignore="*example*" -- \
flutter pub run dart_code_metrics:metrics lib
description: |
Run `dart_code_metrics` in all packages.
- Note: you can also rely on your IDEs Dart Analysis / Issues window.
Options
Usage: metrics [arguments...] <directories>
-h, --help Print this usage information.
-r, --reporter=<console> The format of the output of the analysis
[console (default), console-verbose, codeclimate, github, gitlab, html, json]
-o, --output-directory=<OUTPUT> Write HTML output to OUTPUT
(defaults to "metrics")
--cyclomatic-complexity=<20> Cyclomatic Complexity threshold
--lines-of-code=<100> Lines of Code threshold
--maximum-nesting-level=<5> Maximum Nesting Level threshold
--number-of-methods=<10> Number of Methods threshold
--number-of-parameters=<4> Number of Parameters threshold
--source-lines-of-code=<50> Source lines of Code threshold
--weight-of-class=<0.33> Weight Of a Class threshold
--root-folder=<./> Root folder
(defaults to current directory)
--exclude=<{/**.g.dart,/**.template.dart}> File paths in Glob syntax to be exclude
(defaults to "{/**.g.dart,/**.template.dart}")
--set-exit-on-violation-level=<warning> Set exit code 2 if code violations same or higher level than selected are detected
[noted, warning, alarm]