forked from qunitjs/eslint-plugin-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-assert-equal-boolean.js
More file actions
175 lines (158 loc) · 6.26 KB
/
no-assert-equal-boolean.js
File metadata and controls
175 lines (158 loc) · 6.26 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
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("node:assert"),
utils = require("../utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
const EQUALITY_ASSERTIONS = new Set(["equal", "deepEqual", "strictEqual"]);
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description: "require use of boolean assertions",
category: "Best Practices",
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/main/docs/rules/no-assert-equal-boolean.md",
},
fixable: "code",
messages: {
useAssertTrueOrFalse:
"Use `assert.true or `assert.false` for boolean assertions.",
},
schema: [],
},
create: function (context) {
// Declare a test stack in case of nested test cases (not currently supported by QUnit).
/** @type {Array<{assertVar: string | null}>} */
const testStack = [];
function getCurrentAssertContextVariable() {
assert(testStack.length, "Test stack should not be empty");
return testStack[testStack.length - 1].assertVar;
}
/**
* Check for something like `equal(...)` without assert parameter.
* @param {import('estree').Node} calleeNode
* @returns {boolean}
*/
function isGlobalEqualityAssertion(calleeNode) {
return (
calleeNode &&
calleeNode.type === "Identifier" &&
EQUALITY_ASSERTIONS.has(calleeNode.name)
);
}
/**
* Check for something like `assert.equal(...)`.
* @param {import('estree').Node} calleeNode
* @returns {boolean}
*/
function isAssertEquality(calleeNode) {
return (
calleeNode &&
calleeNode.type === "MemberExpression" &&
calleeNode.property.type === "Identifier" &&
EQUALITY_ASSERTIONS.has(calleeNode.property.name) &&
calleeNode.object.type === "Identifier" &&
calleeNode.object.name === getCurrentAssertContextVariable()
);
}
/**
* Check for something like `equal(...)` or `assert.equal(...)`.
* @param {import('estree').Node} calleeNode
* @returns {boolean}
*/
function isEqualityAssertion(calleeNode) {
return (
isGlobalEqualityAssertion(calleeNode) ||
isAssertEquality(calleeNode)
);
}
/**
* Finds the first boolean argument of a CallExpression if one exists.
* @param {import('estree').CallExpression} node
* @returns {import('estree').Node | undefined}
*/
function getBooleanArgument(node) {
if (node.type !== "CallExpression" || node.arguments.length < 2) {
return undefined; // eslint-disable-line unicorn/no-useless-undefined
}
return [node.arguments[0], node.arguments[1]].find(
(arg) =>
arg.type === "Literal" &&
(arg.value === true || arg.value === false),
);
}
/**
* @param {import('estree').CallExpression} node
*/
function reportError(node) {
context.report({
node: node,
messageId: "useAssertTrueOrFalse",
fix(fixer) {
const booleanArgument = getBooleanArgument(node);
if (
!booleanArgument ||
booleanArgument.type !== "Literal"
) {
return null;
}
const newAssertionFunctionName = booleanArgument.value
? "true"
: "false";
/* istanbul ignore next: deprecated code paths only followed by old eslint versions */
const sourceCode =
context.sourceCode ?? context.getSourceCode();
if (node.type !== "CallExpression") {
return null;
}
const newArgsTextArray = node.arguments
.filter((arg) => arg !== booleanArgument)
.map((arg) => sourceCode.getText(arg));
const newArgsTextJoined = newArgsTextArray.join(", ");
const assertVariablePrefix =
node.callee.type === "Identifier"
? ""
: `${getCurrentAssertContextVariable()}.`;
return fixer.replaceText(
node,
`${assertVariablePrefix}${newAssertionFunctionName}(${newArgsTextJoined})`,
);
},
});
}
return {
CallExpression: function (node) {
/* istanbul ignore else: correctly does nothing */
if (
utils.isTest(node.callee) ||
utils.isAsyncTest(node.callee)
) {
testStack.push({
assertVar: utils.getAssertContextNameForTest(
node.arguments,
),
});
} else if (
testStack.length > 0 &&
isEqualityAssertion(node.callee) &&
getBooleanArgument(node)
) {
reportError(node);
}
},
"CallExpression:exit": function (node) {
/* istanbul ignore else: correctly does nothing */
if (
utils.isTest(node.callee) ||
utils.isAsyncTest(node.callee)
) {
testStack.pop();
}
},
};
},
};