非常教程

Eslint参考手册

规则 | Rules

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 capitalize-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 capitalize-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和disallowCapitalizedComments

该规则在ESLint 3.11.0中引入。

资源

  • 规则来源
  • 文档来源

规则 | Rules相关

1.accessor-pairs
2.array-bracket-newline
3.array-bracket-spacing
4.array-callback-return
5.array-element-newline
6.arrow-body-style
7.arrow-parens
8.arrow-spacing
9.block-scoped-var
10.block-spacing
11.brace-style
12.callback-return
13.camelcase
14.class-methods-use-this
15.comma-dangle
16.comma-spacing
17.comma-style
18.complexity
19.computed-property-spacing
20.consistent-return
21.consistent-this
22.constructor-super
23.curly
24.default-case
25.dot-location
26.dot-notation
27.eol-last
28.eqeqeq
29.for-direction
30.func-call-spacing
31.func-name-matching
32.func-names
33.func-style
34.function-paren-newline
35.generator-star
36.generator-star-spacing
37.getter-return
38.global-require
39.global-strict
40.guard-for-in
41.handle-callback-err
42.id-blacklist
43.id-length
44.id-match
45.implicit-arrow-linebreak
46.indent
47.indent-legacy
48.init-declarations
49.jsx-quotes
50.key-spacing
51.keyword-spacing
52.line-comment-position
53.linebreak-style
54.lines-around-comment
55.lines-around-directive
56.lines-between-class-members
57.max-depth
58.max-len
59.max-lines
60.max-nested-callbacks
61.max-params
62.max-statements
63.max-statements-per-line
64.multiline-comment-style
65.multiline-ternary
66.new-cap
67.new-parens
68.newline-after-var
69.newline-before-return
70.newline-per-chained-call
71.no-alert
72.no-array-constructor
73.no-arrow-condition
74.no-await-in-loop
75.no-bitwise
76.no-buffer-constructor
77.no-caller
78.no-case-declarations
79.no-catch-shadow
80.no-class-assign
81.no-comma-dangle
82.no-compare-neg-zero
83.no-cond-assign
84.no-confusing-arrow
85.no-console
86.no-const-assign
87.no-constant-condition
88.no-continue
89.no-control-regex
90.no-debugger
91.no-delete-var
92.no-div-regex
93.no-dupe-args
94.no-dupe-class-members
95.no-dupe-keys
96.no-duplicate-case
97.no-duplicate-imports
98.no-else-return
99.no-empty
100.no-empty-character-class
Eslint

ESLint 是一个代码规范和错误检查工具,有以下几个特性。所有东西都是可以插拔的。你可以调用任意的 rule api 或者 formatter api 去打包或者定义 rule or formatter。任意的 rule 都是独立的。没有特定的 coding style,你可以自己配置。

主页 https://eslint.org/
源码 https://github.com/eslint/eslint
发布版本 4.12.0

Eslint目录

1.指南 | Guide
2.规则 | Rules