使用 EditorConfig 定义一致的编码样式
EditorConfig 文件中的设置用于在基本代码库中维持一致的编码风格和设置,例如缩进样式、选项卡宽度、行尾字符以及编码等,而无需考虑使用的编辑器或 IDE。
在项目添加 .editorConfig
文件,强制对使用基本代码的所有人实施一致的编码样式。 EditorConfig 文件设置遵循 EditorConfig.org 维护的文件格式规范。许多代码编辑器和应用程序都支持 EditorConfig 文件,包括 Visual Studio。 由于设置位于文件中,因此它们附带代码,并且即使在 Visual Studio 外部也可使用。
安装VS-Code插件
配置 EditorConfig(.editorconfig)
js
# @see: http://editorconfig.org
root = true
[*] # 表示所有文件适用
charset = utf-8 # 设置文件字符集为 utf-8
end_of_line = crlf # 控制换行类型(lf | cr | crlf)
insert_final_newline = true # 始终在文件末尾插入一个新行
indent_style = space # 缩进风格(tab | space)
indent_size = 2 # 缩进大小
max_line_length = 130 # 最大行长度
[*.md] # 表示仅对 md 文件适用以下规则
max_line_length = off # 关闭最大行长度限制
trim_trailing_whitespace = false # 关闭末尾空格修剪
EditorConfig
和Prettier
的功能概念大致相似,你可以这么理解,Prettier
是用来格式化代码,EditorConfig
是用来保持不同编辑器的编辑风格。
EditorConfig
的配置项若与Prettier
相同则两边的配置需要保持一,例如:
js
// prettier配置
module.exports = {
// 缩进制表符宽度 | 空格数
tabWidth: 2,
};
bash
# .editorconfig文件
# 省略其它...
indent_style = space # 缩进风格(tab | space)
indent_size = 2 # 缩进大小