forked from eslint/eslint
-
Notifications
You must be signed in to change notification settings - Fork 0
/
eqeqeq.js
46 lines (37 loc) · 1.47 KB
/
eqeqeq.js
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
/**
* @fileoverview Rule to flag statements that use != and == instead of !== and ===
* @author Nicholas C. Zakas
*/
//------------------------------------------------------------------------------
// Rule Definition
//------------------------------------------------------------------------------
module.exports = function(context) {
"use strict";
function isTypeOf(node) {
return [node.left, node.right].some(function(node) {
return node.type === "UnaryExpression" && node.operator === "typeof";
});
}
function bothAreSameTypeLiterals(node) {
return node.left.type === "Literal" && node.right.type === "Literal" &&
typeof node.left.value === typeof node.right.value;
}
function isNullCheck(node) {
return (node.right.type === "Literal" && node.right.value === null) ||
(node.left.type === "Literal" && node.left.value === null);
}
return {
"BinaryExpression": function(node) {
var operator = node.operator;
if (context.options[0] === "smart" && (isTypeOf(node) ||
bothAreSameTypeLiterals(node)) || isNullCheck(node)) {
return;
}
if (operator === "==") {
context.report(node, "Expected '===' and instead saw '=='.");
} else if (operator === "!=") {
context.report(node, "Expected '!==' and instead saw '!='.");
}
}
};
};