minor adjustment to readme
[dotfiles/.git] / .config / coc / extensions / node_modules / coc-prettier / node_modules / eslint / lib / rules / no-unused-expressions.js
1 /**
2  * @fileoverview Flag expressions in statement position that do not side effect
3  * @author Michael Ficarra
4  */
5 "use strict";
6
7 //------------------------------------------------------------------------------
8 // Rule Definition
9 //------------------------------------------------------------------------------
10
11 module.exports = {
12     meta: {
13         type: "suggestion",
14
15         docs: {
16             description: "disallow unused expressions",
17             category: "Best Practices",
18             recommended: false,
19             url: "https://eslint.org/docs/rules/no-unused-expressions"
20         },
21
22         schema: [
23             {
24                 type: "object",
25                 properties: {
26                     allowShortCircuit: {
27                         type: "boolean",
28                         default: false
29                     },
30                     allowTernary: {
31                         type: "boolean",
32                         default: false
33                     },
34                     allowTaggedTemplates: {
35                         type: "boolean",
36                         default: false
37                     }
38                 },
39                 additionalProperties: false
40             }
41         ]
42     },
43
44     create(context) {
45         const config = context.options[0] || {},
46             allowShortCircuit = config.allowShortCircuit || false,
47             allowTernary = config.allowTernary || false,
48             allowTaggedTemplates = config.allowTaggedTemplates || false;
49
50         // eslint-disable-next-line jsdoc/require-description
51         /**
52          * @param {ASTNode} node any node
53          * @returns {boolean} whether the given node structurally represents a directive
54          */
55         function looksLikeDirective(node) {
56             return node.type === "ExpressionStatement" &&
57                 node.expression.type === "Literal" && typeof node.expression.value === "string";
58         }
59
60         // eslint-disable-next-line jsdoc/require-description
61         /**
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
65          */
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);
70                 }
71             }
72             return list.slice();
73         }
74
75         // eslint-disable-next-line jsdoc/require-description
76         /**
77          * @param {ASTNode} node a Program or BlockStatement node
78          * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
79          */
80         function directives(node) {
81             return takeWhile(looksLikeDirective, node.body);
82         }
83
84         // eslint-disable-next-line jsdoc/require-description
85         /**
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
89          */
90         function isDirective(node, ancestors) {
91             const parent = ancestors[ancestors.length - 1],
92                 grandparent = ancestors[ancestors.length - 2];
93
94             return (parent.type === "Program" || parent.type === "BlockStatement" &&
95                     (/Function/u.test(grandparent.type))) &&
96                     directives(parent).indexOf(node) >= 0;
97         }
98
99         /**
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
103          */
104         function isValidExpression(node) {
105             if (allowTernary) {
106
107                 // Recursive check for ternary and logical expressions
108                 if (node.type === "ConditionalExpression") {
109                     return isValidExpression(node.consequent) && isValidExpression(node.alternate);
110                 }
111             }
112
113             if (allowShortCircuit) {
114                 if (node.type === "LogicalExpression") {
115                     return isValidExpression(node.right);
116                 }
117             }
118
119             if (allowTaggedTemplates && node.type === "TaggedTemplateExpression") {
120                 return true;
121             }
122
123             return /^(?:Assignment|Call|New|Update|Yield|Await)Expression$/u.test(node.type) ||
124                 (node.type === "UnaryExpression" && ["delete", "void"].indexOf(node.operator) >= 0);
125         }
126
127         return {
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." });
131                 }
132             }
133         };
134
135     }
136 };