版本

no-unused-expressions

不允許未使用的表達式

未使用的表達式若對程式狀態沒有影響,則表示存在邏輯錯誤。

例如,n + 1; 不是語法錯誤,但它可能是程式設計師本意是賦值語句 n += 1; 的輸入錯誤。 有時,這種未使用的表達式可能會被生產環境中的某些建置工具消除,這可能會破壞應用程式邏輯。

規則詳情

此規則旨在消除對程式狀態沒有影響的未使用表達式。

此規則不適用於函式呼叫或使用 new 運算子的建構子呼叫,因為它們可能會對程式狀態產生副作用

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

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

此規則不適用於指令(其形式為文字字串表達式,例如腳本、模組或函式開頭的 "use strict";)。

序列表達式(使用逗號的表達式,例如 a = 1, b = 2)始終被視為未使用,除非它們的回傳值被賦值或用於條件評估,或者使用序列表達式值進行函式呼叫。

選項

此規則在其預設狀態下,不需要任何引數。如果您想啟用以下一或多個選項,您可以傳遞一個物件,並將選項設定如下

  • allowShortCircuit 設定為 true 將允許您在表達式中使用短路求值(預設值:false)。
  • allowTernary 設定為 true 將使您能夠在表達式中使用三元運算子,類似於短路求值(預設值:false)。
  • allowTaggedTemplates 設定為 true 將使您能夠在表達式中使用標籤模板字面量(預設值:false)。
  • enforceForJSX 設定為 true 將標記未使用的 JSX 元素表達式(預設值:false)。

這些選項僅在所有程式碼路徑直接更改狀態(例如,賦值語句)或可能具有副作用(例如,函式呼叫)的情況下,才允許未使用的表達式。

預設 { "allowShortCircuit": false, "allowTernary": false } 選項的錯誤程式碼範例

在 Playground 中開啟
/*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; }`

預設 { "allowShortCircuit": false, "allowTernary": false } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: "error"*/

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

請注意,一個或多個字串表達式語句(帶或不帶分號)只有在它們不是位於腳本、模組或函式的開頭(單獨且未被其他語句中斷)時,才被視為未使用。否則,它們將被視為「指令序言」的一部分,JavaScript 引擎可能會使用該部分。這包括「嚴格模式」指令。

關於指令的此規則的正確程式碼範例

在 Playground 中開啟
/*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";
    }
}

關於指令的此規則的錯誤程式碼範例

在 Playground 中開啟
/*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

{ "allowShortCircuit": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/

a || b

{ "allowShortCircuit": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/

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

allowTernary

{ "allowTernary": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/

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

{ "allowTernary": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/

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

allowShortCircuit 和 allowTernary

{ "allowShortCircuit": true, "allowTernary": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/

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

allowTaggedTemplates

{ "allowTaggedTemplates": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/

`some untagged template string`;

{ "allowTaggedTemplates": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/

tag`some tagged template string`;

enforceForJSX

JSX 最常在 React 生態系統中使用,它在其中被編譯為 React.createElement 表達式。 儘管沒有副作用,但這些呼叫不會被 no-unused-expression 規則自動標記。 如果您正在使用 React 或任何其他無副作用的 JSX 語法,則可以啟用此選項來標記這些表達式。

{ "enforceForJSX": true } 選項的錯誤程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/

<MyComponent />;

<></>;

{ "enforceForJSX": true } 選項的正確程式碼範例

在 Playground 中開啟
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/

const myComponentPartial = <MyComponent />;

const myFragment = <></>;

版本

此規則在 ESLint v0.1.0 中引入。

資源

變更語言