2 * @fileoverview Ensures that the results of typeof are compared against a valid string
3 * @author Ian Christian Myers
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "enforce comparing `typeof` expressions against valid strings",
17 category: "Possible Errors",
19 url: "https://eslint.org/docs/rules/valid-typeof"
26 requireStringLiterals: {
31 additionalProperties: false
35 invalidValue: "Invalid typeof comparison value.",
36 notString: "Typeof comparisons should be to string literals."
42 const VALID_TYPES = ["symbol", "undefined", "object", "boolean", "number", "string", "function", "bigint"],
43 OPERATORS = ["==", "===", "!=", "!=="];
45 const requireStringLiterals = context.options[0] && context.options[0].requireStringLiterals;
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
52 function isTypeofExpression(node) {
53 return node.type === "UnaryExpression" && node.operator === "typeof";
56 //--------------------------------------------------------------------------
58 //--------------------------------------------------------------------------
62 UnaryExpression(node) {
63 if (isTypeofExpression(node)) {
64 const parent = context.getAncestors().pop();
66 if (parent.type === "BinaryExpression" && OPERATORS.indexOf(parent.operator) !== -1) {
67 const sibling = parent.left === node ? parent.right : parent.left;
69 if (sibling.type === "Literal" || sibling.type === "TemplateLiteral" && !sibling.expressions.length) {
70 const value = sibling.type === "Literal" ? sibling.value : sibling.quasis[0].value.cooked;
72 if (VALID_TYPES.indexOf(value) === -1) {
73 context.report({ node: sibling, messageId: "invalidValue" });
75 } else if (requireStringLiterals && !isTypeofExpression(sibling)) {
76 context.report({ node: sibling, messageId: "notString" });