Configuration
DCM is designed to be flexible and configurable for your use case. You can turn off every rule and run only with enabled metrics calculation or have both rules and metrics enabled.
To configure DCM, add a dart_code_metrics
entry to analysis_options.yaml
:
dart_code_metrics:
exclude-public-api: # configures the "exclude-public-api" mode for the commands that use it
extends:
- ... # configures the list of preset configurations
exclude:
rules:
- ... # configures the list of files that should be ignored by rules
metrics:
- ... # configures the list of files that should be ignored by metrics
unused-code:
- ... # configures the list of files that should be ignored by unused code checks
unused-files:
- ... # configures the list of files that should be ignored by unused files checks
parameters:
- ... # configures the list of files that should be ignored by parameters checks
formatter:
- ... # configures the formatter
assists:
- ... # configures the list of assists
metrics:
- ... # configures the list of reported metrics
pubspec-rules:
- ... # configures the list of rules for pubspec.yaml
rules:
- ... # configures the list of rules
This configuration is used by both CLI and the analysis server.
Basic config example:
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
- avoid-collapsible-if
- avoid-redundant-else
- avoid-incomplete-copy-with
- avoid-self-compare
- avoid-self-assignment
- avoid-unnecessary-nullable-return-type
- avoid-unrelated-type-casts
- prefer-declaring-const-constructor
You can find a list of recommended rules here.
Basic config with metrics:
dart_code_metrics:
metrics:
cyclomatic-complexity: 20
number-of-parameters: 4
maximum-nesting-level: 5
exclude:
metrics:
- 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
- avoid-collapsible-if
- avoid-redundant-else
- avoid-incomplete-copy-with
- avoid-self-compare
- avoid-self-assignment
- avoid-unnecessary-nullable-return-type
- avoid-unrelated-type-casts
- prefer-declaring-const-constructor
You can find a list of recommended metrics here.
Custom analysis_option Files
Aside from the regular analysis_option.yaml
file, you can add a custom config file that matches one of the given patterns
- starts with
analysis_options
(e.g. analysis_options.1.1.0.yaml) - starts with
dcm
(e.g. dcm_config.yaml or dcm.yaml) - placed inside a folder called
dcm
(e.g. dcm/config.yaml)
If such a file is referenced by any analysis_option.yaml
file, DCM will correctly update configuration changes, validate rule names and display a configuration icon (⚙️) for configurable rules.
However, unlike for regular analysis_options.yaml
files, code actions are not available.
IDE Autocompletion (VS Code only)
To enable autocompletion for DCM configuration, modify the settings.json
to the following:
{
"[yaml]": {
"editor.quickSuggestions": {
"strings": "on"
}
}
}
Excluding Public API
For commands that can affect the package's public API ("Check Unused Code", "Check Unused Files" and "Check Parameters"), setting the exclude-public-api
entry in the analysis_options.yaml
file will override the --exclude-public-api
CLI option.
For example, if publicly exported files of a particular package are not expected to be changed, setting the exclude-public-api
entry to true
and running the command will exclude the public API only for that particular package.
dart_code_metrics:
exclude-public-api: true # excludes public API for a particular package