no-warning-comments

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

开发者经常向不完整或需要审查的代码添加注释。在你认为代码已准备好生产之前,你很可能希望修复或查看代码,然后删除注释。

¥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

    ¥"terms": optional array of terms to match. Defaults to ["todo", "fixme", "xxx"]. Terms are matched case-insensitively and as whole words: fix would match FIX but not fixing. Terms can consist of multiple words: really bad idea.

  • "location":可选字符串,用于配置在你的注释中检查匹配项的位置。默认为 "start"。从第一个非装饰字符开始,忽略 decoration 中指定的空格、换行符和字符。另一个值是注释中的匹配 anywhere

    ¥"location": optional string that configures where in your comments to check for matches. Defaults to "start". The start is from the first non-decorative character, ignoring whitespace, new lines and characters specified in decoration. The other value is match anywhere in comments.

  • "decoration":当位置为 "start" 时,在注释开头忽略的可选字符数组。默认为 []。忽略此属性中的任何空格序列或字符。当位置为 "anywhere" 时,此选项将被忽略。

    ¥"decoration": optional array of characters that are ignored at the start of a comment, when location is "start". Defaults to []. Any sequence of whitespace or the characters from this property are ignored. This option is ignored when location is "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

  • 如果你有一个大型代码库,而该代码库没有按照不使用此类警告术语的政策开发,你可能会收到数百个警告/错误,如果你无法修复所有这些警告/错误(例如,如果你不这样做,则可能会适得其反)没有时间去做)因为你可能会忽略其他警告/错误或习惯其中的许多并且不再关注它。

    ¥If you have a large code base that was not developed with a policy to not use such warning terms, you might get hundreds of warnings / errors which might be counter-productive if you can’t fix all of them (e.g. if you don’t get the time to do it) as you might overlook other warnings / errors or get used to many of them and don’t pay attention on it anymore.

  • 与上述相同的原因:你不应配置经常使用的术语(例如,你的注释中使用的母语的中心部分)。

    ¥Same reason as the point above: You shouldn’t configure terms that are used very often (e.g. central parts of the native language used in your comments).

版本

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

资源

ESLint 中文网
粤ICP备13048890号