This repo is a fork of dart-code-metrics repo.
The free version of Dart Code Metrics as been deprecated and a new paid version is available at https://dcm.dev/pricing/. This fork only provides a support to maintain compatibility of the free version of DCM for those who cannot afford the paid version. No new features will be implemented and all credits goes to DCM team.
These are the fixes that have been added:
- chore: restrict
http
version to>=0.13.0 <2.0.0
. - chore: restrict
uuid
version to>=3.0.0 <5.0.0
.
Note: you can find the full documentation on the website
Configuration | Rules | Metrics | Anti-patterns
DCM is a toolkit that helps you identify and fix problems in your Dart and Flutter code. These problems can range from potential runtime bugs and violations of best practices to styling issues. DCM includes over 70 built-in rules to validate your code against various expectations, and you can customize these rules to fit your specific needs.
- Reports code metrics
- Provides additional rules for the dart analyzer
- Checks for anti-patterns
- Checks unused
*.dart
files - Checks unused l10n
- Checks unnecessary nullable parameters
- Can be used as CLI and the analyzer plugin
- 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
Add configuration to analysis_options.yaml
and reload IDE to allow the analyzer to discover the plugin config.
You can read more about the configuration on the website.
analyzer:
plugins:
- dart_code_metrics
dart_code_metrics:
rules:
- avoid-dynamic
- avoid-passing-async-when-sync-expected
- avoid-redundant-async
- avoid-unnecessary-type-assertions
- avoid-unnecessary-type-casts
- avoid-unrelated-type-assertions
- avoid-unused-parameters
- avoid-nested-conditional-expressions
- newline-before-return
- no-boolean-literal-compare
- no-empty-block
- prefer-trailing-comma
- prefer-conditional-expressions
- no-equal-then-else
- prefer-moving-to-variable
- prefer-match-file-name
analyzer:
plugins:
- dart_code_metrics
dart_code_metrics:
metrics:
cyclomatic-complexity: 20
number-of-parameters: 4
maximum-nesting-level: 5
metrics-exclude:
- test/**
rules:
- avoid-dynamic
- avoid-passing-async-when-sync-expected
- avoid-redundant-async
- avoid-unnecessary-type-assertions
- avoid-unnecessary-type-casts
- avoid-unrelated-type-assertions
- avoid-unused-parameters
- avoid-nested-conditional-expressions
- newline-before-return
- no-boolean-literal-compare
- no-empty-block
- prefer-trailing-comma
- prefer-conditional-expressions
- no-equal-then-else
- prefer-moving-to-variable
- prefer-match-file-name
DCM 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 🛠 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-unnecessary-nullable | dart run dart_code_metrics:metrics check-unnecessary-nullable lib | Checks unnecessary nullable parameters in functions, methods, constructors. |
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. |
check-unused-code | dart run dart_code_metrics:metrics check-unused-code lib | Checks unused code 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 DCM 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 unnecessary nullable parameters in functions, methods, constructors. To execute the command, run
$ dart run dart_code_metrics:metrics check-unnecessary-nullable lib
# or for a Flutter package
$ flutter pub run dart_code_metrics:metrics check-unnecessary-nullable lib
It will produce a result in one of the format:
- Console
- 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:
Checks unused code in *.dart
files. To execute the command, run
$ dart run dart_code_metrics:metrics check-unused-code lib
# or for a Flutter package
$ flutter pub run dart_code_metrics:metrics check-unused-code 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!
- What’s new in DCM for Teams 1.3.0
- What’s new in DCM for Teams 1.2.0
- What’s new in DCM for Teams 1.1.0
- Announcing DCM for Teams
- Finding Unused Files With DCM - This article considers one of the first commands, checking unused Dart files, by Dmitry Zhifarsky
- Improving Code Quality With DCM - Advantages of using DCM, by Dmitry Zhifarsky
- Creating a Custom Plugin for Dart Analyzer - How to develop a custom Dart code analyzer plugin, by Dmitry Zhifarsky
- Flutter Static Analysis, DCM - How to install the dart_code-metrics plugin and effectively use it to analyze dart code, by Fred Grott
- Повышаем качество кода с DCM - Преимущества использования DCM, от Dmitry Zhifarsky
- Как создать кастомный плагин для Dart-анализатора - Описан процесс создания плагина для анализатора кода, от Dmitry Zhifarsky
- DCM — мой первый pull request - Инструкция по созданию нового правила, от Vlad Konoshenko
Please feel free to ask any questions about this tool. Join our community chat on Telegram. We speak both English and Russian.