UNPKG

1.23 kBJavaScriptView 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 = {
14 meta: {
15 type: "suggestion",
16
17 docs: {
18 description: "disallow `null` comparisons without type-checking operators",
19 category: "Best Practices",
20 recommended: false,
21 url: "https://eslint.org/docs/rules/no-eq-null"
22 },
23
24 schema: [],
25
26 messages: {
27 unexpected: "Use '===' to compare with null."
28 }
29 },
30
31 create(context) {
32
33 return {
34
35 BinaryExpression(node) {
36 const badOperator = node.operator === "==" || node.operator === "!=";
37
38 if (node.right.type === "Literal" && node.right.raw === "null" && badOperator ||
39 node.left.type === "Literal" && node.left.raw === "null" && badOperator) {
40 context.report({ node, messageId: "unexpected" });
41 }
42 }
43 };
44
45 }
46};