no-unused-expressions.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183
  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. enforceForJSX: {
  49. type: "boolean",
  50. default: false
  51. }
  52. },
  53. additionalProperties: false
  54. }
  55. ],
  56. messages: {
  57. unusedExpression: "Expected an assignment or function call and instead saw an expression."
  58. }
  59. },
  60. create(context) {
  61. const config = context.options[0] || {},
  62. allowShortCircuit = config.allowShortCircuit || false,
  63. allowTernary = config.allowTernary || false,
  64. allowTaggedTemplates = config.allowTaggedTemplates || false,
  65. enforceForJSX = config.enforceForJSX || false;
  66. // eslint-disable-next-line jsdoc/require-description
  67. /**
  68. * @param {ASTNode} node any node
  69. * @returns {boolean} whether the given node structurally represents a directive
  70. */
  71. function looksLikeDirective(node) {
  72. return node.type === "ExpressionStatement" &&
  73. node.expression.type === "Literal" && typeof node.expression.value === "string";
  74. }
  75. // eslint-disable-next-line jsdoc/require-description
  76. /**
  77. * @param {Function} predicate ([a] -> Boolean) the function used to make the determination
  78. * @param {a[]} list the input list
  79. * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
  80. */
  81. function takeWhile(predicate, list) {
  82. for (let i = 0; i < list.length; ++i) {
  83. if (!predicate(list[i])) {
  84. return list.slice(0, i);
  85. }
  86. }
  87. return list.slice();
  88. }
  89. // eslint-disable-next-line jsdoc/require-description
  90. /**
  91. * @param {ASTNode} node a Program or BlockStatement node
  92. * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
  93. */
  94. function directives(node) {
  95. return takeWhile(looksLikeDirective, node.body);
  96. }
  97. // eslint-disable-next-line jsdoc/require-description
  98. /**
  99. * @param {ASTNode} node any node
  100. * @param {ASTNode[]} ancestors the given node's ancestors
  101. * @returns {boolean} whether the given node is considered a directive in its current position
  102. */
  103. function isDirective(node, ancestors) {
  104. const parent = ancestors[ancestors.length - 1],
  105. grandparent = ancestors[ancestors.length - 2];
  106. return (parent.type === "Program" || parent.type === "BlockStatement" &&
  107. (/Function/u.test(grandparent.type))) &&
  108. directives(parent).indexOf(node) >= 0;
  109. }
  110. /**
  111. * The member functions return `true` if the type has no side-effects.
  112. * Unknown nodes are handled as `false`, then this rule ignores those.
  113. */
  114. const Checker = Object.assign(Object.create(null), {
  115. isDisallowed(node) {
  116. return (Checker[node.type] || alwaysFalse)(node);
  117. },
  118. ArrayExpression: alwaysTrue,
  119. ArrowFunctionExpression: alwaysTrue,
  120. BinaryExpression: alwaysTrue,
  121. ChainExpression(node) {
  122. return Checker.isDisallowed(node.expression);
  123. },
  124. ClassExpression: alwaysTrue,
  125. ConditionalExpression(node) {
  126. if (allowTernary) {
  127. return Checker.isDisallowed(node.consequent) || Checker.isDisallowed(node.alternate);
  128. }
  129. return true;
  130. },
  131. FunctionExpression: alwaysTrue,
  132. Identifier: alwaysTrue,
  133. JSXElement() {
  134. return enforceForJSX;
  135. },
  136. JSXFragment() {
  137. return enforceForJSX;
  138. },
  139. Literal: alwaysTrue,
  140. LogicalExpression(node) {
  141. if (allowShortCircuit) {
  142. return Checker.isDisallowed(node.right);
  143. }
  144. return true;
  145. },
  146. MemberExpression: alwaysTrue,
  147. MetaProperty: alwaysTrue,
  148. ObjectExpression: alwaysTrue,
  149. SequenceExpression: alwaysTrue,
  150. TaggedTemplateExpression() {
  151. return !allowTaggedTemplates;
  152. },
  153. TemplateLiteral: alwaysTrue,
  154. ThisExpression: alwaysTrue,
  155. UnaryExpression(node) {
  156. return node.operator !== "void" && node.operator !== "delete";
  157. }
  158. });
  159. return {
  160. ExpressionStatement(node) {
  161. if (Checker.isDisallowed(node.expression) && !isDirective(node, context.getAncestors())) {
  162. context.report({ node, messageId: "unusedExpression" });
  163. }
  164. }
  165. };
  166. }
  167. };