代码规范工具

sonar
eslint
htmlhint配置
csslint
csslint配置

eslint配合githook校验

package.json文件

 "scripts": {
     "precommit": "npm run lint",
     "prepush": "npm run lint:push",
     "lint:dev":"eslint $(git diff origin/develop --name-only --diff-filter ACMR |grep '\\.\\(jsx\\|es6\\|js\\)$')",
     "lint:devFix":"eslint --fix $(git diff origin/develop --name-only --diff-filter ACMR |grep '\\.\\(jsx\\|es6\\|js\\)$')",
     "lint:push": "eslint $(git diff-index --cached --name-only --diff-filter ACMR |grep '\\.\\(jsx\\|es6\\|js\\)$')",  
     "lint": "eslint $(git diff-index HEAD --name-only --diff-filter ACMR |grep '\\.\\(jsx\\|es6\\|js\\)$')",
     "lint:fix": "eslint --fix $(git diff-index HEAD --name-only --diff-filter ACMR |grep '\\.\\(jsx\\|es6\\|js\\)$')",
     "lint:devHtml":"eslint $(git diff origin/develop --name-only --diff-filter ACMR |grep '\\.\\(jsx\\|es6\\|js\\)$') -f html > Report.html"
    },
"devDependencies": {
  "babel-eslint": "^7.2.3",
  "eslint": "^3.19.0",
  "eslint-config-airbnb-base": "^11.1.3",
  "eslint-plugin-import": "^2.2.0",
  "eslint-plugin-react": "^6.10.3",
  "husky": "^0.13.3"
  }

命令解释:

    “precommit”:代码本地提交前执行
    “prepush”:代码push之前执行
    “lint:dev”:校验本地分支代码和develop分支需要merge文件
    "lint:devFix":修复本地分支代码和develop分支需要merge文件
    "lint:push": 校验需要push文件
    "lint": 校验需要提交代码
    "lint:fix":修复需要提交文件
    "lint:devHtml":以html格式输出本地分支和develop相关错误

.eslintrc.json

    { 
      "parser": "babel-eslint",
      "env": {
          "browser": true,
          "es6": true,
          "jquery": true
      },
    "globals": {
        "$": false,
        "jQuery": false,  
      },
    "extends": "eslint:recommended",
    "rules": {
        "no-alert": 1,
        "no-bitwise": 1,
        "curly": 1,
        "eqeqeq" : 1,
        "no-eq-null": 1,
        "guard-for-in": 1,
        "no-empty": 1,
        "no-console": 1,
        "no-irregular-whitespace": 1
    },
  "parserOptions": {
        "ecmaVersion": 6, //指定ECMAScript支持的版本,6为ES6
        "sourceType": "module", //指定来源的类型,有两种”script”或”module”
        "ecmaFeatures": {
                "jsx": true//启动JSX
         }
    },
"ignorePattern": "app/components/common/**/*.{js,jsx}"
  }

你可能感兴趣的:(代码规范工具)