.gitignore added
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / valid-typeof.js
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
11 module.exports = {
12     meta: {
13         type: "problem",
14
15         docs: {
16             description: "enforce comparing `typeof` expressions against valid strings",
17             category: "Possible Errors",
18             recommended: true,
19             url: "https://eslint.org/docs/rules/valid-typeof"
20         },
21
22         schema: [
23             {
24                 type: "object",
25                 properties: {
26                     requireStringLiterals: {
27                         type: "boolean",
28                         default: false
29                     }
30                 },
31                 additionalProperties: false
32             }
33         ],
34         messages: {
35             invalidValue: "Invalid typeof comparison value.",
36             notString: "Typeof comparisons should be to string literals."
37         }
38     },
39
40     create(context) {
41
42         const VALID_TYPES = ["symbol", "undefined", "object", "boolean", "number", "string", "function", "bigint"],
43             OPERATORS = ["==", "===", "!=", "!=="];
44
45         const requireStringLiterals = context.options[0] && context.options[0].requireStringLiterals;
46
47         /**
48          * Determines whether a node is a typeof expression.
49          * @param {ASTNode} node The node
50          * @returns {boolean} `true` if the node is a typeof expression
51          */
52         function isTypeofExpression(node) {
53             return node.type === "UnaryExpression" && node.operator === "typeof";
54         }
55
56         //--------------------------------------------------------------------------
57         // Public
58         //--------------------------------------------------------------------------
59
60         return {
61
62             UnaryExpression(node) {
63                 if (isTypeofExpression(node)) {
64                     const parent = context.getAncestors().pop();
65
66                     if (parent.type === "BinaryExpression" && OPERATORS.indexOf(parent.operator) !== -1) {
67                         const sibling = parent.left === node ? parent.right : parent.left;
68
69                         if (sibling.type === "Literal" || sibling.type === "TemplateLiteral" && !sibling.expressions.length) {
70                             const value = sibling.type === "Literal" ? sibling.value : sibling.quasis[0].value.cooked;
71
72                             if (VALID_TYPES.indexOf(value) === -1) {
73                                 context.report({ node: sibling, messageId: "invalidValue" });
74                             }
75                         } else if (requireStringLiterals && !isTypeofExpression(sibling)) {
76                             context.report({ node: sibling, messageId: "notString" });
77                         }
78                     }
79                 }
80             }
81
82         };
83
84     }
85 };