]>
git.proxmox.com Git - pve-eslint.git/blob - eslint/lib/rules/no-unused-expressions.js
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
44 unusedExpression
: "Expected an assignment or function call and instead saw an expression."
49 const config
= context
.options
[0] || {},
50 allowShortCircuit
= config
.allowShortCircuit
|| false,
51 allowTernary
= config
.allowTernary
|| false,
52 allowTaggedTemplates
= config
.allowTaggedTemplates
|| false;
54 // eslint-disable-next-line jsdoc/require-description
56 * @param {ASTNode} node any node
57 * @returns {boolean} whether the given node structurally represents a directive
59 function looksLikeDirective(node
) {
60 return node
.type
=== "ExpressionStatement" &&
61 node
.expression
.type
=== "Literal" && typeof node
.expression
.value
=== "string";
64 // eslint-disable-next-line jsdoc/require-description
66 * @param {Function} predicate ([a] -> Boolean) the function used to make the determination
67 * @param {a[]} list the input list
68 * @returns {a[]} the leading sequence of members in the given list that pass the given predicate
70 function takeWhile(predicate
, list
) {
71 for (let i
= 0; i
< list
.length
; ++i
) {
72 if (!predicate(list
[i
])) {
73 return list
.slice(0, i
);
79 // eslint-disable-next-line jsdoc/require-description
81 * @param {ASTNode} node a Program or BlockStatement node
82 * @returns {ASTNode[]} the leading sequence of directive nodes in the given node's body
84 function directives(node
) {
85 return takeWhile(looksLikeDirective
, node
.body
);
88 // eslint-disable-next-line jsdoc/require-description
90 * @param {ASTNode} node any node
91 * @param {ASTNode[]} ancestors the given node's ancestors
92 * @returns {boolean} whether the given node is considered a directive in its current position
94 function isDirective(node
, ancestors
) {
95 const parent
= ancestors
[ancestors
.length
- 1],
96 grandparent
= ancestors
[ancestors
.length
- 2];
98 return (parent
.type
=== "Program" || parent
.type
=== "BlockStatement" &&
99 (/Function/u.test(grandparent
.type
))) &&
100 directives(parent
).indexOf(node
) >= 0;
104 * Determines whether or not a given node is a valid expression. Recurses on short circuit eval and ternary nodes if enabled by flags.
105 * @param {ASTNode} node any node
106 * @returns {boolean} whether the given node is a valid expression
108 function isValidExpression(node
) {
111 // Recursive check for ternary and logical expressions
112 if (node
.type
=== "ConditionalExpression") {
113 return isValidExpression(node
.consequent
) && isValidExpression(node
.alternate
);
117 if (allowShortCircuit
) {
118 if (node
.type
=== "LogicalExpression") {
119 return isValidExpression(node
.right
);
123 if (allowTaggedTemplates
&& node
.type
=== "TaggedTemplateExpression") {
127 return /^(?:Assignment|Call|New|Update|Yield|Await|Import)Expression$/u.test(node
.type
) ||
128 (node
.type
=== "UnaryExpression" && ["delete", "void"].indexOf(node
.operator
) >= 0);
132 ExpressionStatement(node
) {
133 if (!isValidExpression(node
.expression
) && !isDirective(node
, context
.getAncestors())) {
134 context
.report({ node
, messageId
: "unusedExpression" });