跳至内容

@stylistic/

lines-around-comment

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

规则详细信息

此规则要求在注释之前和/或之后留空行。它可以分别为块 (/*) 和行 (//) 注释启用。此规则不适用于与代码位于同一行的注释,也不要求在文件开头或结尾留空行。

选项

此规则有一个对象选项

  • "beforeBlockComment": true (默认) 要求在块注释之前留空行
  • "afterBlockComment": true 要求在块注释之后留空行
  • "beforeLineComment": true 要求在行注释之前留空行
  • "afterLineComment": true 要求在行注释之后留空行
  • "allowBlockStart": true 允许注释出现在块语句、函数体、类、switch 语句和类静态块的开头
  • "allowBlockEnd": true 允许在块语句、函数体、类、switch 语句和类静态块的末尾出现注释
  • "allowObjectStart": true 允许在对象字面量的开头出现注释
  • "allowObjectEnd": true 允许在对象字面量的末尾出现注释
  • "allowArrayStart": true 允许在数组字面量的开头出现注释
  • "allowArrayEnd": true 允许在数组字面量的末尾出现注释
  • "allowClassStart": true 允许在类的开头出现注释
  • "allowClassEnd": true 允许在类的末尾出现注释
  • "applyDefaultIgnorePatterns" 启用或禁用规则忽略的默认注释模式
  • "ignorePattern" 规则忽略的自定义模式
  • "afterHashbangComment": true 要求在哈希邦注释后有一个空行

beforeBlockComment

使用默认选项 { "beforeBlockComment": true } 时,此规则的不正确代码示例

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

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

使用默认选项 { "beforeBlockComment": true } 时,此规则的正确代码示例

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

var night = "long";

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

afterBlockComment

使用 { "afterBlockComment": true } 选项时,此规则的不正确代码示例

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

var night = "long";

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

使用 { "afterBlockComment": true } 选项时,此规则的正确代码示例

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

var night = "long";

/* what a great and wonderful day */

var day = "great"
correct

beforeLineComment

使用 { "beforeLineComment": true } 选项时,此规则的不正确代码示例

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

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

使用 { "beforeLineComment": true } 选项时,此规则的正确代码示例

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

var night = "long";

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

afterLineComment

使用 { "afterLineComment": true } 选项时,此规则的不正确代码示例

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

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

使用 { "afterLineComment": true } 选项时,此规则的正确代码示例

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

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

var day = "great"
correct

allowBlockStart

使用 { "beforeLineComment": true, "allowBlockStart": true } 选项时,此规则的正确代码示例

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

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

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

class C {
    // what a great and wonderful day

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

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

使用 { "beforeBlockComment": true, "allowBlockStart": true } 选项时,此规则的正确代码示例

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

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

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

class C {
    /* what a great and wonderful day */

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

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

switch (foo) {
  /* what a great and wonderful day */

  case 1:
    bar();
    break;
}
correct

allowBlockEnd

使用 { "afterLineComment": true, "allowBlockEnd": true } 选项时,此规则的正确代码示例

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

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

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

class C {

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

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

    // what a great and wonderful day
}
correct

使用 { "afterBlockComment": true, "allowBlockEnd": true } 选项时,此规则的正确代码示例

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

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

    /* what a great and wonderful day */
}

if (bar) {
    foo();

    /* what a great and wonderful day */
}

class C {

    method() {
        foo();

        /* what a great and wonderful day */
    }

    static {
        foo();

        /* what a great and wonderful day */
    }

    /* what a great and wonderful day */
}

switch (foo) {
  case 1:
    bar();
    break;

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

allowClassStart

使用 { "beforeLineComment": true, "allowClassStart": false } 选项时,此规则的错误代码示例

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

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

使用 { "beforeLineComment": true, "allowClassStart": false } 选项时,此规则的正确代码示例

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

class foo {

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

使用 { "beforeLineComment": true, "allowClassStart": true } 选项时,此规则的正确代码示例

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

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

使用 { "beforeBlockComment": true, "allowClassStart": false } 选项时,此规则的错误代码示例

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

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

使用 { "beforeBlockComment": true, "allowClassStart": false } 选项时,此规则的正确代码示例

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

class foo {

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

使用 { "beforeBlockComment": true, "allowClassStart": true } 选项时,此规则的正确代码示例

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

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

allowClassEnd

使用 { "afterLineComment": true, "allowClassEnd": true } 选项时,此规则的正确代码示例

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

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

使用 { "afterBlockComment": true, "allowClassEnd": true } 选项时,此规则的正确代码示例

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

class foo {
    day() {}

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

allowObjectStart

使用 { "beforeLineComment": true, "allowObjectStart": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"};
correct

使用 { "beforeBlockComment": true, "allowObjectStart": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"};
correct

allowObjectEnd

使用 { "afterLineComment": true, "allowObjectEnd": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"};
correct

使用 { "afterBlockComment": true, "allowObjectEnd": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"};
correct

allowArrayStart

使用 { "beforeLineComment": true, "allowArrayStart": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"];
correct

使用 { "beforeBlockComment": true, "allowArrayStart": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"];
correct

allowArrayEnd

使用 { "afterLineComment": true, "allowArrayEnd": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"];
correct

使用 { "afterBlockComment": true, "allowArrayEnd": true } 选项时,此规则的正确代码示例

js
/*eslint @stylistic/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"];
correct

ignorePattern

默认情况下,此规则会忽略以以下单词开头的注释:eslintjshintjslintistanbulglobalexportedjscs。要忽略除默认值之外的更多注释,请将ignorePattern选项设置为将传递给RegExp 构造函数的字符串模式。

ignorePattern 选项的正确代码示例

js
/
*eslint @stylistic/lines-around-comment: ["error"]*/
foo(); /* eslint mentioned in this comment */ bar();
/*eslint @stylistic/lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
foo();
/* a valid comment using pragma in it */
correct

ignorePattern 选项的错误代码示例

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

1 + 1;
/* something else */
incorrect

applyDefaultIgnorePatterns

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

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

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

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

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

js
/
*eslint @stylistic/lines-around-comment: ["error", { "applyDefaultIgnorePatterns": false }] */
foo();
/* eslint mentioned in comment */
incorrect

afterHashbangComment

使用{ "afterHashbangComment": true } 选项时,此规则的错误代码示例

js
#!foo
var day = "great" /*eslint @stylistic/lines-around-comment: ["error", { "afterHashbangComment": true }] */
incorrect

使用{ "afterHashbangComment": true } 选项时,此规则的正确代码示例

js
#!foo

var day = "great"

/*eslint @stylistic/lines-around-comment: ["error", { "afterHashbangComment": true }] */
correct

何时不使用它

许多人喜欢更简洁的代码风格,并且不介意注释紧挨着代码。如果您属于此类,则此规则不适合您。

TypeScript 特定

ts/lines-around-comment

选项

除了js/lines-around-comment 规则支持的选项外,该规则还添加了以下选项

  • allowEnumEnd: true 不需要在枚举体块结束符后留空行
  • allowEnumStart: true 不需要在枚举体块开始符前留空行
  • allowInterfaceEnd: true 不需要在接口体块结束符前留空行
  • allowInterfaceStart: true 不需要在接口体块开始符后留空行
  • allowModuleEnd: true 不需要在模块体块结束符前留空行
  • allowModuleStart: true 不需要在模块体块开始符后留空行
  • allowTypeEnd: true 不需要在类型字面量块结束符前留空行
  • allowTypeStart: true 不需要在类型文字块开始后留空行