no-unused-expressions.js 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  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. default: false
  25. },
  26. allowTernary: {
  27. type: "boolean",
  28. default: false
  29. },
  30. allowTaggedTemplates: {
  31. type: "boolean",
  32. default: false
  33. }
  34. },
  35. additionalProperties: false
  36. }
  37. ]
  38. },
  39. create(context) {
  40. const config = context.options[0] || {},
  41. allowShortCircuit = config.allowShortCircuit || false,
  42. allowTernary = config.allowTernary || false,
  43. allowTaggedTemplates = config.allowTaggedTemplates || false;
  44. /**
  45. * @param {ASTNode} node - any node
  46. * @returns {boolean} whether the given node structurally represents a directive
  47. */
  48. function looksLikeDirective(node) {
  49. return node.type === "ExpressionStatement" &&
  50. node.expression.type === "Literal" && typeof node.expression.value === "string";
  51. }
  52. /**
  53. * @param {Function} predicate - ([a] -> Boolean) the function used to make the determination
  54. * @param {a[]} list - the input list
  55. * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
  56. */
  57. function takeWhile(predicate, list) {
  58. for (let i = 0; i < list.length; ++i) {
  59. if (!predicate(list[i])) {
  60. return list.slice(0, i);
  61. }
  62. }
  63. return list.slice();
  64. }
  65. /**
  66. * @param {ASTNode} node - a Program or BlockStatement node
  67. * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
  68. */
  69. function directives(node) {
  70. return takeWhile(looksLikeDirective, node.body);
  71. }
  72. /**
  73. * @param {ASTNode} node - any node
  74. * @param {ASTNode[]} ancestors - the given node's ancestors
  75. * @returns {boolean} whether the given node is considered a directive in its current position
  76. */
  77. function isDirective(node, ancestors) {
  78. const parent = ancestors[ancestors.length - 1],
  79. grandparent = ancestors[ancestors.length - 2];
  80. return (parent.type === "Program" || parent.type === "BlockStatement" &&
  81. (/Function/u.test(grandparent.type))) &&
  82. directives(parent).indexOf(node) >= 0;
  83. }
  84. /**
  85. * Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
  86. * @param {ASTNode} node - any node
  87. * @returns {boolean} whether the given node is a valid expression
  88. */
  89. function isValidExpression(node) {
  90. if (allowTernary) {
  91. // Recursive check for ternary and logical expressions
  92. if (node.type === "ConditionalExpression") {
  93. return isValidExpression(node.consequent) && isValidExpression(node.alternate);
  94. }
  95. }
  96. if (allowShortCircuit) {
  97. if (node.type === "LogicalExpression") {
  98. return isValidExpression(node.right);
  99. }
  100. }
  101. if (allowTaggedTemplates && node.type === "TaggedTemplateExpression") {
  102. return true;
  103. }
  104. return /^(?:Assignment|Call|New|Update|Yield|Await)Expression$/u.test(node.type) ||
  105. (node.type === "UnaryExpression" && ["delete", "void"].indexOf(node.operator) >= 0);
  106. }
  107. return {
  108. ExpressionStatement(node) {
  109. if (!isValidExpression(node.expression) && !isDirective(node, context.getAncestors())) {
  110. context.report({ node, message: "Expected an assignment or function call and instead saw an expression." });
  111. }
  112. }
  113. };
  114. }
  115. };