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
此規則不適用於指令(指令的形式是文字字串表達式,例如腳本、模組或函數開頭的 "use strict";
)。
序列表達式(使用逗號的表達式,例如 a = 1, b = 2
)始終被視為未使用,除非它們的回傳值被賦值或用於條件評估,或者使用序列表達式值進行函數呼叫。
選項
此規則在其預設狀態下,不需要任何參數。如果您想啟用以下一個或多個選項,您可以傳遞一個物件,並將選項設定如下
- 將
allowShortCircuit
設定為true
將允許您在表達式中使用短路評估(預設值:false
)。 - 將
allowTernary
設定為true
將使您能夠在表達式中使用三元運算子,與短路評估類似(預設值:false
)。 - 將
allowTaggedTemplates
設定為true
將使您能夠在表達式中使用標籤樣板字面值(預設值:false
)。 - 將
enforceForJSX
設定為true
將標記未使用的 JSX 元素表達式(預設值:false
)。
這些選項僅在所有程式碼路徑直接變更狀態(例如,賦值語句)或可能產生副作用(例如,函數呼叫)的情況下,才允許未使用的表達式。
預設 { "allowShortCircuit": false, "allowTernary": false }
選項的不正確程式碼範例
/*eslint no-unused-expressions: "error"*/
if(0)
{}
預設 { "allowShortCircuit": false, "allowTernary": false }
選項的正確程式碼範例
/*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 引擎可能使用的部分。這包括「嚴格模式」指令。
關於指令的此規則的正確程式碼範例
/*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";
}
}
關於指令的此規則的不正確程式碼範例
/*eslint no-unused-expressions: "error"*/
doSomething();
// this isn't in a directive prologue, because there is a non-directive statement before it
function foo() {
}
class Foo {
static {
// class static blocks do not have directive prologues
}
}
allowShortCircuit
{ "allowShortCircuit": true }
選項的不正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
{ "allowShortCircuit": true }
選項的正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true }]*/
a && b()
a() || (b = c)
allowTernary
{ "allowTernary": true }
選項的不正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
{ "allowTernary": true }
選項的正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowTernary": true }]*/
a ? b() : c()
a ? (b = c) : d()
allowShortCircuit 和 allowTernary
{ "allowShortCircuit": true, "allowTernary": true }
選項的正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowShortCircuit": true, "allowTernary": true }]*/
a ? b() || (c = d) : e()
allowTaggedTemplates
{ "allowTaggedTemplates": true }
選項的不正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
{ "allowTaggedTemplates": true }
選項的正確程式碼範例
/*eslint no-unused-expressions: ["error", { "allowTaggedTemplates": true }]*/
tag`some tagged template string`;
enforceForJSX
JSX 最常用於 React 生態系統,它會編譯為 React.createElement
表達式。儘管沒有副作用,但 no-unused-expression
規則不會自動標記這些呼叫。如果您正在使用 React 或任何其他無副作用的 JSX pragma,則可以啟用此選項來標記這些表達式。
{ "enforceForJSX": true }
選項的不正確程式碼範例
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
{ "enforceForJSX": true }
選項的正確程式碼範例
/*eslint no-unused-expressions: ["error", { "enforceForJSX": true }]*/
var myComponentPartial = <MyComponent />;
var myFragment = <></>;
版本
此規則在 ESLint v0.1.0 中引入。