├── .eslintrc ├── LICENSE ├── README.md ├── old.md └── plugins └── eslint-plugin-angular-rules.md /.eslintrc: -------------------------------------------------------------------------------- 1 | # 这是我个人常用的规则 2 | { 3 | "extends": "eslint:recommended", 4 | "env":{ 5 | "browser": true, 6 | "node":true 7 | }, 8 | "rules": { 9 | "no-func-assign":0, 10 | "no-unexpected-multiline": 2, 11 | "block-scoped-var": 2, 12 | "consistent-return": 2, 13 | "curly": 2, 14 | "dot-location":[2,"property"], 15 | "eqeqeq": 2, 16 | "no-alert": 2, 17 | "no-caller": 2, 18 | "no-case-declarations": 2, 19 | "no-div-regex": 2, 20 | "no-else-return": 2, 21 | "no-eq-null": 2, 22 | "no-eval": 2, 23 | "no-extend-native": [2,{"exceptions":["Array"]}], 24 | "no-extra-bind": 2, 25 | "no-floating-decimal": 2, 26 | "no-implied-eval": 2, 27 | "no-invalid-this": 2, 28 | "no-labels": 2, 29 | "no-lone-blocks": 2, 30 | "no-loop-func": 2, 31 | "no-multi-spaces": 2, 32 | "no-multi-str": 2, 33 | "no-native-reassign": 2, 34 | "no-new-func": 2, 35 | "no-new-wrappers": 2, 36 | "no-new": 2, 37 | "no-octal-escape": 2, 38 | "no-process-env": 2, 39 | "no-proto": 2, 40 | "no-return-assign": 2, 41 | "no-self-compare": 2, 42 | "no-sequences": 2, 43 | "no-throw-literal": 2, 44 | "no-unused-expressions": [2, {"allowTernary": true}], 45 | "no-useless-call": 2, 46 | "no-useless-concat": 2, 47 | "no-void": 2, 48 | "no-with": 2, 49 | "radix": [2,"as-needed"], 50 | "wrap-iife": [2,"any"], 51 | "no-catch-shadow": 2, 52 | "no-shadow-restricted-names": 2, 53 | "no-undef-init": 2, 54 | "no-undefined": 2, 55 | "no-unused-vars": 2, 56 | "no-use-before-define": [2, "nofunc"], 57 | "no-new-require": 2, 58 | "no-path-concat": 2, 59 | "eol-last": 2, 60 | "indent": [2, 4, {"SwitchCase": 1}], 61 | "jsx-quotes": 2, 62 | "linebreak-style": [2, "unix"], 63 | "max-len": [2, 120, 4,{"ignoreComments":true}], 64 | "new-cap": 2, 65 | "new-parens": 2, 66 | "no-array-constructor": 2, 67 | "no-bitwise": 2, 68 | "no-lonely-if": 2, 69 | "no-multiple-empty-lines": [2, {"max": 2, "maxEOF": 1}], 70 | "no-negated-condition": 2, 71 | "no-new-object": 2, 72 | "no-trailing-spaces": [2,{ "skipBlankLines": true }], 73 | "no-unneeded-ternary": 2, 74 | "operator-linebreak": 2, 75 | "quote-props": [2,"as-needed",{"numbers": true}], 76 | "quotes": [2, "double", "avoid-escape"], 77 | "semi": 2, 78 | "space-after-keywords": 2, 79 | "space-before-keywords": 2, 80 | "space-infix-ops": 2, 81 | "space-return-throw-case": 2, 82 | "comma-spacing": 2, 83 | "wrap-regex": 2, 84 | "constructor-super": 2, 85 | "generator-star-spacing": 2, 86 | "no-arrow-condition": 2, 87 | "no-class-assign": 2, 88 | "no-const-assign": 2, 89 | "no-dupe-class-members": 2, 90 | "require-yield": 2, 91 | } 92 | } 93 | 94 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | The MIT License (MIT) 2 | 3 | Copyright (c) 2016 YangJiyuan 4 | 5 | Permission is hereby granted, free of charge, to any person obtaining a copy 6 | of this software and associated documentation files (the "Software"), to deal 7 | in the Software without restriction, including without limitation the rights 8 | to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 9 | copies of the Software, and to permit persons to whom the Software is 10 | furnished to do so, subject to the following conditions: 11 | 12 | The above copyright notice and this permission notice shall be included in all 13 | copies or substantial portions of the Software. 14 | 15 | THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 16 | IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 17 | FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 18 | AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 19 | LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 20 | OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE 21 | SOFTWARE. 22 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # ESLint 规则中文版 2 | 3 | Update 2016-11-24 4 | 5 | 发现官方发布了[中文文档](http://cn.eslint.org/),我这里就不班门弄斧了 6 | 7 | 8 | ## ESLint插件 9 | - [eslint-plugin-angular](plugins/eslint-plugin-angular-rules.md) -------------------------------------------------------------------------------- /old.md: -------------------------------------------------------------------------------- 1 | # ESLint 规则中文翻译版 2 | - [规则说明](#规则说明) 3 | - ESLint规则集 4 | - [常见错误](#常见错误) 5 | - [最好的练习](#最好的练习) 6 | - [严格模式](#严格模式) 7 | - [变量](#变量) 8 | - [Node.js和CommonJS](#nodejs和commonjs) 9 | - [代码风格](#代码风格) 10 | - [ECMAScript6(ES2015)](#ecmascript6es2015) 11 | - [被移除的](#被移除的) 12 | - ESLint插件 13 | - [eslint-plugin-angular](plugins/eslint-plugin-angular-rules.md) 14 | 15 | ## 规则说明 16 | ESLint中的规则被分为了多个种类,以便于你能够更好地理解这些规则。所有的规则默认都是不启用的。ESLint推荐给大家一套常用规则来发现比较常见的问题,而且你可以直接在你的配置文件中的`extends`选项下配置`"eslint:recommended"`值,这些被建议的规则就可以直接使用了。下面列出的规则中,指明了含有`(推荐)`的规则,都是在继承了`eslint:recommended`之后会被启用的规则。 17 | 有一些规则是可修复的规则,表示如果在命令行中使用`--fix`参数的话,那么ESLint会自动修复这些规则所描述的问题。下面列出的规则中标注有`(可修复)`的表示此类规则。 18 | 19 | *注:规则说明下面如果有列表项的话,表示该规则可以使用的选项。除个别情况(valid-jsdoc等)外,大部分规则只可以使用一个选项。* 20 | **用前须知:本人英语和语文处于小学5年级水平!** 21 | 22 | ## 常见错误 23 | 下面列出的规则指出了你可能出现问题的地方。 24 | 25 | - [comma-dangle](http://eslint.org/docs/rules/comma-dangle):不允许或强制在对象字面量或者数组属性的结尾使用逗号。(__推荐__) 26 | - `"always"`:必须在对象或者数组的每一个属性后面添加逗号。 27 | - `"never"`(默认):不允许在对象或者数组的属性后面添加逗号。 28 | - `"always-multiline"`:单行的时候不允许添加逗号,多行属性的时候必须在属性后面添加逗号。 29 | - [no-cond-assign](http://eslint.org/docs/rules/no-cond-assign):条件判断语句中不允许赋值操作。(__推荐__) 30 | - `"except-parens"`(默认):不允许赋值操作,除非写在一个括号里面 31 | - `"always"`:不允许所有情况的赋值 32 | - [no-console](http://eslint.org/docs/rules/no-console):不允许使用`console`中的所有方法。(__推荐__) 33 | - [no-constant-condition](http://eslint.org/docs/rules/no-constant-condition):不允许在条件判断语句中使用常数。(__推荐__) 34 | - [no-control-regex](http://eslint.org/docs/rules/no-control-regex):正则表达式中不允许使用控制字符。(__推荐__) 35 | - [no-debugger](http://eslint.org/docs/rules/no-debugger):不允许使用`debugger`。(__推荐__) 36 | - [no-dupe-args](http://eslint.org/docs/rules/no-dupe-args):方法的参数中不允许有重复值。(__推荐__) 37 | - [no-dupe-keys](http://eslint.org/docs/rules/no-dupe-keys):定义对象时不允许有重复的键。(__推荐__) 38 | - [no-duplicate-case](http://eslint.org/docs/rules/no-duplicate-case):`switch`语句中不允许使用相同的`case`值。(__推荐__) 39 | - [no-empty-character-class](http://eslint.org/docs/rules/no-empty-character-class):不允许在正则表达式中使用空的字符类,即`/^abc[]/`。(__推荐__) 40 | - [no-empty](http://eslint.org/docs/rules/no-empty):不允许空的块语句。(__推荐__) 41 | - [no-ex-assign](http://eslint.org/docs/rules/no-ex-assign):不允许对`try...catch`语句中`catch`的参数赋值。(__推荐__) 42 | - [no-extra-boolean-cast](http://eslint.org/docs/rules/no-extra-boolean-cast):不允许多余的布尔值转换操作。如`!!!foo`或`!!bar ? baz : bat`等。(__推荐__) 43 | - [no-extra-parens](http://eslint.org/docs/rules/no-extra-parens):不允许在表达式外面套一层多余的括号。 44 | - `"all"`(默认):不允许任何情况下出现的多余的括号。 45 | - `"functions"`:不允许`function`外面包裹多余的括号,其他表达式除外。 46 | - [no-extra-semi](http://eslint.org/docs/rules/no-extra-semi):不允许多余的分号。(__推荐__)(__可修复__) 47 | - [no-func-assign](http://eslint.org/docs/rules/no-func-assign):不允许为一个函数赋值。(__推荐__) 48 | - [no-inner-declarations](http://eslint.org/docs/rules/no-inner-declarations):函数或者变量的声明要放在程序的最外层或者另一个函数体内,不要在`if`等代码块中定义函数和变量。(__推荐__) 49 | - `"functions"`(默认):仅限定函数不允许在代码块中定义。 50 | - `"both"`:限定函数和变量都不允许在代码块中定义。 51 | - [no-invalid-regexp](http://eslint.org/docs/rules/no-invalid-regexp):不允许在`RegExp`构造函数中传递不合法的正则表达式字符串。(__推荐__) 52 | - [no-irregular-whitespace](http://eslint.org/docs/rules/no-irregular-whitespace):不允许在字符串外面或者注释中使用不规则的空格。(__推荐__) 53 | - [no-negated-in-lhs](http://eslint.org/docs/rules/no-negated-in-lhs):在`in`运算的左侧操作数前不允许使用否定符号`!`。(__推荐__) 54 | - [no-obj-calls](http://eslint.org/docs/rules/no-obj-calls):不允许将`Math`、`JSON`等全局的对象当做函数进行调用。(__推荐__) 55 | - [no-regex-spaces](http://eslint.org/docs/rules/no-regex-spaces):不允许在正则表达式中出现超过1个的连续空格。(__推荐__) 56 | - [no-sparse-arrays](http://eslint.org/docs/rules/no-sparse-arrays):不允许稀疏数组。(__推荐__) 57 | - [no-unexpected-multiline](http://eslint.org/docs/rules/no-unexpected-multiline):不允许两行连续但是不相关的代码作为一个连续表达式执行。 58 | - [no-unreachable](http://eslint.org/docs/rules/no-unreachable):不允许在`return`、`throw`、`continue`、`break`等中断语句之后出现代码,因为这些代码永远不会被执行到。(__推荐__) 59 | - [use-isnan](http://eslint.org/docs/rules/use-isnan):判断一个数是否是`NaN`的时候不允许使用`foo === NaN`这样的操作,而是使用`isNaN`函数进行判断。(__推荐__) 60 | - [valid-jsdoc](http://eslint.org/docs/rules/valid-jsdoc):不允许使用不合法的[JSDoc](http://usejsdoc.org/)注释。下列选项均可作为配置选项。 61 | - `"prefer"`:多个别名的注释标签中强制使用某一个。 62 | - `"requireReturn"`:函数和方法必须使用`@return`标签。 63 | - `"requireParamDescription"`:函数或者方法的`@param`标签必须含有参数描述。 64 | - `"requireReturnDescription"`:函数或者方法的`@return`标签必须含有返回值描述。 65 | - `"matchDescription"`:指定一个正则表达式在规范`JSDoc`的描述。 66 | - `"requireReturnType"`:必须为`@return`指定类型。 67 | - [valid-typeof](http://eslint.org/docs/rules/valid-typeof):`typeof`的结果必须和一个有效的字符串进行比较,如`typeof foo === 'strnig'`即是不合法的字符串。(__推荐__) 68 | 69 | ## 最好的练习 70 | 设计的这些规则都是阻止你犯错的。下面的规则要么是提出一种更好的实践,要么避免出现错误。 71 | 72 | - [accessor-pairs](http://eslint.org/docs/rules/accessor-pairs):在对象中`getter `和`setter`应该成对出现。 73 | - `"getWithoutSet"`:不允许只有`getter`而没有`setter`。 74 | - `"setWithoutGet"`:不允许只有`setter`而没有`getter`。 75 | - [block-scoped-var](http://eslint.org/docs/rules/block-scoped-var):不允许在代码块外使用在代码块内定义的变量。 76 | - [complexity](http://eslint.org/docs/rules/complexity):限制代码层级的复杂度,如`if...else if...else`等。配置参数是一个数值,表示层级数量。 77 | - [consistent-return](http://eslint.org/docs/rules/consistent-return):当函数有分支的时候,确保所有的`return`要么都有返回值,要么都没有返回值。 78 | - [curly](http://eslint.org/docs/rules/curly):`if...else`、`while`等语句必须使用 大括号`{}`包括。 79 | - [default-case](http://eslint.org/docs/rules/default-case):`switch`代码块必须含有一个`default`分支。 80 | - [dot-location](http://eslint.org/docs/rules/dot-location):规定在书写对象的属性或方法时,`.`书写在属性所在行开始还是对象所在行的结束。 81 | - `"object"`(默认):写在对象所在行的结尾位置。 82 | - `"property"`:写在属性或方法所在行的开始位置。 83 | - [dot-notation](http://eslint.org/docs/rules/dot-notation):建议使用`foo.bar`获取对象的属性,不推荐使用中括号法`foo["bar"]`。下列选项均可作为配置选项。 84 | - `"allowKeywords"`:关键字属性可以使用中括号法表示,如`foo['class']`。 85 | - `"allowPattern"`:指定正则表达式字符串,满足正则表达式的属性可以使用中括号法。 86 | - [eqeqeq](http://eslint.org/docs/rules/eqeqeq):使用`===`和`!==`代替`==`和`!=`。(__可修复__) 87 | - `"smart"`:对比字面量值、对比`typeof`计算值或者和`null`对比的时候可以使用`==`或`!=`。 88 | - `"allow-null"`:和`null`比较的时候可以使用`==`或`!=`。 89 | - [guard-for-in](http://eslint.org/docs/rules/guard-for-in):`for...in...`语句代码块中必须使用`if`语句来判断对象的属性是否是从原型链上继承而来的。 90 | - [no-alert](http://eslint.org/docs/rules/no-alert):不允许使用`alert`、`confirm`和`prompt`方法。 91 | - [no-caller](http://eslint.org/docs/rules/no-caller):不允许使用`arguments.caller`或`arguments.callee`。 92 | - [no-case-declarations](http://eslint.org/docs/rules/no-case-declarations):不允许在`case`代码块中声明变量。如果确实需要声明变量的话,必须用`{}`包括。 93 | - [no-div-regex](http://eslint.org/docs/rules/no-div-regex):不允许正则表达式的开头看起来像是一个除号,如`/=bar/`,这样的写法类似`+=`、`-=`等,具有迷惑性,必须使用转义符号才可以`/\=bar/`。 94 | - [no-else-return](http://eslint.org/docs/rules/no-else-return):如果一个`if`语句含有`return`,就没有必要使用`else`语句块了,原本放在`else`语句块内的代码可以直接写在代码块外。 95 | - [no-empty-label](http://eslint.org/docs/rules/no-empty-label):`label`只能在迭代或者`seitch`语句之前使用。 96 | - [no-empty-pattern](http://eslint.org/docs/rules/no-empty-pattern):在结构赋值时,模式不能为空 97 | - [no-eq-null](http://eslint.org/docs/rules/no-eq-null):和`null`比较时,不允许使用`==`或`!=`,而是使用`===`或`!==`。 98 | - [no-eval](http://eslint.org/docs/rules/no-eval):不允许使用`eval`语句。 99 | - [no-extend-native](http://eslint.org/docs/rules/no-extend-native):不允许在原生对象的`prototype`上添加属性。 100 | - `"exceptions"`:数组,用于指定可以在原型链上添加属性的对象。 101 | - [no-extra-bind](http://eslint.org/docs/rules/no-extra-bind):避免在`function`上使用没有必要的`bind`。 102 | - [no-fallthrough](http://eslint.org/docs/rules/no-fallthrough):不允许`switch...case`出现“贯穿”情况,即一个`case`代码块执行过之后继续执行下一个`case`代码块。除非使用`break`、`return`、`throw`或者特殊注释等方法中断下一个`case`执行。(__推荐__) 103 | - [no-floating-decimal](http://eslint.org/docs/rules/no-floating-decimal):浮点数小数点前后必须有数字。 104 | - [no-implicit-coercion](http://eslint.org/docs/rules/no-implicit-coercion):不允许使用简写的类型转换方式,如`+foo`、`''+foo`,下列选项均可作为配置选项。 105 | - `"boolean"`:使用简写操作将变量转化为布尔类型时报错。 106 | - `"number"`:使用简写操作将变量转化为数字类型时报错。 107 | - `"string"`:使用简写操作将变量转化为字符串类型时报错。 108 | - [no-implied-eval](http://eslint.org/docs/rules/no-implied-eval):不允许使用隐含的`eval`语句,例如`setTimeout('var foo=1;',10)`。 109 | - [no-invalid-this](http://eslint.org/docs/rules/no-invalid-this):在严格模式下,不允许在类或者`class-like`的对象外面使用`this`关键字。 110 | - [no-iterator](http://eslint.org/docs/rules/no-iterator):不允许使用`__iterator__`属性。 111 | - [no-labels](http://eslint.org/docs/rules/no-labels):不允许使用`label`语句块。 112 | - [no-lone-blocks](http://eslint.org/docs/rules/no-lone-blocks):不允许使用没有必要的代码块。 113 | - [no-loop-func](http://eslint.org/docs/rules/no-loop-func):不允许在循环中声明函数。 114 | - [no-magic-numbers](http://eslint.org/docs/rules/no-magic-numbers):用变量代替数字。下列选项均可作为配置选项。 115 | - `"ignore"`:指定可以直接使用的数字。 116 | - `"enforceConst"`:所有数字强制使用常量。 117 | - `"detectObjects"`:检测对象中是否有属性是数字。 118 | - [no-multi-spaces](http://eslint.org/docs/rules/no-multi-spaces):在逻辑表达式、条件表达式、申明语句、数组元素、对象属性、sequences、函数参数等地方不使用超过一个的空白符。(__可修复__) 119 | - [no-multi-str](http://eslint.org/docs/rules/no-multi-str):不允许多行创建字符串。 120 | - [no-native-reassign](http://eslint.org/docs/rules/no-native-reassign):不允许对原生对象进行重写。 121 | - `"exceptions"`:指定可以被重写的元素对象。 122 | - [no-new-func](http://eslint.org/docs/rules/no-new-func):不允许使用`Function`构造函数创建函数。 123 | - [no-new-wrappers](http://eslint.org/docs/rules/no-new-wrappers):创建基本数据类型的时候不使用构造器函数生成。如`String`、`Number`、`Boolean`等。 124 | - [no-new](http://eslint.org/docs/rules/no-new):调用`new`关键字生成对象的时候,必须将生成的实例赋值给变量。 125 | - [no-octal-escape](http://eslint.org/docs/rules/no-octal-escape):不允许使用八进制转义。 126 | - [no-octal](http://eslint.org/docs/rules/no-octal):不允许使用八进制的语法。(__推荐__) 127 | - [no-param-reassign](http://eslint.org/docs/rules/no-param-reassign):不允许对函数的形参进行赋值。 128 | - `"props"`:设置为true的话,修改形参的属性也会报错。 129 | - [no-process-env](http://eslint.org/docs/rules/no-process-env):不允许使用`process.env`。 130 | - [no-proto](http://eslint.org/docs/rules/no-proto):不允许使用`__proto__ `属性。 131 | - [no-redeclare](http://eslint.org/docs/rules/no-redeclare):不允许重复定义变量。(__推荐__) 132 | - `"builtinGlobals"`:设置为`true`的时候也会检测全局定义的变量是否被重新定义了,如`Object`。 133 | - [no-return-assign](http://eslint.org/docs/rules/no-return-assign):在`return`语句中不进行赋值操作。 134 | - `"except-parens"`(默认):赋值操作被括号括起来的时候不会报错。 135 | - `"always"`:在`return`语句中出现的任何形式的赋值都会报错。 136 | - [no-script-url](http://eslint.org/docs/rules/no-script-url):不允许使用`javascript:`这样的语句。如`location.href = "javascript:void(0)";`会报错。 137 | - [no-self-compare](http://eslint.org/docs/rules/no-self-compare):不允许和自身比较的比较操作。 138 | - [no-sequences](http://eslint.org/docs/rules/no-sequences):不允许使用逗号操作符。 139 | - [no-throw-literal](http://eslint.org/docs/rules/no-throw-literal):不允许使用`throw`抛出一个基本数据类型。由于静态检查代码,所以`throw foo;`并不会报错,因为无法确定`foo`是否是`Error`对象。 140 | - [no-unused-expressions](http://eslint.org/docs/rules/no-unused-expressions):不允许出现没有被使用的表达式或值。下列选项均可作为配置选项。 141 | - `"allowShortCircuit"`:设置为`true`的时候允许使用短路操作,如`a && b()`; 142 | - `"allowTernary"`:设置为`true`的时候,允许三元运算。 143 | - [no-useless-call](http://eslint.org/docs/rules/no-useless-call):不允许使用没必要的`.call()`和`.apply()`。 144 | - [no-useless-concat](http://eslint.org/docs/rules/no-useless-concat):不允许使用没有必要的字符串连接,如`"a"+"b"`。 145 | - [no-void](http://eslint.org/docs/rules/no-void):不允许使用`void`操作符。 146 | - [no-warning-comments](http://eslint.org/docs/rules/no-warning-comments):不允许使用含有警告提示信息的注释。下列选项均可作为配置选项。 147 | - `"terms"`:指定包含哪些字符为警告信息。默认`["todo", "fixme", "xxx"]`。 148 | - `"location"`:指定哪些位置的注释需要做匹配。默认是`"start"`。 149 | - [no-with](http://eslint.org/docs/rules/no-with):不允许使用`with`语句。 150 | - [radix](http://eslint.org/docs/rules/radix):调用`parseInt`函数的时候必须传递第二个参数指明进制。 151 | - `"always"`(默认):必须传递第二个参数。 152 | - `"as-needed"`:10进制不需要传递。 153 | - [vars-on-top](http://eslint.org/docs/rules/vars-on-top):变量要在其所在作用域的最开始声明。 154 | - [wrap-iife](http://eslint.org/docs/rules/wrap-iife):立即执行函数需要使用括号包裹。 155 | - `"outside"`(默认):括号包裹在调用外层。如`(function(){}())`。 156 | - `"inside"`:括号包裹在函数外层。如`(function(){})()`。 157 | - `"any"`:上述两种风格都可以。 158 | - [yoda](http://eslint.org/docs/rules/yoda):比较运算中,对象字面量应该写在比较操作符的左边,而变量应该写在比较操作符的右边。 159 | - `"never"`(默认):字面量必须在右边。 160 | - `"always"`:字面量必须在左边。 161 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 162 | - `"exceptRange"`:设置为`true`时,表示范围判断的不需要遵守左右。 163 | - `"onlyEquality"`:设置为`true`时,表示只有当等于判断时才需要遵守左右。 164 | 165 | ## 严格模式 166 | 这些规则和严格模式有关系。 167 | 168 | - [strict](http://eslint.org/docs/rules/strict):在文件头部或者函数的开始部分使用`"use strict";` 即可开启严格模式。 169 | - `"never"`:不允许使用严格模式。 170 | - `"global"`:只允许在全局使用严格模式。 171 | - `"function"`:只允许在函数开始部分使用严格模式。 172 | 173 | ## 变量 174 | 这些规则主要是和变量声明有关的。 175 | 176 | - [init-declarations](http://eslint.org/docs/rules/init-declarations):指定变量在定义的时候是否要赋值。 177 | - `"always"`(默认):所有变量声明的时候必须赋值。 178 | - `"never"`:所有变量声明的时候不能赋值,`const`定义的常量除外。 179 | 下列选项均可额外配置。 180 | - `"ignoreForLoopInit"`:设置为`true`的时候忽略`for`循环中的变量定义。 181 | - [no-catch-shadow](http://eslint.org/docs/rules/no-catch-shadow):不在`catch`语句外面的作用域定于和`catch`语句参数同名的变量。 182 | - [no-delete-var](http://eslint.org/docs/rules/no-delete-var):不允许使用`delete foo`删除变量。(__推荐__) 183 | - [no-label-var](http://eslint.org/docs/rules/no-label-var):不允许有和某一个`label`同名的变量。 184 | - [no-shadow-restricted-names](http://eslint.org/docs/rules/no-shadow-restricted-names):声明变量不能覆盖JavaScript中的保留关键字。 185 | - [no-shadow](http://eslint.org/docs/rules/no-shadow):不允许在当前作用域内定义作用域外已有的同名变量。下列选项均可作为配置选项。 186 | - `"builtinGlobals"`:设置为`true`的时候,全局变量也不允许重复定义,如`var Object`报错。 187 | - `"hoist"`:什么情况下定义的变量会报错。有下列三个选项可选。 188 | - `"all"`:外层作用域中的变量和方法都不允许重新定义 189 | - `"function"`(默认):外层作用域中的方法不允许重新定义 190 | - `"never"`:外层作用域中的变量和方法都可以再重新定义 191 | - `"allow"`:定义哪些变量是可以重复定义的,一个数组。 192 | - [no-undef-init](http://eslint.org/docs/rules/no-undef-init):不允许直接将一个变量定义为`undefined`,如`var foo = undefined;`。 193 | - [no-undef](http://eslint.org/docs/rules/no-undef):不允许使用没有定义的变量。(__推荐__) 194 | - `"typeof"`:设置为`true`时,执行`typeof`的变量也必须定义。 195 | - 通过添加注释`/*global foo*/`定义`foo`变量,但是这样定义的变量是只读的。使用`/*global foo:true*/`定义的`foo`变量就不是只读了。 196 | - [no-undefined](http://eslint.org/docs/rules/no-undefined):不允许使用`undefined`作为变量名或者函数形参。 197 | - [no-unused-vars](http://eslint.org/docs/rules/no-unused-vars):不允许出现定义了但是没有使用的变量。下列选项均可作为配置选项。(__推荐__) 198 | - `"vars"`:设置哪些变量可以定义了不使用。有下列两个选项可选。 199 | - `"all"`(默认):所有的被定义的变量都必须使用,包括全局的变量。 200 | - `"local"`:所有本地的变量定义之后必须使用,全局的变量可以不使用。 201 | - `"args"`:设置函数的参数是否需要都使用。有下列三个选项可选。 202 | - `"all"`:所有的参数都必须被使用。 203 | - `"after-used"`(默认):以被使用的最后一个参数为准,该参数之后的参数会警告没有被使用,之前的参数即使没有被使用也不报错。如`function(a,b){console.log(b);}`这样不会报错。 204 | - `"none"`:所有的参数都可以不被使用。 205 | - `"varsIgnorePattern"`:满足该正则的所有参数不会被检测是否被使用。 206 | - `"argsIgnorePattern"`:满足改正则的所有函数的参数不会被检测是否被使用。 207 | - [no-use-before-define](http://eslint.org/docs/rules/no-use-before-define):变量应该先定义后使用。 208 | - `"nofunc"`:函数可以在起定义之前调用。但用法必须是`function foo(){}`,而不能是`var foo = function(){};`。 209 | 210 | ## Node.js和CommonJS 211 | 这些规则是特定应用于运行在Node.js或者浏览器中CommonJS规范的代码。 212 | 213 | - [callback-return](http://eslint.org/docs/rules/callback-return):执行`callback`回调函数的时候必须使用`return`,函数最后调用回调函数的不需要添加`return`。 214 | - `[]`:数组,指定哪些是表示回调的函数。如`["callback", "cb", "next"]`。 215 | - [global-require](http://eslint.org/docs/rules/global-require):Node.JS中`require()`方法必须在最外层的作用域进行调用。 216 | - [handle-callback-err](http://eslint.org/docs/rules/handle-callback-err):回调函数中必须进行错误处理。默认的代表错误变量的参数是`err`。 217 | - `""`:字符串。指定代表错误变量的参数。如`error`。如果字符串以`^`开头,表示一个正则,如`^.*(e|E)rr`。 218 | - [no-mixed-requires](http://eslint.org/docs/rules/no-mixed-requires):调用`require()`方法不和其他变量声明或者`require()`混合使用。也就是require多个模块的生活不要使用同一个`var`,即`var fs=require('fs'),path=require('path');`是不允许的。 219 | - `{"grouping": true}`:默认grouping是开启的,也就是不允许混合使用,设置为`false`就可以是用混合了。 220 | - [no-new-require](http://eslint.org/docs/rules/no-new-require):不允许使用`new require()`。 221 | - [no-path-concat](http://eslint.org/docs/rules/no-path-concat):不允许使用`__dirname+"foo.js"`或`__filename+"bar.html"`这样拼接路径,建议使用`path.join`或者`path.resolve`方法。 222 | - [no-process-exit](http://eslint.org/docs/rules/no-process-exit):不允许使用`process.exit()`方法。 223 | - [no-restricted-modules](http://eslint.org/docs/rules/no-restricted-modules):限制使用某些模块。 224 | - `[]`:被限制的模块名称数组。如`['fs','path']`。 225 | - [no-sync](http://eslint.org/docs/rules/no-sync):不允许使用同步方法,如`fs.readFileSync()`等。 226 | 227 | ## 代码风格 228 | 这些规则是纯粹的风格化,使用因人而异。 229 | 230 | - [array-bracket-spacing](http://eslint.org/docs/rules/array-bracket-spacing):数组的`[`之后和`]`之前是否有空格。(__可修复__) 231 | - `"never"`(默认):`[`之后和`]`之前不允许有空格。 232 | - `"always"`:`[`之后和`]`之前必须有空格。 233 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 234 | - `"singleValue"`:当只有一个元素的时候是否需要使用空格。如`[ 2 ]`或`[{a:2}]`在规则配置为`[2, "always", { singleValue: false }]`时仍然会报错。`[2]`在配置为`[2, "never", { singleValue: true }]`的规则时也会报错。 235 | - `"objectsInArrays"`:当元素是对象的时候是否需要使用空格,用法同上。 236 | - `"arraysInArrays"`:当元素是数组的时候是否需要空格,用法同上。 237 | - [block-spacing](http://eslint.org/docs/rules/block-spacing):对象写在单行的时候是否需要在`{`之后和`}`之前加空格。(__可修复__) 238 | - `"always"`:必须加空格。 239 | - `"never"` :不允许添加空格。 240 | - [brace-style](http://eslint.org/docs/rules/brace-style):大括号的样式和缩进样式,该规则主要是用来描述大括号在控制语句中的位置。 241 | - `"1tbs"`(默认):不要把大括号单独写一行,大括号应该放在与之相对应的语句或者声明语句中。 242 | - `"stroustrup"`:`if`、`else`、`catch`等应该重起一行书写。 243 | - `"allman"`:所有的大括号都应该在单独的一行书写。 244 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 245 | - `"allowSingleLine"`:设置`true`或`false`表明大括号的`{`和`}`能否在同一行。 246 | - [camelcase](http://eslint.org/docs/rules/camelcase):是否使用驼峰命名法。 247 | - `"Properties"`:是否对对象的属性使用该规则。 248 | - `"always"`(默认):检查所有的对象属性名,必须满足驼峰命名法。 249 | - `"never"`:不检查对象的属性名。 250 | - [comma-spacing](http://eslint.org/docs/rules/comma-spacing):逗号前后是否添加空格。(__可修复__) 251 | - `"before"`:逗号前面是否添加空格。 252 | - `"true"`:必须有空格。 253 | - `"false"`(默认):不允许有空格。 254 | - `"after"`:逗号之后是否添加空格。 255 | - `"true"`(默认):必须有空格。 256 | - `"false"`:不允许有空格。 257 | - [comma-style](http://eslint.org/docs/rules/comma-style):设置逗号放置的位置。 258 | - `"first"`:逗号放在行首。 259 | - `"last"`(默认):逗号放在行尾。 260 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 261 | - `"exceptions"`:设置意外情况,有下列三个选项可配置,取值为`true`和`false`。 262 | - `"ArrayExpression"`:`true`表示忽略数组中的逗号设置。 263 | - `"ObjectExpression"`:`true`表示忽略对象中的逗号设置。 264 | - `"VariableDeclaration"`:`true`表示忽略声明变量的逗号设置。 265 | - [computed-property-spacing](http://eslint.org/docs/rules/computed-property-spacing):规定是否在对象的[动态属性](http://babeljs.io/docs/learn-es2015/#destructuring)中添加空格。(__可修复__) 266 | - `"never"`(默认):不允许在计算动态属性中添加空格,如`var x = {[ b ]: a}`会报错。 267 | - `"always"`:计算动态属性中必须添加空格。 268 | - [consistent-this](http://eslint.org/docs/rules/consistent-this):指定哪些变量可以赋值为`this`。被指定为`this`别名的变量不能被赋予其他值,`this`也只能赋值到这些指定的别名变量上且变量必须在定义的作用域内被赋值,如下代码是错误的。 269 | 270 | ``` 271 | "consistent-this": [2, "self"] 272 | 273 | var self; 274 | function foo(){ 275 | self = this; 276 | } 277 | ``` 278 | - `"xxx"`:指定别名。 279 | - [eol-last](http://eslint.org/docs/rules/eol-last):文件最后必须有一行空行。(__可修复__) 280 | - [func-names](http://eslint.org/docs/rules/func-names):方法必须有名字,也就是不允许匿名方法。 281 | - [func-style](http://eslint.org/docs/rules/func-style):指定使用哪种函数声明方式,即函数申明和函数表达式。 282 | - `"declaration"`:指定使用函数申明方式创建函数。 283 | - `"expression"`:指定使用函数表达式创建函数。 284 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 285 | - `"allowArrowFunctions"`:是否允许ES2015的[箭头函数](https://babeljs.io/docs/learn-es2015/#arrows-and-lexical-this),当设置为`true`的时候,即使配置为`"declaration"`也是合法的。 286 | - [id-length](http://eslint.org/docs/rules/id-length):规定标识符的长短,默认最小是2个字符,因为标示符设置太短的话,不利于理解。 287 | - `"min"`(默认为2):定义标识符的最小长度。 288 | - `"max"`(默认无穷大):定义标识符的最大长度。 289 | - `"properties"`:是否检查对象属性名。有下列两个值可选。 290 | - `"always"`(默认):检查所有对象的属性名 291 | - `"never"`:对象的变量名不受改规则影响。 292 | - `"exceptions"`:定义特殊的标示符数组,这些标示符不受规则影响。 293 | - [id-match](http://eslint.org/docs/rules/id-match):规定标示符必须符合一定的书写规则。 294 | - `"xxx"`:描述标示符必须符合的正则表达式。 295 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 296 | - `"properties"`:对象的属性名是否受影响。取值为`true`或`false`(默认)。 297 | - [indent](http://eslint.org/docs/rules/indent):规定代码的缩进方式。默认是4个空格。(__可修复__) 298 | - `"tab"/2`:数字或者`"tab"`数字的话,指定使用几个空格,`"tab"`表示使用tab表示缩进。 299 | - `{}`:配置可选的验证 300 | - `"SwitchCase"`:`switch...case`语句的缩进标准,默认是0,也就是上一个配置项设置的缩进的倍数,如下代码不会报错。 301 | 302 | ``` 303 | /*eslint indent: [2, 2, {"SwitchCase": 2}]*/ 304 | 305 | switch(a){ 306 | case "a": 307 | break; 308 | case "b": 309 | break; 310 | } 311 | 312 | ``` 313 | - `"VariableDeclarator"`:指定变量缩进的标准,计算方法类似`"SwitchCase"`,也可以分别对变量标示符进行设置,如`{ "var": 2, "let": 2, "const": 3}`。下面的代码不会报错。 314 | 315 | ``` 316 | /* eslint indent: [2, 2, { 317 | "VariableDeclarator": { "var": 2, "let": 2, "const": 3} 318 | }] */ 319 | var a, 320 | b, 321 | c; 322 | let a, 323 | b, 324 | c; 325 | const a = 1, 326 | b = 2, 327 | c = 3; 328 | ``` 329 | - [jsx-quotes](http://eslint.org/docs/rules/jsx-quotes):规定JSX语法中,属性使用单引号还是双引号。 330 | - `"prefer-double"`(默认):使用双引号。 331 | - `"prefer-single"`:使用单引号。 332 | - [key-spacing](http://eslint.org/docs/rules/key-spacing):规定对象字面量中的键值对之间是否有空格。 333 | - `"beforeColon"`:冒号前是否需要空格。默认`false`。 334 | - `"afterColon"`:冒号之后是否需要空格。默认`true`。 335 | - `"mode"`:指定空格的数量。可选配置如下。 336 | - `"strict"`(默认):允许使用一个空格。 337 | - `"minimum"`:允许最少一个空格。 338 | - `"align"`:取值为`"value"`,表示值对齐。 339 | - [linebreak-style](http://eslint.org/docs/rules/linebreak-style):规定换行的方式,'LF'和 'CRLF'不能混合使用。 340 | - `"unix"`:使用Unix风格的换行,也就是`LF`。 341 | - `"windows"`:使用Windows风格的换行,也就是`CRLF`。 342 | - [lines-around-comment](http://eslint.org/docs/rules/lines-around-comment):规定注释和代码之间的空行。可选配置如下。 343 | - `"beforeBlockComment"`(默认为`true`):`/*xxx*/`块注释之前必须有空行。 344 | - `"afterBlockComment"`:块注释之后必须有空行。 345 | - `"beforeLineComment"`:`//xxx`行注释之前必须有空行。 346 | - `"afterLineComment"`:行注释之后必须有空行。 347 | _其他特殊配置_ 348 | - `"allowBlockStart"`:当设置为`true`的时候,表示允许注释在任何代码块的开始的位置,这时即使设置`"beforeLineComment"`或`"beforeBlockComment"`不生效。 349 | - `"allowBlockEnd"`:当设置为`true`的时候,表示允许注释在任何代码块的结尾的位置。`"afterBlockComment"`和`"afterLineComment"`不生效。 350 | - `"allowObjectStart"`:当设置为`true`的时候,表示允许注释在任何对象的开始的位置,这时即使设置`"beforeLineComment"`或`"beforeBlockComment"`不生效。 351 | - `"allowObjectEnd"`:当设置为`true`的时候,表示允许注释在任何对象的结尾的位置。`"afterBlockComment"`和`"afterLineComment"`不生效。 352 | - `"allowArrayStart"`:当设置为`true`的时候,表示允许注释在任何数组的开始的位置,这时即使设置`"beforeLineComment"`或`"beforeBlockComment"`不生效。 353 | - `"allowArrayEnd"`:当设置为`true`的时候,表示允许注释在任何数组的结尾的位置。`"afterBlockComment"`和`"afterLineComment"`不生效。 354 | - [max-depth](http://eslint.org/docs/rules/max-depth):规定代码最大的嵌套层次。设置数字,默认为4。 355 | - [max-len](http://eslint.org/docs/rules/max-len):规定单行代码最大长度。 356 | - `{number}`:设置tab缩进,配置在规则的第三个参数。 357 | _其他特殊情况,写在规则配置数组的第四个元素位置。_ 358 | - `"ignoreComments"`:是否忽略注释。 359 | - `"ignoreUrls"`:是否忽略链接地址。 360 | - `"ignorePattern"`:设置正则表达式,匹配到该正则的代码会被忽略。 361 | - [max-nested-callbacks](http://eslint.org/docs/rules/max-nested-callbacks):规定回调函数最大的嵌套层次。类似于[max-depth](http://eslint.org/docs/rules/max-depth) 362 | - [max-params](http://eslint.org/docs/rules/max-params):规定函数参数的最大数量。 363 | - [max-statements](http://eslint.org/docs/rules/max-statements):规定函数中可以声明的变量的最大个数。 364 | - [new-cap](http://eslint.org/docs/rules/new-cap):规定作为构造函数的函数名首字母必须大写,不作为构造函数的首字母必须小写。 365 | - `"capIsNewExceptions"`:数组。指明一些首字母大写,但是不是构造函数的函数名,首字母必须大写,如`["Person"]`。默认值是`["Array","Boolean","Date",...]`。 366 | - `"newIsCapExceptions"`:数组。指明一些构造函数,首字母是小写的,如`["person"]`。这些函数会被作为构造函数使用。 367 | - `"newIsCap"`:默认为`true`,表示检测所有的使用`new`运算符的函数必须是首字母大写的。`"newIsCapExceptions"`配置项里设置的除外。 368 | - `"capIsNew"`:默认为`true`,表示检测所有没有使用`new`的函数首字母必须是小写的。`"capIsNewExceptions"`配置项里设置的除外。 369 | - [new-parens](http://eslint.org/docs/rules/new-parens):不允许在调用构造函数的时候遗漏`()`,即`new Person`是不合法的。 370 | - [newline-after-var](http://eslint.org/docs/rules/newline-after-var):规定声明变量之后是否需要空一行在开始其他代码。 371 | - `"always"`(默认): 必须空一行。 372 | - `"never"`:不允许空行。 373 | - [no-array-constructor](http://eslint.org/docs/rules/no-array-constructor):不允许使用`Array`构造函数创建数组。 374 | - [no-bitwise](http://eslint.org/docs/rules/no-bitwise):不允许使用位操作符,如`|`、`^`、`~`、`>>`、`<<`等。 375 | - [no-continue](http://eslint.org/docs/rules/no-continue):不允许使用`continue`语句。 376 | - [no-inline-comments](http://eslint.org/docs/rules/no-inline-comments):不允许在代码同一行内添加注释。 377 | - [no-lonely-if](http://eslint.org/docs/rules/no-lonely-if):不允许在`else`语句中只出现一个单独的`if`语句,如下代码。 378 | 379 | ``` 380 | if(){ 381 | //... 382 | } else { 383 | if(){ 384 | //... 385 | } 386 | } 387 | ``` 388 | - [no-mixed-spaces-and-tabs](http://eslint.org/docs/rules/no-mixed-spaces-and-tabs):不要空格和`tab`混合使用进行缩进。(__推荐__) 389 | - `"smart-tabs"`:当配置该选项之后,如果空格仅仅是用作对齐的话,并不会报错。通过配置`[2, "smart-tabs"]`开启改选项。 390 | - [no-multiple-empty-lines](http://eslint.org/docs/rules/no-multiple-empty-lines):规定连续空行的数量。 391 | - `"max"`:设置连续空行最大数量。 392 | - `"maxEOF"`:规定文件末尾空行最大数量。 393 | - [no-negated-condition](http://eslint.org/docs/rules/no-negated-condition):在`if`语句中使用了否定表达式,同时`else`语句又不为空,那么这样的`if-else`语句将被视为不合法的,可以将其反过来,该规则同样使用三元操作符。 394 | - [no-nested-ternary](http://eslint.org/docs/rules/no-nested-ternary):不允许嵌套三元运算符。 395 | - [no-new-object](http://eslint.org/docs/rules/no-new-object):不允许使用`new Object()`这样的写法。 396 | - [no-plusplus](http://eslint.org/docs/rules/no-plusplus):不允许使用`++`和`--`运算符。 397 | - `"allowForLoopAfterthoughts"`:设置为`true`的时候,`for`循环中出现的`++`或`--`操作不会视为错误。 398 | - [no-restricted-syntax](http://eslint.org/docs/rules/no-restricted-syntax):不允许使用某些语法。 399 | - `"FunctionExpression"`:不允许使用函数表达式声明函数。 400 | - `"WithStatement"`:不允许使用`with`语句。用法`[2, "FunctionExpression", "WithStatement"]`。 401 | - [no-spaced-func](http://eslint.org/docs/rules/no-spaced-func):函数调用时,允许函数名和括号之间出现空格。(__可修复__) 402 | - [no-ternary](http://eslint.org/docs/rules/no-ternary):不允许三元运算符。 403 | - [no-trailing-spaces](http://eslint.org/docs/rules/no-trailing-spaces):行末不允许出现空格。(__可修复__) 404 | - `"skipBlankLines"`:当设置为`true`的时候,表示忽略空行内的末尾空格。通过`[2, { "skipBlankLines": true }]`开启该配置项。 405 | - [no-underscore-dangle](http://eslint.org/docs/rules/no-underscore-dangle):不允许在标示符前后使用下划线。 406 | - `"allow"`:数组,指定哪些标示符是可以添加下划线的,如`[2, { "allow": ["foo_", "_bar"] }]`。 407 | - [no-unneeded-ternary](http://eslint.org/docs/rules/no-unneeded-ternary):不允许使用没有必要的三元操作符。如`bar ? bar : 1;`。 408 | - `"defaultAssignment"`(默认为`true`):默认赋值。设置为`true`的时候,`x ? x : 1;`的语法会报错,`false`的时候不会报错。 409 | - [object-curly-spacing](http://eslint.org/docs/rules/object-curly-spacing):定义对象字面量的时候`{`之后和`}`之前是否要加空格。 410 | - `"never"`(默认):不允许出现空格。 411 | - `"always"`:必须有空格。 412 | - [one-var](http://eslint.org/docs/rules/one-var):规定在一个作用域内是否只使用一个`var`或`let`或`const`。 413 | - `"always"`(默认):只能使用一个`var`或`let`或`const`。 414 | - `"never"`:每一个变量需要一个`var`来声明。 415 | _以上配置项使用`"one-var": [2, "always"]`的方法来配置,还有一种方式,通过配置一个对象。可选配置如下_ 416 | - `"var"`:以`var`是否可以多次使用定义变量,可选`always`和`never`。 417 | - `"let"`:以`let`是否可以多次使用定义变量,可选`always`和`never`。 418 | - `"const"`:以`const`是否可以多次使用定义变量,可选`always`和`never`。 419 | _除了上述三个配置用于针对不同的变量定义符,还可以按照变量是否初始化进行配置。_ 420 | - `"uninitialized"`:未初始化的变量使用一个`var`,如`var a, b, c;`。 421 | - `"initialized"`:初始化了值的变量使用多个`var`,如`var foo = true;var bar = false;`。 422 | - [operator-assignment](http://eslint.org/docs/rules/operator-assignment):规定赋值操作符的简写,如`foo += bar;`。 423 | - `"always"`:必须使用简写方式。 424 | - `"never"`:不允许出现简写方式。 425 | - [operator-linebreak](http://eslint.org/docs/rules/operator-linebreak):换行时,操作符应该写在行首还是行尾。 426 | - `"after"`(默认):写在行尾。 427 | - `"before"`:写在行首。 428 | - `"none"`:行首行尾都不对,也就是不换行。 429 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 430 | - `"overrides"`:指定一些运算符特殊情况。如`[2, "before", { "overrides": { "?": "after", "+=": "none" } }]`。 431 | - [padded-blocks](http://eslint.org/docs/rules/padded-blocks):规定代码块前后是否要加空行。 432 | - `"always"`(默认):必须加空行。 433 | - `"never"`:不允许加空行。 434 | - [quote-props](http://eslint.org/docs/rules/quote-props):对象的属性名是否需要加引号。 435 | - `"always"`(默认):需要添加引号。 436 | - `"as-needed"`:确实需要的时候添加引号,不需要的时候如果也添加了引号,会报错。 437 | - `"consistent"`:整个对象保持一致,加引号就都加,不加就都不加。 438 | - `"consistent-as-needed"`:视情况保持一致,如果有一个属性必须加引号,如`foo-bar`,那么整个对象都必须加,如果都比要加引号,就不必要加了。 439 | _当第二个参数设置为`"as-needed"`的时候,下列配置可以写在规则配置数组的第三个元素位置。_ 440 | - `"keywords"`(默认为`false `):关键词是否需要添加引号,取值为`true`或`false`。 441 | - `"unnecessary"`(默认为`true`):不必要添加引号的属性添加了之后是否报错。 442 | - `"numbers"`:数字的属性名是否需要引号。取值为`true`或`false`。 443 | _当第二个参数设置为`"consistent-as-needed"`的时候,下列配置可以写在规则配置数组的第三个元素位置。_ 444 | - `"keywords"`:(默认为`false `):关键词是否需要添加引号,取值为`true`或`false`。 445 | - [quotes](http://eslint.org/docs/rules/quotes):规定使用单引号、双引号还是反义符(ES2015)。(__可修复__) 446 | - `"double"`(默认):使用双引号。 447 | - `"single"`:使用单引号。 448 | - `"backtick"`:使用反义符。如\`string\`。 449 | _下列配置可以写在规则配置数组的第三个元素位置作为额外配置。_ 450 | - `"avoid-escape"`:设置该配置项的话,下面代码不会报错。 451 | 452 | ``` 453 | var str = 'a string containing "double" quotes'; 454 | ``` 455 | - [require-jsdoc](http://eslint.org/docs/rules/require-jsdoc):注释要求是[JSDoc](http://usejsdoc.org/)格式。 456 | - `"FunctionDeclaration"`(默认为`true`):设置为`true`表示定义函数必须添加JSDoc。 457 | - `"ClassDeclaration"`(默认为`false`):设置为`true`表示定义类必须添加JSDoc。 458 | - `"MethodDefinition"`(默认为`false `):设置为`true`表示定义方法必须添加JSDoc。 459 | 460 | ``` 461 | 上述规则通过下列代码进行配置 462 | "require-jsdoc": [2, { 463 | "require": { 464 | "FunctionDeclaration": true, 465 | "MethodDefinition": false, 466 | "ClassDeclaration": false 467 | } 468 | }] 469 | ``` 470 | - [semi-spacing](http://eslint.org/docs/rules/semi-spacing):定义分号前后是否需要添加空格。配置如`"semi-spacing": [2, {"before": false, "after": true}]`。 471 | - `"before"`(默认`false`):分号之前是否需要添加空格。 472 | - `"after"`(默认`true`):分号之后是否需要添加空格。 473 | - [semi](http://eslint.org/docs/rules/semi):分号党和无分号党之争 :)(__可修复__) 474 | - `"always"`(默认):分号党万岁! 475 | - `"never"`:无分号才是王道。 476 | - [sort-vars](http://eslint.org/docs/rules/sort-vars):定义按照顺序声明变量,如`var a,b,c;`而不是`var b,c,a;`。 477 | - `"ignoreCase"`:设置为`true`的时候表示忽略变量名大小写。 478 | - [space-after-keywords](http://eslint.org/docs/rules/space-after-keywords):关键词之后是否需要空格。(__可修复__) 479 | - `"always"`(默认):必须添加空格。 480 | - `"never"`:不允许添加空格。 481 | - [space-before-blocks](http://eslint.org/docs/rules/space-before-blocks):规定在代码块之前是否需要加空格。(__可修复__) 482 | - `"always"`(默认):必须添加空格。 483 | - `"never"`:不允许添加空格。 484 | *除了上述配置,还可以有下面配置对象。* 485 | - `"functions"`:函数代码块之前是否需要空格。可选`always`和`never`。 486 | - `"keywords"`:关键词(如`if`、`else`、`switch`等)代码块之前是否需要空格。可选`always`和`never`。 487 | - [space-before-function-paren](http://eslint.org/docs/rules/space-before-function-paren):函数定义的小括号`()`之前是否需要加空格。(__可修复__) 488 | - `"always"`(默认):必须添加空格。 489 | - `"never"`:不允许添加空格。 490 | *除了上述配置,还可以有下面配置对象。* 491 | - `"anonymous"`:匿名函数的`()`之前是否需要空格。可选`always`和`never`。 492 | - `"named"`:具名函数的`()`之前是否需要空格。可选`always`和`never`。 493 | - [space-before-keywords](http://eslint.org/docs/rules/space-before-keywords):规定关键字前面是否加空格,包括的关键字有:`if`, `else`, `for`, `while`, `do`, `switch`, `throw`, `try`, `catch`, `finally`,`with`,`break`, `continue`, `return`, `function`, `yield`, `class`。(__可修复__) 494 | - `"always"`(默认):必须添加空格。 495 | - `"never"`:不允许添加空格。 496 | - [space-in-parens](http://eslint.org/docs/rules/space-in-parens):规定括号内部的空格。规定是否需要在(右边,或者)左边加空格。但是无论哪一种要求,() 写法都是可以的。(__可修复__) 497 | - `"always"`:必须添加空格。 498 | - `"never"`(默认):不允许添加空格。 499 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 500 | - `"exceptions"`:数组,定义哪些是不需要符合规则的,有下列配置项。 501 | - `"{}"`:括号内有`{}`的话表示不需要遵守规则。 502 | - `"[]"`:括号内有`[]`的话表示不需要遵守规则。 503 | - `"()"`:括号内有`()`的话表示不需要遵守规则。 504 | - `"empty"`:括号内为空的话表示不需要遵守规则。 505 | - [space-infix-ops](http://eslint.org/docs/rules/space-infix-ops):规定操作符左右是否需要添加空格。(__可修复__) 506 | - `"int32Hint"`(默认为`false`):设置为`true`的时候,表示`a|0`这样的运算不需要添加空格。 507 | - [space-return-throw-case](http://eslint.org/docs/rules/space-return-throw-case):规定`return`,`throw`和`case`之后加一个空格。(__可修复__) 508 | - [space-unary-ops](http://eslint.org/docs/rules/space-unary-ops):规定在一元操作符前后是否加空格。(__可修复__) 509 | - `"words"`:应用于关键词操作符,如`delete`,`typeof`,`void`等。 510 | - `"nonwords"`:应用于一元操作符,如`++`,`--`,`!!`。 511 | - [spaced-comment](http://eslint.org/docs/rules/spaced-comment):规定是否需要在代码注释符号(`//`和`/*`)后面加一个空格。 512 | - `"always"`(默认):必须添加空格。 513 | - `"never"`:不允许添加空格。 514 | _其他特殊情况,写在规则配置数组的第三个元素位置。_ 515 | - `"markers"`:指定标识来指定哪些注释是不需要符合规则的。 516 | - `"exceptions"`:数组,如果注释符合是该项配置中的字符串,会忽略配置规则。 517 | 518 | ``` 519 | 规则配置如下 520 | "spaced-comment": [2, "always", { 521 | "line": { 522 | "markers": ["/"], 523 | "exceptions": ["-", "+"] 524 | }, 525 | "block": { 526 | "markers": ["!"], 527 | "exceptions": ["*"] 528 | } 529 | }] 530 | ``` 531 | - [wrap-regex](http://eslint.org/docs/rules/wrap-regex):要求在正则表达式的双斜杠外面加一个圆括号,来消除歧义。如`/foo/.test("bar")`是不合法的,必须是`(/foo/).test("bar")`。 532 | 533 | ## ECMAScript6(ES2015) 534 | 下列的规则都是和ES6语法相关的规则。 535 | 536 | - [arrow-body-style](http://eslint.org/docs/rules/arrow-body-style):箭头函数的函数体必须使用`{}`包括。 537 | - `"always"`:强制所有的箭头函数的函数体使用`{}`。 538 | - `"as-needed"`(默认):视情况而定是否需要大括号。 539 | - [arrow-parens](http://eslint.org/docs/rules/arrow-parens):箭头函数的参数必须有括号包裹。 540 | - `"always"`(默认):强制使用圆括号包裹参数。 541 | - `"as-needed"`:视情况而定是否需要圆括号。 542 | - [arrow-spacing](http://eslint.org/docs/rules/arrow-spacing):箭头函数的“箭头”`=>`前后是否需要空格。如`[2,{ "before": true, "after": true }]`。(__可修复__) 543 | - `"before"`(默认`true `):`=>`之前是否需要添加空格。 544 | - `"after"`(默认`true`):`=>`之后是否需要添加空格。 545 | - [constructor-super](http://eslint.org/docs/rules/constructor-super):继承父类的类必须在构造函数中调用`super`。 546 | - [generator-star-spacing](http://eslint.org/docs/rules/generator-star-spacing):规定`generator`函数中`*`前后的空白。(__可修复__) 547 | - `"before"`(默认`true `):`*`之前是否需要添加空格。 548 | - `"after"`(默认`false `):`*`之后是否需要添加空格。 549 | *下列简写对应不同的`before`和`after`的搭配* 550 | - `"before"`等同于`{"before": true, "after": false}` 551 | - `"after"`等同于`{"before": false, "after": true}` 552 | - `"both"`等同于`{"before": true, "after": true}` 553 | - `"neither"`等同于`{"before": false, "after": false}` 554 | 也就是说下面两个的配置等同 555 | 556 | ``` 557 | "generator-star-spacing": [2, {"before": true, "after": true}] 558 | "generator-star-spacing": [2, "both"] 559 | ``` 560 | - [no-arrow-condition](http://eslint.org/docs/rules/no-arrow-condition):在条件语句中禁止使用箭头函数,因为箭头函数的`=>`和大于等于符号`=>`很容易混淆。 561 | - [no-class-assign](http://eslint.org/docs/rules/no-class-assign):禁止重命名classes名。 562 | - [no-const-assign](http://eslint.org/docs/rules/no-const-assign):禁止修改`const`定义的常量。 563 | - [no-dupe-class-members](http://eslint.org/docs/rules/no-dupe-class-members):class中的成员不允许有相同的名字。 564 | - [no-this-before-super](http://eslint.org/docs/rules/no-this-before-super):继承父类的类的构造函数中,调动`super()`之前不允许使用`this/super`。 565 | - [no-var](http://eslint.org/docs/rules/no-var):不要使用`var`来定义变量,使用`let`和`const`。 566 | - [object-shorthand](http://eslint.org/docs/rules/object-shorthand):对象字面量中使用简写。 567 | - `"always"`(默认):所有地方都使用简写。 568 | - `"methods"`:只有对象中的方法使用简写。 569 | - `"properties"`:只有对象中的属性值使用简写。 570 | - `"never"`:任何地方都不使用简写。 571 | - [prefer-arrow-callback](http://eslint.org/docs/rules/prefer-arrow-callback):要求在回调函数需是箭头函数,也就是说函数作为函数的参数传入时,传入的函数需要时箭头函数。 572 | - [prefer-const](http://eslint.org/docs/rules/prefer-const):如果一个变量申明后就不再被修改,那么使用const来申明该变量。 573 | - [prefer-reflect](http://eslint.org/docs/rules/prefer-reflect):推荐使用Reflect上的方法替代以前老方法。 574 | - `"exceptions"`:数组,指定哪些是不需要被替代的。如`{exceptions:["getPrototypeOf"] }` 575 | - [prefer-spread](http://eslint.org/docs/rules/prefer-spread):建议使用`spread`操作来替代`.apply()`。 576 | - [prefer-template](http://eslint.org/docs/rules/prefer-template):建议使用模板字符串处理字符串拼接。 577 | - [require-yield](http://eslint.org/docs/rules/require-yield):`generates`函数中必须有`yield`。 578 | 579 | ## 被移除的 580 | 这些规则在早起的ESLint版本中使用,但是现在都有新的规则作为替代。 581 | 因为已经被移除了,所以没有罗列,可以去官网[查看](http://eslint.org/docs/rules/#removed)。 -------------------------------------------------------------------------------- /plugins/eslint-plugin-angular-rules.md: -------------------------------------------------------------------------------- 1 | # [ESLint-angular-rules](https://github.com/Gillespie59/eslint-plugin-angular) # 2 | 3 | 4 | eslint-plugin-angular中的规则分成了多个不同的分类,能帮助你更好的理解他们代表的含义。 5 | 6 | ### 可能的错误 7 | 下面的规则检查模式会检查出错误。 8 | - [module-getter](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/module-getter.md):使用module的时候,避免直接用一个变量,而是使用getter的链式语法 9 | - [module-setter](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/module-setter.md):不使用任何一个使用了setter语法的变量来定义modules。【不是很明白,看例子好像是说不要把模块赋值到变量】 10 | - [no-private-call](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-private-call.md):不允许使用以`$$`开头的angular私有变量。 11 | - `allow`:数组,指定哪些私有变量是可以使用的 12 | 13 | ### 最好的实践 14 | 这些规则都是为了避免你犯错误。他们制定了一个更好的做事方式或帮助你避免失误。 15 | 16 | - [component-limit](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/component-limit.md):一个文件中应该被限制多少个angular 组件,这里的组件指的是controller,directive等,默认为1。通过`angular/component-limit: [2,3]`可以指定想要限定的数量。 17 | - [controller-as-route](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/controller-as-route.md):在定义router或state的时候,应该使用angular的controllerAs语法。这样使用的好处参考[style guide](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y031)。 18 | - [controller-as-vm](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/controller-as-vm.md):使用controllerAs语法时把this 赋值给一个可捕获的变量,选择一个有代表性的名称,例如vm代表ViewModel。 19 | - `"xxx"`:定义使用哪些变量,如 20 | 21 | ``` 22 | angular/controller-as-vm: [2,"viewModel"] 23 | ``` 24 | - [controller-as](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/controller-as.md):参考[y031 by johnpapa - controllerAs Controller Syntax](https://github.com/johnpapa/angular-styleguide/blob/master/a1/README.md#style-y031),不要在`$scope`上面添加属性,使用`controllerAs`语法将数据和属性添加到`this`上。 25 | - [deferred](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/deferred.md):定义promise的时候不要使用`$q.deferred`,而是`$q(function(resolve, reject){})`。 26 | - [di-unused](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di-unused.md):不用的依赖就不要注入。 27 | - [directive-restrict](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/directive-restrict.md):当创建一个directive需要作为一个独立元素时,restrict值设置为E(自定义元素),也可以设置可选值A(自定义属性)。一般来说,如果它就是为了独立存在,用E是合适的做法。一般原则是允许EA,但是当它是独立的时候这更倾向于作为一个元素来实施,当它是为了增强已存在的DOM元素时则更倾向于作为一个属性来实施。定义指令的时候,`restrict`属性只能设置为`A`、`E`或`AE`,不能使用`C`和`M`。 28 | - explicit:是否需要指定`restrict`属性,有`always`和`never`可选。 29 | - restrict:指定可以使用的`restrict`属性,如`{"restrict":"A"}`,则定义指令的时候不允许使用`"restrict":"E"`或`"restrict":"AE"`。 30 | - [empty-controller](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/empty-controller.md):不要定义空的`controller`。 31 | - [no-controller](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-controller.md):根据Component-First原则,不允许定义和使用`controller`。 32 | - [no-inline-template](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-inline-template.md):不允许使用template属性定义模块,建议使用templateUrl,从外部文件定义模板。 33 | - allowSimple:是否允许简单的内联模板,默认为`true`,即简单的模板(有不超过两个闭合标签或一个自闭和标签)可以定义在template属性中,如`template:"
I'm inline template。
"`。 34 | - [no-run-logic](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-run-logic.md):在`run`方法中不能包含逻辑代码,仅作为入口。 35 | - `allowParams`:`run`方法中注入的参数在调用的时候是否可以传递参数进去。默认为`true`,当设置为`false`的时候,下列代码视为错误. 36 | 37 | ``` 38 | angular.module('app').run(function(startup) { 39 | startup('foo', true, 1); 40 | }); 41 | ``` 42 | - [no-services](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-services.md):不允许注入特定的一些services,如`$http`不应该注入到controller中。 43 | - 可以按照services进行配置,也可以按照controller或directive等进行配置。 44 | 45 | - [on-watch](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/on-watch.md):scope对象中的`$on`和`$watch`方法应该被赋值到变量中,以便在`$destroy`事件中删除。 46 | 47 | ### Angular弃用的特性 48 | 这些规则帮助你避免使用Angular中已经弃用的属性。 49 | 50 | - [no-cookiestore](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-cookiestore.md):在Angular1.4中,`$cookieStore`这个service已经被弃用了,取而代之的是`$cookies`。 51 | - [no-directive-replace](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-directive-replace.md):1.3之后的版本中,定义指令的时候不允许使用`replace`属性。 52 | - `ignoreReplaceFalse`:默认为`false`,指明当`replace`设置为`false`的时候是否忽略该属性。 53 | - [no-http-callback](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-http-callback.md):不允许使用`$http`方法返回的`Promise`的`success`和`error`方法,应该使用标准的`promise`API,即`then`。 54 | 55 | ### 命名 56 | 这些规则帮你指定一些命名方面的约定。 57 | 58 | - [controller-name](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/controller-name.md):为所有controller提供统一的名称,先特征后名字,鉴于controller是构造函数,所以要采用UpperCamelCase(每个单词首字母大写)的方式。 59 | - 可以配置一个字符串参数,如果是普通的字符串,表示待检测的controller名字里必须包含这个字符串,如 60 | 61 | ``` 62 | angular/controller-name:[2,"Ctrl"] 63 | app.controller("demoCtrl")或app.controller("demoCtrlTest") 64 | 都不会报错 65 | ``` 66 | 字符串也可以为正则,如`angular/controller-name:[2,"/[A-Z].*Ctrl/"]` 67 | - [directive-name](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/directive-name.md):提供一个短小、唯一、具有描述性的directive前缀,例如acmeSalesCustomerInfo在HTML中声明为acme-sales-customer-info。这样方便快速识别directive的内容和起源,例如acme-可能预示着这个directive是服务于Acme company。_避免使用ng-为前缀_ 68 | - 和`controller-name`规则相同的是,同样可以指定字符串或者正则,不同点是该规则主要用于指定前缀。 69 | - [file-name](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/file-name.md):组件文件名能够描述组件功能,给所有的组件提供统一的命名,推荐的做法是`feature.type.js`。大多数文件都有2个名字。 70 | - 文件名 (avengers.controller.js) 71 | - 带有Angular的注册组件名 (AvengersController) 72 | 有以下配置项 73 | - `nameStyle`:名字格式,可选择`"dash"`、`"dot"`和`"underscore"`三种,如 74 | 75 | ``` 76 | angular/file-name: [2,{"nameStyle":"dot"}] 77 | // 文件 src/app/my.util.services.js 中的services可以如下定义 78 | app.service('myUtilServices') 79 | ``` 80 | - `typeSeparator`:名字和类型之间的分隔符类型,也可选择`"dash"`、`"dot"`和`"underscore"`三种,如 81 | 82 | ``` 83 | angular/file-name: [2,{"typeSeparator":"underscore"}] 84 | // 文件 src/app/myUtil_services.js 中的services可以如下定义 85 | app.service('myUtilServices') 86 | ``` 87 | - `ignoreTypeSuffix`:配置是否忽略类型后缀,可选`true`和`false` 88 | 89 | ``` 90 | angular/file-name: [2,{"typeSeparator":"underscore","ignoreTypeSuffix":true}] 91 | // 文件 src/app/myUtil_services.js 中的services可以如下定义 92 | app.service('myUtil') //注意这里不再使用myUtilServices 93 | ``` 94 | - `ignorePrefix`:是否忽略特定前缀,如 95 | 96 | ``` 97 | angular/file-name: [2,{"typeSeparator":"dot","ignorePrefix":"ui"}] 98 | // 文件 src/app/modal.directive.js 中的directive可以如下定义 99 | app.service('uiModalDirective') 100 | ``` 101 | - [filter-name](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/filter-name.md):需要为所有的`filter`名字指定字符串前缀。 102 | - 配置同`directive-name` 103 | - [module-name](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/module-name.md):需要为所有的`module`名字指定字符串前缀。 104 | - 配置同`directive-name` 105 | - [service-name](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/service-name.md):需要为所有的`service`名字指定字符串前缀。 106 | - 配置同`directive-name` 107 | 108 | ### 约定 109 | Angular会为一种实现提供多种不同的方法,这些规则帮助你在项目中定义约定 110 | 111 | - [di-order](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di-order.md):依赖注入的参数列表应该按照字母表排序。 112 | - 指定是否忽略首尾有`_`的参数的下划线。可以是`true`或`false`,如`inject(function (_$compile_, $httpBackend, _$log_, _$rootScope_)`在该项配置为`false`的时候报错,为`true`的时候不报错。 113 | - [di](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/di.md):所有的依赖注入使用同一种方法。 114 | - 指定依赖注入的方式,可选`array`,`function`和`$inject`。默认为`function` 115 | - [dumb-inject](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/dumb-inject.md):单元测试中使用依赖注入以及对外部变量赋值的时候严格按照字母表排序,并且在注入的函数中只对外部变量进行赋值,不进行其他操作。 116 | - [function-type](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/function-type.md):指定回调函数使用命名函数亦或是匿名函数。 117 | - 指定使用何种函数,可选配置项为`named`和`anonymous`。默认为使用匿名函数 118 | - 指定哪些angular对象需要被检查,数组。可选的值有`['animation', 'config', 'constant', 'controller', 'directive', 'factory', 'filter', 'provider', 'service', 'value', 'decorator']` 119 | - [module-dependency-order](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/module-dependency-order.md):模块的依赖项应该有一个合理的排列顺序。有分组和不分组两种方式可配置。 120 | - `grouped`:是否分组,默认为true,即分为标准模块(`ngAnimate`等),第三方模块(`ui.router`),自定义模块(`myModule`)三组,并按此顺序,同一个组内的模块按照字母表顺序排列。标准模块包括`['ng','ngAnimate','ngAria','ngCookies','ngLocale','ngMessageFormat','ngMessages','ngMock','ngResource','ngRoute','ngSanitize','ngTouch','ngMaterial','ngNewRouter']` 121 | - `prefix`:指定一个前缀,模块名有此前缀的表示为自定义模块。当没有指定该参数的时候,非标准模块都算是自定义模块一组的,指定了该参数之后,既不是标准模块,也不符合这个前缀的模块被视为第三方模块。 122 | - [no-service-method](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-service-method.md):定义`service`的时候不要使用`app.service('myServices')`方法,而是使用`factory`方法。参考[angular.service vs angular.factory](http://stackoverflow.com/questions/14324451/angular-service-vs-angular-factory) 123 | - [one-dependency-per-line](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/one-dependency-per-line.md):每一个依赖注入独占一行,如下 124 | 125 | ``` 126 | app.controller('MyController', [ 127 | '$http', 128 | '$q', 129 | function($http, 130 | $q) { 131 | }]); 132 | ``` 133 | - [rest-service](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/rest-service.md):调用REST API的service中应该使用统一的rest service,可选择的有` '$http'`、`'$resource'`和`'Restangular'` 134 | - 指定使用哪个service 135 | - [watchers-execution](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/watchers-execution.md):手动触发脏数据检查的时候使用`$apply`方法还是`$digest`,两者的不同点在于:`$digest`从我们调用方法的作用域开始执行数据检查,而`$apply`方法会从`$rootScope`逐层往下进行数据检查。 136 | - 指定使用何种方法,默认是`$digest`。 137 | 138 | ### Angular封装 139 | 这些规则帮助你强制使用angular的内置封装(service) 140 | 141 | - [angularelement](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/angularelement.md):使用`angular.element`作为选择器,而不是使用`$`或`jQuery`。即使代码中使用了jQuery,`angular.element`也会调用jQuery的方法。 142 | - [definedundefined](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/definedundefined.md):判断一个变量是否是`undefined`的时候使用`angular.isUndefined`或`angular.isDefined`,注意不要使用`!angular.isUndefined(foo)`或`!angular.isDefined(foo)` 143 | - [document-service](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/document-service.md):使用`$document`代替`document` 144 | - [foreach](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/foreach.md):使用`angular.forEach`方法来进行遍历,不使用数组原生的`Array.prototype.forEach`方法。 145 | - [interval-service](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/interval-service.md):使用`$interval`代替`setInterval` 146 | - [json-functions](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/json-functions.md):使用`angular.fromJson`代替`JSON.parse`,使用`angular.toJson`代替`JSON.stringify` 147 | - [log](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/log.md):使用`$log`service代替`console`中的方法,包括`log()`、`warn()`、`error()`、`debug()`和`info()` 148 | - [no-angular-mock](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-angular-mock.md):`angular.mock`中定义的所有的方法同样可以在`window`中进行直接调用,使用这些方法的时候就不要使用`angular.mock`了,直接调用即可。 149 | - [no-jquery-angularelement](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/no-jquery-angularelement.md):经过`angular.element`初始化的方法已经是jqlite对象了,不需要再用`$`或`jQuery`。 150 | - [timeout-service](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/timeout-service.md):使用`$timeout`代替`setTimeout` 151 | - [typecheck-array](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-array.md):判断一个值是否是数组的时候使用`angular.isArray()` 152 | - [typecheck-date](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-date.md):判断一个值是否是日期对象的时候使用`angular.isDate()` 153 | - [typecheck-function](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-function.md):判断一个值是否是函数的时候使用`angular.isFunction()` 154 | - [typecheck-number](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-number.md):判断一个值是否是数字的时候使用`angular.isNumber()` 155 | - [typecheck-object](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-object.md):判断一个值是否是对象的时候使用`angular.isObject()` 156 | - [typecheck-string](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/typecheck-string.md):判断一个值是否是字符串的时候使用`angular.isString()` 157 | - [window-service](https://github.com/Gillespie59/eslint-plugin-angular/blob/master/docs/rules/window-service.md):使用`$window`代替`window` 158 | 159 | # 参考资料 # 160 | [angular-styleguide](https://github.com/johnpapa/angular-styleguide/blob/master/a1/i18n/zh-CN.md) 161 | --------------------------------------------------------------------------------