no-unused-expressions

禁止未使用的表达式

对程序状态没有影响的未使用表达式表示逻辑错误。

例如,n + 1; 不是语法错误,但它可能是一个打字错误,程序员的意思是赋值语句 n += 1;。有时,这些未使用的表达式可能会被生产环境中的某些构建工具消除,这可能会破坏应用程序逻辑。

规则详情

该规则旨在消除对程序状态没有影响的未使用表达式。

此规则不适用于带有 new 运算符的函数调用或构造函数调用,因为它们可能对程序的状态产生副作用。

var i = 0;
function increment() { i += 1; }
increment(); // return value is unused, but i changed as a side effect

var nThings = 0;
function Thing() { nThings += 1; }
new Thing(); // constructed object is unused, but nThings changed as a side effect

Kg85hldKPluIs19bwJPZz1wVyqqikk9q1U5z2RkuWC/OYZvTtGvaSWIE8WVW1iSbjRzYsw9DHqn24Sl4f5USBG51Rs3+u7VCgfO+IwaEKZqXCkDZ7GY/y0h0R1k8beWglTqJ7zk0RQjz0BqGqp1aFmNUBtWwA2mA5MFkv54EQYwamHVhDBRA9QWeo4ONtHM3

e8E2I7TeG2MIooXzLoQMlFj+FB8VWZEKvVGUspkUTZjd8u//545CpuJPm7U+Y+8omw6twRI+aGPkxJ1WRgeE5gI9CGZtwom4L4yK3qexrtcKQ6NZSHEnqB+HwCxkQQjLuF2PXseQYYHZc+E7mVfWJUXYE2t8hI5lQM0LA9FAVsqMRr8D1PkQR0+d4YyTiKyGn/QY+o5DdDShliSyZBWLRvw41oLGehkXMHj5SC90gdfr/UcyOpWKY1nyjg5m2ycDLWryEm5ikQ4un+JvSDi6lgOguFEx7pOE0Bw4IiQl9+U=

选项

1tTJ6oqXNlgAJ4QWYZojl4cnjAG0N4q8KmMF+4DDDKGHhwX8l2Zcjcj9ZFjE5yFeXbYp6Td6bWA+t5+qhp7fqXbjK2HdXMdZsrC8GRhh3hOYIyXOmcL2yS/EF6GGjarxMpI73xOG/InuBkHbRoHQkuD3pYaeQCquzJiWQILL6Yo+O1e0Mr9rLj8iLZsEQ4b8X88x+O7TLhHmcQE/i0LYiw==

    m3rf32UKV4EHnIaTzvJm2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Fm7iktnvE=

dSNzrmk6iXPCPxy5DmKY7VHzZFjLq/qBVjgaPvq8eVqtBnk5itktRne/KRUojeevXgygHI3GUJEp7+s2EFwaweNjqWAkp1szSxlyMXD4mEXsTlKfkySXElOeh+Hc07AG3GP6N9Z/xa6cR5narKoUCg==

/*eslint no-unused-expressions: "error"*/

0

if(0) 0

{0}

f(0), {}

a && b()

a, b()

c = a, b;

a() && function namedFunctionInExpressionContext () {f();}

(function anIncompleteIIFE () {});

injectGlobal`body{ color: red; }`

dSNzrmk6iXPCPxy5DmKY7VHzZFjLq/qBVjgaPvq8eVqtBnk5itktRne/KRUojeevXgygHI3GUJEp7+s2EFwaweNjqWAkp1szSxlyMXD4mEUvqGqTx7sl3sfEoea9jPJRP0m26zfJeskS1cMilLEENw==

/*eslint no-unused-expressions: "error"*/

{} // In this context, this is a block statement, not an object literal

{myLabel: someVar} // 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

fpJanbq5eT5PRX1gExPq6yxoSeb28wZg+TYhxiw2L6c5H6uCI+k1nPq4Zj/UziqOkRvpgpaWSt5pBU+xv19EDBPTGKq3JB5VODUS2Cgz5hqelLo23xDJ7aYLm0l7MGWuAYc/xZg+DmX8bHVa/x2GJkCfRTS4voomVitJ1Pelf9L5f4vayv7jCSm+tP0Oq99O9jUY9QsAD3O/R8BRXL04+vMBxeu4jom9cO6dklsds5OPm7jaDZqedJr/c05HqhK3B2anAgoCMvTJlBUbKC25f+Bpt/jmRdWO4aP7cZSsX4uVybGd+EgLxCNaQVL7j+l7Pd5hAIRz0O1tG+M6Sh9yuBl5MLS6t5NMpcXlfOOWjJ43f8F3bPXPAcIcwO4QlytISG9bJNR0JRT8E8fbJGHiOAAGrf0NcTV5D5iMB3z7oODDOCOeDFXKiEiDLIvM26uzANfjYJ2m87iQziZqyb4dxQ==

n6So2vke4INVZ0w3vXd2cuGctSz+2MWLf/8cMdflcwgIqR8MSwjKnd2904OeqmeB

/*eslint no-unused-expressions: "error"*/

"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";
    }
}

mkAbykHr/HNE5gXjBtx2zWmu9/JIM+7+CFughS0gs9lhJl7fnBK5/hXOoPK92rEvRdBRVpmSN73vtIanKzUXbA==

/*eslint no-unused-expressions: "error"*/

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
    }
}

allowShortCircuit

DsUU0YR7eo6/WKnnECrSHXwalVFC0p29fu/tdNzd/GNZh+0uNPCP/v0l7IbNBwnAvGGfEY2cOtlF59TfcPSITlgpAO3Fv8VB6wdSBS2+olg=

/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/

a || b

DsUU0YR7eo6/WKnnECrSHXwalVFC0p29fu/tdNzd/GNZh+0uNPCP/v0l7IbNBwnAUeeSxuOZOiy6JfI8tJJrB/vuhimeMyDfXvIq8iAHFS4=

/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/

a && b()
a() || (b = c)

allowTernary

7R7WH4TYQq+YxdH4rM80irSQuC091Fwh/dVppf7vBNNfKRg7JAjW2b0CaO7GRQNfSBnqomydK2rzVTjhUcOwph2ucP659i0KFu5VdEPErFw=

/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/

a ? b : 0
a ? b : c()

7R7WH4TYQq+YxdH4rM80irSQuC091Fwh/dVppf7vBNM7B1mTyh5wiUCcFn3DV7NhOEOusDB0T776Sb3jwiGwRZ7sOxijIPg+XFywxXqHko0=

/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/

a ? b() : c()
a ? (b = c) : d()

allowShortCircuit 和 allowTernary

DsUU0YR7eo6/WKnnECrSHXwalVFC0p29fu/tdNzd/GMaxT5oFmo34zuE0pzTdPAmYcm17A4OfD6ed11qpWLgllBe4lpBVEPxZ4j8imfENV5CpQYXBrGxcEC6fHWXvFNe

/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/

a ? b() || (c = d) : e()

allowTaggedTemplates

0Zg9GsUJJYcMVUyeR/bLtDbcRMUtW9Y5+e6GlszF7QXuiYtgwKk3YqIvHdtbzNM7rjgAeQwkrMe0wZ4MCZ0femrzjPKShwO196sjAuJhyjQ=

/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/

`some untagged template string`;

0Zg9GsUJJYcMVUyeR/bLtDbcRMUtW9Y5+e6GlszF7QXuiYtgwKk3YqIvHdtbzNM7XpaxR04ERPgKGuxUdBYKquF7p1jcDiq72jNn6ptxArg=

/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/

tag`some tagged template string`;

enforceForJSX

XMIG50OmG0ilu6NNQJPC0XcNHOiQwqn/QupsIX3Z4oECPuN2HzAnVR/iki07I+iRDu7L17/UO8gR1dF7K5jv/TF4tnUT0xvUKzfxhOsel5mwyTVyqIT/Rb0L7upBXpfighg5HmD/6VyQznWqJmX2lioL1zd5UriH0/g8HzbhZhAUmEJKxQTiNY9jiJSgY29AHyxXHt/+ls5iNFmpmu+uDE7h64sQ9SI3ZDvfoXS95iIv+Q7WnVW9l/ccOa6cSPH8n5b2Q1ZW5IN0lLELr/jgSJHmHPX56njcgm/CAGGBeEdAlFVZvLtXxfrz6ITyHI1jt594nRgLMLFmmEMdbNS4G98JSOF1HWvN2hZg6AEMk+M6lsglzG8ZwlNGSI0QgIr+1HkjOYPwgF4s1m3Qcbg+AW/kWR0bY2Uoc0iT2c1BiW3CQAcrw3ha8ZOLK1fAPzVZ

RXG8/uGxHdKJrAiwkAbJQCH7JescyeRfhe0hvrU+EsZK2O0VbaXQxedaehlLwO5P4WZI+WUYcUcbpXZpHPtxil+Xy90/C1XMPxHePw1HGIg=

/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/

<MyComponent />;

<></>;

RXG8/uGxHdKJrAiwkAbJQCH7JescyeRfhe0hvrU+EsZK2O0VbaXQxedaehlLwO5PQM75I88aYfml/Hx/rHI8YM0ORA+bRj/LVbv1r/DfzYE=

/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/

var myComponentPartial = <MyComponent />;

var myFragment = <></>;