コンテンツへスキップ

@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"
不正

デフォルトの`{ "beforeBlockComment": true }`オプションを使用したこのルールの**正しい**コードの例

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

var night = "long";

/* what a great and wonderful day */
var day = "great"
正しい

afterBlockComment

`{ "afterBlockComment": true }`オプションを使用したこのルールの**不正な**コードの例

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

var night = "long";

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

`{ "afterBlockComment": true }`オプションを使用したこのルールの**正しい**コードの例

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

var night = "long";

/* what a great and wonderful day */

var day = "great"
正しい

beforeLineComment

`{ "beforeLineComment": true }`オプションを使用したこのルールの**不正な**コードの例

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

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

`{ "beforeLineComment": true }`オプションを使用したこのルールの**正しい**コードの例

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

var night = "long";

// what a great and wonderful day
var day = "great"
正しい

afterLineComment

`{ "afterLineComment": true }`オプションを使用したこのルールの**不正な**コードの例

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

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

`{ "afterLineComment": true }`オプションを使用したこのルールの**正しい**コードの例

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

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

var day = "great"
正しい

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();
    }
}
正しい

`{ "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;
}
正しい

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
}
正しい

`{ "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 */
}
正しい

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() {} };
不正

`{ "beforeLineComment": true, "allowClassStart": false }`オプションを使用したこのルールの**正しい**コードの例

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

class foo {

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

`{ "beforeLineComment": true, "allowClassStart": true }`オプションを使用したこのルールの**正しい**コードの例

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

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

`{ "beforeBlockComment": true, "allowClassStart": false }`オプションを使用したこのルールの**不正な**コードの例

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

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

`{ "beforeBlockComment": true, "allowClassStart": false }`オプションを使用したこのルールの**正しい**コードの例

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

class foo {

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

`{ "beforeBlockComment": true, "allowClassStart": true }`オプションを使用したこのルールの**正しい**コードの例

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

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

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
};
正しい

`{ "afterBlockComment": true, "allowClassEnd": true }`オプションを使用したこのルールの**正しい**コードの例

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

class foo {
    day() {}

    /* what a great and wonderful day */
};
正しい

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"};
正しい

`{ "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"};
正しい

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"};
正しい

`{ "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"};
正しい

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"];
正しい

`{ "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"];
正しい

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"];
正しい

`{ "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"];
正しい

ignorePattern

デフォルトでは、このルールは`eslint`、`jshint`、`jslint`、`istanbul`、`global`、`exported`、`jscs`という単語で始まるコメントを無視します。デフォルトに加えてさらに多くのコメントを無視するには、``RegExp` コンストラクタ`に渡される文字列パターンに`ignorePattern`オプションを設定します。

`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 */
正しい

`ignorePattern`オプションの**不正な**コードの例

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

1 + 1;
/* something else */
不正

applyDefaultIgnorePatterns

`ignorePattern`が提供されている場合でも、デフォルトの無視パターンが適用されます。デフォルトのパターンを省略する場合は、このオプションを`false`に設定します。

`{ "applyDefaultIgnorePatterns": false }`オプションの**正しい**コードの例

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

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

`{ "applyDefaultIgnorePatterns": false }`オプションの**不正な**コードの例

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

afterHashbangComment

`{ "afterHashbangComment": true }`オプションを使用したこのルールの**不正な**コードの例

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

`{ "afterHashbangComment": true }`オプションを使用したこのルールの**正しい**コードの例

js
#!foo

var day = "great"

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

使用しない場合

多くの人は、より簡潔なコードスタイルを好み、コメントがコードに隣接していても気になりません。そのような場合は、このルールは必要ありません。

TypeScript固有

ts/lines-around-comment

オプション

`js/lines-around-comment`ルールでサポートされているオプションに加えて、このルールでは次のオプションが追加されています。

  • `allowEnumEnd: true` は、enumボディブロックの末尾の後に空行を要求しません。
  • `allowEnumStart: true` は、enumボディブロックの先頭に空行を要求しません。
  • `allowInterfaceEnd: true` は、インターフェースボディブロックの末尾の前に空行を要求しません。
  • `allowInterfaceStart: true` は、インターフェースボディブロックの先頭の後に空行を要求しません。
  • allowModuleEnd: true は、モジュール本体ブロックの終了前に空行を必要としません。
  • allowModuleStart: true は、モジュール本体ブロックの開始後に空行を必要としません。
  • allowTypeEnd: true は、型リテラルブロックの終了前に空行を必要としません。
  • allowTypeStart: true は、型リテラルブロックの開始後に空行を必要としません。

MITライセンスの下で公開されています。