dart-run-static-analysis
Compare original and translation side by side
🇺🇸
Original
English🇨🇳
Translation
ChineseAnalyzing and Fixing Dart Code
Dart代码的分析与修复
Contents
目录
Analysis Configuration
分析配置
Configure the Dart analyzer using the file located at the package root.
analysis_options.yaml- Base Configuration: Always include a standard rule set (e.g., or
package:lints/recommended.yaml) using thepackage:flutter_lints/flutter.yamldirective.include: - Strict Type Checks: Enable strict type checks under the node to prevent implicit downcasts and dynamic inferences. Set
analyzer: language:,strict-casts: true, andstrict-inference: true.strict-raw-types: true - Linter Rules: Explicitly enable or disable specific rules under the node. Use a key-value map (
linter: rules:) when overriding included rules, or a list (rule_name: true/false) when defining a fresh set. Do not mix list and map syntax in the same- rule_nameblock.rules - Formatter Configuration: Configure behavior under the
dart formatnode. Setformatter:(default 80) andpage_width(trailing_commasorautomate).preserve - Analyzer Plugins: Enable custom diagnostics by adding plugins under the node. Ensure the plugin package is added as a
analyzer: plugins:indev_dependency.pubspec.yaml
使用位于包根目录下的文件配置Dart分析器。
analysis_options.yaml- 基础配置:始终通过指令引入标准规则集(例如
include:或package:lints/recommended.yaml)。package:flutter_lints/flutter.yaml - 严格类型检查:在节点下启用严格类型检查,防止隐式向下转换和动态推断。设置
analyzer: language:、strict-casts: true和strict-inference: true。strict-raw-types: true - 代码规范规则:在节点下显式启用或禁用特定规则。覆盖已引入规则时使用键值映射(
linter: rules:),定义全新规则集时使用列表(rule_name: true/false)。请勿在同一个- rule_name块中混合使用列表和映射语法。rules - 格式化配置:在节点下配置
formatter:的行为。设置dart format(默认值80)和page_width(可选trailing_commas或automate)。preserve - 分析器插件:在节点下添加插件以启用自定义诊断功能。确保插件包已作为
analyzer: plugins:添加到dev_dependency中。pubspec.yaml
Diagnostic Suppression
诊断抑制
When a diagnostic (lint or warning) yields a false positive or applies to generated code, suppress it explicitly.
- File-level Exclusion: Use the node in
analyzer: exclude:to exclude entire files or directories (e.g.,analysis_options.yaml) using glob patterns.**/*.g.dart - File-level Suppression: Add at the top of a Dart file to suppress specific diagnostics for the entire file. Use
// ignore_for_file: <diagnostic_code>to suppress all linter rules.// ignore_for_file: type=lint - Line-level Suppression: Add on the line directly above the offending code, or appended to the end of the offending line.
// ignore: <diagnostic_code> - Pubspec Suppression: Add above the offending line in
# ignore: <diagnostic_code>files (e.g.,pubspec.yaml).# ignore: sort_pub_dependencies - Plugin Diagnostics: Prefix the diagnostic code with the plugin name when suppressing plugin-specific issues (e.g., ).
// ignore: some_plugin/some_code
当诊断(代码规范检查或警告)出现误报,或针对生成代码时,可显式抑制该诊断。
- 文件级排除:在的
analysis_options.yaml节点中,使用通配符模式排除整个文件或目录(例如analyzer: exclude:)。**/*.g.dart - 文件级抑制:在Dart文件顶部添加,以抑制整个文件中的特定诊断。使用
// ignore_for_file: <diagnostic_code>可抑制所有代码规范规则。// ignore_for_file: type=lint - 行级抑制:在问题代码的上一行添加,或追加到问题代码行的末尾。
// ignore: <diagnostic_code> - Pubspec抑制:在的问题代码行上方添加
pubspec.yaml(例如# ignore: <diagnostic_code>)。# ignore: sort_pub_dependencies - 插件诊断:抑制插件特定问题时,需在诊断代码前添加插件名称前缀(例如)。
// ignore: some_plugin/some_code
Workflow: Executing Static Analysis
工作流:执行静态分析
Use this workflow to identify type-related bugs, style violations, and potential runtime errors.
Task Progress:
- 1. Verify exists at the project root.
analysis_options.yaml - 2. Run the analyzer using the MCP tool (if available) or the CLI command
analyze_files.dart analyze <target_directory> - 3. Review the diagnostic output.
- 4. If info-level issues must be treated as failures, append the flag.
--fatal-infos - 5. Resolve reported errors manually or proceed to the Automated Fixes workflow.
使用此工作流识别类型相关bug、风格违规和潜在运行时错误。
任务进度:
- 1. 验证项目根目录下存在文件。
analysis_options.yaml - 2. 使用MCP工具(若可用)或CLI命令
analyze_files运行分析器。dart analyze <target_directory> - 3. 查看诊断输出结果。
- 4. 若需将信息级问题视为失败,追加参数。
--fatal-infos - 5. 手动解决报告的错误,或进入自动修复工作流。
Workflow: Applying Automated Fixes
工作流:应用自动修复
Use this workflow to resolve outdated API usages, apply quick fixes, and migrate code (e.g., Dart 3 migrations).
Task Progress:
- 1. Execute a dry run to preview proposed changes using the MCP tool or CLI command
dart_fix.dart fix --dry-run - 2. Review the proposed fixes to ensure they align with the intended architecture.
- 3. If additional fixes are required, verify that the corresponding linter rules are enabled in .
analysis_options.yaml - 4. Apply the fixes using the MCP tool or CLI command
dart_fix.dart fix --apply - 5. Format the modified code using the MCP tool or CLI command
dart_format.dart format . - 6. Run the static analysis workflow to verify all diagnostics are resolved.
使用此工作流解决过时API用法、应用快速修复和迁移代码(例如Dart 3迁移)。
任务进度:
- 1. 使用MCP工具或CLI命令
dart_fix执行试运行,预览拟议的变更。dart fix --dry-run - 2. 查看拟议的修复,确保其符合预期架构。
- 3. 若需要额外修复,验证对应的代码规范规则已在中启用。
analysis_options.yaml - 4. 使用MCP工具或CLI命令
dart_fix应用修复。dart fix --apply - 5. 使用MCP工具或CLI命令
dart_format格式化修改后的代码。dart format . - 6. 运行静态分析工作流,验证所有诊断已解决。
Examples
示例
Comprehensive analysis_options.yaml
analysis_options.yaml完整的analysis_options.yaml
analysis_options.yamlyaml
include: package:flutter_lints/recommended.yaml
analyzer:
exclude:
- "**/*.g.dart"
- "lib/generated/**"
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
errors:
todo: ignore
invalid_assignment: warning
missing_return: error
linter:
rules:
avoid_shadowing_type_parameters: false
await_only_futures: true
use_super_parameters: true
formatter:
page_width: 100
trailing_commas: preserveyaml
include: package:flutter_lints/recommended.yaml
analyzer:
exclude:
- "**/*.g.dart"
- "lib/generated/**"
language:
strict-casts: true
strict-inference: true
strict-raw-types: true
errors:
todo: ignore
invalid_assignment: warning
missing_return: error
linter:
rules:
avoid_shadowing_type_parameters: false
await_only_futures: true
use_super_parameters: true
formatter:
page_width: 100
trailing_commas: preserveInline Diagnostic Suppression
内联诊断抑制
dart
// Suppress for the entire file
// ignore_for_file: unused_local_variable, dead_code
void processData() {
// Suppress for a specific line
// ignore: invalid_assignment
int x = '';
const y = 10; // ignore: constant_identifier_names
}dart
// 抑制整个文件的诊断
// ignore_for_file: unused_local_variable, dead_code
void processData() {
// 抑制特定行的诊断
// ignore: invalid_assignment
int x = '';
const y = 10; // ignore: constant_identifier_names
}