Skip to content

前端代码构建规范相关 #13

@lisaRao

Description

@lisaRao

前端代码构建规范相关

eslint 使用

前公司使用了eslint ,但很多时候并未发展到很好的作用,大多是用来检测简单的语法书写规范问题。(笑)

使用发展

  1. 早期直接简单粗暴的使用了这套,会直接对rule进行调整
    eslint-config-standard

  2. 后面以阿里eslint-config作为底层的配置
    eslint-config-ali,主要用到了base中的 variables.js

关于 .eslintrc 配置大概如下,rules规则件eslint rules , eslint config配置

{
    "extends": "eslint-config-ali",
    "rules": {
        // 自定义规则 可以覆盖 原配置
    }
}
  1. 同时还会添加一个 .eslintignore 对需要eslint的文件进行屏蔽
    // 例如
    build/*.js

editorconfig 使用

项目中通常还会加入 .editorconfig 书写规范配置,配置如下。也可参editorconfig照官网

# editorconfig.org
root = true

[*]
indent_style =space
indent_size = 4
end_of_line = lf
charset = utf-8
trim_trailing_whitespace = true
insert_final_newline = true

[*.md]
trim_trailing_whitespace = false

这个定义时参考了腾讯团队代码规范 做了一些根据业务场景的细小调整例如 样式文件命名使用中划线
sass或less 换行等

end

附上一个简单编写规范配置的eslint

{
    "extends": "eslint-config-ali",
    "parser": "babel-eslint",
    "env": "windows",
    "globals": {
        "GLOBAL": true
    },
    "rules": {
        "arrow-body-style": ["error", "always"],
        "no-const-assign": "error",
        "no-var": "error",
        "no-new-symbol": "error",
        "no-useless-rename": ["error"],
        "prefer-const": "error",
        "rest-spread-spacing": ["error", "always"],
        "lines-around-directive": ["error", { "before": "always", "after": "never" }],
        "array-bracket-spacing": ["error", "never"],
        "capitalized-comments": [
            "error",
            "always",
            {
                "ignorePattern": "pragma|ignored",
                "ignoreInlineComments": true
            }
        ],
        "eol-last": ["error", "always"],
        "line-comment-position": ["error", { "position": "above" }],
        "linebreak-style": ["error", "windows"],
        "multiline-comment-style": ["error", "starred-block"],
        "no-mixed-spaces-and-tabs": "error",
        "no-trailing-spaces": "error",
        "no-multiple-empty-lines": ["error", {"max": 2}]
    }
}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions