Software zum Installieren eines Smart-Mirror Frameworks , zum Nutzen von hochschulrelevanten Informationen, auf einem Raspberry-Pi.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

valid-typeof.js 3.0KB

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