require empty lines around comments (lines-around-comment)

强制注释周围有空行 (lines-around-comment)

The --fix option on the command line can automatically fix some of the problems reported by this rule.

命令行中的 --fix 选项可以自动修复一些该规则报告的问题。

Many style guides require empty lines before or after comments. The primary goal of these rules is to make the comments easier to read and improve readability of the code.

很多风格指南要求在注释前后要有空行。该规则的主要目标是使这些注释更易阅读,提高代码的可读性。

Rule Details

This rule requires empty lines before and/or after comments. It can be enabled separately for both block (/*) and line (//) comments. This rule does not apply to comments that appear on the same line as code and does not require empty lines at the beginning or end of a file.

该规则允许你指定在注释之前或之后是否应该有空行。它可以区分块级注释 (/*) 和单行注释(//)。该规则不适用于注释和代码出现在同一行的情况,也不要求在文件开头和末尾有空行。空行

Options

This rule has an object option:

该规则有一个对象选项:

beforeBlockComment

Examples of incorrect code for this rule with the default { "beforeBlockComment": true } option:

默认选项 { "beforeBlockComment": true }错误 代码示例:

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

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

Examples of correct code for this rule with the default { "beforeBlockComment": true } option:

默认选项 { "beforeBlockComment": true }正确 代码示例:

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

var night = "long";

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

afterBlockComment

Examples of incorrect code for this rule with the { "afterBlockComment": true } option:

选项 { "afterBlockComment": true }错误 代码示例:

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

var night = "long";

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

Examples of correct code for this rule with the { "afterBlockComment": true } option:

选项 { "afterBlockComment": true }正确 代码示例:

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

var night = "long";

/* what a great and wonderful day */

var day = "great"

beforeLineComment

Examples of incorrect code for this rule with the { "beforeLineComment": true } option:

选项 { "beforeLineComment": true }错误 代码示例:

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

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

Examples of correct code for this rule with the { "beforeLineComment": true } option:

选项 { "beforeLineComment": true }正确 代码示例:

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

var night = "long";

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

afterLineComment

Examples of incorrect code for this rule with the { "afterLineComment": true } option:

选项 { "afterLineComment": true }错误 代码示例:

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

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

Examples of correct code for this rule with the { "afterLineComment": true } option:

选项 { "afterLineComment": true }正确 代码示例:

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

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

var day = "great"

allowBlockStart

Examples of correct code for this rule with the { "beforeLineComment": true, "allowBlockStart": true } options:

选项 { "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;
}

Examples of correct code for this rule with the { "beforeBlockComment": true, "allowBlockStart": true } options:

选项 { "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

Examples of correct code for this rule with the { "afterLineComment": true, "allowBlockEnd": true } option:

选项 { "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
}

Examples of correct code for this rule with the { "afterBlockComment": true, "allowBlockEnd": true } option:

选项 { "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

Examples of incorrect code for this rule with the { "beforeLineComment": true, "allowClassStart": false } option:

选项 { "beforeLineComment": true, "allowClassStart": false }错误 代码示例:

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

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

Examples of correct code for this rule with the { "beforeLineComment": true, "allowClassStart": false } option:

选项 { "beforeLineComment": true, "allowClassStart": false }正确 代码示例:

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

class foo {

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

Examples of correct code for this rule with the { "beforeLineComment": true, "allowClassStart": true } option:

选项 { "beforeLineComment": true, "allowClassStart": true }正确 代码示例:

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

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

Examples of incorrect code for this rule with the { "beforeBlockComment": true, "allowClassStart": false } option:

选项 { "beforeBlockComment": true, "allowClassStart": false }错误 代码示例:

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

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

Examples of correct code for this rule with the { "beforeBlockComment": true, "allowClassStart": false } option:

选项 { "beforeBlockComment": true, "allowClassStart": false }正确 代码示例:

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

class foo {

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

Examples of correct code for this rule with the { "beforeBlockComment": true, "allowClassStart": true } option:

选项 { "beforeBlockComment": true, "allowClassStart": true }正确 代码示例:

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

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

allowClassEnd

Examples of correct code for this rule with the { "afterLineComment": true, "allowClassEnd": true } option:

选项 { "afterLineComment": true, "allowClassEnd": true }正确 代码示例:

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

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

Examples of correct code for this rule with the { "afterBlockComment": true, "allowClassEnd": true } option:

选项 { "afterBlockComment": true, "allowClassEnd": true }正确 代码示例:

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

class foo {
    day() {}

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

allowObjectStart

Examples of correct code for this rule with the { "beforeLineComment": true, "allowObjectStart": true } option:

选项 { "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"};

Examples of correct code for this rule with the { "beforeBlockComment": true, "allowObjectStart": true } option:

选项 { "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

Examples of correct code for this rule with the { "afterLineComment": true, "allowObjectEnd": true } option:

选项 { "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"};

Examples of correct code for this rule with the { "afterBlockComment": true, "allowObjectEnd": true } option:

选项 { "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

Examples of correct code for this rule with the { "beforeLineComment": true, "allowArrayStart": true } option:

选项 { "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"];

Examples of correct code for this rule with the { "beforeBlockComment": true, "allowArrayStart": true } option:

选项 { "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

Examples of correct code for this rule with the { "afterLineComment": true, "allowArrayEnd": true } option:

选项 { "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"];

Examples of correct code for this rule with the { "afterBlockComment": true, "allowArrayEnd": true } option:

选项 { "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

By default this rule ignores comments starting with the following words: eslint, jshint, jslint, istanbul, global, exported, jscs. An alternative regular expression can be provided.

默认情况下,该规则忽略以 eslintjshintjslintistanbulglobalexportedjscs 开头的注释。可以提供另一种正则表达式。

Examples of correct code for the ignorePattern option:

选项 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 */

Examples of incorrect code for the ignorePattern option:

选项 ignorePattern错误 代码示例:

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

1 + 1;
/* something else */

applyDefaultIgnorePatterns

Default ignore patterns are applied even when ignorePattern is provided. If you want to omit default patterns, set this option to false.

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

Examples of correct code for the { "applyDefaultIgnorePatterns": false } option:

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

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

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

Examples of incorrect code for the { "applyDefaultIgnorePatterns": false } option:

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

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

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

When Not To Use It

Many people enjoy a terser code style and don’t mind comments bumping up against code. If you fall into that category this rule is not for you.

很多人喜欢简洁的代码风格,不介意代码中注释的风格。如果你也是这样的,此规则不适合你。

Version

This rule was introduced in ESLint 0.22.0.

该规则在 ESLint 0.22.0 中被引入。

Resources