一、安裝
npm install -g eslint 安裝eslint
編輯器安裝插件eslint(具體安裝方法根據不同編輯器而不同)
二、使用
使用方法一:
eslint --init npm中用命令新建eslintrc.js文件
eslint yourfile.js npm中用命令檢查自己文件中的錯誤
使用方法二:
手動在項目的根目錄下新建eslintrc.*文件(.js、.json、.yaml、.yml等),進行配置(具體配置規則詳見下文),即可在安裝好eslint的編輯器中查看到出現錯誤的位置。
三、配置:http://eslint.org/docs/rules/ (以下規則文件配置一個即可,置於項目根目錄下)
(1).eslintrc文件中進行配置 http://eslint.org/docs/user-guide/configuring
{
"rules": {
"semi": ["error", "always"],
"quotes": ["error", "double"]
}
}
四、其他
1.在樣式之前標注“/* eslint-disable */”,可忽略配置的規則;標注“/* esint-enable */“,開啟配置的規則
2.“/* eslint-enable */“必須在“/* eslint-disable */”之后使用
3.忽略指定的內容:新建一個.eslineignore文件,例如:
# /node_modules/* and /bower_components/* ignored by default
# Ignore built files except build/index.js
build/*
!build/index.js
通過命令 eslint --ignore-path .eslintignore file.js可檢測是否被忽視
4.enlint 有很多的rules,為了改變rule的設置,可以設置rule ID等同於一些規則屬性:如
"off" or 0 關閉規則
"warn" or 1 打開規則,出現警告提示
"error" or 2 打開規則,出現錯誤提示
規則可如下定義:
"rules": {
"camel_case": 2
}
五、配置文件
{
"rules": {
"array-callback-return": "error",
"indent": ["error", 4, {"SwitchCase": 1}],
"block-spacing": "error",
"brace-style": ["error", "1tbs"],
"camelcase": ["error", { "properties": "never" }],
"callback-return": ["error", ["cb", "callback", "next"]],
"comma-spacing": "error",
"comma-style": ["error", "last"],
"consistent-return": "error",
"curly": ["error", "all"],
"default-case": "error",
"dot-notation": ["error", { "allowKeywords": false }],
"eol-last": "error",
"eqeqeq": "error",
"guard-for-in": "error",
"key-spacing": ["error", { "beforeColon": false, "afterColon": true }],
"keyword-spacing": "error",
"lines-around-comment": ["error", {
"beforeBlockComment": true,
"afterBlockComment": false,
"beforeLineComment": true,
"afterLineComment": false
}],
"new-cap": "error",
"newline-after-var": ["error", "never"],
"new-parens": "error",
"no-array-constructor": "error",
"no-invalid-this": "error",
"no-multi-spaces": "error",
"no-redeclare": "error",
"no-return-assign": "error",
"no-spaced-func": "error",
"no-trailing-spaces": "error",
"semi": "error",
"semi-spacing": "error",
"quotes":["error","double"],
"space-before-function-paren": ["error", "never"],
"space-in-parens": "error",
"space-infix-ops": "error",
"space-unary-ops": ["error", {"words": true, "nonwords": false}],
"spaced-comment": "error",
"yoda": ["error", "never"],
"no-mixed-requires": "error",
"handle-callback-err": ["error", "err"]
}
}
六、配置文件說明
| 屬性名 | 屬性值 | 描述 |
| [array-callback-return](http://eslint.org/docs/rules/array-callback-return) | "error" | Array執行回調函數返回語句 |
| [indent](http://eslint.org/docs/rules/indent) | ["error", 4, {"SwitchCase": 1}] | 縮寫格式的一致性 |
| [block-spacing](http://eslint.org/docs/rules/block-spacing) | "error" | 禁止執行空間內出現'-' |
| [brace-style](http://eslint.org/docs/rules/brace-style) | ["error","1tbs"] | 代碼書寫格式驗證 |
| [camelcase](http://eslint.org/docs/rules/camelcase) | ["error", { "properties": "never" }] | 屬性命名規則可以不使用駝峰命名法 |
| [callback-return](http://eslint.org/docs/rules/callback-return) | ["error", ["cb", "callback", "next"]] | 回調函數需要return進行返回 |
| [comma-spacing](http://eslint.org/docs/rules/comma-spacing) | "error" | 不允許在逗號前面出現空格 |
| [comma-style](http://eslint.org/docs/rules/comma-style) | ["error", "last"] | 方數組元素、變量聲明等直接需要逗號隔開 |
| [consistent-return](http://eslint.org/docs/rules/consistent-return) | "error" | 保持return返回的一致性 |
| [curly](http://eslint.org/docs/rules/curly) | ["error", "all"] | 函數或者條件判斷時需要統一使用大括號 |
| [default-case](http://eslint.org/docs/rules/default-case) | "error" | switch語句中必須有default條件 |
| [dot-notation](http://eslint.org/docs/rules/dot-notation) | ["error", { "allowKeywords": false }] | 不允許關鍵字出現在變量中 |
| [eol-last](http://eslint.org/docs/rules/eol-last) | "error" | 代碼間間隔出現一行 |
| [eqeqeq](http://eslint.org/docs/rules/eqeqeq) | "error" | 消除不安全類型的全等操作 |
| [guard-for-in](http://eslint.org/docs/rules/guard-for-in) | "error" | for循環中過濾掉一下不被需要的行為 |
| [key-spacing](http://eslint.org/docs/rules/key-spacing) | ["error", { "beforeColon": false, "afterColon": true }] | 鍵和值前保留一個空格 |
| [keyword-spacing](http://eslint.org/docs/rules/keyword-spacing) | "error" | 確保字符前后空格的一致性 |
| [lines-around-comment](http://eslint.org/docs/rules/lines-around-comment) | ["error", { "beforeBlockComment": true, "afterBlockComment": false, "beforeLineComment": true, "afterLineComment": false }] | 注釋前需要空行,注釋后不需要空行 |
| [new-cap](http://eslint.org/docs/rules/new-cap) | "error" | 構造函數首字母需要大寫 |
| [newline-after-var](http://eslint.org/docs/rules/newline-after-var) | ["error", "never"] | var定義后不空行 |
| [new-parens](http://eslint.org/docs/rules/new-parens) | "error" | 沒有參數時,構造函數也需要添加括號 |
| [no-invalid-this](http://eslint.org/docs/rules/no-invalid-this) | "error" | 不允許關鍵字this在函數或者類的外面 |
| [no-multi-spaces](http://eslint.org/docs/rules/no-multi-spaces) | "error" | 不允許鍵和值之間存在多個空格 |
| [no-redeclare](http://eslint.org/docs/rules/no-redeclare) | "error" | 不允許重復聲明 |
| [no-return-assign](http://eslint.org/docs/rules/no-return-assign) | "error" | 不允許在return語句中任務 |
| [no-spaced-func](http://eslint.org/docs/rules/no-spaced-func) | "error" | 調用函數時,函數名和括號之間不能有空格。 |
| [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces) | "error" | 不允許在語句后存在多余的空格 |
| [semi](http://eslint.org/docs/rules/semi) | "error" | 語句以分號結尾 |
| [semi-spacing](http://eslint.org/docs/rules/semi-spacing) | "error" | 分號前后不能有空格 |
| [quotes](http://eslint.org/docs/rules/quotes) | ["error","double"] | 使用雙引號 |
| [space-before-function-paren](http://eslint.org/docs/rules/space-before-function-paren) | "space-before-function-paren": ["error", "never"] | 不允許函數括號之間存在空格 |
| [space-in-parens](http://eslint.org/docs/rules/space-in-parens) | "error" | 不允許在括號里面存在空格 |
| [space-infix-ops](http://eslint.org/docs/rules/space-infix-ops) | "error" | 插入符合變量之間需要添加一個空格 |
| [space-unary-ops](http://eslint.org/docs/rules/space-unary-ops) | ["error", {"words": true, "nonwords": false}] | 允許一元運算符操作 |
| [spaced-comment](http://eslint.org/docs/rules/spaced-comment) | "error" | 注釋前需要一個空格 |
| [yoda](http://eslint.org/docs/rules/yoda) | ["error", "never"] | 條件語句中,變量在賦值語句的前面 |
| [no-mixed-requires](http://eslint.org/docs/rules/no-mixed-requires) | "error" | 不允許混合requires文件 |
| [no-new-require](http://eslint.org/docs/rules/no-new-require) | "error" | 不允許new require出現 |
| [no-path-concat](http://eslint.org/docs/rules/no-path-concat) | "error" | 不允許路徑以_鏈接 |
| [handle-callback-err](http://eslint.org/docs/rules/handle-callback-err) | ["error", "err"] | 處理錯誤的回調函數 |
