src/content/docs/linter/rules/no-unused-expressions.mdx
import { Tabs, TabItem } from '@astrojs/starlight/components';
<Tabs> <TabItem label="JavaScript (and super languages)" icon="seti:javascript"> ## Summary - Rule available since: `v2.2.5` - Diagnostic Category: [`lint/suspicious/noUnusedExpressions`](/reference/diagnostics#diagnostic-category) - This rule isn't recommended, so you need to enable it. - This rule doesn't have a fix. - The default severity of this rule is [**warning**](/reference/diagnostics#warning). - Sources: - Same as [`no-unused-expressions`](https://eslint.org/docs/latest/rules/no-unused-expressions){
"linter": {
"rules": {
"suspicious": {
"noUnusedExpressions": "error"
}
}
}
}
Disallow expression statements that are neither a function call nor an assignment.
When an expression is used as a statement, it should be explicitly clear
what the intention behind the expression is. This is clear for function
calls and assignments, because the call or the assignment itself is the
primary intention behind the statement. For other expression kinds, the
intention is much more ambiguous; it could be the expression contains
side-effects that are not very explicit, but it could also be that it is
an error where the author forgot to use the result of the expression,
such as a forgotten return keyword, or it could point to a function
that the author forgot to call.
0
if(0) 0
{0}
f(0), {}
a && b()
a, b()
c = a, b
a() && function namedFunctionInExpressionContext () {f();}
(function anIncompleteIIFE () {});
injectGlobal`body{ color: red; }`
Set<number>
1 as number
window!
JSX expressions are considered invalid when used as a statement too:
<MyComponent />
<></>
{} // In this context, this is a block statement, not an object literal
{ myLabel: foo() } // In this context, this is a block statement with a label and expression, not an object literal
function namedFunctionDeclaration () {}
(function aGenuineIIFE () {}());
f()
a = 0
new C
delete a.b
void a
Any stand-alone string at the start of a script, module, or function is considered a directive and is therefore allowed.
"use strict";
"use asm"
"use stricter";
"use babel"
"any other strings like this in the directive prologue";
"this is still the directive prologue";
function foo() {
"bar";
}
class Foo {
someMethod() {
"use strict";
}
}
The following are not considered valid directives:
doSomething();
"use strict"; // this isn't in a directive prologue, because there is a non-directive statement before it
function foo() {
"bar" + 1;
}
class Foo {
static {
"use strict"; // class static blocks do not have directive prologues
}
}