78 lines
4.4 KiB
YAML
78 lines
4.4 KiB
YAML
# 1. 继承 Lint 规则集 (必选其一)
|
||
# --------------------------------------------------------------------------
|
||
# 强烈推荐!根据你的项目类型选择一个 Lint 规则集作为起点。
|
||
# 这能大大减少手动配置的工作量,并与社区最佳实践保持一致。
|
||
# Linter 规则
|
||
|
||
# https://dart.ac.cn/tools/linter-rules
|
||
|
||
# 如果是 Flutter 项目,推荐使用:
|
||
include: package:flutter_lints/flutter.yaml
|
||
|
||
# 如果是纯 Dart 项目,推荐使用:
|
||
# include: package:lints/recommended.yaml
|
||
|
||
# 2. 配置分析器 (必选)
|
||
# --------------------------------------------------------------------------
|
||
# 分析器用于检查代码的语法和潜在问题。
|
||
# 强烈建议启用所有规则,以确保代码质量和一致性。
|
||
|
||
analyzer:
|
||
errors:
|
||
require_trailing_commas: ignore
|
||
# 排除不想被分析的文件或目录。
|
||
# 对于由代码生成工具(如 json_serializable, freezed)生成的 *.g.dart 文件,
|
||
# 强烈建议排除,因为你通常不需要对它们进行 Lint 检查。
|
||
exclude:
|
||
- '**/*.g.dart' # 排除所有以 .g.dart 结尾的文件
|
||
- 'lib/generated/**' # 排除 lib/generated/ 目录下的所有文件 (如果你的生成文件都在这里)
|
||
- 'build/**' # 排除 Flutter/Dart 构建输出目录
|
||
|
||
|
||
# 3. 配置 Lint 规则
|
||
# --------------------------------------------------------------------------
|
||
linter:
|
||
# 在此处启用或禁用特定的 Lint 规则。
|
||
# `include` 中的规则集已经包含了大部分常用规则,这里可以进行微调。
|
||
rules:
|
||
# 常用且推荐启用的规则 (即使默认集没有包含,也建议手动添加)
|
||
- avoid_empty_else # 避免空的 else 块
|
||
# - avoid_print # 在生产代码中避免使用 print (可根据项目需求启用/禁用)
|
||
- avoid_relative_lib_imports # 避免从 'lib/' 相对导入
|
||
- directives_ordering # 强制 import/export 指令排序
|
||
# - avoid_return_and_type_annotation # 避免冗余的返回类型注解
|
||
- curly_braces_in_flow_control_structures # 控制流语句强制使用大括号
|
||
- empty_catches # 避免空的 catch 块
|
||
- empty_constructor_bodies # 避免空的构造函数体
|
||
- empty_statements # 避免空的语句
|
||
- file_names # 文件名使用小写下划线命名 (my_file.dart)
|
||
# - prefer_const_constructors # 尽可能使用 const 构造函数
|
||
# - prefer_const_declarations # 尽可能使用 const 声明
|
||
# - prefer_const_literals_to_create_immutables # 尽可能使用 const 创建不可变集合
|
||
# - prefer_single_quotes # 优先使用单引号 (或 prefer_double_quotes)
|
||
- prefer_final_fields # 类中的私有字段尽可能使用 final
|
||
- prefer_final_locals # 局部变量尽可能使用 final
|
||
# - prefer_for_elements_to_map_fromIterable # 优先使用 for 元素创建 Map
|
||
# - prefer_is_empty # 优先使用 .isEmpty
|
||
# - prefer_is_not_empty # 优先使用 .isNotEmpty
|
||
- unnecessary_new # Dart 2.0 后 new 关键字是可选的,推荐省略
|
||
- unnecessary_this # 避免不必要的 this 关键字
|
||
# - use_key_in_widget_constructors # Flutter Widget 构造函数中推荐使用 Key
|
||
|
||
# 根据项目特性考虑启用的规则 (可能需要团队讨论)
|
||
# - annotate_overrides # 推荐:覆写方法添加 @override 注解 (如果 flutter_lints 已包含则无需重复)
|
||
# - lines_longer_than_80_chars # 强制行长 80 字符 (默认是警告,但通常较严格)
|
||
# - public_member_api_docs # 推荐:为公共 API 编写文档注释 (对库项目非常重要,应用项目可酌情)
|
||
- require_trailing_commas # 强制多行参数列表使用尾随逗号 (有助于格式化)
|
||
# - sort_constructors_first # 构造函数在类中声明在前
|
||
# - sort_declarations_as_members # 类成员按字母顺序排序
|
||
# - sort_pub_dependencies # pubspec.yaml 依赖按字母排序
|
||
|
||
# 4. 格式化器配置
|
||
# --------------------------------------------------------------------------
|
||
formatter:
|
||
# 设置 `dart format` 工具的行宽。
|
||
# Dart 官方推荐 80,但许多团队会使用 100 或 120 以适应现代宽屏显示器。
|
||
# 最重要的是整个团队**保持一致**。
|
||
page_width: 80
|