]> git.proxmox.com Git - pve-eslint.git/blob - eslint/docs/rules/no-unused-expressions.md
import eslint 7.28.0
[pve-eslint.git] / eslint / docs / rules / no-unused-expressions.md
1 # Disallow Unused Expressions (no-unused-expressions)
2
3 An unused expression which has no effect on the state of the program indicates a logic error.
4
5 For example, `n + 1;` is not a syntax error, but it might be a typing mistake where a programmer meant an assignment statement `n += 1;` instead. Sometimes, such unused expressions may be eliminated by some build tools in production environment, which possibly breaks application logic.
6
7 ## Rule Details
8
9 This rule aims to eliminate unused expressions which have no effect on the state of the program.
10
11 This rule does not apply to function calls or constructor calls with the `new` operator, because they could have *side effects* on the state of the program.
12
13 ```js
14 var i = 0;
15 function increment() { i += 1; }
16 increment(); // return value is unused, but i changed as a side effect
17
18 var nThings = 0;
19 function Thing() { nThings += 1; }
20 new Thing(); // constructed object is unused, but nThings changed as a side effect
21 ```
22
23 This rule does not apply to directives (which are in the form of literal string expressions such as `"use strict";` at the beginning of a script, module, or function).
24
25 Sequence expressions (those using a comma, such as `a = 1, b = 2`) are always considered unused unless their return value is assigned or used in a condition evaluation, or a function call is made with the sequence expression value.
26
27 ## Options
28
29 This rule, in its default state, does not require any arguments. If you would like to enable one or more of the following you may pass an object with the options set as follows:
30
31 * `allowShortCircuit` set to `true` will allow you to use short circuit evaluations in your expressions (Default: `false`).
32 * `allowTernary` set to `true` will enable you to use ternary operators in your expressions similarly to short circuit evaluations (Default: `false`).
33 * `allowTaggedTemplates` set to `true` will enable you to use tagged template literals in your expressions (Default: `false`).
34 * `enforceForJSX` set to `true` will flag unused JSX element expressions (Default: `false`).
35
36 These options allow unused expressions *only if all* of the code paths either directly change the state (for example, assignment statement) or could have *side effects* (for example, function call).
37
38 Examples of **incorrect** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options:
39
40 ```js
41 /*eslint no-unused-expressions: "error"*/
42
43 0
44
45 if(0) 0
46
47 {0}
48
49 f(0), {}
50
51 a && b()
52
53 a, b()
54
55 c = a, b;
56
57 a() && function namedFunctionInExpressionContext () {f();}
58
59 (function anIncompleteIIFE () {});
60
61 injectGlobal`body{ color: red; }`
62
63 ```
64
65 Note that one or more string expression statements (with or without semi-colons) will only be considered as unused if they are not in the beginning of a script, module, or function (alone and uninterrupted by other statements). Otherwise, they will be treated as part of a "directive prologue", a section potentially usable by JavaScript engines. This includes "strict mode" directives.
66
67 ```js
68 "use strict";
69 "use asm"
70 "use stricter";
71 "use babel"
72 "any other strings like this in the prologue";
73 ```
74
75 Examples of **correct** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options:
76
77 ```js
78 /*eslint no-unused-expressions: "error"*/
79
80 {} // In this context, this is a block statement, not an object literal
81
82 {myLabel: someVar} // In this context, this is a block statement with a label and expression, not an object literal
83
84 function namedFunctionDeclaration () {}
85
86 (function aGenuineIIFE () {}());
87
88 f()
89
90 a = 0
91
92 new C
93
94 delete a.b
95
96 void a
97 ```
98
99 ### allowShortCircuit
100
101 Examples of **incorrect** code for the `{ "allowShortCircuit": true }` option:
102
103 ```js
104 /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
105
106 a || b
107 ```
108
109 Examples of **correct** code for the `{ "allowShortCircuit": true }` option:
110
111 ```js
112 /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
113
114 a && b()
115 a() || (b = c)
116 ```
117
118 ### allowTernary
119
120 Examples of **incorrect** code for the `{ "allowTernary": true }` option:
121
122 ```js
123 /*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
124
125 a ? b : 0
126 a ? b : c()
127 ```
128
129 Examples of **correct** code for the `{ "allowTernary": true }` option:
130
131 ```js
132 /*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
133
134 a ? b() : c()
135 a ? (b = c) : d()
136 ```
137
138 ### allowShortCircuit and allowTernary
139
140 Examples of **correct** code for the `{ "allowShortCircuit": true, "allowTernary": true }` options:
141
142 ```js
143 /*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/
144
145 a ? b() || (c = d) : e()
146 ```
147
148 ### allowTaggedTemplates
149
150 Examples of **incorrect** code for the `{ "allowTaggedTemplates": true }` option:
151
152 ```js
153 /*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
154
155 `some untagged template string`;
156 ```
157
158 Examples of **correct** code for the `{ "allowTaggedTemplates": true }` option:
159
160 ```js
161 /*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
162
163 tag`some tagged template string`;
164 ```
165
166 ### enforceForJSX
167
168 JSX is most-commonly used in the React ecosystem, where it is compiled to `React.createElement` expressions. Though free from side-effects, these calls are not automatically flagged by the `no-unused-expression` rule. If you're using React, or any other side-effect-free JSX pragma, this option can be enabled to flag these expressions.
169
170 Examples of **incorrect** code for the `{ "enforceForJSX": true }` option:
171
172 ```jsx
173 /*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
174
175 <MyComponent />;
176
177 <></>;
178 ```
179
180 Examples of **correct** code for the `{ "enforceForJSX": true }` option:
181
182 ```jsx
183 /*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
184
185 var myComponentPartial = <MyComponent />;
186
187 var myFragment = <></>;
188 ```