From f17c3c371789ffa84f0cda57101e8193899adbe6 Mon Sep 17 00:00:00 2001 From: Milos Djermanovic Date: Fri, 15 Jan 2021 21:13:22 +0100 Subject: [PATCH] Update: check logical assignment operators in the complexity rule (#13979) --- docs/rules/complexity.md | 9 +++++++++ lib/rules/complexity.js | 8 +++++++- tests/lib/rules/complexity.js | 18 +++++++++++++++++- 3 files changed, 33 insertions(+), 2 deletions(-) diff --git a/docs/rules/complexity.md b/docs/rules/complexity.md index 3a665491c39..a57b4366c1c 100644 --- a/docs/rules/complexity.md +++ b/docs/rules/complexity.md @@ -32,6 +32,11 @@ function a(x) { return 4; // 3rd path } } + +function b() { + foo ||= 1; + bar &&= 1; +} ``` Examples of **correct** code for a maximum of 2: @@ -46,6 +51,10 @@ function a(x) { return 4; } } + +function b() { + foo ||= 1; +} ``` ## Options diff --git a/lib/rules/complexity.js b/lib/rules/complexity.js index 7fc8bf9bc2e..5d62c6ff44b 100644 --- a/lib/rules/complexity.js +++ b/lib/rules/complexity.js @@ -153,7 +153,13 @@ module.exports = { IfStatement: increaseComplexity, SwitchCase: increaseSwitchComplexity, WhileStatement: increaseComplexity, - DoWhileStatement: increaseComplexity + DoWhileStatement: increaseComplexity, + + AssignmentExpression(node) { + if (astUtils.isLogicalAssignmentOperator(node.operator)) { + increaseComplexity(); + } + } }; } diff --git a/tests/lib/rules/complexity.js b/tests/lib/rules/complexity.js index b49ccfc5077..cf7605baffa 100644 --- a/tests/lib/rules/complexity.js +++ b/tests/lib/rules/complexity.js @@ -48,7 +48,7 @@ function makeError(name, complexity, max) { }; } -const ruleTester = new RuleTester(); +const ruleTester = new RuleTester({ parserOptions: { ecmaVersion: 2021 } }); ruleTester.run("complexity", rule, { valid: [ @@ -66,6 +66,18 @@ ruleTester.run("complexity", rule, { { code: "function a(x) {return x === 4 ? 3 : (x === 3 ? 2 : 1);}", options: [3] }, { code: "function a(x) {return x || 4;}", options: [2] }, { code: "function a(x) {x && 4;}", options: [2] }, + { code: "function a(x) {x ?? 4;}", options: [2] }, + { code: "function a(x) {x ||= 4;}", options: [2] }, + { code: "function a(x) {x &&= 4;}", options: [2] }, + { code: "function a(x) {x ??= 4;}", options: [2] }, + { code: "function a(x) {x = 4;}", options: [1] }, + { code: "function a(x) {x |= 4;}", options: [1] }, + { code: "function a(x) {x &= 4;}", options: [1] }, + { code: "function a(x) {x += 4;}", options: [1] }, + { code: "function a(x) {x >>= 4;}", options: [1] }, + { code: "function a(x) {x >>>= 4;}", options: [1] }, + { code: "function a(x) {x == 4;}", options: [1] }, + { code: "function a(x) {x === 4;}", options: [1] }, { code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: 3;}}", options: [3] }, { code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: if(x == 'foo') {5;};}}", options: [4] }, { code: "function a(x) {while(true) {'foo';}}", options: [2] }, @@ -95,6 +107,10 @@ ruleTester.run("complexity", rule, { { code: "function a(x) {return x === 4 ? 3 : (x === 3 ? 2 : 1);}", options: [2], errors: 1 }, { code: "function a(x) {return x || 4;}", options: [1], errors: 1 }, { code: "function a(x) {x && 4;}", options: [1], errors: 1 }, + { code: "function a(x) {x ?? 4;}", options: [1], errors: 1 }, + { code: "function a(x) {x ||= 4;}", options: [1], errors: 1 }, + { code: "function a(x) {x &&= 4;}", options: [1], errors: 1 }, + { code: "function a(x) {x ??= 4;}", options: [1], errors: 1 }, { code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: 3;}}", options: [2], errors: 1 }, { code: "function a(x) {switch(x){case 1: 1; break; case 2: 2; break; default: if(x == 'foo') {5;};}}", options: [3], errors: 1 }, { code: "function a(x) {while(true) {'foo';}}", options: [1], errors: 1 },