世界上最伟大的投资就是投资自己的教育

全场限时 5 折

首页JavaScript
随风 · 练气

团队统一代码分格 editorconfig 工具研究

随风发布于2731 次阅读

资源

官网: https://editorconfig.org/

vscode 插件:https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig

vim 插件:https://github.com/editorconfig/editorconfig-vim#readme

.editorconfig

root = true

[*]
charset = utf-8
end_of_line = lf
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
insert_final_newline = false
trim_trailing_whitespace = false

[*.{js,jsx,json,ts,tsx,yml}]
indent_size = 2
indent_style = space

使用原理与方法

在根目录下配置 .editorconfig 文件,语法格式和意义相信比较简单,按照字面就能理解。

比如:

  • charset 规定字符集

  • end_of_line set to lf, cr, or crlf to control how line breaks are represented.

  • trim_trailing_whitespace 删除多余的空格

  • insert_final_newline set to true to ensure file ends with a newline when saving and false to ensure it doesn't.

  • indent_size 缩进大小

  • indent_style 用空格缩进还是用 tab 缩进

配置文件都差不多,可能不同的项目有略微的差异,可以查看官方的文档来调整,或者找一些现成的,适合自己的团队的配置文件。

接下来要安装插件,配合插件来使用,才能发挥它的效果。

比如 vscode 的插件:https://marketplace.visualstudio.com/items?itemName=EditorConfig.EditorConfig

image

你一保存就会按照两个空格来缩进。

在 vscode 中你可以要有这样的用户配置:

image

不同的编辑器配置方法不太一样,要按照情况进行配置

trim_trailing_whitespace

这个可以移除无用的空格。在团队代码规范中,是有作用的。

trim_trailing_whitespace = true

我们可以通过相关的插件来做到:

image

或者在设置里:

image

只要能实现和保持统一的规范就行。

本站文章均为原创内容,如需转载请注明出处,谢谢。

2 条回复
  • SALTED-FISH · 道祖
    SALTED-FISH · 道祖 #1

    分享一下自己在 WebStorm 下的配置

    root = true
    
    [*]
    tab_width = 2
    indent_size = 2
    charset = utf-8
    end_of_line = crlf
    indent_style = space
    max_line_length = 1024
    insert_final_newline = true
    trim_trailing_whitespace = true
    
    [*.{ts, tsx}]
    ij_typescript_force_quote_style = true
    ij_typescript_use_double_quotes = false
    ij_typescript_import_sort_members = true
    ij_typescript_spaces_within_imports = true
    ij_typescript_spaces_within_brackets = true
    ij_typescript_import_sort_module_name = false
    ij_typescript_spaces_within_object_literal_braces = true
    ij_typescript_spaces_within_interpolation_expressions = true
    ij_typescript_spaces_within_array_initializer_brackets = true
    
    [*.{js, jsx}]
    ij_javascript_force_quote_style = true
    ij_javascript_use_double_quotes = false
    ij_javascript_import_sort_members = true
    ij_javascript_spaces_within_imports = true
    ij_javascript_spaces_within_brackets = true
    ij_javascript_import_sort_module_name = false
    ij_javascript_spaces_within_object_literal_braces = true
    ij_javascript_spaces_within_interpolation_expressions = true
    ij_javascript_spaces_within_array_initializer_brackets = true
    
    [*.md]
    max_line_length = off
    trim_trailing_whitespace = false
    

    随风 · 练气 打赏此回复 1 积分

  • 随风 · 练气
    随风 · 练气 #2

    厉害的,大哥

喜欢
统计信息
    学员: 29193
    视频数量: 1985
    文章数量: 489

© 汕尾市求知科技有限公司 | Rails365 Gitlab | Qiuzhi99 Gitlab | 知乎 | b 站 | 搜索

粤公网安备 44152102000088号粤公网安备 44152102000088号 | 粤ICP备19038915号

Top