/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeBlockComment": true }]*/var night = "long";/* what a great and wonderful day */var day = "great"
/*eslint @stylistic/js/lines-around-comment: ["error", { "afterBlockComment": true }]*/var night = "long";/* what a great and wonderful day */var day = "great"
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeLineComment": true }]*/var night = "long";// what a great and wonderful dayvar day = "great"
/*eslint @stylistic/js/lines-around-comment: ["error", { "afterLineComment": true }]*/var night = "long";// what a great and wonderful dayvar day = "great"
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeLineComment": true, "allowBlockStart": true }]*/function foo(){// what a great and wonderful dayvar day = "great"return day;}if (bar) {// what a great and wonderful dayfoo();}class C {// what a great and wonderful daymethod() {// what a great and wonderful dayfoo();}static {// what a great and wonderful dayfoo();}}
/*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;}
/*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}
/*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 */}
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": false }]*/class foo {// what a great and wonderful dayday() {}};
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeLineComment": true, "allowClassStart": true }]*/class foo {// what a great and wonderful dayday() {}};
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": false }]*/class foo {/* what a great and wonderful day */day() {}};
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeBlockComment": true, "allowClassStart": true }]*/class foo {/* what a great and wonderful day */day() {}};
/*eslint @stylistic/js/lines-around-comment: ["error", { "afterLineComment": true, "allowClassEnd": true }]*/class foo {day() {}// what a great and wonderful day};
/*eslint @stylistic/js/lines-around-comment: ["error", { "afterBlockComment": true, "allowClassEnd": true }]*/class foo {day() {}/* what a great and wonderful day */};
/*eslint @stylistic/js/lines-around-comment: ["error", { "beforeLineComment": true, "allowObjectStart": true }]*/var foo = {// what a great and wonderful dayday: "great"};const {// what a great and wonderful dayfoo: someDay} = {foo: "great"};const {// what a great and wonderful dayday} = {day: "great"};
/*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"};
/*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"};
/*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"};
/*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 daysomeDay] = ["great", "not great"];
/*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"];
/*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"];
/*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"];
/*eslint @stylistic/js/lines-around-comment: ["error", { "ignorePattern": "pragma", applyDefaultIgnorePatterns: false }] */foo();/* a valid comment using pragma in it */
@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 }
オプションを使用したこのルールの不正なコードの例デフォルトの
{ "beforeBlockComment": true }
オプションを使用したこのルールの正しいコードの例afterBlockComment
{ "afterBlockComment": true }
オプションを使用したこのルールの不正なコードの例{ "afterBlockComment": true }
オプションを使用したこのルールの正しいコードの例beforeLineComment
{ "beforeLineComment": true }
オプションを使用したこのルールの不正なコードの例{ "beforeLineComment": true }
オプションを使用したこのルールの正しいコードの例afterLineComment
{ "afterLineComment": true }
オプションを使用したこのルールの不正なコードの例{ "afterLineComment": true }
オプションを使用したこのルールの正しいコードの例allowBlockStart
{ "beforeLineComment": true, "allowBlockStart": true }
オプションを使用したこのルールの正しいコードの例{ "beforeBlockComment": true, "allowBlockStart": true }
オプションを使用したこのルールの正しいコードの例allowBlockEnd
{ "afterLineComment": true, "allowBlockEnd": true }
オプションを使用したこのルールの正しいコードの例{ "afterBlockComment": true, "allowBlockEnd": true }
オプションを使用したこのルールの正しいコードの例allowClassStart
{ "beforeLineComment": true, "allowClassStart": false }
オプションを使用したこのルールの不正なコードの例{ "beforeLineComment": true, "allowClassStart": false }
オプションを使用したこのルールの正しいコードの例{ "beforeLineComment": true, "allowClassStart": true }
オプションを使用したこのルールの正しいコードの例{ "beforeBlockComment": true, "allowClassStart": false }
オプションを使用したこのルールの不正なコードの例{ "beforeBlockComment": true, "allowClassStart": false }
オプションを使用したこのルールの正しいコードの例{ "beforeBlockComment": true, "allowClassStart": true }
オプションを使用したこのルールの正しいコードの例allowClassEnd
{ "afterLineComment": true, "allowClassEnd": true }
オプションを使用したこのルールの正しいコードの例{ "afterBlockComment": true, "allowClassEnd": true }
オプションを使用したこのルールの正しいコードの例allowObjectStart
{ "beforeLineComment": true, "allowObjectStart": true }
オプションを使用したこのルールの正しいコードの例{ "beforeBlockComment": true, "allowObjectStart": true }
オプションを使用したこのルールの正しいコードの例allowObjectEnd
{ "afterLineComment": true, "allowObjectEnd": true }
オプションを使用したこのルールの正しいコードの例{ "afterBlockComment": true, "allowObjectEnd": true }
オプションを使用したこのルールの正しいコードの例allowArrayStart
{ "beforeLineComment": true, "allowArrayStart": true }
オプションを使用したこのルールの正しいコードの例{ "beforeBlockComment": true, "allowArrayStart": true }
オプションを使用したこのルールの正しいコードの例allowArrayEnd
{ "afterLineComment": true, "allowArrayEnd": true }
オプションを使用したこのルールの正しいコードの例{ "afterBlockComment": true, "allowArrayEnd": true }
オプションを使用したこのルールの正しいコードの例ignorePattern
デフォルトでは、このルールは、
eslint
、jshint
、jslint
、istanbul
、global
、exported
、jscs
という単語で始まるコメントを無視します。デフォルトに加えてさらに多くのコメントを無視するには、RegExp
コンストラクタに渡される文字列パターンにignorePattern
オプションを設定します。ignorePattern
オプションの正しいコードの例ignorePattern
オプションの不正なコードの例applyDefaultIgnorePatterns
ignorePattern
が提供されている場合でも、デフォルトの無視パターンが適用されます。デフォルトのパターンを省略する場合は、このオプションをfalse
に設定します。{ "applyDefaultIgnorePatterns": false }
オプションの正しいコードの例{ "applyDefaultIgnorePatterns": false }
オプションの不正なコードの例afterHashbangComment
{ "afterHashbangComment": true }
オプションを使用したこのルールの不正なコードの例{ "afterHashbangComment": true }
オプションを使用したこのルールの正しいコードの例使用しない場合
多くの人はより簡潔なコードスタイルを好み、コメントがコードに隣接することについては気にしていません。そのような場合は、このルールは必要ありません。