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
|
/**
* @fileoverview Don't allow accidental assignments inside `ok()`,
* and encourage people to use appropriate alternatives
* when using comparisons between 2 values.
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
"use strict";
const operatorToAssertionMap = {
"==": "Assert.equal",
"===": "Assert.strictEqual",
"!=": "Assert.notEqual",
"!==": "Assert.notStrictEqual",
">": "Assert.greater",
"<": "Assert.less",
"<=": "Assert.lessOrEqual",
">=": "Assert.greaterOrEqual",
};
module.exports = {
meta: {
docs: {
url: "https://firefox-source-docs.mozilla.org/code-quality/lint/linters/eslint-plugin-mozilla/rules/no-comparison-or-assignment-inside-ok.html",
},
fixable: "code",
messages: {
assignment:
"Assigning to a variable inside ok() is odd - did you mean to compare the two?",
comparison:
"Use dedicated assertion methods rather than ok(a {{operator}} b).",
},
schema: [],
type: "suggestion",
},
create(context) {
const exprs = new Set(["BinaryExpression", "AssignmentExpression"]);
return {
CallExpression(node) {
if (node.callee.type != "Identifier" || node.callee.name != "ok") {
return;
}
let firstArg = node.arguments[0];
if (!exprs.has(firstArg.type)) {
return;
}
if (firstArg.type == "AssignmentExpression") {
context.report({
node: firstArg,
messageId: "assignment",
});
} else if (
firstArg.type == "BinaryExpression" &&
operatorToAssertionMap.hasOwnProperty(firstArg.operator)
) {
context.report({
node,
messageId: "comparison",
data: { operator: firstArg.operator },
fix: fixer => {
let left = context.sourceCode.getText(firstArg.left);
let right = context.sourceCode.getText(firstArg.right);
return [
fixer.replaceText(firstArg, left + ", " + right),
fixer.replaceText(
node.callee,
operatorToAssertionMap[firstArg.operator]
),
];
},
});
}
},
};
},
};
|