no-unused-expressions.js 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  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. /** @type {import('../shared/types').Rule} */
  24. module.exports = {
  25. meta: {
  26. type: "suggestion",
  27. docs: {
  28. description: "Disallow unused expressions",
  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. /**
  67. * Has AST suggesting a directive.
  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. /**
  76. * Gets the leading sequence of members in a list that pass the predicate.
  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. /**
  90. * Gets leading directives nodes in a Node body.
  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. /**
  98. * Detect if a Node is a directive.
  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. /**
  107. * https://tc39.es/ecma262/#directive-prologue
  108. *
  109. * Only `FunctionBody`, `ScriptBody` and `ModuleBody` can have directive prologue.
  110. * Class static blocks do not have directive prologue.
  111. */
  112. return (parent.type === "Program" || parent.type === "BlockStatement" &&
  113. (/Function/u.test(grandparent.type))) &&
  114. directives(parent).includes(node);
  115. }
  116. /**
  117. * The member functions return `true` if the type has no side-effects.
  118. * Unknown nodes are handled as `false`, then this rule ignores those.
  119. */
  120. const Checker = Object.assign(Object.create(null), {
  121. isDisallowed(node) {
  122. return (Checker[node.type] || alwaysFalse)(node);
  123. },
  124. ArrayExpression: alwaysTrue,
  125. ArrowFunctionExpression: alwaysTrue,
  126. BinaryExpression: alwaysTrue,
  127. ChainExpression(node) {
  128. return Checker.isDisallowed(node.expression);
  129. },
  130. ClassExpression: alwaysTrue,
  131. ConditionalExpression(node) {
  132. if (allowTernary) {
  133. return Checker.isDisallowed(node.consequent) || Checker.isDisallowed(node.alternate);
  134. }
  135. return true;
  136. },
  137. FunctionExpression: alwaysTrue,
  138. Identifier: alwaysTrue,
  139. JSXElement() {
  140. return enforceForJSX;
  141. },
  142. JSXFragment() {
  143. return enforceForJSX;
  144. },
  145. Literal: alwaysTrue,
  146. LogicalExpression(node) {
  147. if (allowShortCircuit) {
  148. return Checker.isDisallowed(node.right);
  149. }
  150. return true;
  151. },
  152. MemberExpression: alwaysTrue,
  153. MetaProperty: alwaysTrue,
  154. ObjectExpression: alwaysTrue,
  155. SequenceExpression: alwaysTrue,
  156. TaggedTemplateExpression() {
  157. return !allowTaggedTemplates;
  158. },
  159. TemplateLiteral: alwaysTrue,
  160. ThisExpression: alwaysTrue,
  161. UnaryExpression(node) {
  162. return node.operator !== "void" && node.operator !== "delete";
  163. }
  164. });
  165. return {
  166. ExpressionStatement(node) {
  167. if (Checker.isDisallowed(node.expression) && !isDirective(node, context.getAncestors())) {
  168. context.report({ node, messageId: "unusedExpression" });
  169. }
  170. }
  171. };
  172. }
  173. };