コンテンツにスキップ

@stylistic/js/

spaced-comment

スタイルガイドによっては、コメントの最初の `//` または `/*` の直後に空白を入れるか入れないかを規定しています。`//` または `/*` の後に空白を入れると、コメント内のテキストが読みやすくなります。一方、`//` または `/*` の直後に空白を入れる必要がないと、コードをコメントアウトするのが簡単になります。

ルールの詳細

このルールは、コメント `//` または `/*` の開始後のスペースの一貫性を強制します。また、さまざまなドキュメントスタイルに対して、いくつかの例外を提供します。

オプション

このルールは2つのオプションを取ります。

  • 1つ目は、`“always”` または `“never”` のいずれかの文字列です。デフォルトは `“always”` です。

    • `“always”` の場合、`//` または `/*` の後には少なくとも1つの空白が必要です。

    • `“never”` の場合、空白があってはなりません。

  • このルールは、2番目のオプションとして、`“exceptions”` と `“markers”` のキーを持つオブジェクトを取ることもできます。

    • `“exceptions”` の値は、ルールの例外とみなされる文字列パターンの配列です。ルールは、パターンがコメントの先頭から始まり、行末またはコメントが単一行コメントの場合は `*/` まで繰り返される場合、警告を発しません。最初の引数が `“never”` の場合、例外は無視されることに注意してください。
    js
    "spaced-comment": ["error", "always", { "exceptions": ["-", "+"] }]
    • `“markers”` の値は、doxygen、vsdoc などで読み取られるドキュメントを示すために使用される追加の `/` など、docblock スタイルのコメントのマーカーとみなされる文字列パターンの配列です。`“markers”` 配列は、最初の引数の値(例: `“always”` または `“never”`)に関係なく適用されます。
    js
    "spaced-comment": ["error", "always", { "markers": ["/"] }]

マーカーと例外の違いは、マーカーはコメントの先頭にのみ表示されるのに対し、例外はコメント文字列のどこにでも発生する可能性があることです。

ブロックコメントと行コメントに対して、個別の例外とマーカーを定義することもできます。`“block”` オブジェクトには、インラインブロックコメントのスペースのバランスを取るかどうかを指定するブール値である `“balanced”` という追加キーを設定できます。デフォルト値は `false` です。

  • `“balanced”: true` かつ `“always”` の場合、`/*` の後には少なくとも1つの空白が必要であり、`*/` の前には少なくとも1つの空白が必要です。

  • `“balanced”: true` かつ `“never”` の場合、`/*` の後または `*/` の前に空白があってはなりません。

  • `“balanced”: false` の場合、バランスの取れた空白は強制されません。

json
"spaced-comment": ["error", "always", {
    "line": {
        "markers": ["/"],
        "exceptions": ["-", "+"]
    },
    "block": {
        "markers": ["!"],
        "exceptions": ["*"],
        "balanced": true
    }
}]

always

`"always"` オプションでこのルールに **違反している** コードの例

js
/*eslint @stylistic/js/spaced-comment: ["error", "always"]*/
//This is a comment with no whitespace at the beginning
/*This is a comment with no whitespace at the beginning */
違反しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
/* This is a comment with whitespace at the beginning but not the end*/
違反しているコード

`"always"` オプションでこのルールに **準拠している** コードの例

js
/* eslint @stylistic/js/spaced-comment: ["error", "always"] */

// This is a comment with a whitespace at the beginning

/* This is a comment with a whitespace at the beginning */

/*
 * This is a comment with a whitespace at the beginning
 */

/**This comment has a newline
*/
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always"] */

/**
* I am jsdoc
*/
準拠しているコード

never

`"never"` オプションでこのルールに **違反している** コードの例

js
/*eslint @stylistic/js/spaced-comment: ["error", "never"]*/

// This is a comment with a whitespace at the beginning
/* This is a comment with a whitespace at the beginning */
/* \nThis is a comment with a whitespace at the beginning */
違反しているコード
js
/*eslint @stylistic/js/spaced-comment: ["error", "never", { "block": { "balanced": true } }]*/
/*This is a comment with whitespace at the end */
違反しているコード

違反しているコード

js
/*eslint @stylistic/js/spaced-comment: ["error", "never"]*/

/*This is a comment with no whitespace at the beginning */
準拠しているコード
js
/*eslint @stylistic/js/spaced-comment: ["error", "never"]*/

/**
* I am jsdoc
*/
準拠しているコード

`"never"` オプションでこのルールに **準拠している** コードの例

準拠しているコード

js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "block": { "exceptions": ["-"] } }] */

//--------------
// Comment block
//--------------
違反しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */

//------++++++++
// Comment block
//------++++++++
違反しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */

/*------++++++++*/
/* Comment block */
/*------++++++++*/
違反しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "line": { "exceptions": ["-+"] } }] */

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
違反しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */

/******** COMMENT *******/
違反しているコード

exceptions

js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "exceptions": ["-"] }] */

//--------------
// Comment block
//--------------
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "line": { "exceptions": ["-"] } }] */

//--------------
// Comment block
//--------------
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "exceptions": ["*"] }] */

/****************
 * Comment block
 ****************/
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "exceptions": ["-+"] }] */

//-+-+-+-+-+-+-+
// Comment block
//-+-+-+-+-+-+-+

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "block": { "exceptions": ["-+"] } }] */

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */

/***************/

/********
COMMENT
*******/
準拠しているコード

`"always"` オプションと `"exceptions"` を組み合わせた場合に、このルールに **違反している** コードの例

違反しているコード

js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "markers": ["/"] }] */

///This is a comment with a marker but without whitespace
違反しているコード
js
/*eslint @stylistic/js/spaced-comment: ["error", "always", { "block": { "markers": ["!"], "balanced": true } }]*/
/*! This is a comment with a marker but without whitespace at the end*/
違反しているコード
js
/*eslint @stylistic/js/spaced-comment: ["error", "never", { "block": { "markers": ["!"], "balanced": true } }]*/
/*!This is a comment with a marker but with whitespace at the end */
違反しているコード

`"always"` オプションと `"exceptions"` を組み合わせた場合に、このルールに **準拠している** コードの例

js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "markers": ["/"] }] */

/// This is a comment with a marker
準拠しているコード
js
/*eslint @stylistic/js/spaced-comment: ["error", "never", { "markers": ["!<"] }]*/

//!<This is a line comment with a marker

/*!<this is a block comment with a marker
subsequent lines are ignored
*/
準拠しているコード
js
/* eslint @stylistic/js/spaced-comment: ["error", "always", { "markers": ["global"] }] */

/*global ABC*/
準拠しているコード

`"always"` オプションと `“markers”` を組み合わせた場合に、このルールに **準拠している** コードの例