UNPKG

1.35 kBJavaScriptView Raw
1/**
2 * @fileoverview Ensures that the results of typeof are compared against a valid string
3 * @author Ian Christian Myers
4 */
5"use strict";
6
7//------------------------------------------------------------------------------
8// Rule Definition
9//------------------------------------------------------------------------------
10
11module.exports = function(context) {
12
13 var VALID_TYPES = ["symbol", "undefined", "object", "boolean", "number", "string", "function"],
14 OPERATORS = ["==", "===", "!=", "!=="];
15
16 //--------------------------------------------------------------------------
17 // Public
18 //--------------------------------------------------------------------------
19
20 return {
21
22 "UnaryExpression": function(node) {
23 var parent, sibling;
24
25 if (node.operator === "typeof") {
26 parent = context.getAncestors().pop();
27
28 if (parent.type === "BinaryExpression" && OPERATORS.indexOf(parent.operator) !== -1) {
29 sibling = parent.left === node ? parent.right : parent.left;
30
31 if (sibling.type === "Literal" && VALID_TYPES.indexOf(sibling.value) === -1) {
32 context.report(sibling, "Invalid typeof comparison value");
33 }
34 }
35 }
36 }
37
38 };
39
40};
41
42module.exports.schema = [];