版本

no-warning-comments

不允許在註解中使用指定的警告字詞

開發人員經常在程式碼中加入註解,表示程式碼尚未完成或需要審查。您很可能想要在程式碼準備好用於生產環境之前,修復或審查程式碼,然後移除註解。

// TODO: do something
// FIXME: this is not a good idea

規則詳情

此規則會回報在其設定中指定的任何預定義字詞的註解。

選項

此規則有一個選項物件字面值

  • "terms":可選的要比對的字詞陣列。預設為 ["todo", "fixme", "xxx"]。字詞比對不區分大小寫,且作為完整單字比對:fix 會比對到 FIX,但不會比對到 fixing。字詞可以包含多個單字:really bad idea
  • "location":可選的字串,設定要在註解中的哪個位置檢查比對。預設為 "start"。開始位置是從第一個非裝飾字元開始,忽略空白字元、換行符號和在 decoration 中指定的字元。另一個值是比對註解中的 anywhere(任何位置)。
  • "decoration":可選的字元陣列,當位置是 "start" 時,會忽略註解開頭的這些字元。預設為 []。會忽略任何空白字元序列或此屬性中的字元。當位置是 "anywhere" 時,會忽略此選項。

預設 { "terms": ["todo", "fixme", "xxx"], "location": "start" } 選項的不正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: "error"*/

/*
FIXME
*/
function callback(err, results) {
  if (err) {
    console.error(err);
    return;
  }
  // TODO
}

預設 { "terms": ["todo", "fixme", "xxx"], "location": "start" } 選項的正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: "error"*/

function callback(err, results) {
  if (err) {
    console.error(err);
    return;
  }
  // NOT READY FOR PRIME TIME
  // but too bad, it is not a predefined warning term
}

字詞和位置

{ "terms": ["todo", "fixme", "any other term"], "location": "anywhere" } 選項的不正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: ["error", { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }]*/

// TODO: this
// todo: this too
// Even this: TODO
/*
 * The same goes for this TODO comment
 * Or a fixme
 * as well as any other term
 */

{ "terms": ["todo", "fixme", "any other term"], "location": "anywhere" } 選項的正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: ["error", { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" }]*/

// This is to do
// even not any other    term
// any other terminal
/*
 * The same goes for block comments
 * with any other interesting term
 * or fix me this
 */

裝飾字元

{ "decoration": ["*"] } 選項的不正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: ["error", { "decoration": ["*"] }]*/

//***** todo decorative asterisks are ignored *****//
/**
 * TODO new lines and asterisks are also ignored in block comments.
 */

{ "decoration": ["/", "*"] } 選項的不正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: ["error", { "decoration": ["/", "*"] }]*/

////// TODO decorative slashes and whitespace are ignored //////
//***** todo decorative asterisks are also ignored *****//
/**
 * TODO new lines are also ignored in block comments.
 */

{ "decoration": ["/", "*"] } 選項的正確程式碼範例

在線上編輯器中開啟
/*eslint no-warning-comments: ["error", { "decoration": ["/", "*"] }]*/

//!TODO preceded by non-decoration character
/**
 *!TODO preceded by non-decoration character in a block comment
 */

何時不該使用

  • 如果您有一個大型程式碼庫,且沒有制定不使用此類警告字詞的政策,您可能會收到數百個警告/錯誤,如果您無法修復所有這些警告/錯誤(例如,如果您沒有時間修復),這可能會適得其反,因為您可能會忽略其他警告/錯誤,或者習慣了許多警告/錯誤,而不再注意它們。
  • 與上述觀點相同的原因:您不應該設定經常使用的字詞(例如,您註解中使用的原生語言的中心部分)。

版本

此規則是在 ESLint v0.4.4 中引入的。

資源

變更語言