JAVASCRIPT   52

no-eq-null.js

Guest on 21st August 2021 09:30:28 AM

  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.  
  13. module.exports = {
  14.     meta: {
  15.         docs: {
  16.             description: "disallow `null` comparisons without type-checking operators",
  17.             category: "Best Practices",
  18.             recommended: false
  19.         },
  20.  
  21.         schema: []
  22.     },
  23.  
  24.     create(context) {
  25.  
  26.         return {
  27.  
  28.             BinaryExpression(node) {
  29.                 const badOperator = node.operator === "==" || node.operator === "!=";
  30.  
  31.                 if (node.right.type === "Literal" && node.right.raw === "null" && badOperator ||
  32.                         node.left.type === "Literal" && node.left.raw === "null" && badOperator) {
  33.                     context.report({ node, message: "Use ‘===’ to compare with ‘null’."˜null’." });
  34.                 }
  35.             }
  36.  

Raw Paste


Login or Register to edit or fork this paste. It's free.