Index

no-warning-comments

不允许在注释中指定警告条款

❄️ Frozen

此规则目前已 冻结,不接受功能请求。

开发者经常在不完整或需要审查的代码中添加注释。最可能的情况是你想修复或审查代码,然后在认为代码可以投入生产之前删除注释。

🌐 Developers often add comments to code which is not complete or needs review. Most likely you want to fix or review the code, and then remove the comment, before you consider the code to be production ready.

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

规则详情

🌐 Rule Details

此规则报告包含其配置中指定的任何预定义术语的注释。

🌐 This rule reports comments that include any of the predefined terms specified in its configuration.

选项

🌐 Options

此规则有一个选项对象字面量:

🌐 This rule has an options object literal:

  • "terms":可选的要匹配的术语数组。默认值为 ["todo", "fixme", "xxx"]。术语匹配不区分大小写,并且按整个单词匹配:fix 会匹配 FIX 但不会匹配 fixing。术语可以由多个单词组成:really bad idea
  • "location":可选字符串,用于配置在评论中检查匹配的位置。默认为 "start"。起点从第一个非装饰字符开始,忽略空格、换行符和 decoration 中指定的字符。另一个值是在评论中匹配 anywhere
  • "decoration":可选的字符数组,在注释开头被忽略,当位置为 "start" 时。默认值为 []。任何空白字符或来自此属性的字符序列都会被忽略。当位置为 "anywhere" 时,此选项会被忽略。

默认 { "terms": ["todo", "fixme", "xxx"], "location": "start" } 选项的错误代码示例:

🌐 Example of incorrect code for the default { "terms": ["todo", "fixme", "xxx"], "location": "start" } options:

在线运行
/*eslint no-warning-comments: "error"*/

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

默认 { "terms": ["todo", "fixme", "xxx"], "location": "start" } 选项的 正确 代码示例:

🌐 Example of correct code for the default { "terms": ["todo", "fixme", "xxx"], "location": "start" } options:

在线运行
/*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 and location

针对 { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" } 选项的错误代码示例:

🌐 Examples of incorrect code for the { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" } options:

在线运行
/*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" } 选项的正确代码示例:

🌐 Examples of correct code for the { "terms": ["todo", "fixme", "any other term"], "location": "anywhere" } options:

在线运行
/*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 Characters

针对 { "decoration": ["*"] } 选项的错误代码示例:

🌐 Examples of incorrect code for the { "decoration": ["*"] } options:

在线运行
/*eslint no-warning-comments: ["error", { "decoration": ["*"] }]*/

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

针对 { "decoration": ["/", "*"] } 选项的错误代码示例:

🌐 Examples of incorrect code for the { "decoration": ["/", "*"] } options:

在线运行
/*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": ["/", "*"] } 选项的正确代码示例:

🌐 Examples of correct code for the { "decoration": ["/", "*"] } options:

在线运行
/*eslint no-warning-comments: ["error", { "decoration": ["/", "*"] }]*/

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

何时不使用

🌐 When Not To Use It

  • 如果你有一个大型代码库,而该代码库没有按照不使用此类警告术语的政策开发,你可能会收到数百个警告/错误,如果你无法修复所有这些警告/错误(例如,如果你不这样做,则可能会适得其反)没有时间去做)因为你可能会忽略其他警告/错误或习惯其中的许多并且不再关注它。
  • 与上面一点的理由相同:你不应该配置使用频率很高的术语(例如在评论中使用的母语核心部分)。

版本

此规则是在 ESLint v0.4.4 中引入。

资源