eslint-plugin-absolute 0.0.1 → 0.0.2

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/dist/index.js CHANGED
@@ -1553,7 +1553,7 @@ var max_depth_extended_default = {
1553
1553
  meta: {
1554
1554
  type: "suggestion",
1555
1555
  docs: {
1556
- description: "disallow too many nested blocks except when the block only contains an early return",
1556
+ description: "disallow too many nested blocks except when the block only contains an early exit (return or throw)",
1557
1557
  category: "Best Practices",
1558
1558
  recommended: false
1559
1559
  },
@@ -1575,14 +1575,14 @@ var max_depth_extended_default = {
1575
1575
  }
1576
1576
  return ancestors;
1577
1577
  }
1578
- function isEarlyReturnBlock(node) {
1579
- return node.body.length === 1 && node.body[0].type === "ReturnStatement";
1578
+ function isEarlyExitBlock(node) {
1579
+ return node.body.length === 1 && (node.body[0].type === "ReturnStatement" || node.body[0].type === "ThrowStatement");
1580
1580
  }
1581
1581
  function checkDepth(node, depth) {
1582
1582
  if (depth > maxDepth) {
1583
1583
  context.report({
1584
1584
  node,
1585
- message: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}} or an early return.",
1585
+ message: "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}} or an early exit.",
1586
1586
  data: { depth, maxDepth }
1587
1587
  });
1588
1588
  }
@@ -1603,7 +1603,7 @@ var max_depth_extended_default = {
1603
1603
  if (parent && (parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") && node === parent.body) {
1604
1604
  return;
1605
1605
  }
1606
- if (isEarlyReturnBlock(node)) {
1606
+ if (isEarlyExitBlock(node)) {
1607
1607
  return;
1608
1608
  }
1609
1609
  if (functionStack.length > 0) {
@@ -1617,7 +1617,7 @@ var max_depth_extended_default = {
1617
1617
  if (parent && (parent.type === "FunctionDeclaration" || parent.type === "FunctionExpression" || parent.type === "ArrowFunctionExpression") && node === parent.body) {
1618
1618
  return;
1619
1619
  }
1620
- if (isEarlyReturnBlock(node)) {
1620
+ if (isEarlyExitBlock(node)) {
1621
1621
  return;
1622
1622
  }
1623
1623
  if (functionStack.length > 0) {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "eslint-plugin-absolute",
3
- "version": "0.0.1",
3
+ "version": "0.0.2",
4
4
  "description": "ESLint plugin for AbsoluteJS",
5
5
  "repository": {
6
6
  "type": "git",
@@ -3,7 +3,7 @@ export default {
3
3
  type: "suggestion",
4
4
  docs: {
5
5
  description:
6
- "disallow too many nested blocks except when the block only contains an early return",
6
+ "disallow too many nested blocks except when the block only contains an early exit (return or throw)",
7
7
  category: "Best Practices",
8
8
  recommended: false
9
9
  },
@@ -30,11 +30,12 @@ export default {
30
30
  return ancestors;
31
31
  }
32
32
 
33
- // Check if a block only contains a single return statement.
34
- function isEarlyReturnBlock(node) {
33
+ // Check if a block only contains a single early exit: return or throw.
34
+ function isEarlyExitBlock(node) {
35
35
  return (
36
36
  node.body.length === 1 &&
37
- node.body[0].type === "ReturnStatement"
37
+ (node.body[0].type === "ReturnStatement" ||
38
+ node.body[0].type === "ThrowStatement")
38
39
  );
39
40
  }
40
41
 
@@ -44,7 +45,7 @@ export default {
44
45
  context.report({
45
46
  node,
46
47
  message:
47
- "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}} or an early return.",
48
+ "Blocks are nested too deeply ({{depth}}). Maximum allowed is {{maxDepth}} or an early exit.",
48
49
  data: { depth, maxDepth }
49
50
  });
50
51
  }
@@ -60,9 +61,11 @@ export default {
60
61
  ArrowFunctionExpression() {
61
62
  functionStack.push(0);
62
63
  },
64
+
63
65
  BlockStatement(node) {
64
66
  const ancestors = getAncestors(node);
65
67
  const parent = ancestors[0];
68
+
66
69
  // Do not count if this block is the body of a function.
67
70
  if (
68
71
  parent &&
@@ -73,15 +76,18 @@ export default {
73
76
  ) {
74
77
  return;
75
78
  }
76
- // Skip blocks that only have an early return.
77
- if (isEarlyReturnBlock(node)) {
79
+
80
+ // Skip blocks that only have an early exit.
81
+ if (isEarlyExitBlock(node)) {
78
82
  return;
79
83
  }
84
+
80
85
  if (functionStack.length > 0) {
81
86
  functionStack[functionStack.length - 1]++;
82
87
  checkDepth(node, functionStack[functionStack.length - 1]);
83
88
  }
84
89
  },
90
+
85
91
  "BlockStatement:exit"(node) {
86
92
  const ancestors = getAncestors(node);
87
93
  const parent = ancestors[0];
@@ -94,13 +100,16 @@ export default {
94
100
  ) {
95
101
  return;
96
102
  }
97
- if (isEarlyReturnBlock(node)) {
103
+
104
+ if (isEarlyExitBlock(node)) {
98
105
  return;
99
106
  }
107
+
100
108
  if (functionStack.length > 0) {
101
109
  functionStack[functionStack.length - 1]--;
102
110
  }
103
111
  },
112
+
104
113
  "FunctionDeclaration:exit"() {
105
114
  functionStack.pop();
106
115
  },