Ohm-Management - Projektarbeit B-ME
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.

no-unused-expressions.js 4.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129
  1. /**
  2. * @fileoverview Flag expressions in statement position that do not side effect
  3. * @author Michael Ficarra
  4. */
  5. "use strict";
  6. //------------------------------------------------------------------------------
  7. // Rule Definition
  8. //------------------------------------------------------------------------------
  9. module.exports = {
  10. meta: {
  11. type: "suggestion",
  12. docs: {
  13. description: "disallow unused expressions",
  14. category: "Best Practices",
  15. recommended: false,
  16. url: "https://eslint.org/docs/rules/no-unused-expressions"
  17. },
  18. schema: [
  19. {
  20. type: "object",
  21. properties: {
  22. allowShortCircuit: {
  23. type: "boolean"
  24. },
  25. allowTernary: {
  26. type: "boolean"
  27. },
  28. allowTaggedTemplates: {
  29. type: "boolean"
  30. }
  31. },
  32. additionalProperties: false
  33. }
  34. ]
  35. },
  36. create(context) {
  37. const config = context.options[0] || {},
  38. allowShortCircuit = config.allowShortCircuit || false,
  39. allowTernary = config.allowTernary || false,
  40. allowTaggedTemplates = config.allowTaggedTemplates || false;
  41. /**
  42. * @param {ASTNode} node - any node
  43. * @returns {boolean} whether the given node structurally represents a directive
  44. */
  45. function looksLikeDirective(node) {
  46. return node.type === "ExpressionStatement" &&
  47. node.expression.type === "Literal" && typeof node.expression.value === "string";
  48. }
  49. /**
  50. * @param {Function} predicate - ([a] -> Boolean) the function used to make the determination
  51. * @param {a[]} list - the input list
  52. * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
  53. */
  54. function takeWhile(predicate, list) {
  55. for (let i = 0; i < list.length; ++i) {
  56. if (!predicate(list[i])) {
  57. return list.slice(0, i);
  58. }
  59. }
  60. return list.slice();
  61. }
  62. /**
  63. * @param {ASTNode} node - a Program or BlockStatement node
  64. * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
  65. */
  66. function directives(node) {
  67. return takeWhile(looksLikeDirective, node.body);
  68. }
  69. /**
  70. * @param {ASTNode} node - any node
  71. * @param {ASTNode[]} ancestors - the given node's ancestors
  72. * @returns {boolean} whether the given node is considered a directive in its current position
  73. */
  74. function isDirective(node, ancestors) {
  75. const parent = ancestors[ancestors.length - 1],
  76. grandparent = ancestors[ancestors.length - 2];
  77. return (parent.type === "Program" || parent.type === "BlockStatement" &&
  78. (/Function/.test(grandparent.type))) &&
  79. directives(parent).indexOf(node) >= 0;
  80. }
  81. /**
  82. * Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
  83. * @param {ASTNode} node - any node
  84. * @returns {boolean} whether the given node is a valid expression
  85. */
  86. function isValidExpression(node) {
  87. if (allowTernary) {
  88. // Recursive check for ternary and logical expressions
  89. if (node.type === "ConditionalExpression") {
  90. return isValidExpression(node.consequent) && isValidExpression(node.alternate);
  91. }
  92. }
  93. if (allowShortCircuit) {
  94. if (node.type === "LogicalExpression") {
  95. return isValidExpression(node.right);
  96. }
  97. }
  98. if (allowTaggedTemplates && node.type === "TaggedTemplateExpression") {
  99. return true;
  100. }
  101. return /^(?:Assignment|Call|New|Update|Yield|Await)Expression$/.test(node.type) ||
  102. (node.type === "UnaryExpression" && ["delete", "void"].indexOf(node.operator) >= 0);
  103. }
  104. return {
  105. ExpressionStatement(node) {
  106. if (!isValidExpression(node.expression) && !isDirective(node, context.getAncestors())) {
  107. context.report({ node, message: "Expected an assignment or function call and instead saw an expression." });
  108. }
  109. }
  110. };
  111. }
  112. };