dart_code_metrics 1.5.0

  • Readme
  • Changelog
  • Example
  • Installing
  • 77

Build Status Pub Version

The Dart command line tool which helps to improve code quality #

Reports:

  • Cyclomatic complexity of methods
  • Too long methods
  • Number of Arguments

Output formats:

  • Plain terminal
  • JSON
  • HTML
  • Codeclimate

Simple usage: #

pub global activate dart_code_metrics
metrics lib

Full usage: #

Usage: metrics [options...] <directories>
-h, --help                                             Print this usage information.
-r, --reporter=<console>                               The format of the output of the analysis
                                                       [console (default), json, html, codeclimate]

    --cyclomatic-complexity=<20>                       Cyclomatic complexity threshold
                                                       (defaults to "20")

    --lines-of-code=<50>                               Lines of code threshold
                                                       (defaults to "50")

    --number-of-arguments=<4>                          Number of arguments threshold
                                                       (defaults to "4")

    --root-folder=<./>                                 Root folder
                                                       (defaults to current directory)

    --ignore-files=<{/**.g.dart,/**.template.dart}>    Filepaths in Glob syntax to be ignored
                                                       (defaults to "{/**.g.dart,/**.template.dart}")

    --verbose
    --set-exit-on-violation-level=<warning>            Set exit code 2 if code violations same or higher level than selected are detected
                                                       [noted, warning, alarm]

Use as library #

See example/example.dart

Changelog #

1.5.0 #

  • Add experimental static code diagnostics double-literal-format, no-boolean-literal-compare

1.4.0 #

  • Drop dependency on built_collection
  • Add set-exit-on-violation-level cli argument

1.3.1 #

  • Fix get arguments count

1.3.0 #

  • Add Number of Arguments metrics

1.2.1 #

  • Validate root-folder argument
  • Fix paths to analyze fail to validate with non-default root-folder
  • Fix paths weren't validated to be inside root-folder
  • Support factory constructors analysis

1.2.0 #

  • Allow to analyze multiple directories

1.1.5 #

  • Tweak console reporter

1.1.4 #

  • Add some dartdocs
  • Update readme
  • Add library usage example

1.1.3 #

  • Fix validate input arguments

1.1.2 #

  • Improve CodeClimate report

1.1.1 #

  • Added support extension methods

1.1.0 #

  • Added support for CodeClimate

1.0.0 #

  • Initial release

example/example.dart

import 'package:dart_code_metrics/metrics_analyzer.dart';
import 'package:dart_code_metrics/reporters.dart';

void main() {
  // Get some files you would like to analyze
  const filesToAnalyze = ['some_file.dart', 'another_file.dart'];
  // Root folder path is used to resolve relative file paths
  const rootFolder = 'lib/src';

  // Recorder keeps reported issues in format-agnostic way
  final recorder = MetricsAnalysisRecorder();

  // Analyzer traverses files and report its findings to passed recorder
  final analyzer = MetricsAnalyzer(recorder);

  // Runner coordinates recorder and analyzer
  final runner = MetricsAnalysisRunner(recorder, analyzer, filesToAnalyze,
      rootFolder: rootFolder);

  // Execute run() to analyze files and collect results
  runner.run();

  // Now runner.results() contains some insights about analyzed code. Let's report it!
  // For a simple example we would report results to terminal

  // First of all config has to be created for a reporter
  const reporterConfig = Config(
      cyclomaticComplexityWarningLevel: 10,
      linesOfCodeWarningLevel: 50,
      numberOfArgumentsWarningLevel: 4);

  // Now the reporter itself
  final reporter = ConsoleReporter(reportConfig: reporterConfig);

  // Now pass collected analysis reports from runner to reporter and that's it
  reporter.report(runner.results()).forEach(print);

  // There is also JsonReporter for making machine-readable reports
  // HtmlReporter produces fancy html-documents with bells and whistles
  // And CodeClimateReporter produces reports that are widely understood by various CI tools
  // If none of these fits your case you can always access raw analysis info via results() method of runner and process it any way you see fit
}

Use this package as an executable

1. Install it

You can install the package from the command line:


$ pub global activate dart_code_metrics

2. Use it

The package has the following executables:


$ metrics

Use this package as a library

1. Depend on it

Add this to your package's pubspec.yaml file:


dependencies:
  dart_code_metrics: ^1.5.0

2. Install it

You can install packages from the command line:

with pub:


$ pub get

with Flutter:


$ flutter pub get

Alternatively, your editor might support pub get or flutter pub get. Check the docs for your editor to learn more.

3. Import it

Now in your Dart code, you can use:


import 'package:dart_code_metrics/analyzer_plugin.dart';
import 'package:dart_code_metrics/metrics_analyzer.dart';
import 'package:dart_code_metrics/reporters.dart';
  
Popularity:
Describes how popular the package is relative to other packages. [more]
53
Health:
Code health derived from static analysis. [more]
100
Maintenance:
Reflects how tidy and up-to-date the package is. [more]
100
Overall:
Weighted score of the above. [more]
77
Learn more about scoring.

We analyzed this package on Jun 4, 2020, and provided a score, details, and suggestions below. Analysis was completed with status completed using:

  • Dart: 2.8.2
  • pana: 0.13.8-dev

Dependencies

Package Constraint Resolved Available
Direct dependencies
Dart SDK >=2.4.0 <3.0.0
analyzer >=0.38.0 <0.40.0 0.39.10
analyzer_plugin ^0.2.1 0.2.4
ansicolor ^1.0.0 1.0.2
args ^1.5.0 1.6.0
crypto ^2.1.0 2.1.5
glob ^1.2.0 1.2.0
html >=0.13.0 <1.0.0 0.14.0+3
meta ^1.1.0 1.1.8
path ^1.6.0 1.7.0
quiver ^2.1.0 2.1.3
resource ^2.1.0 2.1.6
source_span ^1.5.0 1.7.0
yaml ^2.2.0 2.2.1
Transitive dependencies
_fe_analyzer_shared 4.0.0
async 2.4.1
charcode 1.1.3
collection 1.14.12
convert 2.1.1
csslib 0.16.1
dart_style 1.3.6
js 0.6.1+1
matcher 0.12.6
node_interop 1.1.1
node_io 1.1.1
package_config 1.9.3
pub_semver 1.4.4
stack_trace 1.9.3
string_scanner 1.0.5
term_glyph 1.1.0
typed_data 1.1.6
watcher 0.9.7+15
Dev dependencies
coverage ^0.13.5
coveralls ^5.4.0
dependency_validator ^1.5.0
lcov ^5.4.0
mockito ^4.1.0
pedantic ^1.9.0 1.9.0
test ^1.13.0