UNPKG

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