非常教程

Eslint参考手册

规则 | Rules

lines-around-comment

--fix命令行上的选项可以自动修复一些被这条规则反映的问题。

许多风格指南在评论之前或之后需要空行。这些规则的主要目标是使评论更易于阅读并提高代码的可读性。

规则细节

此规则在评论之前和/或之后需要空行。它可以单独为block(/*)和line(//)注释启用。此规则不适用于与代码显示在同一行上的注释,并且不需要在文件的开头或结尾处出现空行。

选项

该规则有一个对象选项:

  • "beforeBlockComment": true (默认)在块注释之前需要一个空行
  • "afterBlockComment": true 块注释后需要一个空行
  • "beforeLineComment": true 行注释前需要一个空行
  • "afterLineComment": true 行注释后需要一个空行
  • "allowBlockStart": true 允许评论出现在块语句的开始处
  • "allowBlockEnd": true 允许注释出现在块语句的末尾
  • "allowObjectStart": true 允许评论出现在对象文字的开头
  • "allowObjectEnd": true 允许注释出现在对象文字的末尾
  • "allowArrayStart": true 允许注释出现在数组文字的开头
  • "allowArrayEnd": true 允许注释出现在数组文字的末尾
  • "allowClassStart": true 允许评论出现在课程开始时
  • "allowClassEnd": true 允许评论出现在课程结束时
  • "applyDefaultIgnorePatterns" 启用或禁用规则忽略的默认评论模式
  • "ignorePattern" 自定义模式被规则忽略

beforeBlockComment

此规则的默认代码错误代码示例{ "beforeBlockComment": true }

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/

var night = "long";
/* what a great and wonderful day */
var day = "great"

具有默认选项的此规则的正确代码示例{ "beforeBlockComment": true }

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/

var night = "long";

/* what a great and wonderful day */
var day = "great"

afterBlockComment

此规则的错误代码示例包含以下{ "afterBlockComment": true }选项:

/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/

var night = "long";

/* what a great and wonderful day */
var day = "great"

此规则的正确代码示例包含以下{ "afterBlockComment": true }选项:

/*eslint lines-around-comment: ["error", { "afterBlockComment": true }]*/

var night = "long";

/* what a great and wonderful day */

var day = "great"

beforeLineComment

此规则的错误代码示例包含以下{ "beforeLineComment": true }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/

var night = "long";
// what a great and wonderful day
var day = "great"

此规则的正确代码示例包含以下{ "beforeLineComment": true }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true }]*/

var night = "long";

// what a great and wonderful day
var day = "great"

afterLineComment

此规则的错误代码示例包含以下{ "afterLineComment": true }选项:

/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/

var night = "long";
// what a great and wonderful day
var day = "great"

此规则的正确代码示例包含以下{ "afterLineComment": true }选项:

/*eslint lines-around-comment: ["error", { "afterLineComment": true }]*/

var night = "long";
// what a great and wonderful day

var day = "great"

allowBlockStart

此规则的正确代码示例包含以下{ "beforeLineComment": true, "allowBlockStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/

function foo(){
    // what a great and wonderful day
    var day = "great"
    return day;
}

此规则的正确代码示例包含以下{ "beforeBlockComment": true, "allowBlockStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowBlockStart": true }]*/

function foo(){
    /* what a great and wonderful day */
    var day = "great"
    return day;
}

allowBlockEnd

此规则的正确代码示例包含以下{ "afterLineComment": true, "allowBlockEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowBlockEnd": true }]*/

function foo(){
    var day = "great"
    return day;
    // what a great and wonderful day
}

此规则的正确代码示例包含以下{ "afterBlockComment": true, "allowBlockEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowBlockEnd": true }]*/

function foo(){
    var day = "great"
    return day;

    /* what a great and wonderful day */
}

allowClassStart

此规则的错误代码示例包含以下{ "beforeLineComment": true, "allowClassStart": false }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/

class foo {
    // what a great and wonderful day
    day() {}
};

此规则的正确代码示例包含以下{ "beforeLineComment": true, "allowClassStart": false }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/

class foo {

    // what a great and wonderful day
    day() {}
};

此规则的正确代码示例包含以下{ "beforeLineComment": true, "allowClassStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/

class foo {
    // what a great and wonderful day
    day() {}
};

此规则的错误代码示例包含以下{ "beforeBlockComment": true, "allowClassStart": false }选项:

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/

class foo {
    /* what a great and wonderful day */
    day() {}
};

此规则的正确代码示例包含以下{ "beforeBlockComment": true, "allowClassStart": false }选项:

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/

class foo {

    /* what a great and wonderful day */
    day() {}
};

此规则的正确代码示例包含以下{ "beforeBlockComment": true, "allowClassStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/

class foo {
    /* what a great and wonderful day */
    day() {}
};

allowClassEnd

此规则的正确代码示例包含以下{ "afterLineComment": true, "allowClassEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/

class foo {
    day() {}
    // what a great and wonderful day
};

此规则的正确代码示例包含以下{ "afterBlockComment": true, "allowClassEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/

class foo {
    day() {}

    /* what a great and wonderful day */
};

allowObjectStart

此规则的正确代码示例包含以下{ "beforeLineComment": true, "allowObjectStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/

var foo = {
    // what a great and wonderful day
    day: "great"
};

const {
    // what a great and wonderful day
    foo: someDay
} = {foo: "great"};

const {
    // what a great and wonderful day
    day
} = {day: "great"};

此规则的正确代码示例包含以下{ "beforeBlockComment": true, "allowObjectStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowObjectStart": true }]*/

var foo = {
    /* what a great and wonderful day */
    day: "great"
};

const {
    /* what a great and wonderful day */
    foo: someDay
} = {foo: "great"};

const {
    /* what a great and wonderful day */
    day
} = {day: "great"};

allowObjectEnd

此规则的正确代码示例包含以下{ "afterLineComment": true, "allowObjectEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowObjectEnd": true }]*/

var foo = {
    day: "great"
    // what a great and wonderful day
};

const {
    foo: someDay
    // what a great and wonderful day
} = {foo: "great"};

const {
    day
    // what a great and wonderful day
} = {day: "great"};

此规则的正确代码示例包含以下{ "afterBlockComment": true, "allowObjectEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowObjectEnd": true }]*/

var foo = {
    day: "great"

    /* what a great and wonderful day */
};

const {
    foo: someDay

    /* what a great and wonderful day */
} = {foo: "great"};

const {
    day

    /* what a great and wonderful day */
} = {day: "great"};

allowArrayStart

此规则的正确代码示例包含以下{ "beforeLineComment": true, "allowArrayStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowArrayStart": true }]*/

var day = [
    // what a great and wonderful day
    "great",
    "wonderful"
];

const [
    // what a great and wonderful day
    someDay
] = ["great", "not great"];

此规则的正确代码示例包含以下{ "beforeBlockComment": true, "allowArrayStart": true }选项:

/*eslint lines-around-comment: ["error", { "beforeBlockComment": true, "allowArrayStart": true }]*/

var day = [
    /* what a great and wonderful day */
    "great",
    "wonderful"
];

const [
    /* what a great and wonderful day */
    someDay
] = ["great", "not great"];

allowArrayEnd

此规则的正确代码示例包含以下{ "afterLineComment": true, "allowArrayEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterLineComment": true, "allowArrayEnd": true }]*/

var day = [
    "great",
    "wonderful"
    // what a great and wonderful day
];

const [
    someDay
    // what a great and wonderful day
] = ["great", "not great"];

此规则的正确代码示例包含以下{ "afterBlockComment": true, "allowArrayEnd": true }选项:

/*eslint lines-around-comment: ["error", { "afterBlockComment": true, "allowArrayEnd": true }]*/

var day = [
    "great",
    "wonderful"

    /* what a great and wonderful day */
];

const [
    someDay

    /* what a great and wonderful day */
] = ["great", "not great"];

ignorePattern

默认情况下,这个规则忽略先从下面的话评论:eslintjshintjslintistanbulglobalexportedjscs。可以提供一个替代的正则表达式。

选项的正确代码示例ignorePattern

/*eslint lines-around-comment: ["error"]*/

foo();
/* eslint mentioned in this comment */,
bar();


/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */

foo();
/* a valid comment using pragma in it */

选项的错误代码示例ignorePattern

/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */

1 + 1;
/* something else */

applyDefaultIgnorePatterns

即使ignorePattern提供默认忽略模式也会应用。如果您想省略默认模式,请将此选项设置为false

选项的正确代码示例{ "applyDefaultIgnorePatterns": false }

/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */

foo();
/* a valid comment using pragma in it */

选项的错误代码示例{ "applyDefaultIgnorePatterns": false }

/*eslint lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */

foo();
/* eslint mentioned in comment */

何时不使用它

许多人都喜欢一种比较特别的代码风格,不介意代码碰撞的评论。如果你属于这个类别,这条规则不适合你。

相关规则

  • space-before-blocks
  • spaced-comment

版本

这条规则是在 ESLint 0.22.0 中引入的。

资源

  • Rule source
  • Documentation source

规则 | 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.capitalized-comments
15.class-methods-use-this
16.comma-dangle
17.comma-spacing
18.comma-style
19.complexity
20.computed-property-spacing
21.consistent-return
22.consistent-this
23.constructor-super
24.curly
25.default-case
26.dot-location
27.dot-notation
28.eol-last
29.eqeqeq
30.for-direction
31.func-call-spacing
32.func-name-matching
33.func-names
34.func-style
35.function-paren-newline
36.generator-star
37.generator-star-spacing
38.getter-return
39.global-require
40.global-strict
41.guard-for-in
42.handle-callback-err
43.id-blacklist
44.id-length
45.id-match
46.implicit-arrow-linebreak
47.indent
48.indent-legacy
49.init-declarations
50.jsx-quotes
51.key-spacing
52.keyword-spacing
53.line-comment-position
54.linebreak-style
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