UNPKG

1.25 kBJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag statements that use != and == instead of !== and ===
3 * @author Nicholas C. Zakas
4 */
5
6//------------------------------------------------------------------------------
7// Rule Definition
8//------------------------------------------------------------------------------
9
10module.exports = function(context) {
11
12 "use strict";
13
14 function isTypeOf(node) {
15 return [node.left, node.right].some(function(node) {
16 return node.type === "UnaryExpression" && node.operator === "typeof";
17 });
18 }
19
20 function bothAreSameTypeLiterals(node) {
21 return node.left.type === "Literal" && node.right.type === "Literal" && typeof node.left.value === typeof node.right.value;
22 }
23
24 return {
25 "BinaryExpression": function(node) {
26 var operator = node.operator;
27
28 if (context.options[0] === "smart" && (isTypeOf(node) || bothAreSameTypeLiterals(node))) {
29 return;
30 }
31
32 if (operator === "==") {
33 context.report(node, "Expected '===' and instead saw '=='.");
34 } else if (operator === "!=") {
35 context.report(node, "Expected '!==' and instead saw '!='.");
36 }
37 }
38 };
39};
\No newline at end of file