版本

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": 可选的字符数组,当 location 为 "start" 时,在注释开头处被忽略。默认为 []。任何空格序列或来自此属性的字符都将被忽略。当 location 为 "anywhere" 时,此选项将被忽略。

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

在 Playground 中打开
/*eslint no-warning-comments: "error"*/

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

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

在 Playground 中打开
/*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 和 location

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

在 Playground 中打开
/*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" }正确代码示例

在 Playground 中打开
/*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": ["*"] }错误代码示例

在 Playground 中打开
/*eslint no-warning-comments: ["error", { "decoration": ["*"] }]*/

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

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

在 Playground 中打开
/*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": ["/", "*"] }正确代码示例

在 Playground 中打开
/*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 中引入。

资源

更改语言