]> git.proxmox.com Git - pve-eslint.git/blame - eslint/docs/src/rules/no-unused-expressions.md
import 8.41.0 source
[pve-eslint.git] / eslint / docs / src / rules / no-unused-expressions.md
CommitLineData
8f9d1d4d
DC
1---
2title: no-unused-expressions
8f9d1d4d
DC
3rule_type: suggestion
4---
5
eb39fafa
DC
6
7An unused expression which has no effect on the state of the program indicates a logic error.
8
9For 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.
10
11## Rule Details
12
13This rule aims to eliminate unused expressions which have no effect on the state of the program.
14
15This 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.
16
17```js
18var i = 0;
19function increment() { i += 1; }
20increment(); // return value is unused, but i changed as a side effect
21
22var nThings = 0;
23function Thing() { nThings += 1; }
24new Thing(); // constructed object is unused, but nThings changed as a side effect
25```
26
27This 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).
28
29Sequence 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.
30
31## Options
32
33This 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:
34
35* `allowShortCircuit` set to `true` will allow you to use short circuit evaluations in your expressions (Default: `false`).
36* `allowTernary` set to `true` will enable you to use ternary operators in your expressions similarly to short circuit evaluations (Default: `false`).
37* `allowTaggedTemplates` set to `true` will enable you to use tagged template literals in your expressions (Default: `false`).
5422a9cc 38* `enforceForJSX` set to `true` will flag unused JSX element expressions (Default: `false`).
eb39fafa
DC
39
40These 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).
41
42Examples of **incorrect** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options:
43
8f9d1d4d
DC
44::: incorrect
45
eb39fafa
DC
46```js
47/*eslint no-unused-expressions: "error"*/
48
490
50
51if(0) 0
52
53{0}
54
55f(0), {}
56
57a && b()
58
59a, b()
60
61c = a, b;
62
63a() && function namedFunctionInExpressionContext () {f();}
64
65(function anIncompleteIIFE () {});
66
67injectGlobal`body{ color: red; }`
68
69```
70
8f9d1d4d
DC
71:::
72
eb39fafa
DC
73Examples of **correct** code for the default `{ "allowShortCircuit": false, "allowTernary": false }` options:
74
8f9d1d4d
DC
75::: correct
76
eb39fafa
DC
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
84function namedFunctionDeclaration () {}
85
86(function aGenuineIIFE () {}());
87
88f()
89
90a = 0
91
92new C
93
94delete a.b
95
96void a
97```
98
8f9d1d4d
DC
99:::
100
609c276f
TL
101Note 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.
102
103Examples of **correct** code for this rule in regard to directives:
104
8f9d1d4d
DC
105::: correct
106
609c276f
TL
107```js
108/*eslint no-unused-expressions: "error"*/
109
110"use strict";
111"use asm"
112"use stricter";
113"use babel"
114"any other strings like this in the directive prologue";
115"this is still the directive prologue";
116
117function foo() {
118 "bar";
119}
120
121class Foo {
122 someMethod() {
123 "use strict";
124 }
125}
126```
127
8f9d1d4d
DC
128:::
129
609c276f
TL
130Examples of **incorrect** code for this rule in regard to directives:
131
8f9d1d4d
DC
132::: incorrect
133
609c276f
TL
134```js
135/*eslint no-unused-expressions: "error"*/
136
137doSomething();
138"use strict"; // this isn't in a directive prologue, because there is a non-directive statement before it
139
140function foo() {
141 "bar" + 1;
142}
143
144class Foo {
145 static {
146 "use strict"; // class static blocks do not have directive prologues
147 }
148}
149```
150
8f9d1d4d
DC
151:::
152
eb39fafa
DC
153### allowShortCircuit
154
155Examples of **incorrect** code for the `{ "allowShortCircuit": true }` option:
156
8f9d1d4d
DC
157::: incorrect
158
eb39fafa
DC
159```js
160/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
161
162a || b
163```
164
8f9d1d4d
DC
165:::
166
eb39fafa
DC
167Examples of **correct** code for the `{ "allowShortCircuit": true }` option:
168
8f9d1d4d
DC
169::: correct
170
eb39fafa
DC
171```js
172/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
173
174a && b()
175a() || (b = c)
176```
177
8f9d1d4d
DC
178:::
179
eb39fafa
DC
180### allowTernary
181
182Examples of **incorrect** code for the `{ "allowTernary": true }` option:
183
8f9d1d4d
DC
184::: incorrect
185
eb39fafa
DC
186```js
187/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
188
189a ? b : 0
190a ? b : c()
191```
192
8f9d1d4d
DC
193:::
194
eb39fafa
DC
195Examples of **correct** code for the `{ "allowTernary": true }` option:
196
8f9d1d4d
DC
197::: correct
198
eb39fafa
DC
199```js
200/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
201
202a ? b() : c()
203a ? (b = c) : d()
204```
205
8f9d1d4d
DC
206:::
207
eb39fafa
DC
208### allowShortCircuit and allowTernary
209
210Examples of **correct** code for the `{ "allowShortCircuit": true, "allowTernary": true }` options:
211
8f9d1d4d
DC
212::: correct
213
eb39fafa
DC
214```js
215/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/
216
217a ? b() || (c = d) : e()
218```
219
8f9d1d4d
DC
220:::
221
eb39fafa
DC
222### allowTaggedTemplates
223
224Examples of **incorrect** code for the `{ "allowTaggedTemplates": true }` option:
225
8f9d1d4d
DC
226::: incorrect
227
eb39fafa
DC
228```js
229/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
230
231`some untagged template string`;
232```
233
8f9d1d4d
DC
234:::
235
eb39fafa
DC
236Examples of **correct** code for the `{ "allowTaggedTemplates": true }` option:
237
8f9d1d4d
DC
238::: correct
239
eb39fafa
DC
240```js
241/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
242
243tag`some tagged template string`;
244```
5422a9cc 245
8f9d1d4d
DC
246:::
247
5422a9cc
TL
248### enforceForJSX
249
250JSX 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.
251
252Examples of **incorrect** code for the `{ "enforceForJSX": true }` option:
253
8f9d1d4d
DC
254::: incorrect
255
5422a9cc
TL
256```jsx
257/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
258
259<MyComponent />;
260
261<></>;
262```
263
8f9d1d4d
DC
264:::
265
5422a9cc
TL
266Examples of **correct** code for the `{ "enforceForJSX": true }` option:
267
8f9d1d4d
DC
268::: correct
269
5422a9cc
TL
270```jsx
271/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
272
273var myComponentPartial = <MyComponent />;
274
275var myFragment = <></>;
276```
8f9d1d4d
DC
277
278:::