【整理】阿里代码规范错误

1,235 阅读2分钟

1、imported multiple times

Rule: import/no-duplicates

import 引入不要多次引入同一模块

如上由于多次引用react模块,导致报多次引用的错误

将引用并为一处,即可消去报错

参考链接:blog.csdn.net/LiuGuangXin…

2、Expected 1 empty line after import statement not followed by another import

Rule: import/newline-after-import

import语句后的空行后面没有另一个import。(导入/导入后换行)

回车换行一下就解决了

参考链接:blog.csdn.net/weixin_4417…

3、Unexpected if as the only statement in an else block.

Rule: no-lonely-if

该规则禁止 if 语句作为唯一语句出现在 else 语句块中

参考链接:eslint.cn/docs/rules/…

4、Assignment to property of function parameter 'values'

Rule: no-param-reassign

当需要使用修改过后的函数参数时,将函数参数复制一份进行修改使用。

参考链接:www.cnblogs.com/wangjiaosho…

5、Expected exception block, space or tab after '//' in comment.

Rule: spaced-comment

根据提示可以知道通过在“//”符号后面打空格或者是按Tab键,即可解决此问题

参考链接:blog.csdn.net/maxle/artic…

6、Expected { after 'if' condition.

Rule: curly

if 后面要加括号

参考链接:blog.csdn.net/weixin_4922…

7、Expected no linebreak before this statement

Rule: nonblock-statement-body-position

要求所有的单行 if 语句直接出现在条件后面,不加换行符,如:if (foo) bar();

这条规则的目的是为单行语句执行一个一致的位置。

请注意,这条规则并不强制要求使用一般的单行语句。如果你想禁止单行语句,请使用 curly 规则代替。

参考链接:zh-hans.eslint.org/docs/latest…

8、Operands of '+' operation must either be both strings or both numbers. Consider using a template literal.

Rule: @typescript-eslint/restrict-plus-operands

“+”运算的操作数必须同时为字符串或数字,Typescript不会隐式地将数字转换为string,因此应该先转换为string,然后再与string进行连接

修改如下:

参考链接:

restrict-plus-operands | typescript-eslint

cloud.tencent.com/developer/a…

9、Unexpected string concatenation.

Rule: prefer-template

错误原因:ESLint推荐用ES6方法来拼接字符串,而不能使用加号。 解决办法:拼接字符串使用形如:

`字符串字符串字符串${变量名}字符串字符串字符串${返回字符串的方法}字符串字符串`1

的写法。

参考链接:blog.csdn.net/nju_zjy/art…

10、Unnecessary return statement.

Rule: no-useless-return

不必要的return语句,没有无用的回报

只需要在return后面添加上 false 即可解决这个问题

参考链接:www.jianshu.com/p/dd465fe2b…

11、Expected property shorthand.

Rule: object-shorthand

该问题是语法检查报错,见链接方法

参考链接:blog.csdn.net/qq_37432929…

12、Array.prototype.map() expects a value to be returned at the end of arrow function.

Rule: array-callback-return

参考链接:blog.csdn.net/sinat_37186…