Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Update: check logical assignment operators in the complexity rule (#1…
  • Loading branch information
mdjermanovic committed Jan 15, 2021
1 parent 672deb0 commit f17c3c3
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 2 deletions.
9 changes: 9 additions & 0 deletions docs/rules/complexity.md
Expand Up @@ -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:
Expand All @@ -46,6 +51,10 @@ function a(x) {
return 4;
}
}

function b() {
foo ||= 1;
}
```

## Options
Expand Down
8 changes: 7 additions & 1 deletion lib/rules/complexity.js
Expand Up @@ -153,7 +153,13 @@ module.exports = {
IfStatement: increaseComplexity,
SwitchCase: increaseSwitchComplexity,
WhileStatement: increaseComplexity,
DoWhileStatement: increaseComplexity
DoWhileStatement: increaseComplexity,

AssignmentExpression(node) {
if (astUtils.isLogicalAssignmentOperator(node.operator)) {
increaseComplexity();
}
}
};

}
Expand Down
18 changes: 17 additions & 1 deletion tests/lib/rules/complexity.js
Expand Up @@ -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: [
Expand All @@ -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] },
Expand Down Expand Up @@ -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 },
Expand Down

0 comments on commit f17c3c3

Please sign in to comment.