2017年9月12日
14:41
Rules
为了让你对规则有个更好的理解,ESLint对其进行了分门别类。
所有的规则默认都是禁用的。在配置文件中,使用"extends": "eslint:recommended" 来启用推荐的规则,报告一些常见的问题,在下文中这些推荐的规则都带有一个标记。
命令行的--fix选项用来自动修复规则所报告的问题(目前,大部分是对空白的修复),在下文中会有一个的图标。
Possible Errors
这些规则与 JavaScript代码中可能的错误或逻辑错误有关:
enforce “for” loop update clause moving the counter in the right direction. | |
disallow await inside of loops | |
disallow comparing against -0 | |
禁止条件表达式中出现赋值操作符 | |
禁用 console | |
禁止在条件中使用常量表达式 | |
禁止在正则表达式中使用控制字符 | |
禁用 debugger | |
禁止 function定义中出现重名参数 | |
禁止对象字面量中出现重复的 key | |
禁止出现重复的 case 标签 | |
禁止出现空语句块 | |
禁止在正则表达式中使用空字符集 | |
禁止对 catch子句的参数重新赋值 | |
禁止不必要的布尔转换 | |
禁止不必要的括号 | |
禁止不必要的分号 | |
禁止对 function声明重新赋值 | |
禁止在嵌套的块中出现变量声明或 function声明 | |
禁止 RegExp构造函数中存在无效的正则表达式字符串 | |
禁止在字符串和注释之外不规则的空白 | |
禁止把全局对象作为函数调用 | |
禁止直接调用 Object.prototypes的内置属性 | |
禁止正则表达式字面量中出现多个空格 | |
禁用稀疏数组 | |
disallow template literal placeholder syntax in regular strings | |
禁止出现令人困惑的多行表达式 | |
禁止在return、throw、continue和 break语句之后出现不可达代码 | |
禁止在 finally语句块中出现控制流语句 | |
disallow negating the left operand of relational operators | |
要求使用 isNaN()检查 NaN | |
强制使用有效的 JSDoc 注释 | |
强制 typeof表达式与有效的字符串进行比较 |
Best Practices
这些规则是关于最佳实践的,帮助你避免一些问题:
强制 getter 和 setter 在对象中成对出现 | |
强制数组方法的回调函数中有 return语句 | |
强制把变量的使用限制在其定义的作用域范围内 | |
enforce that class methods utilize this | |
指定程序中允许的最大环路复杂度 | |
要求 return语句要么总是指定返回的值,要么不指定 | |
强制所有控制语句使用一致的括号风格 | |
要求 switch语句中有 default分支 | |
强制在点号之前和之后一致的换行 | |
强制尽可能地使用点号 | |
要求使用 ===和 !== | |
要求 for-in循环中有一个 if语句 | |
禁用 alert、confirm和 prompt | |
禁用 arguments.caller或 arguments.callee | |
不允许在 case 子句中使用词法声明 | |
禁止除法操作符显式的出现在正则表达式开始的位置 | |
禁止 if 语句中 return语句之后有 else块 | |
禁止出现空函数 | |
禁止使用空解构模式 | |
禁止在没有类型检查操作符的情况下与 null进行比较 | |
禁用 eval() | |
禁止扩展原生类型 | |
禁止不必要的 .bind()调用 | |
禁用不必要的标签 | |
禁止 case语句落空 | |
禁止数字字面量中使用前导和末尾小数点 | |
disallow assignments to native objects or read-only global variables | |
禁止使用短符号进行类型转换 | |
禁止在全局范围内使用变量声明和 function声明 | |
禁止使用类似 eval()的方法 | |
禁止 this关键字出现在类和类对象之外 | |
禁用 __iterator__属性 | |
禁用标签语句 | |
禁用不必要的嵌套块 | |
禁止在循环中出现 function声明和表达式 | |
禁用魔术数字 | |
禁止使用多个空格 | |
禁止使用多行字符串 | |
disallow new operators outside of assignments or comparisons | |
禁止对 Function对象使用 new操作符 | |
禁止对 String,Number和 Boolean使用 new操作符 | |
禁用八进制字面量 | |
禁止在字符串中使用八进制转义序列 | |
禁止对 function的参数进行重新赋值 | |
禁用 __proto__属性 | |
禁止多次声明同一变量 | |
disallow certain properties on certain objects | |
禁止在 return语句中使用赋值语句 | |
disallow unnecessary return await | |
禁止使用 javascript: url | |
禁止自我赋值 | |
禁止自身比较 | |
禁用逗号操作符 | |
禁止抛出异常字面量 | |
禁用一成不变的循环条件 | |
禁止出现未使用过的表达式 | |
禁用出现未使用过的标 | |
禁止不必要的 .call()和 .apply() | |
禁止不必要的字符串字面量或模板字面量的连接 | |
禁用不必要的转义字符 | |
disallow redundant return statements | |
禁用 void操作符 | |
禁止在注释中使用特定的警告术语 | |
禁用 with语句 | |
require using Error objects as Promise rejection reasons | |
强制在parseInt()使用基数参数 | |
disallow async functions which have no await expression | |
要求所有的 var声明出现在它们所在的作用域顶部 | |
要求 IIFE 使用括号括起来 | |
要求或禁止 “Yoda” 条件 |
Strict Mode
该规则与使用严格模式和严格模式指令有关:
要求或禁止使用严格模式指令 |
Variables
这些规则与变量声明有关:
要求或禁止 var声明中的初始化 | |
禁止 catch子句的参数与外层作用域中的变量同名 | |
禁止删除变量 | |
不允许标签与变量同名 | |
禁用特定的全局变量 | |
禁止变量声明与外层作用域的变量同名 | |
禁止将标识符定义为受限的名字 | |
禁用未声明的变量,除非它们在 /*global */注释中被提到 | |
disallow initializing variables to undefined | |
禁止将 undefined作为标识符 | |
禁止出现未使用过的变量 | |
禁止在变量定义之前使用它们 |
Node.js and CommonJS
这些规则是关于Node.js 或在浏览器中使用CommonJS 的:
require return statements after callbacks | |
要求 require()出现在顶层模块作用域中 | |
要求回调函数中有容错处理 | |
| |
禁止混合常规变量声明和 require调用 | |
禁止调用 require时使用 new操作符 | |
禁止对 __dirname和 __filename进行字符串连接 | |
禁用 process.env | |
禁用 process.exit() | |
禁用通过 require加载的指定模块 | |
禁用同步方法 |
Stylistic Issues
这些规则是关于风格指南的,而且是非常主观的:
enforce linebreaks after opening and before closing array brackets | |
强制数组方括号中使用一致的空格 | |
enforce line breaks after each array element | |
强制在单行代码块中使用一致的空格 | |
强制在代码块中使用一致的大括号风格 | |
强制使用骆驼拼写法命名约定 | |
enforce or disallow capitalization of the first letter of a comment | |
要求或禁止末尾逗号 | |
强制在逗号前后使用一致的空格 | |
强制使用一致的逗号风格 | |
强制在计算的属性的方括号中使用一致的空格 | |
当获取当前执行环境的上下文时,强制使用一致的命名 | |
要求或禁止文件末尾存在空行 | |
require or disallow spacing between function identifiers and their invocations | |
require function names to match the name of the variable or property to which they are assigned | |
要求或禁止使用命名的 function表达式 | |
强制一致地使用 function声明或表达式 | |
禁用指定的标识符 | |
强制标识符的最小和最大长度 | |
要求标识符匹配一个指定的正则表达式 | |
强制使用一致的缩进 | |
强制在 JSX 属性中一致地使用双引号或单引号 | |
强制在对象字面量的属性中键和值之间使用一致的间距 | |
强制在关键字前后使用一致的空格 | |
enforce position of line comments | |
强制使用一致的换行风格 | |
要求在注释周围有空行 | |
强制可嵌套的块的最大深度 | |
强制一行的最大长度 | |
强制最大行数 | |
强制回调函数最大嵌套深度 | |
强制函数定义中最多允许的参数数量 | |
强制函数块最多允许的的语句数量 | |
强制每一行中所允许的最大语句数量 | |
enforce newlines between operands of ternary expressions | |
要求构造函数首字母大写 | |
要求调用无参构造函数时有圆括号 | |
要求方法链中每个调用都有一个换行符 | |
禁用 Array构造函数 | |
禁用按位运算符 | |
禁用 continue语句 | |
禁止在代码后使用内联注释 | |
禁止 if 作为唯一的语句出现在 else语句中 | |
禁止混合使用不同的操作符 | |
禁止空格和 tab 的混合缩进 | |
disallow use of chained assignment expressions | |
禁止出现多行空行 | |
禁用否定的表达式 | |
禁用嵌套的三元表达式 | |
禁用 Object的构造函数 | |
禁用一元操作符 ++和 -- | |
禁用特定的语法 | |
disallow all tabs | |
禁用三元操作符 | |
禁用行尾空格 | |
禁止标识符中有悬空下划线 | |
禁止可以在有更简单的可替代的表达式时使用三元操作符 | |
禁止属性前有空白 | |
enforce the location of single-line statements | |
强制大括号内换行符的一致性 | |
强制在大括号中使用一致的空格 | |
强制将对象的属性放在不同的行上 | |
强制函数中的变量要么一起声明要么分开声明 | |
require or disallow newlines around variable declarations | |
要求或禁止在可能的情况下使用简化的赋值操作符 | |
强制操作符使用一致的换行符 | |
要求或禁止块内填充 | |
require or disallow padding lines between statements | |
要求对象字面量属性名称用引号括起来 | |
强制使用一致的反勾号、双引号或单引号 | |
要求使用 JSDoc 注释 | |
require or disallow semicolons instead of ASI | |
强制分号之前和之后使用一致的空格 | |
强制分号的位置 | |
require object keys to be sorted | |
要求同一个声明块中的变量按顺序排列 | |
强制在块之前使用一致的空格 | |
强制在 function的左括号之前使用一致的空格 | |
强制在圆括号内使用一致的空格 | |
要求操作符周围有空格 | |
强制在一元操作符前后使用一致的空格 | |
强制在注释中 //或 /*使用一致的空格 | |
enforce spacing around colons of switch statements | |
require or disallow spacing between template tags and their literals | |
要求或禁止 Unicode 字节顺序标记 (BOM) | |
要求正则表达式被括号括起来 |
ECMAScript 6
这些规则只与 ES6 有关,即通常所说的 ES2015:
要求箭头函数体使用大括号 | |
要求箭头函数的参数使用圆括号 | |
强制箭头函数的箭头前后使用一致的空格 | |
要求在构造函数中有 super()的调用 | |
强制 generator 函数中 *号周围使用一致的空格 | |
禁止修改类声明的变量 | |
disallow arrow functions where they could be confused with comparisons | |
禁止修改 const声明的变量 | |
禁止类成员中出现重复的名称 | |
disallow duplicate module imports | |
disallow new operators with the Symbol object | |
disallow specified modules when loaded by import | |
禁止在构造函数中,在调用 super()之前使用 this或 super | |
disallow unnecessary computed property keys in object literals | |
禁用不必要的构造函数 | |
disallow renaming import, export, and destructured assignments to the same name | |
要求使用 let或 const而不是 var | |
要求或禁止对象字面量中方法和属性使用简写语法 | |
要求使用箭头函数作为回调 | |
要求使用 const声明那些声明后不再被修改的变量 | |
require destructuring from arrays and/or objects | |
disallow parseInt() in favor of binary, octal, and hexadecimal literals | |
require rest parameters instead of arguments | |
要求使用扩展运算符而非 .apply() | |
要求使用模板字面量而非字符串连接 | |
要求 generator 函数内有 yield | |
enforce spacing between rest and spread operators and their expressions | |
强制模块内的 import 排序 | |
require symbol descriptions | |
要求或禁止模板字符串中的嵌入表达式周围空格的使用 | |
强制在 yield*表达式中 *周围使用空格 |
Deprecated
Deprecated rule | Replaced by |
(no replacement) |
Removed
These rules fromolder versions of ESLint have been replaced by newer rules:
| Replaced by |
结果输出格式:--format or -f
checkstyle
codeframe
compact
html
jslint-xml
json
junit
stylish
table
tap
unix
visualstudio
路径配置
{
"eslint.options":{ "configFile": "C:/mydirectory/.eslintrc.json" }
}
识别顺序:
client/
.eslintignore
.eslintrc.json
client.js
server/
.eslintignore
.eslintrc.json
server.js
配置镜像
- by config command
npm config set registry http://registry.cnpmjs.org
npm info underscore (如果上面配置正确这个命令会有字符串response) - 命令行指定
npm --registry http://registry.cnpmjs.org info underscore - 编辑 ~/.npmrc 加入下面内容
registry = http://registry.cnpmjs.org
设置代理
npmconfig set proxy http://server:port
npmconfig set https-proxy http://server:port
如果需要认证的话可以这样设置:
npmconfig set proxy http://username:password@server:port
npmconfit set https-proxy http://username:password@server:port
如果代理不支持https的话需要修改npm存放package的网站地址。
npmconfig set registry "http://registry.npmjs.org/"
使用nrm快速切换npm源
nrm 是一个 NPM源管理器,允许你快速地在如下 NPM 源间切换:
- 列表项目
- npm
- cnpm
- strongloop
- enropean
- australia
- nodejitsu
- taobao
Install
sudo npm install -gnrm
如何使用?
列出可用的源:
➜ ~ nrm ls
npm ---- https://registry.npmjs.org/
cnpm --- http://r.cnpmjs.org/
taobao - http://registry.npm.taobao.org/
eu ----- http://registry.npmjs.eu/
au ----- http://registry.npmjs.org.au/
sl ----- http://npm.strongloop.com/
nj ----- https://registry.nodejitsu.com/
pt ----- http://registry.npmjs.pt/
切换:
➜ ~ nrm use taobao
Registry has been set to: http://registry.npm.taobao.org/
增加源:
nrm add<registry> <url> [home]
删除源:
nrm del<registry>
测试速度:
nrm test