Eslint
规则 | Rules

complexity

限制环复杂性(复杂性)

循环复杂度测量程序源代码中线性独立路径的数量。该规则允许设置圈复杂度阈值。

function a(x) { if (true) { return x; // 1st path } else if (false) { return x+1; // 2nd path } else { return 4; // 3rd path } }

规则细节

规则旨在通过限制程序中允许的圈复杂度来降低代码复杂性。因此,它会在圈复杂度超过配置的阈值(默认值20)时发出警告。

不正确代码的示例最多为2:

/*eslint complexity: ["error", 2]*/ function a(x) { if (true) { return x; } else if (false) { return x+1; } else { return 4; // 3rd path } }

正确代码的例子最多为2:

/*eslint complexity: ["error", 2]*/ function a(x) { if (true) { return x; } else { return 4; } }

选项

或者,您可以指定一个max对象属性:

"complexity": ["error", 2]

相当于

"complexity": ["error", { "max": 2 }]

推荐使用maximum对象属性已弃用。请改用该属性max

何时不使用它

如果您无法确定适合您代码的复杂性限制,则最好禁用此规则。

进一步阅读

相关规则

  • max-depth

  • max-len

  • max-nested-callbacks

  • max-params

  • max-statements

版本

规则在ESLint 0.0.9中引入。

资源