非常教程

Standard JS 参考手册

规则 | Rules

规则 | Rules

规则

  • 使用2个空格进行缩进。eslint:indent function hello(name){console.log('hi',name)}
  • 对字符串使用单引号,以避免转义。eslint:quotes console.log('你好那里')$(“<div class ='box'>”)
  • 没有未使用的变量 eslint:no-unused-vars function myFunction(){var result = something()//✗avoid}
  • 在关键字后添加空格。 eslint:keyword-spacing if(condition){...} //✓okif(condition){...} //✗忌
  • 在函数声明的括号前添加一个空格。 eslint:space-before-function-paren function name(arg){...} //✓okfunction name(arg){...} //✗避免run(function(){...})//✓okrun(function(){。 ..})//✗避免
  • 总是使用 ===而不是==。例外:obj == null允许检查null || undefined。eslint:eqeqeq if(name ==='John')//✓okif(name =='John')//✗避免if(name!=='John')//✓okif(name!='John') //✗避免
  • 中缀运营商必须间隔开。eslint:space-infix-ops //✓okvarx = 2var message ='hello,'+ name +'!' //✗avoidvar x = 2var message ='hello,'+ name +'!'
  • 逗号应该有一个空格。eslint:comma-spacing //✓okvarlist = [1,2,3,4] function greet(name,options){...} //✗avoidvar list = [1,2,3,4] function greet(name,options ){...}
  • 将else语句与花括号保持在同一行。eslint:brace-style //✓okif(condition){// ...} else {// ...} //✗avoidif(condition){// ...} else {// ...}
  • 对于多行if语句,请使用花括号。eslint:curly //✓okif(options.quiet!== true)console.log('done')//✓okif(options.quiet!== true){console.log('done')} //✗avoidif (options.quiet!== true)console.log('done')
  • 始终处理 err函数参数。eslint:handle-callback-err //✓okrun(function(err){if(err)throw err window.alert('done')})//✗avoidrun(function(err){window.alert('done')})
  • 使用/* global */注释声明浏览器全局变量。例外的是:windowdocumentnavigator。防止不良的命名浏览器全局喜欢的意外使用openlengthevent,和name。/ *全局警报,提示* /警告('hi')提示符('ok?')明确引用函数或属性window也是可以的,尽管这样的代码不会在使用self而不是使用的Worker中运行window。eslint:no-undef window.alert('hi')//✓好的
  • 不允许多个空行。 eslint:no-multiple-empty-lines //✓okvarvalue ='hello world'console.log(value)//✗avoidvar value ='hello world'console.log(value)
  • 对于多线设置中的三元运算符,放置?:放在自己的线上。eslint:operator-linebreak //✓okvarlocation = env.development?'localhost':'www.api.com'//✓okvarlocation = env.development?'localhost':'www.api.com'//✗avoidvar location = env.development?'localhost':'www.api.com'
  • 对于var声明,请在其自己的语句中编写每个声明。eslint:one-var //✓okvarsilent = truevar verbose = true //✗avoidvar silent = true,verbose = true //✗avoidvar silent = true,verbose = true
  • 用附加括号换行条件赋值。这清楚地表明表达式故意是赋值(=)而不是对于equality(===)的拼写错误。eslint:no-cond-assign //✓okwhile((m = text.match(expr))){// ...} //✗avoidwhile(m = text.match(expr)){// ...}
  • 在单行块内添加空格。 eslint:block-spacing function foo(){return true} //✗避免函数foo(){return true} //✓确定
  • 在命名变量和函数时使用camelcase。 eslint:camelcase function my_function(){} //✗避免函数myFunction(){} //✓确定var my_var ='hello'//✗避免var myVar ='hello'//✓确定
  • 尾随逗号不允许。 eslint:comma-dangle var obj = {message:'hello',//✗avoid}
  • 逗号必须放在当前行的末尾。 eslint:comma-style var obj = {foo:'foo',bar:'bar'//✗avoid} var obj = {foo:'foo',bar:'bar'//✓ok}
  • 点应该与属性在同一行。 eslint:dot-location console。log('hello')//✗避免控制台.log('hello')//✓确定
  • 文件必须以换行符结尾。 eslint:eol-last
  • 函数标识符与其调用之间没有空格。 eslint:func-call-spacing console.log('hello')//✗avoidconsole.log('hello')//✓ok
  • 在键值对中添加冒号和值之间的空格。 eslint:key-spacing var obj = {'key':'value'} //✗avoidvar obj = {'key':'value'} //✗avoidvar obj = {'key':'value'} //✗avoidvar obj = {'key':'value'} //✓好的
  • 构造函数名称必须以大写字母开头。 eslint:new-cap function animal(){} var dog = new animal()//✗避免函数Animal(){} var dog = new Animal()//✓ok
  • 必须使用括号调用不带参数的构造函数。 eslint:new-parens function Animal(){} var dog = new Animal //✗avoidvar dog = new Animal()//✓确定
  • 定义setter时,对象必须包含getter。 eslint:accessor-pairs var person = {set name(value){//✗avoid this._name = value}} var person = {set name(value){this._name = value},get name(){//✓okreturn this._name}}
  • 派生类的构造函数必须调用 super eslint:constructor-super class Dog {constructor(){super()//✗avoid}} class Dog extends Mammal {constructor(){super()//✓ok}}}
  • 使用数组文字而不是数组构造函数。 eslint:no-array-constructor var nums = new Array(1,2,3)//✗avoidvar nums = [1,2,3] //✓确定
  • 避免使用 和。 eslint: function foo(n){if(n <= 0)return arguments.callee(n - 1)//✗avoid} function foo(n){if(n <= 0)return foo(n - 1)}arguments.callee arguments.callerno-caller
  • 避免修改类声明的变量。 eslint:no-class-assign class Dog {} Dog ='Fido'//✗忌
  • 避免修改使用声明的变量 const eslint:no-const-assign const score = 100score = 125 //✗避免
  • 避免在条件中使用常量表达式(循环除外)。 eslint:no-constant-condition if(false){//✗避免// ...} if(x === 0){//✓ok// ...} while(true){//✓ok// // }
  • 正则表达式中没有控制字符。 eslint:no-control-regex var pattern = / \ x1f / //✗avoidvar pattern = / \ x20 / //✓确定
  • 没有 陈述。 eslint: 函数sum(a,b){debugger //✗避免返回a + b}debugger no-debugger
  • 变量上没有运算符。 eslint: var命名名称//✗避免delete no-delete-var
  • 函数定义中没有重复的参数。 eslint:no-dupe-args 函数sum(a,b,a){//✗避免// ...}函数sum(a,b,c){//✓ok// ...}
  • 类成员中没有重复的名称。 eslint:no-dupe-class-members class Dog {bark(){} bark(){} //✗avoid}
  • 对象文字中没有重复的键。 eslint:no-dupe-keys var user = {name:'Jane Doe',name:'John Doe'//✗avoid}
  • 声明中没有重复的标签。 eslint: switch(id){case 1:// ... case 1://✗avoid}case switch no-duplicate-case
  • 每个模块使用一个import语句。 eslint:no-duplicate-imports 从'module'导入来自'module'import {myFunc2}的{myFunc1} //✗避免从'module'导入{myFunc1,myFunc2} //✓确定
  • 正则表达式中没有空字符类。 eslint:no-empty-character-class const myRegex = / ^ abc [] / //✗avoidconst myRegex = / ^ abc [az] / //✓ok
  • 没有空洞的解构模式。 eslint:no-empty-pattern const {a:{}} = foo //✗avoidconst {a:{b}} = foo //✓确定
  • 没用 eval() eslint:no-eval eval(“var result = user。”+ propName)//✗avoidvar result = user [propName] //✓ok
  • 条款中没有重新分配的例外情况。 eslint: try {// ...} catch(e){e ='new value'//✗avoid} try {// ...} catch(e){const newVal ='new value'//✓ok }catch no-ex-assign
  • 没有扩展本机对象。 eslint:no-extend-native Object.prototype.age = 21 //✗避免
  • 避免不必要的功能绑定 eslint:no-extra-bind const name = function(){getName()}。bind(user)//✗avoid const name = function(){this.getName()}。bind(user)//✓ok
  • 避免不必要的boolean强制转换。 eslint:no-extra-boolean-cast const result = trueif(!! result){//✗避免// ...} const result = trueif(result){//✓ok// ...}
  • 函数表达式周围没有不必要的括号。 eslint:no-extra-parens const myFunc =(function(){})//✗avoidconst myFunc = function(){} //✓ok
  • 使用 以防止下通的情况下。 eslint: switch(filter){case 1:doSomething()//✗避免案例2:doSomethingElse()} switch(过滤器){case 1:doSomething()break //✓okcase 2:doSomethingElse()} switch(filter ){case 1:doSomething()// fallthrough //✓ok case 2:doSomethingElse()}break switch no-fallthrough
  • 没有浮动小数。 eslint:no-floating-decimal const discount = .5 //✗avoidconst discount = 0.5 //✓确定
  • 避免重新分配函数声明。 eslint:no-func-assign function myFunc(){} myFunc = myOtherFunc //✗忌
  • 没有重新分配的只读全局变量。 eslint:no-global-assign window = {} //✗避免
  • 没有暗示 eval() eslint:no-implied-eval setTimeout(“alert('Hello world')”)//✗avoidsetTimeout(function(){alert('Hello world')})//✓ok
  • 嵌套块中没有函数声明。 eslint:no-inner-declarations if(authenticated){function setAuthUser(){} //✗avoid}
  • 构造函数中没有无效的正则表达式字符串 eslint: RegExp('[a-z')//✗avoidRegExp('[az]')//✓okRegExp no-invalid-regexp
  • 没有不规则的空白。 eslint:no-irregular-whitespace function myFunc()/ * <NBSP> * / {} //✗避免
  • 没用 __iterator__ eslint:no-iterator Foo.prototype .__ iterator__ = function(){} //✗避免
  • 没有与范围内变量共享名称的标签。 eslint:no-label-var var score = 100function game(){得分:while(true){//✗避免得分 - = 10如果(得分> 0)继续得分休息}}
  • 没有标签声明。 eslint:no-labels label:while(true){break label //✗avoid}
  • 没有不必要的嵌套块。 eslint:no-lone-blocks function myFunc(){{//✗avoid myOtherFunc()}} function myFunc(){myOtherFunc()//✓ok}
  • 避免混合空格和标签进行压痕。 eslint:no-mixed-spaces-and-tabs
  • 除缩进外,不要使用多个空格。 eslint:no-multi-spaces const id = 1234 //✗avoidconst id = 1234 //✓确定
  • 没有多行字符串。 eslint:no-multi-str const message ='Hello \ world'//✗忌
  • 没有 将对象分配给变量。 eslint: new Character()//✗avoidconst character = new Character()//✓确定new no-new
  • 不使用 构造函数。 eslint: var sum = new函数('a','b','返回a + b')//✗避免Function no-new-func
  • 不使用 构造函数。 eslint: let config = new Object()//✗避免Object no-new-object
  • 没用 new require eslint:no-new-require const myModule = new require('my-module')//✗避免
  • 不使用 构造函数。 eslint: const foo = new Symbol('foo')//✗避免Symbol no-new-symbol
  • 不使用原始包装器实例。 eslint:no-new-wrappers const message = new String('hello')//✗忌
  • 不调用全局对象属性作为函数。 eslint:no-obj-calls const math = Math()//✗忌
  • 没有八进制文字。 eslint:no-octal const num = 042 //✗avoidconst num ='042'//✓确定
  • 字符串文字中没有八进制转义序列。 eslint:no-octal-escape const copyright ='Copyright \ 251'//✗避免
  • 使用 和时避免字符串连接。 eslint: const pathToFile = __dirname +'/ app.js'//✗avoidconst pathToFile = path.join(__ dirname,'app.js')//✓ok__dirname __filenameno-path-concat
  • 避免使用 __proto__getPrototypeOf改用。eslint:no-proto const foo = obj .__ proto__ //✗avoidconst foo = Object.getPrototypeOf(obj)//✓确定
  • 没有重新声明的变量。 eslint:no-redeclare 让name ='John'let name ='Jane'//✗避免让name ='John'name ='Jane'//✓确定
  • 避免在正则表达式文字中使用多个空格。 eslint:no-regex-spaces const regexp = / test value / //✗避免const regexp = / test {3} value / //✓okconstregexp = / test value / //✓ok
  • return语句中的赋值必须用括号括起来。 eslint:no-return-assign 函数sum(a,b){return result = a + b //✗avoid}函数sum(a,b){return(result = a + b)//✓ok}
  • 避免为自己的 eslint 分配变量no-self-assign name = name // ✗avoid
  • 避免将变量与自身进行比较。 eslint:no-self-compare if(得分===得分){} //✗避免
  • 避免使用逗号运算符。 eslint:no-sequences if(doSomething(),!! test){} //✗避免
  • 受限制的名称不应该被遮蔽。 eslint:no-shadow-restricted-names let undefined ='value'//✗忌
  • 不允许使用稀疏数组。 eslint:no-sparse-arrays let fruits = ['apple',,'orange'] //✗避免
  • 标签不应该用于 eslint:no-tabs
  • 常规字符串不得包含模板文字占位符。 eslint:no-template-curly-in-string const message ='Hello $ {name}'//✗avoidconst message =`Hello $ {name}`//✓on
  • super() 必须在使用前调用 this eslint:no-this-before-super class Dog extends Animal {constructor(){this.legs = 4 //✗avoid super()}}
  • 只有 一个对象。 eslint:throw'error '//✗avoidthrow new Error('error')//✓确定throw Error no-throw-literal
  • 在行尾不允许有空格。 eslint:no-trailing-spaces
  • 不允许初始化为。 eslint: let name = undefined //✗避免让namename ='value'//✓确定undefined no-undef-init
  • 没有未修改的循环条件。 eslint:no-unmodified-loop-condition for(let i = 0; i <items.length; j ++){...} //✗avoidfor(let i = 0; i <items.length; i ++){...} //✓ok
  • 当存在更简单的替代方案时,没有三元运营商。 eslint:no-unneeded-ternary 让得分= val?val:0 //✗sickletscore = val || 0 //✓好的
  • 后无可达代码 return throw continue,和 语句。 eslint: function doSomething(){return true console.log('never called')//✗avoid}break no-unreachable
  • 块中没有流控制语句。 eslint: try {// ...} catch(e){// ...} finally {return 42 //✗avoid}finally no-unsafe-finally
  • 不能否定关系运算符的左操作数。 eslint:no-unsafe-negation if(!key in obj){} //✗avoidif(!(key in obj)){} //✓ok
  • 避免不必要的使用 和。 eslint: sum.call(null,1,2,3)//✗避免.call() .apply()no-useless-call
  • 避免在对象上使用不必要的计算属性键。 eslint:no-useless-computed-key const user = {['name']:'John Doe'} //✗avoidconst user = {name:'John Doe'} //✓ok
  • 没有必要的构造函数。 eslint:no-useless-constructor class Car {constructor(){//✗avoid}}
  • 没有不必要的逃避。 eslint:no-useless-escape let message ='Hell \ o'//✗避免
  • 不允许将导入,导出和解构分配重命名为相同名称。 eslint:no-useless-rename 从'./config'导入{config as config} //✗从'。/ config'中避免导入{config} //✓确定
  • 在属性之前没有空格。 eslint:no-whitespace-before-property user .name //✗avoiduser.name //✓确定
  • 没有使用 声明。 eslint: with(val){...} //✗避免with no-with
  • 保持对象属性之间换行符的一致性。 eslint:object-property-newline const user = {name:'Jane Doe',年龄:30,用户名:'jdoe86'//✗avoid} const user = {name:'Jane Doe',年龄:30,用户名:'jdoe86'} // ✓okconst user = {name:'Jane Doe',年龄:30,用户名:'jdoe86'} //✓确定
  • 块内无填充。 eslint:padded-blocks if(user){// ✗avoid const name = getName()} if(user){const name = getName()//✓ok}
  • 扩展运算符与其表达式之间没有空格。 eslint:rest-spread-spacing fn(... args)//✗avoidfn(... args)//✓确定
  • 分号后面必须有空格,前面没有空格。 eslint:semi-spacing for(let i = 0; i <items.length; i ++){...} //✗avoidfor(let i = 0; i <items.length; i ++){...} //✓ok
  • 在块之前必须有空格。 eslint:space-before-blocks if(admin){...} //✗avoidif(admin){...} //✓确定
  • 括号内没有空格。 eslint:space-in-parens getName(name)//✗avoidgetName(name)//✓确定
  • 一元运算符之后必须有空格。 eslint:space-unary-ops typeof!admin //✗avoidtypeof!admin //✓好的
  • 在注释中使用空格。 eslint:spaced-comment //评论//✗避免//评论//✓确定/ *评论* / //✗避免/ *评论* / //✓确定
  • 模板字符串中没有间距。 eslint:template-curly-spacing const message =`Hello,$ {name}`//✗avoidconst message =`Hello,$ {name}`//✓ok
  • 使用 检查时。 eslint: if(price === NaN){} //✗avoidif(isNaN(price)){} //✓确定isNaN() NaNuse-isnan
  • typeof 必须与有效字符串进行比较。 eslint:valid-typeof typeof name ==='undefimed'//✗avoidtypeof name ==='undefined'//✓ok
  • 必须立即调用函数表达式(IIFE)。 eslint:wrap-iife const getName = function(){}()//✗避免const getName =(function(){}())//✓okconstgetName =(function(){})()//✓ok
  • 在之前和之后的表达式必须有一个空间。 eslint: yield * increment()//✗avoidyield * increment()//✓确定* yield*yield-star-spacing
  • 避免尤达的条件。 eslint:yoda if(42 === age){} //✗avoidif(age === 42){} //✓ok 使用2个空格进行缩进。eslint:indent function hello(name){console.log('hi',name)}
  • 对字符串使用单引号,以避免转义。eslint:quotes console.log('你好那里')$(“<div class ='box'>”)
  • 没有未使用的变量 eslint:no-unused-vars function myFunction(){var result = something()//✗avoid}
  • 在关键字后添加空格。 eslint:keyword-spacing if(condition){...} //✓okif(condition){...} //✗忌
  • 在函数声明的括号前添加一个空格。 eslint:space-before-function-paren function name(arg){...} //✓okfunction name(arg){...} //✗避免run(function(){...})//✓okrun(function(){。 ..})//✗避免
  • 总是使用 ===而不是==。例外:obj == null允许检查null || undefined。eslint:eqeqeq if(name ==='John')//✓okif(name =='John')//✗避免if(name!=='John')//✓okif(name!='John') //✗避免
  • 中缀运营商必须间隔开。eslint:space-infix-ops //✓okvarx = 2var message ='hello,'+ name +'!' //✗avoidvar x = 2var message ='hello,'+ name +'!'
  • 逗号应该有一个空格。eslint:comma-spacing //✓okvarlist = [1,2,3,4] function greet(name,options){...} //✗avoidvar list = [1,2,3,4] function greet(name,options ){...}
  • 将else语句与花括号保持在同一行。eslint:brace-style //✓okif(condition){// ...} else {// ...} //✗avoidif(condition){// ...} else {// ...}
  • 对于多行if语句,请使用花括号。eslint:curly //✓okif(options.quiet!== true)console.log('done')//✓okif(options.quiet!== true){console.log('done')} //✗avoidif (options.quiet!== true)console.log('done')
  • 始终处理 err函数参数。eslint:handle-callback-err //✓okrun(function(err){if(err)throw err window.alert('done')})//✗avoidrun(function(err){window.alert('done')})
  • 使用/* global */注释声明浏览器全局变量。例外的是:windowdocumentnavigator。防止不良的命名浏览器全局喜欢的意外使用openlengthevent,和name。/ *全局警报,提示* /警告('hi')提示符('ok?')明确引用函数或属性window也是可以的,尽管这样的代码不会在使用self而不是使用的Worker中运行window。eslint:no-undef window.alert('hi')//✓好的
  • 不允许多个空行。 eslint:no-multiple-empty-lines //✓okvarvalue ='hello world'console.log(value)//✗avoidvar value ='hello world'console.log(value)
  • 对于多线设置中的三元运算符,放置?:放在自己的线上。eslint:operator-linebreak //✓okvarlocation = env.development?'localhost':'www.api.com'//✓okvarlocation = env.development?'localhost':'www.api.com'//✗avoidvar location = env.development?'localhost':'www.api.com'
  • 对于var声明,请在其自己的语句中编写每个声明。eslint:one-var //✓okvarsilent = truevar verbose = true //✗avoidvar silent = true,verbose = true //✗avoidvar silent = true,verbose = true
  • 用附加括号换行条件赋值。这清楚地表明表达式故意是赋值(=)而不是对于equality(===)的拼写错误。eslint:no-cond-assign //✓okwhile((m = text.match(expr))){// ...} //✗avoidwhile(m = text.match(expr)){// ...}
  • 在单行块内添加空格。 eslint:block-spacing function foo(){return true} //✗避免函数foo(){return true} //✓确定
  • 在命名变量和函数时使用camelcase。 eslint:camelcase function my_function(){} //✗避免函数myFunction(){} //✓确定var my_var ='hello'//✗避免var myVar ='hello'//✓确定
  • 尾随逗号不允许。 eslint:comma-dangle var obj = {message:'hello',//✗avoid}
  • 逗号必须放在当前行的末尾。 eslint:comma-style var obj = {foo:'foo',bar:'bar'//✗avoid} var obj = {foo:'foo',bar:'bar'//✓ok}
  • 点应该与属性在同一行。 eslint:dot-location console。log('hello')//✗避免控制台.log('hello')//✓确定
  • 文件必须以换行符结尾。 eslint:eol-last
  • 函数标识符与其调用之间没有空格。 eslint:func-call-spacing console.log('hello')//✗avoidconsole.log('hello')//✓ok
  • 在键值对中添加冒号和值之间的空格。 eslint:key-spacing var obj = {'key':'value'} //✗avoidvar obj = {'key':'value'} //✗avoidvar obj = {'key':'value'} //✗avoidvar obj = {'key':'value'} //✓好的
  • 构造函数名称必须以大写字母开头。 eslint:new-cap function animal(){} var dog = new animal()//✗避免函数Animal(){} var dog = new Animal()//✓ok
  • 必须使用括号调用不带参数的构造函数。 eslint:new-parens function Animal(){} var dog = new Animal //✗avoidvar dog = new Animal()//✓确定
  • 定义setter时,对象必须包含getter。 eslint:accessor-pairs var person = {set name(value){//✗avoid this._name = value}} var person = {set name(value){this._name = value},get name(){//✓okreturn this._name}}
  • 派生类的构造函数必须调用 super eslint:constructor-super class Dog {constructor(){super()//✗avoid}} class Dog extends Mammal {constructor(){super()//✓ok}}}
  • 使用数组文字而不是数组构造函数。 eslint:no-array-constructor var nums = new Array(1,2,3)//✗avoidvar nums = [1,2,3] //✓确定
  • 避免使用 和。 eslint: function foo(n){if(n <= 0)return arguments.callee(n - 1)//✗avoid} function foo(n){if(n <= 0)return foo(n - 1)}arguments.callee arguments.callerno-caller
  • 避免修改类声明的变量。 eslint:no-class-assign class Dog {} Dog ='Fido'//✗忌
  • 避免修改使用声明的变量 const eslint:no-const-assign const score = 100score = 125 //✗避免
  • 避免在条件中使用常量表达式(循环除外)。 eslint:no-constant-condition if(false){//✗避免// ...} if(x === 0){//✓ok// ...} while(true){//✓ok// // }
  • 正则表达式中没有控制字符。 eslint:no-control-regex var pattern = / \ x1f / //✗avoidvar pattern = / \ x20 / //✓确定
  • 没有 陈述。 eslint: 函数sum(a,b){debugger //✗避免返回a + b}debugger no-debugger
  • 变量上没有运算符。 eslint: var命名名称//✗避免delete no-delete-var
  • 函数定义中没有重复的参数。 eslint:no-dupe-args 函数sum(a,b,a){//✗避免// ...}函数sum(a,b,c){//✓ok// ...}
  • 类成员中没有重复的名称。 eslint:no-dupe-class-members class Dog {bark(){} bark(){} //✗avoid}
  • 对象文字中没有重复的键。 eslint:no-dupe-keys var user = {name:'Jane Doe',name:'John Doe'//✗avoid}
  • 声明中没有重复的标签。 eslint: switch(id){case 1:// ... case 1://✗avoid}case switch no-duplicate-case
  • 每个模块使用一个import语句。 eslint:no-duplicate-imports 从'module'导入来自'module'import {myFunc2}的{myFunc1} //✗避免从'module'导入{myFunc1,myFunc2} //✓确定
  • 正则表达式中没有空字符类。 eslint:no-empty-character-class const myRegex = / ^ abc [] / //✗avoidconst myRegex = / ^ abc [az] / //✓ok
  • 没有空洞的解构模式。 eslint:no-empty-pattern const {a:{}} = foo //✗avoidconst {a:{b}} = foo //✓确定
  • 没用 eval() eslint:no-eval eval(“var result = user。”+ propName)//✗avoidvar result = user [propName] //✓ok
  • 条款中没有重新分配的例外情况。 eslint: try {// ...} catch(e){e ='new value'//✗avoid} try {// ...} catch(e){const newVal ='new value'//✓ok }catch no-ex-assign
  • 没有扩展本机对象。 eslint:no-extend-native Object.prototype.age = 21 //✗避免
  • 避免不必要的功能绑定 eslint:no-extra-bind const name = function(){getName()}。bind(user)//✗avoid const name = function(){this.getName()}。bind(user)//✓ok
  • 避免不必要的boolean强制转换。 eslint:no-extra-boolean-cast const result = trueif(!! result){//✗避免// ...} const result = trueif(result){//✓ok// ...}
  • 函数表达式周围没有不必要的括号。 eslint:no-extra-parens const myFunc =(function(){})//✗avoidconst myFunc = function(){} //✓ok
  • 使用 以防止下通的情况下。 eslint: switch(filter){case 1:doSomething()//✗避免案例2:doSomethingElse()} switch(过滤器){case 1:doSomething()break //✓okcase 2:doSomethingElse()} switch(filter ){case 1:doSomething()// fallthrough //✓ok case 2:doSomethingElse()}break switch no-fallthrough
  • 没有浮动小数。 eslint:no-floating-decimal const discount = .5 //✗avoidconst discount = 0.5 //✓确定
  • 避免重新分配函数声明。 eslint:no-func-assign function myFunc(){} myFunc = myOtherFunc //✗忌
  • 没有重新分配的只读全局变量。 eslint:no-global-assign window = {} //✗避免
  • 没有暗示 eval() eslint:no-implied-eval setTimeout(“alert('Hello world')”)//✗avoidsetTimeout(function(){alert('Hello world')})//✓ok
  • 嵌套块中没有函数声明。 eslint:no-inner-declarations if(authenticated){function setAuthUser(){} //✗avoid}
  • 构造函数中没有无效的正则表达式字符串 eslint: RegExp('[a-z')//✗avoidRegExp('[az]')//✓okRegExp no-invalid-regexp
  • 没有不规则的空白。 eslint:no-irregular-whitespace function myFunc()/ * <NBSP> * / {} //✗避免
  • 没用 __iterator__ eslint:no-iterator Foo.prototype .__ iterator__ = function(){} //✗避免
  • 没有与范围内变量共享名称的标签。 eslint:no-label-var var score = 100function game(){得分:while(true){//✗避免得分 - = 10如果(得分> 0)继续得分休息}}
  • 没有标签声明。 eslint:no-labels label:while(true){break label //✗avoid}
  • 没有不必要的嵌套块。 eslint:no-lone-blocks function myFunc(){{//✗avoid myOtherFunc()}} function myFunc(){myOtherFunc()//✓ok}
  • 避免混合空格和标签进行压痕。 eslint:no-mixed-spaces-and-tabs
  • 除缩进外,不要使用多个空格。 eslint:no-multi-spaces const id = 1234 //✗avoidconst id = 1234 //✓确定
  • 没有多行字符串。 eslint:no-multi-str const message ='Hello \ world'//✗忌
  • 没有 将对象分配给变量。 eslint: new Character()//✗avoidconst character = new Character()//✓确定new no-new
  • 不使用 构造函数。 eslint: var sum = new函数('a','b','返回a + b')//✗避免Function no-new-func
  • 不使用 构造函数。 eslint: let config = new Object()//✗避免Object no-new-object
  • 没用 new require eslint:no-new-require const myModule = new require('my-module')//✗避免
  • 不使用 构造函数。 eslint: const foo = new Symbol('foo')//✗避免Symbol no-new-symbol
  • 不使用原始包装器实例。 eslint:no-new-wrappers const message = new String('hello')//✗忌
  • 不调用全局对象属性作为函数。 eslint:no-obj-calls const math = Math()//✗忌
  • 没有八进制文字。 eslint:no-octal const num = 042 //✗avoidconst num ='042'//✓确定
  • 字符串文字中没有八进制转义序列。 eslint:no-octal-escape const copyright ='Copyright \ 251'//✗避免
  • 使用 和时避免字符串连接。 eslint: const pathToFile = __dirname +'/ app.js'//✗avoidconst pathToFile = path.join(__ dirname,'app.js')//✓ok__dirname __filenameno-path-concat
  • 避免使用 __proto__getPrototypeOf改用。eslint:no-proto const foo = obj .__ proto__ //✗avoidconst foo = Object.getPrototypeOf(obj)//✓确定
  • 没有重新声明的变量。 eslint:no-redeclare 让name ='John'let name ='Jane'//✗避免让name ='John'name ='Jane'//✓确定
  • 避免在正则表达式文字中使用多个空格。 eslint:no-regex-spaces const regexp = / test value / //✗避免const regexp = / test {3} value / //✓okconstregexp = / test value / //✓ok
  • return语句中的赋值必须用括号括起来。 eslint:no-return-assign 函数sum(a,b){return result = a + b //✗avoid}函数sum(a,b){return(result = a + b)//✓ok}
  • 避免为自己的 eslint 分配变量no-self-assign name = name // ✗avoid
  • 避免将变量与自身进行比较。 eslint:no-self-compare if(得分===得分){} //✗避免
  • 避免使用逗号运算符。 eslint:no-sequences if(doSomething(),!! test){} //✗避免
  • 受限制的名称不应该被遮蔽。 eslint:no-shadow-restricted-names let undefined ='value'//✗忌
  • 不允许使用稀疏数组。 eslint:no-sparse-arrays let fruits = ['apple',,'orange'] //✗避免
  • 标签不应该用于 eslint:no-tabs
  • 常规字符串不得包含模板文字占位符。 eslint:no-template-curly-in-string const message ='Hello $ {name}'//✗avoidconst message =`Hello $ {name}`//✓on
  • super() 必须在使用前调用 this eslint:no-this-before-super class Dog extends Animal {constructor(){this.legs = 4 //✗avoid super()}}
  • 只有 一个对象。 eslint:throw'error '//✗avoidthrow new Error('error')//✓确定throw Error no-throw-literal
  • 在行尾不允许有空格。 eslint:no-trailing-spaces
  • 不允许初始化为。 eslint: let name = undefined //✗避免让namename ='value'//✓确定undefined no-undef-init
  • 没有未修改的循环条件。 eslint:no-unmodified-loop-condition for(let i = 0; i <items.length; j ++){...} //✗avoidfor(let i = 0; i <items.length; i ++){...} //✓ok
  • 当存在更简单的替代方案时,没有三元运营商。 eslint:no-unneeded-ternary 让得分= val?val:0 //✗sickletscore = val || 0 //✓好的
  • 后无可达代码 return throw continue,和 语句。 eslint: function doSomething(){return true console.log('never called')//✗avoid}break no-unreachable
  • 块中没有流控制语句。 eslint: try {// ...} catch(e){// ...} finally {return 42 //✗avoid}finally no-unsafe-finally
  • 不能否定关系运算符的左操作数。 eslint:no-unsafe-negation if(!key in obj){} //✗avoidif(!(key in obj)){} //✓ok
  • 避免不必要的使用 和。 eslint: sum.call(null,1,2,3)//✗避免.call() .apply()no-useless-call
  • 避免在对象上使用不必要的计算属性键。 eslint:no-useless-computed-key const user = {['name']:'John Doe'} //✗avoidconst user = {name:'John Doe'} //✓ok
  • 没有必要的构造函数。 eslint:no-useless-constructor class Car {constructor(){//✗avoid}}
  • 没有不必要的逃避。 eslint:no-useless-escape let message ='Hell \ o'//✗避免
  • 不允许将导入,导出和解构分配重命名为相同名称。 eslint:no-useless-rename 从'./config'导入{config as config} //✗从'。/ config'中避免导入{config} //✓确定
  • 在属性之前没有空格。 eslint:no-whitespace-before-property user .name //✗avoiduser.name //✓确定
  • 没有使用 声明。 eslint: with(val){...} //✗避免with no-with
  • 保持对象属性之间换行符的一致性。 eslint:object-property-newline const user = {name:'Jane Doe',年龄:30,用户名:'jdoe86'//✗avoid} const user = {name:'Jane Doe',年龄:30,用户名:'jdoe86'} // ✓okconst user = {name:'Jane Doe',年龄:30,用户名:'jdoe86'} //✓确定
  • 块内无填充。 eslint:padded-blocks if(user){// ✗avoid const name = getName()} if(user){const name = getName()//✓ok}
  • 扩展运算符与其表达式之间没有空格。 eslint:rest-spread-spacing fn(... args)//✗avoidfn(... args)//✓确定
  • 分号后面必须有空格,前面没有空格。 eslint:semi-spacing for(let i = 0; i <items.length; i ++){...} //✗avoidfor(let i = 0; i <items.length; i ++){...} //✓ok
  • 在块之前必须有空格。 eslint:space-before-blocks if(admin){...} //✗avoidif(admin){...} //✓确定
  • 括号内没有空格。 eslint:space-in-parens getName(name)//✗avoidgetName(name)//✓确定
  • 一元运算符之后必须有空格。 eslint:space-unary-ops typeof!admin //✗avoidtypeof!admin //✓好的
  • 在注释中使用空格。 eslint:spaced-comment //评论//✗避免//评论//✓确定/ *评论* / //✗避免/ *评论* / //✓确定
  • 模板字符串中没有间距。 eslint:template-curly-spacing const message =`Hello,$ {name}`//✗avoidconst message =`Hello,$ {name}`//✓ok
  • 使用 检查时。 eslint: if(price === NaN){} //✗avoidif(isNaN(price)){} //✓确定isNaN() NaNuse-isnan
  • typeof 必须与有效字符串进行比较。 eslint:valid-typeof typeof name ==='undefimed'//✗avoidtypeof name ==='undefined'//✓ok
  • 必须立即调用函数表达式(IIFE)。 eslint:wrap-iife const getName = function(){}()//✗避免const getName =(function(){}())//✓okconstgetName =(function(){})()//✓ok
  • 在之前和之后的表达式必须有一个空间。 eslint: yield * increment()//✗avoidyield * increment()//✓确定* yield*yield-star-spacing
  • 避免尤达的条件。 eslint:yoda if(42 ===年龄){} //✗避免(年龄=== 42){} //✓确定
规则 | Rules
分号 | Semicolons 详细
帮助阅读 | Helpful reading 详细
Standard JS

JavaScript 代码规范,自带 linter & 代码自动修正,可以帮助你(及你的团队)节省大量时间