コンテンツへスキップ

@stylistic/js/

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/js/lines-around-comment: ["error", { "beforeBlockComment": true }]*/

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

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

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

var night = "long";

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

afterBlockComment

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

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

var night = "long";

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

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

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

var night = "long";

/* what a great and wonderful day */

var day = "great"
正しい

beforeLineComment

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

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

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

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

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

var night = "long";

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

afterLineComment

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

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

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

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

js
/*eslint @stylistic/js/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/js/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/js/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/js/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/js/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/js/lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/

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

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

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

class foo {

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

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

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

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

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

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

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

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

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

class foo {

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

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

js
/*eslint @stylistic/js/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/js/lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/

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

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

js
/*eslint @stylistic/js/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/js/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/js/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/js/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/js/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/js/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/js/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/js/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/js/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という単語で始まるコメントを無視します。デフォルトに加えてさらに多くのコメントを無視するには、RegExpコンストラクタに渡される文字列パターンにignorePatternオプションを設定します。

ignorePatternオプションの正しいコードの例

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

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

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

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

applyDefaultIgnorePatterns

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

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

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

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

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

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

afterHashbangComment

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

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

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

js
#!foo

var day = "great"

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

使用しない場合

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

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