Dart Code Metrics
Dart Code Metrics 是一个帮助您分析和提高代码质量的静态分析工具。
快速开始
分析器插件
Dart analyzer package 的插件,提供 Dart Code Metrics 的附加规则。规则或反模式产生的所有问题都将在 IDE 中高亮显示。
-
将软件包作为开发依赖安装
$ dart pub add --dev dart_code_metrics # or for a Flutter package $ flutter pub add --dev dart_code_metrics或者
手动添加到
pubspec.yamldev_dependencies: dart_code_metrics: ^4.2.0-dev.1然后运行
$ dart pub get # or for a Flutter package $ flutter pub get -
将配置添加到
analysis_options.yamlanalyzer: 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 -
重新加载 IDE 以允许分析器发现插件
CLI
该软件包可用作命令行工具。
它将以一种支持的格式产生结果
- 控制台
- GitHub
- Codeclimate
- HTML
- JSON
用法
按 分析器插件用法示例 中列出的方式安装软件包。
如果您希望命令行工具检查规则,则需要首先在 analysis_options.yaml 中 配置 rules 条目。
dart pub run dart_code_metrics:metrics lib
# or for a Flutter package
flutter pub run dart_code_metrics:metrics lib
多包存储库用法
如果您使用 Melos,可以将自定义命令添加到 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.
选项
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
--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
--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]