/dart-code-metrics

Software analytics tool that helps developers analyse and improve software quality.

Primary LanguageDartMIT LicenseMIT

Build Status Coverage Status Pub Version Dart SDK Version License Pub popularity GitHub popularity

Dart Code Metrics

Note: you can find the full documentation on the website

Configuration | Rules | Metrics | Anti-patterns

Dart Code Metrics logo

Dart Code Metrics is a static analysis tool that helps you analyse and improve your code quality.

Links

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.

  1. 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.3.3

    and then run

    $ dart pub get
    
    # or for a Flutter package
    $ flutter pub get
  2. 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
  3. 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.
    --halstead-volume=<150>                       Halstead Volume 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.
    --maintainability-index=<50>                  Maintainability Index 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]
    --[no-]fatal-style                            Treat style level issues as fatal.
    --[no-]fatal-performance                      Treat performance level issues as fatal.
    --[no-]fatal-warnings                         Treat warning level issues as fatal.

Troubleshooting

Please read the following guide if the plugin is not working as you'd expect it to work.

Contributing

If you are interested in contributing, please check out the contribution guidelines. Feedback and contributions are welcome!

How to reach us

Please feel free to ask any questions about this tool. Join our community chat on Telegram. We speak both English and Russian.

LICENCE

MIT