Check Unused Files
Reports unused *.dart
files.
To execute the command, run:
$ dcm check-unused-files lib # or dcm uf lib
Full command description:
Usage: dcm check-unused-files [arguments] <directories>
-h, --help Print this usage information.
-r, --reporter="console" (--output-format) Analysis output format.
[console (default), json, codeclimate, gitlab, checkstyle, sonar]
-a, --absolute-path Show absolute paths in console reporter output.
--output-to="path/to/file" Path to the file with the analysis output.
-c, --print-config Print resolved config.
--root-folder="./" Root folder.
(defaults to the current directory)
-s, --sdk-path="directory-path" Dart SDK directory path.
If the project has a `.fvm/flutter_sdk` symlink, it will be used if the SDK is not found.
-e, --exclude="{**/*.g.dart,**/*.freezed.dart}" Files to exclude (in Glob syntax).
(defaults to "{**/*.g.dart,**/*.freezed.dart}")
--root-exclude Files to exclude, relative to the root folder (in Glob syntax).
--[no-]congratulate Show output even when there are no issues.
--verbose Show verbose logs.
--ci-key The license key to run on CI server. Can be provided via DCM_CI_KEY env variable.
--email The email used to purchase the license. Can be provided via DCM_EMAIL env variable.
--no-analytics Disable sending anonymous usage statistics.
--exclude-public-api (--ep) Exclude publicly exported files from the analysis output.
--[no-]fatal-unused Treat unused files as fatal.
(defaults to on)
Suppressing the Command​
To suppress the command for an entire file, add the ignore_for_file: unused-files
comment to the beginning of a file.
You can also set the exclude:unused-files:
config entry in the analysis_options.yaml
(this configuration is used by both CLI and the IDE integration).
Cyclic Usage Detection​
If you have several files that reference each other, but are not referenced by other files and technically are unused, the command will only detect 1 level of cyclic usage.
Excluding Public API​
By default, this command analyzes all files, even if they are part a package's public API.
To exclude such files from the analysis, pass the exclude-public-api
CLI flag.
This command follows this convention when locating API and implementation files.
Overriding "exclude-public-api" for a Particular Package​
To override the exclude-public-api
flag for a particular package, set the exclude-public-api
entry in the analysis_options.yaml
.
Output Example​
Console (default)​
Use --reporter=console
to get output in console format.
JSON​
Use --reporter=json
to get output as a single JSON object containing metadata and the list of unused files.
Format specification
The root object fields are​
formatVersion
- an integer representing the format version (will be incremented each time the serialization format changes)timestamp
- a creation time of the report in YYYY-MM-DD HH:MM:SS formatunusedFilesResults
- an array of objectssummary
- an array of objects
{
"formatVersion": 10,
"timestamp": "2021-04-11 14:44:42",
"unusedFilesResults": [
{
...
},
{
...
},
{
...
}
],
"summary": [
{
...
},
{
...
}
]
}
The result object fields are​
path
- the relative path to the file with issuesissues
- an array of issues detected in the target file
{
"path": "lib/src/some/file.dart",
"issues": [
...
]
}
The issue object fields are​
id
- issue idmessage
- the message associated with the issueeffortInMinutes
- an estimated effort to fix the issue (in minutes)usedOnlyInTests
- a boolean value indicating if the file is only used by other test files
{
"id": "unused-file-issue",
"message": "Unused file",
"effortInMinutes": 5,
"usedOnlyInTests": false
}
The summary-record object fields are​
title
- a message with the summary entry titlevalue
- the actual value of the entry
{
"title": "Total unused files",
"value": 1
}
Old format specification (prior to DCM 1.26.0)
The root object fields are​
formatVersion
- an integer representing the format version (will be incremented each time the serialization format changes)timestamp
- a creation time of the report in YYYY-MM-DD HH:MM:SS formatunusedFiles
- an array of unused files
{
"formatVersion": 2,
"timestamp": "2021-04-11 14:44:42",
"unusedFiles": [
{
...
},
{
...
},
{
...
}
]
}
The unusedFiles object fields are​
path
- a relative path of the unused fileusedOnlyInTests
a boolean value indicating the file is only used by other test files
{
"path": "lib/src/some/file.dart",
"usedOnlyInTests": false
}
GitLab​
Use --reporter=gitlab
to get output in a GitLab-compatible format. To learn how to integrate DCM with GitLab, refer to this guide.
Code Climate​
Use --reporter=codeclimate
to get output in Code Climate
format.
Output example
{"type":"issue","check_name":"unused-file-issue","description":"Unused file","categories":["Bug Risk"],"location":{"path":"lib/src/unused_widget.dart","positions":{"begin":{"column":1,"line":1},"end":{"column":1,"line":1}}},"severity":"major","fingerprint":"527a5c75d3a9cd48e159621891ac24e7"}
Checkstyle​
Use --reporter=checkstyle
to get output in Checkstyle
format.
Output example
<?xml version="1.0"?>
<checkstyle version="10.0">
<file name="example.dart">
<error line="0" message="Unused file" severity="warning" source="unused-file-issue"/>
</file>
</checkstyle>
Checkstyle
format is supported by Bitbucket. To learn how to integrate DCM with Bitbucket, refer to this guide.
Sonar​
Use --reporter=sonar
to get output in SonarQube's generic format for external issues.
Output example
{
"rules": [
{
"cleanCodeAttribute": "CLEAR",
"description": "To learn more, visit the documentation https://dcm.dev/docs/cli/code-quality-checks/unused-files/",
"engineId": "dcm",
"id": "unused-file-issue",
"impacts": [
{
"severity": "MEDIUM",
"softwareQuality": "MAINTAINABILITY"
}
],
"name": "unused-file-issue"
}
],
"issues": [
{
"effortMinutes": 5,
"primaryLocation": {
"filePath": "lib/src/unused_widget.dart",
"message": "Unused file"
},
"ruleId": "unused-file-issue"
}
]
}