2 * @fileoverview Flag expressions in statement position that do not side effect
3 * @author Michael Ficarra
7 //------------------------------------------------------------------------------
9 //------------------------------------------------------------------------------
16 description: "disallow unused expressions",
17 category: "Best Practices",
19 url: "https://eslint.org/docs/rules/no-unused-expressions"
34 allowTaggedTemplates: {
39 additionalProperties: false
45 const config = context.options[0] || {},
46 allowShortCircuit = config.allowShortCircuit || false,
47 allowTernary = config.allowTernary || false,
48 allowTaggedTemplates = config.allowTaggedTemplates || false;
50 // eslint-disable-next-line jsdoc/require-description
52 * @param {ASTNode} node any node
53 * @returns {boolean} whether the given node structurally represents a directive
55 function looksLikeDirective(node) {
56 return node.type === "ExpressionStatement" &&
57 node.expression.type === "Literal" && typeof node.expression.value === "string";
60 // eslint-disable-next-line jsdoc/require-description
62 * @param {Function} predicate ([a] -> Boolean) the function used to make the determination
63 * @param {a[]} list the input list
64 * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
66 function takeWhile(predicate, list) {
67 for (let i = 0; i < list.length; ++i) {
68 if (!predicate(list[i])) {
69 return list.slice(0, i);
75 // eslint-disable-next-line jsdoc/require-description
77 * @param {ASTNode} node a Program or BlockStatement node
78 * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
80 function directives(node) {
81 return takeWhile(looksLikeDirective, node.body);
84 // eslint-disable-next-line jsdoc/require-description
86 * @param {ASTNode} node any node
87 * @param {ASTNode[]} ancestors the given node's ancestors
88 * @returns {boolean} whether the given node is considered a directive in its current position
90 function isDirective(node, ancestors) {
91 const parent = ancestors[ancestors.length - 1],
92 grandparent = ancestors[ancestors.length - 2];
94 return (parent.type === "Program" || parent.type === "BlockStatement" &&
95 (/Function/u.test(grandparent.type))) &&
96 directives(parent).indexOf(node) >= 0;
100 * Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
101 * @param {ASTNode} node any node
102 * @returns {boolean} whether the given node is a valid expression
104 function isValidExpression(node) {
107 // Recursive check for ternary and logical expressions
108 if (node.type === "ConditionalExpression") {
109 return isValidExpression(node.consequent) && isValidExpression(node.alternate);
113 if (allowShortCircuit) {
114 if (node.type === "LogicalExpression") {
115 return isValidExpression(node.right);
119 if (allowTaggedTemplates && node.type === "TaggedTemplateExpression") {
123 return /^(?:Assignment|Call|New|Update|Yield|Await)Expression$/u.test(node.type) ||
124 (node.type === "UnaryExpression" && ["delete", "void"].indexOf(node.operator) >= 0);
128 ExpressionStatement(node) {
129 if (!isValidExpression(node.expression) && !isDirective(node, context.getAncestors())) {
130 context.report({ node, message: "Expected an assignment or function call and instead saw an expression." });