UNPKG

837 BJavaScriptView Raw
1/**
2 * @fileoverview Rule to flag comparisons to null without a type-checking
3 * operator.
4 * @author Ian Christian Myers
5 */
6
7"use strict";
8
9//------------------------------------------------------------------------------
10// Rule Definition
11//------------------------------------------------------------------------------
12
13module.exports = function(context) {
14
15 return {
16
17 "BinaryExpression": function(node) {
18 var badOperator = node.operator === "==" || node.operator === "!=";
19
20 if (node.right.type === "Literal" && node.right.raw === "null" && badOperator ||
21 node.left.type === "Literal" && node.left.raw === "null" && badOperator) {
22 context.report(node, "Use ‘===’ to compare with ‘null’.");
23 }
24 }
25 };
26
27};
28
29module.exports.schema = [];