Sha256: 72ffc71de9d5f55fb176282c050510937b6ff1897a785f81debc4175e8a366d9
Contents?: true
Size: 1.61 KB
Versions: 45
Compression:
Stored size: 1.61 KB
Contents
/** * @fileoverview Ensures that the results of typeof are compared against a valid string * @author Ian Christian Myers */ "use strict"; //------------------------------------------------------------------------------ // Rule Definition //------------------------------------------------------------------------------ module.exports = { meta: { docs: { description: "enforce comparing `typeof` expressions against valid strings", category: "Possible Errors", recommended: true }, schema: [] }, create: function(context) { var VALID_TYPES = ["symbol", "undefined", "object", "boolean", "number", "string", "function"], OPERATORS = ["==", "===", "!=", "!=="]; //-------------------------------------------------------------------------- // Public //-------------------------------------------------------------------------- return { UnaryExpression: function(node) { var parent, sibling; if (node.operator === "typeof") { parent = context.getAncestors().pop(); if (parent.type === "BinaryExpression" && OPERATORS.indexOf(parent.operator) !== -1) { sibling = parent.left === node ? parent.right : parent.left; if (sibling.type === "Literal" && VALID_TYPES.indexOf(sibling.value) === -1) { context.report(sibling, "Invalid typeof comparison value"); } } } } }; } };
Version data entries
45 entries across 45 versions & 2 rubygems