当前位置: 首页 > 工具软件 > fcode.js > 使用案例 >

JS代码质量SonarQube常见问题解决

田永春
2023-12-01

**

JS代码质量SonarQube常见问题及解决思路

**
示例问题:
“i” hides or potentially hides a variable declared in an outer scope at line 10.
Overiding a variable dedlred in an outer scope can strongliy impact th readebliy, and therefore the mantainability,ofa piece fcode. furthr itcoud lad maintines to int oduce bugs because they think they’re using one variable but are really using another.

1、 设置了全局变量i之后,下边所有方法中,例如:init方法参数名不能为i,方法内也不可声明局部变量名称为i,(例如 init方法中var i;),即方法参数名和方法内局部变量名称都不能和全局变量名称一样;

Add curly braces around the nested statement(s) in this “if” block.
2、 if判断格式不标准,
解决方法,if(判断条件){内容};

Refactor this function to reduce itsCognitive Complexity from 49 to the15 allowed.
3、 方法中if判断逻辑太多,逻辑太复杂,if嵌套出现3层以上
解决方法,将单独的,完整的if判断方法拿出来,写成一个独立的函数方法,去调用;
注意:单独抽出的方法中参数传递要准确,有些方法返回值也要准确。

Refactor this code to not nest morethan 3 if/for/while/switch/try statements.
4、for循环中有if判断,
解决方法:根据需求,if判断结束后,是否要终止循环

Add the missing “else” clause.
5、 if-else 判断问题
解决方法:if-else 判断一定要以else结尾,补全else{}即可。

Reduce the number of conditional operators (4) used in the expression (maximum allowed 3).
6、 if()判断中条件超过3个了
解决方法:拆分if判断,if判断条件最多不超过3个

Add a nested comment explaining why this function is empty or complete the implementation.
7、 空的方法,例如 onClick:function(){},onSelect:function(){},没有用到的方法要注释掉或者直接不写。

 类似资料: