# capitalized-comments
强制或禁止评论首字母大写
一些该规则报告的问题可以通过 --fix 命令行选项 自动修复
注释对于为未来的开发人员留下信息很有用。为了使该信息有用且不分散注意力,有时希望评论遵循特定的风格。注释格式样式的一个元素是注释的第一个单词是大写还是小写。
一般来说,没有评论风格比其他任何评论风格或多或少都有效,但许多开发人员会同意一致的风格可以提高项目的可维护性。
# 规则详情
此规则旨在在您的代码库中强制使用一致的注释样式,特别是通过要求或禁止将大写字母作为注释中的第一个单词字符。当使用非大写字母时,此规则不会发出警告。
默认情况下,此规则将要求评论开头使用非小写字母。
此规则的错误代码示例:
/* eslint capitalized-comments: ["error"] */
// lowercase comment
此规则的正确代码示例:
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
# 选项
这个规则有两个选项:一个字符串值 "always" 或 "never",它决定了注释的第一个单词是否应该被要求或禁止,以及一个可选的包含更多规则配置参数的对象。
以下是支持的对象选项:
- ignorePattern:表示此规则应忽略的单词的正则表达式模式的字符串。如果评论的第一个单词与模式匹配,则此规则不会报告该评论。- 请注意,此规则始终忽略以下单词:["jscs", "jshint", "eslint", "istanbul", "global", "globals", "exported"]。
- ignoreInlineComments:如果这是- true,则规则不会报告代码中间的注释。默认情况下,这是- false。
- ignoreConsecutiveComments:如果这是- true,则规则不会报告违反规则的评论,只要该评论紧跟在另一条评论之后。默认情况下,这是- false。
这是一个示例配置:
{
    "capitalized-comments": [
        "error",
        "always",
        {
            "ignorePattern": "pragma|ignored",
            "ignoreInlineComments": true
        }
    ]
}
# "always"
使用 "always" 选项意味着此规则将报告任何以小写字母开头的注释。这是此规则的默认配置。
请注意,从不报告以 URL 开头的配置注释和注释。
此规则的错误代码示例:
/* eslint capitalized-comments: ["error", "always"] */
// lowercase comment
此规则的正确代码示例:
/* eslint capitalized-comments: ["error", "always"] */
// Capitalized comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
/* eslint semi:off */
/* eslint-env node */
/* eslint-disable */
/* eslint-enable */
/* istanbul ignore next */
/* jscs:enable */
/* jshint asi:true */
/* global foo */
/* globals foo */
/* exported myVar */
// eslint-disable-line
// eslint-disable-next-line
// https://github.com
# "never"
使用 "never" 选项意味着该规则将报告任何以大写字母开头的注释。
带有 "never" 选项的错误代码示例:
/* eslint capitalized-comments: ["error", "never"] */
// Capitalized comment
带有 "never" 选项的正确代码示例:
/* eslint capitalized-comments: ["error", "never"] */
// lowercase comment
// 1. Non-letter at beginning of comment
// 丈 Non-Latin character at beginning of comment
# ignorePattern
ignorePattern 对象接受一个字符串值,该值用作应用于评论第一个单词的正则表达式。
将 "ignorePattern" 选项设置为 "pragma" 的正确代码示例:
/* eslint capitalized-comments: ["error", "always", { "ignorePattern": "pragma" }] */
function foo() {
    /* pragma wrap(true) */
}
# ignoreInlineComments
将 ignoreInlineComments 选项设置为 true 意味着代码中间的注释(与注释开头在同一行的标记,与注释结尾在同一行的另一个标记)将不会被此规则报告.
将 "ignoreInlineComments" 选项设置为 true 的正确代码示例:
/* eslint capitalized-comments: ["error", "always", { "ignoreInlineComments": true }] */
function foo(/* ignored */ a) {
}
# ignoreConsecutiveComments
如果将 ignoreConsecutiveComments 选项设置为 true,则不会报告否则违反规则的评论,只要它们紧跟另一条评论即可。这可以多次应用。
ignoreConsecutiveComments 设置为 true 的正确代码示例:
/* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// This comment is valid since it has the correct capitalization.
// this comment is ignored since it follows another comment,
// and this one as well because it follows yet another comment.
/* Here is a block comment which has the correct capitalization, */
/* but this one is ignored due to being consecutive; */
/*
 * in fact, even if any of these are multi-line, that is fine too.
 */
ignoreConsecutiveComments 设置为 true 的错误代码示例:
/* eslint capitalized-comments: ["error", "always", { "ignoreConsecutiveComments": true }] */
// this comment is invalid, but only on this line.
// this comment does NOT get reported, since it is a consecutive comment.
# 对行和块注释使用不同的选项
如果您希望对行注释和块注释有不同的配置,您可以通过使用两种不同的对象配置来实现(请注意,行注释和块注释将一致地强制执行大写选项):
{
    "capitalized-comments": [
        "error",
        "always",
        {
            "line": {
                "ignorePattern": "pragma|ignored",
            },
            "block": {
                "ignoreInlineComments": true,
                "ignorePattern": "ignored"
            }
        }
    ]
}
具有不同行和块注释配置的错误代码示例:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// capitalized line comment, this is incorrect, blockignore does not help here
/* lowercased block comment, this is incorrect too */
具有不同行和块注释配置的正确代码示例:
/* eslint capitalized-comments: ["error", "always", { "block": { "ignorePattern": "blockignore" } }] */
// Uppercase line comment, this is correct
/* blockignore lowercase block comment, this is correct due to ignorePattern */
# 何时不使用
如果您不关心代码库中注释的语法风格,则可以禁用此规则。
# 兼容性
- JSCS:- requireCapitalizedComments
- JSCS:- disallowCapitalizedComments
