forked from qunitjs/eslint-plugin-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-hooks-from-ancestor-modules.js
More file actions
193 lines (174 loc) · 7.52 KB
/
no-hooks-from-ancestor-modules.js
File metadata and controls
193 lines (174 loc) · 7.52 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
/**
* @fileoverview disallow the use of hooks from ancestor modules
* @author Raymond Cohen
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const utils = require("../utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const NESTABLE_HOOK_NAMES = new Set(["afterEach", "beforeEach"]);
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "problem",
docs: {
description: "disallow the use of hooks from ancestor modules",
category: "Possible Errors",
recommended: false,
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/main/docs/rules/no-hooks-from-ancestor-modules.md",
},
fixable: null,
messages: {
noHooksFromAncestorModules:
"Do not call {{usedHooksIdentifierName}}.{{invokedMethodName}} from an ancestor module.",
},
schema: [],
},
create: function (context) {
/** @type {Array<{callExpression: import('eslint').Rule.Node, description: string, hookIdentifierName?: string | null}>} */
const moduleStack = [];
//----------------------------------------------------------------------
// Helpers
//----------------------------------------------------------------------
/**
* @param {import('eslint').Rule.Node} callExpressionNode
* @returns {boolean}
*/
function isInModuleCallbackBody(callExpressionNode) {
return !!(
callExpressionNode &&
callExpressionNode.parent &&
callExpressionNode.parent.type === "ExpressionStatement" &&
callExpressionNode.parent.parent &&
callExpressionNode.parent.parent.type === "BlockStatement" &&
callExpressionNode.parent.parent.parent &&
["FunctionExpression", "ArrowFunctionExpression"].includes(
callExpressionNode.parent.parent.parent.type,
) &&
callExpressionNode.parent.parent.parent.parent &&
callExpressionNode.parent.parent.parent.parent.type ===
"CallExpression" &&
utils.isModule(
callExpressionNode.parent.parent.parent.parent.callee,
)
);
}
/**
* @param {import('eslint').Rule.Node} node
* @returns {boolean}
*/
function isHookInvocation(node) {
return (
node.type === "CallExpression" &&
node.callee.type === "MemberExpression" &&
node.callee.object.type === "Identifier" &&
node.callee.property.type === "Identifier" &&
NESTABLE_HOOK_NAMES.has(node.callee.property.name) &&
isInModuleCallbackBody(node)
);
}
/**
* @param {import('estree').Node[]} args
* @returns {import('estree').Node | undefined}
*/
function getCallbackArg(args) {
// Callback can be either args[1] or args[2]
// https://api.qunitjs.com/QUnit/module/
return args
.slice(1, 3)
.find((arg) =>
["FunctionExpression", "ArrowFunctionExpression"].includes(
arg.type,
),
);
}
/**
* @param {import('estree').Node[]} params
* @returns {import('estree').Node | undefined}
*/
function getHooksIdentifierFromParams(params) {
// In TypeScript, `this` can be passed as the first function parameter to add a type to it,
// and we want to ignore that parameter since we're looking for the `hooks` variable.
return params.find(
(p) => p.type === "Identifier" && p.name !== "this",
);
}
//----------------------------------------------------------------------
// Public
//----------------------------------------------------------------------
return {
// eslint-disable-next-line complexity
CallExpression: function (node) {
if (utils.isModule(node.callee)) {
if (node.arguments.length === 0) {
return;
}
const arg = node.arguments[0];
const description =
arg.type === "Literal" && typeof arg.value === "string"
? arg.value
: /* istanbul ignore next: deprecated code paths only followed by old eslint versions */
(
context.sourceCode ?? context.getSourceCode()
).getText(arg);
/** @type {{callExpression: import('eslint').Rule.Node, description: string, hookIdentifierName?: string | null}} */
const moduleStackInfo = {
callExpression: node,
description: description,
};
const callback = getCallbackArg(node.arguments);
if (
!callback ||
(callback.type !== "FunctionExpression" &&
callback.type !== "ArrowFunctionExpression")
) {
return;
}
const hooksParam = getHooksIdentifierFromParams(
callback.params,
);
moduleStackInfo.hookIdentifierName =
hooksParam && hooksParam.type === "Identifier"
? hooksParam.name
: null;
moduleStack.push(moduleStackInfo);
} else if (isHookInvocation(node)) {
const containingModuleInfo =
moduleStack[moduleStack.length - 1];
const expectedHooksIdentifierName =
containingModuleInfo.hookIdentifierName;
if (
node.callee.type !== "MemberExpression" ||
node.callee.object.type !== "Identifier" ||
node.callee.property.type !== "Identifier"
) {
return;
}
const usedHooksIdentifierName = node.callee.object.name;
const invokedMethodName = node.callee.property.name;
if (
expectedHooksIdentifierName !== usedHooksIdentifierName
) {
context.report({
node: node.callee,
messageId: "noHooksFromAncestorModules",
data: {
invokedMethodName,
usedHooksIdentifierName,
},
});
}
}
},
"CallExpression:exit": function (node) {
if (utils.isModule(node.callee)) {
moduleStack.pop();
}
},
};
},
};