1 | /**
|
2 | * @fileoverview Rule to flag statements that use != and == instead of !== and ===
|
3 | * @author Nicholas C. Zakas
|
4 | * @copyright 2013 Nicholas C. Zakas. All rights reserved.
|
5 | * See LICENSE file in root directory for full license.
|
6 | */
|
7 |
|
8 | ;
|
9 |
|
10 | //------------------------------------------------------------------------------
|
11 | // Rule Definition
|
12 | //------------------------------------------------------------------------------
|
13 |
|
14 | module.exports = function(context) {
|
15 |
|
16 | /**
|
17 | * Checks if an expression is a typeof expression
|
18 | * @param {ASTNode} node The node to check
|
19 | * @returns {boolean} if the node is a typeof expression
|
20 | */
|
21 | function isTypeOf(node) {
|
22 | return node.type === "UnaryExpression" && node.operator === "typeof";
|
23 | }
|
24 |
|
25 | /**
|
26 | * Checks if either operand of a binary expression is a typeof operation
|
27 | * @param {ASTNode} node The node to check
|
28 | * @returns {boolean} if one of the operands is typeof
|
29 | * @private
|
30 | */
|
31 | function isTypeOfBinary(node) {
|
32 | return isTypeOf(node.left) || isTypeOf(node.right);
|
33 | }
|
34 |
|
35 | /**
|
36 | * Checks if operands are literals of the same type (via typeof)
|
37 | * @param {ASTNode} node The node to check
|
38 | * @returns {boolean} if operands are of same type
|
39 | * @private
|
40 | */
|
41 | function areLiteralsAndSameType(node) {
|
42 | return node.left.type === "Literal" && node.right.type === "Literal" &&
|
43 | typeof node.left.value === typeof node.right.value;
|
44 | }
|
45 |
|
46 | /**
|
47 | * Checks if one of the operands is a literal null
|
48 | * @param {ASTNode} node The node to check
|
49 | * @returns {boolean} if operands are null
|
50 | * @private
|
51 | */
|
52 | function isNullCheck(node) {
|
53 | return (node.right.type === "Literal" && node.right.value === null) ||
|
54 | (node.left.type === "Literal" && node.left.value === null);
|
55 | }
|
56 |
|
57 | /**
|
58 | * Gets the location (line and column) of the binary expression's operator
|
59 | * @param {ASTNode} node The binary expression node to check
|
60 | * @param {String} operator The operator to find
|
61 | * @returns {Object} { line, column } location of operator
|
62 | * @private
|
63 | */
|
64 | function getOperatorLocation(node) {
|
65 | var opToken = context.getTokenAfter(node.left);
|
66 | return {line: opToken.loc.start.line, column: opToken.loc.start.column};
|
67 | }
|
68 |
|
69 | return {
|
70 | "BinaryExpression": function(node) {
|
71 | if (node.operator !== "==" && node.operator !== "!=") {
|
72 | return;
|
73 | }
|
74 |
|
75 | if (context.options[0] === "smart" && (isTypeOfBinary(node) ||
|
76 | areLiteralsAndSameType(node) || isNullCheck(node))) {
|
77 | return;
|
78 | }
|
79 |
|
80 | if (context.options[0] === "allow-null" && isNullCheck(node)) {
|
81 | return;
|
82 | }
|
83 |
|
84 | context.report({
|
85 | node: node,
|
86 | loc: getOperatorLocation(node),
|
87 | message: "Expected '{{op}}=' and instead saw '{{op}}'.",
|
88 | data: { op: node.operator }
|
89 | });
|
90 |
|
91 | }
|
92 | };
|
93 |
|
94 | };
|
95 |
|
96 | module.exports.schema = [
|
97 | {
|
98 | "enum": ["smart", "allow-null"]
|
99 | }
|
100 | ];
|