《如何做好 Code Review》中我论述过代码审查对于保证代码品质的重要性,最近开始采用Git Hooks的方式为Flutter项目增加提交前的强制自我审查。这样做的好处在于将机械化检查交给电脑,把思考的部分留给大脑。

我认为代码提交前需要做的最基本检查包括格式和代码规范。前者交给Prettier,后者由Analyze负责。

Lefthook

我采用Lefthook实现Git钩子,如果你有更好的选择,欢迎推荐分享。

# Mac
brew install lefthook && lefthook install

安装Lefthook后,在项目根目录下运行lefthook install命令来生成lefthook.yml文件,并在其中的pre-commit > commands节点下添加两项配置:

prettier:
glob: "*.dart"
run: flutter format {staged_files} && git add {staged_files}
linter:
run: flutter analyze

Analyze

flutter analyze通过flutter_lints插件对Dart代码进行静态语言检查,检查规则配置在analysis_options.yaml文件中。但是打开该文件可以看到,Flutter 并没有为我们配置任何默认规则。我把我正在采用的规则配置放在文末,欢迎酌情采纳。

接下来我们可以测试一下配置能否正常工作。比如,将main()替换为:

void main() {
const title= 'Flutter';
runApp(const MyApp());} aa() {}

然后运行命令:

git add .&&git commit -m 'Commit for analysis.'

接下来你会发现,Prettier 将代码格式化为:

void main() {
const title = 'Flutter';
runApp(const MyApp());
} aa() {}

但是静态检查没有通过,因为title变量没有被使用、函数aa没有返回类型等:

EXECUTE > linter
Analyzing demo_lint_flutter... info • The value of the local variable 'title' isn't used • lib/main.dart:4:9 • unused_local_variable
info • The function 'aa' should have a return type but doesn't • lib/main.dart:8:1 • always_declare_return_types
info • Type annotate public APIs • lib/main.dart:8:1 • type_annotate_public_apis 3 issues found. (ran in 2.0s)

强烈建议你在接下来的每一个Flutter项目里都坚持这样做,不要提交Smelling Code。

analysis_options.yaml

include: package:flutter_lints/flutter.yaml

analyzer:
strong-mode:
implicit-casts: false
errors:
missing_required_param: warning
missing_return: warning
todo: ignore linter:
rules:
always_declare_return_types: true
always_require_non_null_named_parameters: true
annotate_overrides: true
avoid_bool_literals_in_conditional_expressions: true
avoid_catching_errors: true
avoid_classes_with_only_static_members: true
avoid_empty_else: true
avoid_escaping_inner_quotes: true
avoid_field_initializers_in_const_classes: true
avoid_function_literals_in_foreach_calls: true
avoid_implementing_value_types: true
avoid_init_to_null: true
avoid_multiple_declarations_per_line: true
avoid_null_checks_in_equality_operators: true
avoid_positional_boolean_parameters: true
avoid_print: true
avoid_private_typedef_functions: true
avoid_redundant_argument_values: true
avoid_relative_lib_imports: true
avoid_return_types_on_setters: true
avoid_returning_null_for_future: true
avoid_returning_null_for_void: true
avoid_setters_without_getters: true
avoid_shadowing_type_parameters: true
avoid_single_cascade_in_expression_statements: true
avoid_type_to_string: true
avoid_types_as_parameter_names: true
avoid_unnecessary_containers: true
avoid_unused_constructor_parameters: true
avoid_void_async: true
avoid_web_libraries_in_flutter: true
await_only_futures: true
camel_case_extensions: true
camel_case_types: true
cancel_subscriptions: true
cast_nullable_to_non_nullable: true
constant_identifier_names: true
control_flow_in_finally: true
curly_braces_in_flow_control_structures: true
depend_on_referenced_packages: true
deprecated_consistency: true
empty_catches: true
empty_constructor_bodies: true
empty_statements: true
eol_at_end_of_file: true
exhaustive_cases: true
file_names: true
hash_and_equals: true
implementation_imports: true
invariant_booleans: true
iterable_contains_unrelated_type: true
join_return_with_assignment: true
leading_newlines_in_multiline_strings: true
library_names: true
library_prefixes: true
list_remove_unrelated_type: true
missing_whitespace_between_adjacent_strings: true
no_adjacent_strings_in_list: true
no_duplicate_case_values: true
no_logic_in_create_state: true
no_runtimeType_toString: true
non_constant_identifier_names: true
noop_primitive_operations: true
null_check_on_nullable_type_parameter: true
null_closures: true
overridden_fields: true
package_names: true
package_prefixed_library_names: true
parameter_assignments: true
prefer_asserts_in_initializer_lists: true
prefer_collection_literals: true
prefer_conditional_assignment: true
prefer_const_constructors: true
prefer_const_constructors_in_immutables: true
prefer_const_declarations: true
prefer_const_literals_to_create_immutables: true
prefer_constructors_over_static_methods: true
prefer_contains: true
prefer_equal_for_default_values: true
prefer_final_fields: true
prefer_final_in_for_each: true
prefer_final_locals: true
prefer_for_elements_to_map_fromIterable: true
prefer_function_declarations_over_variables: true
prefer_generic_function_type_aliases: true
prefer_if_elements_to_conditional_expressions: true
prefer_if_null_operators: true
prefer_initializing_formals: true
prefer_inlined_adds: true
prefer_interpolation_to_compose_strings: true
prefer_is_empty: true
prefer_is_not_empty: true
prefer_is_not_operator: true
prefer_iterable_whereType: true
prefer_null_aware_method_calls: true
prefer_spread_collections: true
prefer_typing_uninitialized_variables: true
prefer_void_to_null: true
provide_deprecation_message: true
recursive_getters: true
require_trailing_commas: true
sized_box_for_whitespace: true
slash_for_doc_comments: true
sort_child_properties_last: true
sort_unnamed_constructors_first: true
test_types_in_equals: true
throw_in_finally: true
tighten_type_of_initializing_formals: true
type_annotate_public_apis: true
type_init_formals: true
unnecessary_await_in_return: true
unnecessary_brace_in_string_interps: true
unnecessary_const: true
unnecessary_getters_setters: true
unnecessary_new: true
unnecessary_null_aware_assignments: true
unnecessary_null_checks: true
unnecessary_null_in_if_null_operators: true
unnecessary_nullable_for_final_variable_declarations: true
unnecessary_overrides: true
unnecessary_parenthesis: true
unnecessary_raw_strings: true
unnecessary_statements: true
unnecessary_string_escapes: true
unnecessary_string_interpolations: true
unnecessary_this: true
unrelated_type_equality_checks: true
unsafe_html: true
use_build_context_synchronously: true
use_full_hex_values_for_flutter_colors: true
use_function_type_syntax_for_parameters: true
use_named_constants: true
use_late_for_private_fields_and_variables: true
use_rethrow_when_possible: true
use_setters_to_change_properties: true
use_string_buffers: true
use_test_throws_matchers: true
valid_regexps: true
void_checks: true

Flutter的强制自我审查的更多相关文章

  1. 软件开发过程中的审查 (Review)

    http://blog.csdn.net/horkychen/article/details/5035769 软件开发过程中的审查 (Review)   希望别人做些什么->定义出流程 希望别人 ...

  2. Flutter的原理及美团的实践

    导读 Flutter是Google开发的一套全新的跨平台.开源UI框架,支持iOS.Android系统开发,并且是未来新操作系统Fuchsia的默认开发套件.自从2017年5月发布第一个版本以来,目前 ...

  3. flutter兼论

    Flutter是Google开发的一套全新的跨平台.开源UI框架,支持iOS.Android系统开发,并且是未来新操作系统Fuchsia的默认开发套件.自从2017年5月发布 第一个版本以来,目前Fl ...

  4. Flutter原理简介

    Flutter 是怎么运转的? 与用于构建移动应用程序的其他大多数框架不同,Flutter 是重写了一整套包括底层渲染逻辑和上层开发语言的完整解决方案.这样不仅可以保证视图渲染在 Android 和 ...

  5. 斯考特·杨(Scott Young)快速学习方法

    上午在网上看到了斯考特·杨(Scott Young)的快速学习方法,感觉很受鼓舞. 现在已经读研究生了,可是发现自己自从上大学以来到现在,发现自己的学习方法有很大的问题. 我是个特别喜欢读书的人,在大 ...

  6. App Store审核被拒的23个理由

    原文地址 iOS 应用提交审核要持续一周或者更久,在提交之前,我们一定要进行「自我审查」,避免被拒.ASO100 为大家收集整理了2015年 App Store 审核被拒的23个理由,并且附上官方拒绝 ...

  7. Telegram传奇:俄罗斯富豪、黑客高手、极权和阴谋…

    说了很久要写Telegram的故事,一直拖延没有写.在我拖延的这段时间里面,Telegarm继续快速增长,前几天,在旧金山的TechCrunch Disrupt活动上,创始人Durov说现在Teleg ...

  8. ibatis+spring+cxf+mysql搭建webservice

    首先需必备:mysql.myeclipse6.5.apache-cxf-2.6.2 一.建数据库,库名:cxf_demo:表名:book CREATE DATABASE `cxf_demo`  --数 ...

  9. OpenGLES 怎样在十天内掌握线性代数 - 希望这是真的!

    OpenGLES 怎样在十天内掌握线性代数 - 希望这是真的! 太阳火神的漂亮人生 (http://blog.csdn.net/opengl_es) 本文遵循"署名-非商业用途-保持一致&q ...

随机推荐

  1. 尚硅谷全套课件整理:Java、前端、大数据、安卓、面试题

    目录 Java 尚硅谷 IT 精英计划 JavaSE 内部学习笔记.pdf 尚硅谷 Java 基础实战之银行项目.pdf 尚硅谷 Java 技术之 JDBC.pdf 尚硅谷 Java 技术之 Java ...

  2. [NOI Online #1 提高组]

    A 首先从 \(t = 2\) 的特殊部分分出发. 不难发现这个操作是很不直观的,于是可以考虑对于每个操作 \((u, v)\) 在 \(u, v\) 之间连一条无向边. 显然连通块之间要分开考虑,对 ...

  3. CF1399F Yet Another Segments Subset

    首先注意一下题面要求,使得选出的线段两两要么包含要么不相交,也就是说一条线段可能会出现不相交的几条线段,而这些线段上面也可能继续这样包含线段.然后我们可以发现我们要做的实际上是在这条线段上选取几条线段 ...

  4. CentOS 7下iptables配置添加修改规则端口方法(转)

    简介: Linux CentOS 7默认没有安装iptables,默认的防火墙是firewalld,云吞铺子分享CentOS 7系统下iptables安装.iptables规则配置(放行或者禁用端口) ...

  5. 学习jsp篇:jsp简单实例之二登录

    编程环境:IDEA,Tomcat,JavaEE 一.实例二登录 1.在自己建的工程下的web目录下建一个文件夹为login,在login中编写登录代码(其实就是和实例一同一个项目) 2.先建登录页面j ...

  6. linux_20

    总结tomcat优化方法 java程序出现oom如何解决?什么场景下会出现oom? 简述redis特点及其应用场景 对比redis的RDB.AOF模式的优缺点 实现redis哨兵,模拟master故障 ...

  7. 在win10上安装face_recognition(人脸识别)

    github上有个项目face_recognition,是用于人脸识别的 主要是window上安装这个项目会繁琐些,linux上据项目文档上介绍是妥妥的. 项目地址:  https://github. ...

  8. MXNet学习-第一个例子:训练MNIST数据集

    一个门外汉写的MXNET跑MNIST的例子,三层全连接层最后验证率是97%左右,毕竟是第一个例子,主要就是用来理解MXNet怎么使用. #导入需要的模块 import numpy as np #num ...

  9. suse 12 安装git客户端

    suse-linux:~ # zypper addrepo http://download.opensuse.org/repositories/devel:/tools:/scm/SLE_12_SP5 ...

  10. Aluminum: An Asynchronous, GPU-Aware Communication Library Optimized for Large-Scale Training of Deep Neural Networks on HPC Systems

    本文发表在MLHPC 2018上,主要介绍了一个名为Aluminum通信库,这个库针对Allreduce做了一些关于计算通信重叠以及针对延迟的优化,以加速分布式深度学习训练过程. 分布式训练的通信需求 ...