forked from qunitjs/eslint-plugin-qunit
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathno-compare-relation-boolean.js
More file actions
204 lines (185 loc) · 7.16 KB
/
no-compare-relation-boolean.js
File metadata and controls
204 lines (185 loc) · 7.16 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
194
195
196
197
198
199
200
201
202
203
204
/**
* @fileoverview forbid comparing relational expression to boolean in assertions
* @author Kevin Partington
*/
"use strict";
//------------------------------------------------------------------------------
// Requirements
//------------------------------------------------------------------------------
const assert = require("node:assert"),
utils = require("../utils");
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
/** @type {import('eslint').Rule.RuleModule} */
module.exports = {
meta: {
type: "suggestion",
docs: {
description:
"disallow comparing relational expressions to booleans in assertions",
category: "Best Practices",
url: "https://github.com/platinumazure/eslint-plugin-qunit/blob/main/docs/rules/no-compare-relation-boolean.md",
},
fixable: "code",
messages: {
redundantComparison:
"Redundant comparison of relational expression to boolean literal.",
},
schema: [],
},
create: function (context) {
/** @type {Array<{assertContextVar: string | null}>} */
const testStack = [],
RELATIONAL_OPS = new Set([
"==",
"!=",
"===",
"!==",
"<",
"<=",
">",
">=",
"in",
"instanceof",
]);
/**
* @param {import('estree').Node} calleeNode
* @returns {boolean}
*/
function shouldCheckArguments(calleeNode) {
assert.ok(testStack.length);
const assertContextVar =
testStack[testStack.length - 1].assertContextVar;
if (!assertContextVar) {
return false;
}
return (
utils.isAssertion(calleeNode, assertContextVar) &&
utils.isComparativeAssertion(calleeNode, assertContextVar)
);
}
/**
* @param {import('estree').Node} a
* @param {import('estree').Node} b
* @returns {0 | 1 | -1}
*/
function sortLiteralFirst(a, b) {
if (a.type === "Literal" && b.type !== "Literal") {
return -1; // Literal is first and should remain first
}
if (a.type !== "Literal" && b.type === "Literal") {
return 1; // Literal is second and should be first
}
return 0;
}
/**
* @param {import('estree').CallExpression} callExprNode
* @param {import('estree').Literal} literalNode
* @param {import('estree').BinaryExpression} binaryExprNode
*/
function checkAndReport(callExprNode, literalNode, binaryExprNode) {
if (
binaryExprNode.type === "BinaryExpression" &&
RELATIONAL_OPS.has(binaryExprNode.operator) &&
literalNode.type === "Literal" &&
typeof literalNode.value === "boolean"
) {
context.report({
node: callExprNode,
messageId: "redundantComparison",
fix(fixer) {
/* istanbul ignore next: deprecated code paths only followed by old eslint versions */
const sourceCode =
context.sourceCode ?? context.getSourceCode();
/* istanbul ignore next */
if (callExprNode.type !== "CallExpression") {
return null;
}
/* istanbul ignore next */
if (callExprNode.callee.type !== "MemberExpression") {
return null;
}
/* istanbul ignore next */
if (callExprNode.callee.object.type !== "Identifier") {
return null;
}
/* istanbul ignore next */
if (
callExprNode.callee.property.type !== "Identifier"
) {
return null;
}
const assertionVariableName =
callExprNode.callee.object.name;
// Decide which assertion function to use based on how many negations we have.
let countNegations = 0;
if (
callExprNode.callee.property.name.startsWith("not")
) {
countNegations++;
}
if (!literalNode.value) {
countNegations++;
}
const newAssertionFunctionName =
countNegations % 2 === 0 ? "ok" : "notOk";
const newArgsTextArray = [
binaryExprNode,
...callExprNode.arguments.slice(2),
].map((arg) => sourceCode.getText(arg));
const newArgsTextJoined = newArgsTextArray.join(", ");
return fixer.replaceText(
callExprNode,
`${assertionVariableName}.${newAssertionFunctionName}(${newArgsTextJoined})`,
);
},
});
}
}
/**
* @param {import('estree').CallExpression} callExprNode
*/
function checkAssertArguments(callExprNode) {
if (callExprNode.type !== "CallExpression") {
return;
}
const args = [...callExprNode.arguments];
if (args.length < 2) {
return;
}
const firstTwoArgsSorted = args.slice(0, 2).sort(sortLiteralFirst);
if (
firstTwoArgsSorted[0].type === "Literal" &&
firstTwoArgsSorted[1].type === "BinaryExpression"
) {
checkAndReport(
callExprNode,
firstTwoArgsSorted[0],
firstTwoArgsSorted[1],
);
}
}
return {
CallExpression: function (node) {
if (utils.isTest(node.callee)) {
testStack.push({
assertContextVar: utils.getAssertContextNameForTest(
node.arguments,
),
});
} else if (
testStack.length > 0 &&
shouldCheckArguments(node.callee)
) {
checkAssertArguments(node);
}
},
"CallExpression:exit": function (node) {
if (utils.isTest(node.callee)) {
testStack.pop();
}
},
};
},
};