no-unused-expressions.js 4.9 KB

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