Upgrade ESLint
This pulls in version 8, which covers numerous breaking changes. Most
notably, the `CLIEngine` class has been removed, for which we should now
use `ESLint`.
R=jacktfranklin@chromium.org
Bug: none
Change-Id: I3b64600e690f073d4db19fb34539db7200f3b561
Reviewed-on: https://chromium-review.googlesource.com/c/devtools/devtools-frontend/+/3268298
Reviewed-by: Jack Franklin <jacktfranklin@chromium.org>
Commit-Queue: Tim van der Lippe <tvanderlippe@chromium.org>
diff --git a/node_modules/eslint/lib/rules/complexity.js b/node_modules/eslint/lib/rules/complexity.js
index 116c8ad..b833aaf 100644
--- a/node_modules/eslint/lib/rules/complexity.js
+++ b/node_modules/eslint/lib/rules/complexity.js
@@ -23,7 +23,6 @@
docs: {
description: "enforce a maximum cyclomatic complexity allowed in a program",
- category: "Best Practices",
recommended: false,
url: "https://eslint.org/docs/rules/complexity"
},
@@ -75,60 +74,16 @@
// Helpers
//--------------------------------------------------------------------------
- // Using a stack to store complexity (handling nested functions)
- const fns = [];
+ // Using a stack to store complexity per code path
+ const complexities = [];
/**
- * When parsing a new function, store it in our function stack
- * @returns {void}
- * @private
- */
- function startFunction() {
- fns.push(1);
- }
-
- /**
- * Evaluate the node at the end of function
- * @param {ASTNode} node node to evaluate
- * @returns {void}
- * @private
- */
- function endFunction(node) {
- const name = upperCaseFirst(astUtils.getFunctionNameWithKind(node));
- const complexity = fns.pop();
-
- if (complexity > THRESHOLD) {
- context.report({
- node,
- messageId: "complex",
- data: { name, complexity, max: THRESHOLD }
- });
- }
- }
-
- /**
- * Increase the complexity of the function in context
+ * Increase the complexity of the code path in context
* @returns {void}
* @private
*/
function increaseComplexity() {
- if (fns.length) {
- fns[fns.length - 1]++;
- }
- }
-
- /**
- * Increase the switch complexity in context
- * @param {ASTNode} node node to evaluate
- * @returns {void}
- * @private
- */
- function increaseSwitchComplexity(node) {
-
- // Avoiding `default`
- if (node.test) {
- increaseComplexity();
- }
+ complexities[complexities.length - 1]++;
}
//--------------------------------------------------------------------------
@@ -136,13 +91,14 @@
//--------------------------------------------------------------------------
return {
- FunctionDeclaration: startFunction,
- FunctionExpression: startFunction,
- ArrowFunctionExpression: startFunction,
- "FunctionDeclaration:exit": endFunction,
- "FunctionExpression:exit": endFunction,
- "ArrowFunctionExpression:exit": endFunction,
+ onCodePathStart() {
+
+ // The initial complexity is 1, representing one execution path in the CodePath
+ complexities.push(1);
+ },
+
+ // Each branching in the code adds 1 to the complexity
CatchClause: increaseComplexity,
ConditionalExpression: increaseComplexity,
LogicalExpression: increaseComplexity,
@@ -150,14 +106,49 @@
ForInStatement: increaseComplexity,
ForOfStatement: increaseComplexity,
IfStatement: increaseComplexity,
- SwitchCase: increaseSwitchComplexity,
WhileStatement: increaseComplexity,
DoWhileStatement: increaseComplexity,
+ // Avoid `default`
+ "SwitchCase[test]": increaseComplexity,
+
+ // Logical assignment operators have short-circuiting behavior
AssignmentExpression(node) {
if (astUtils.isLogicalAssignmentOperator(node.operator)) {
increaseComplexity();
}
+ },
+
+ onCodePathEnd(codePath, node) {
+ const complexity = complexities.pop();
+
+ /*
+ * This rule only evaluates complexity of functions, so "program" is excluded.
+ * Class field initializers are implicit functions. Therefore, they shouldn't contribute
+ * to the enclosing function's complexity, but their own complexity should be evaluated.
+ */
+ if (
+ codePath.origin !== "function" &&
+ codePath.origin !== "class-field-initializer"
+ ) {
+ return;
+ }
+
+ if (complexity > THRESHOLD) {
+ const name = codePath.origin === "class-field-initializer"
+ ? "class field initializer"
+ : astUtils.getFunctionNameWithKind(node);
+
+ context.report({
+ node,
+ messageId: "complex",
+ data: {
+ name: upperCaseFirst(name),
+ complexity,
+ max: THRESHOLD
+ }
+ });
+ }
}
};