@dineroregnskab/eslint-plugin-custom-rules 4.2.0 → 4.3.0

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/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "@dineroregnskab/eslint-plugin-custom-rules",
3
- "version": "4.2.0",
3
+ "version": "4.3.0",
4
4
  "description": "ESLint plugin with custom rules for Dinero Regnskab",
5
5
  "main": "eslint-plugin-custom-rules.js",
6
6
  "scripts": {
@@ -14,19 +14,29 @@ module.exports = {
14
14
  },
15
15
 
16
16
  create(context) {
17
- let describeStack = [];
17
+ // Map each describe/context CallExpression node -> whether its beforeEach contains feature toggles
18
+ const describeFeatureMap = new WeakMap();
19
+
20
+ function findNearestDescribe(node) {
21
+ let current = node && node.parent;
22
+ while (current) {
23
+ if (
24
+ current.type === 'CallExpression' &&
25
+ current.callee &&
26
+ (current.callee.name === 'describe' || current.callee.name === 'context')
27
+ ) {
28
+ return current;
29
+ }
30
+ current = current.parent;
31
+ }
32
+ return null;
33
+ }
18
34
 
19
35
  return {
20
- 'CallExpression[callee.name="describe"], CallExpression[callee.name="context"]'() {
21
- describeStack.push({
22
- beforeEachHasFeatureToggles: false,
23
- });
24
- },
25
-
26
36
  'CallExpression[callee.name="beforeEach"]'(node) {
27
- if (describeStack.length === 0) return;
37
+ const describeNode = findNearestDescribe(node);
38
+ if (!describeNode) return;
28
39
 
29
- const currentLevel = describeStack[describeStack.length - 1];
30
40
  const callback = node.arguments[0];
31
41
  if (!callback) return;
32
42
 
@@ -61,24 +71,33 @@ module.exports = {
61
71
  });
62
72
 
63
73
  if (hasFeatureToggles) {
64
- currentLevel.beforeEachHasFeatureToggles = true;
74
+ describeFeatureMap.set(describeNode, true);
65
75
  }
66
76
  },
67
77
 
68
78
  'CallExpression[callee.name="it"]'(node) {
69
- if (describeStack.length === 0) return;
70
-
71
- const hasFeatureTogglesInAnyAncestor = describeStack.some(
72
- (level) => level.beforeEachHasFeatureToggles
73
- );
79
+ // Walk ancestors to see if any describe/context has feature toggles
80
+ let current = node.parent;
81
+ let hasFeatureTogglesInAnyAncestor = false;
82
+ while (current) {
83
+ if (
84
+ current.type === 'CallExpression' &&
85
+ current.callee &&
86
+ (current.callee.name === 'describe' || current.callee.name === 'context')
87
+ ) {
88
+ if (describeFeatureMap.get(current)) {
89
+ hasFeatureTogglesInAnyAncestor = true;
90
+ break;
91
+ }
92
+ }
93
+ current = current.parent;
94
+ }
74
95
  if (!hasFeatureTogglesInAnyAncestor) return;
75
96
 
76
-
77
- const callback = node.arguments[1];
97
+ const callback = node.arguments[1] || node.arguments[0];
78
98
  if (!callback) return;
79
99
 
80
100
  let hasAwaitFeatureReady = false;
81
-
82
101
  walkNode(callback, (child) => {
83
102
  if (
84
103
  child.type === 'CallExpression' &&
@@ -101,10 +120,6 @@ module.exports = {
101
120
  });
102
121
  }
103
122
  },
104
-
105
- 'CallExpression[callee.name="describe"]:exit, CallExpression[callee.name="context"]:exit'() {
106
- describeStack.pop();
107
- },
108
123
  };
109
124
  },
110
125
  };