lines-around-comment
要求注释周围有空行
此规则报告的某些问题可以通过 --fix 命令行 选项自动修复
许多风格指南要求在注释之前或之后有空行。这些规则的主要目标是使注释更易于阅读并提高代码的可读性。
规则详情
此规则要求在注释之前和/或之后有空行。它可以分别为块 (/*) 和行 (//) 注释启用。此规则不适用于与代码出现在同一行上的注释,并且不要求在文件开头或结尾处有空行。
选项
此规则具有对象选项
- "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要求在 Hashbang 注释之后有空行
beforeBlockComment
对于使用默认 { "beforeBlockComment": true } 选项的此规则,以下是错误代码示例
/*eslint lines-around-comment: ["error", { "beforeBlockComment": true }]*/
var night = "long";
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";
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";
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";
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;
}
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();
    }
}
对于使用 { "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;
}
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;
}
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
}
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
}
对于使用 { "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 */
}
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 */
}
allowClassStart
对于使用 { "beforeLineComment": true, "allowClassStart": false } 选项的此规则,以下是错误代码示例
/*eslint lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/
class foo {
    
    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 {
    
    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
默认情况下,此规则忽略以以下单词开头的注释:eslint, jshint, jslint, istanbul, global, exported, jscs。
对于此规则,以下是正确代码示例
/*eslint lines-around-comment: ["error"]*/
foo();
/* jshint mentioned in this comment */
bar();
要忽略除默认值之外的更多注释,请将 ignorePattern 选项设置为将传递给 RegExp 构造函数的字符串模式。
对于 ignorePattern 选项,以下是正确代码示例
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
foo();
/* jshint mentioned in this comment */
bar();
foo();
/* a valid comment using pragma in it */
对于 ignorePattern 选项,以下是错误代码示例
/*eslint lines-around-comment: ["error", { "ignorePattern": "pragma" }] */
1 + 1;
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();
afterHashbangComment
对于使用 { "afterHashbangComment": true } 选项的此规则,以下是错误代码示例
var day = "great"
/*eslint lines-around-comment: ["error", { "afterHashbangComment": true }] */
对于使用 { "afterHashbangComment": true } 选项的此规则,以下是正确代码示例
#!foo
var day = "great"
/*eslint lines-around-comment: ["error", { "afterHashbangComment": true }] */
何时不使用它
许多人喜欢更简洁的代码风格,并且不介意注释紧靠代码。如果您属于这一类,则此规则不适合您。
相关规则
版本
此规则在 ESLint v0.22.0 中引入。