no-unused-expressions.js 4.5 KB

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