no-unused-expressions.js 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  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. /**
  10. * Returns `true`.
  11. * @returns {boolean} `true`.
  12. */
  13. function alwaysTrue() {
  14. return true;
  15. }
  16. /**
  17. * Returns `false`.
  18. * @returns {boolean} `false`.
  19. */
  20. function alwaysFalse() {
  21. return false;
  22. }
  23. module.exports = {
  24. meta: {
  25. type: "suggestion",
  26. docs: {
  27. description: "disallow unused expressions",
  28. category: "Best Practices",
  29. recommended: false,
  30. url: "https://eslint.org/docs/rules/no-unused-expressions"
  31. },
  32. schema: [
  33. {
  34. type: "object",
  35. properties: {
  36. allowShortCircuit: {
  37. type: "boolean",
  38. default: false
  39. },
  40. allowTernary: {
  41. type: "boolean",
  42. default: false
  43. },
  44. allowTaggedTemplates: {
  45. type: "boolean",
  46. default: false
  47. }
  48. },
  49. additionalProperties: false
  50. }
  51. ],
  52. messages: {
  53. unusedExpression: "Expected an assignment or function call and instead saw an expression."
  54. }
  55. },
  56. create(context) {
  57. const config = context.options[0] || {},
  58. allowShortCircuit = config.allowShortCircuit || false,
  59. allowTernary = config.allowTernary || false,
  60. allowTaggedTemplates = config.allowTaggedTemplates || false;
  61. // eslint-disable-next-line jsdoc/require-description
  62. /**
  63. * @param {ASTNode} node any node
  64. * @returns {boolean} whether the given node structurally represents a directive
  65. */
  66. function looksLikeDirective(node) {
  67. return node.type === "ExpressionStatement" &&
  68. node.expression.type === "Literal" && typeof node.expression.value === "string";
  69. }
  70. // eslint-disable-next-line jsdoc/require-description
  71. /**
  72. * @param {Function} predicate ([a] -> Boolean) the function used to make the determination
  73. * @param {a[]} list the input list
  74. * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
  75. */
  76. function takeWhile(predicate, list) {
  77. for (let i = 0; i < list.length; ++i) {
  78. if (!predicate(list[i])) {
  79. return list.slice(0, i);
  80. }
  81. }
  82. return list.slice();
  83. }
  84. // eslint-disable-next-line jsdoc/require-description
  85. /**
  86. * @param {ASTNode} node a Program or BlockStatement node
  87. * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
  88. */
  89. function directives(node) {
  90. return takeWhile(looksLikeDirective, node.body);
  91. }
  92. // eslint-disable-next-line jsdoc/require-description
  93. /**
  94. * @param {ASTNode} node any node
  95. * @param {ASTNode[]} ancestors the given node's ancestors
  96. * @returns {boolean} whether the given node is considered a directive in its current position
  97. */
  98. function isDirective(node, ancestors) {
  99. const parent = ancestors[ancestors.length - 1],
  100. grandparent = ancestors[ancestors.length - 2];
  101. return (parent.type === "Program" || parent.type === "BlockStatement" &&
  102. (/Function/u.test(grandparent.type))) &&
  103. directives(parent).indexOf(node) >= 0;
  104. }
  105. /**
  106. * The member functions return `true` if the type has no side-effects.
  107. * Unknown nodes are handled as `false`, then this rule ignores those.
  108. */
  109. const Checker = Object.assign(Object.create(null), {
  110. isDisallowed(node) {
  111. return (Checker[node.type] || alwaysFalse)(node);
  112. },
  113. ArrayExpression: alwaysTrue,
  114. ArrowFunctionExpression: alwaysTrue,
  115. BinaryExpression: alwaysTrue,
  116. ChainExpression(node) {
  117. return Checker.isDisallowed(node.expression);
  118. },
  119. ClassExpression: alwaysTrue,
  120. ConditionalExpression(node) {
  121. if (allowTernary) {
  122. return Checker.isDisallowed(node.consequent) || Checker.isDisallowed(node.alternate);
  123. }
  124. return true;
  125. },
  126. FunctionExpression: alwaysTrue,
  127. Identifier: alwaysTrue,
  128. Literal: alwaysTrue,
  129. LogicalExpression(node) {
  130. if (allowShortCircuit) {
  131. return Checker.isDisallowed(node.right);
  132. }
  133. return true;
  134. },
  135. MemberExpression: alwaysTrue,
  136. MetaProperty: alwaysTrue,
  137. ObjectExpression: alwaysTrue,
  138. SequenceExpression: alwaysTrue,
  139. TaggedTemplateExpression() {
  140. return !allowTaggedTemplates;
  141. },
  142. TemplateLiteral: alwaysTrue,
  143. ThisExpression: alwaysTrue,
  144. UnaryExpression(node) {
  145. return node.operator !== "void" && node.operator !== "delete";
  146. }
  147. });
  148. return {
  149. ExpressionStatement(node) {
  150. if (Checker.isDisallowed(node.expression) && !isDirective(node, context.getAncestors())) {
  151. context.report({ node, messageId: "unusedExpression" });
  152. }
  153. }
  154. };
  155. }
  156. };