コンテンツにスキップ

@stylistic/

spaced-comment

一部のスタイルガイドでは、コメントの先頭の // または /* の直後に空白が必要か不要かが規定されています。// または /* の後に空白があると、コメント内のテキストが読みやすくなります。一方、コードをコメントアウトする場合、// または /* の直後に空白を入れる必要がない方が簡単です。

ルール詳細

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

オプション

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

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

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

    • "never" の場合、後に空白があってはなりません。

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

    • "exceptions" の値は、ルールに対する例外とみなされる文字列パターンの配列です。パターンがコメントの先頭から始まり、行末または */ (コメントが1行コメントの場合) まで繰り返される場合、ルールは警告を発しません。最初の引数が "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/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/spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
/* This is a comment with whitespace at the beginning but not the end*/
不正

"always" オプションを使用したこのルールの正しいコードの例

js
/* eslint @stylistic/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/spaced-comment: ["error", "always"] */

/**
* I am jsdoc
*/
正しい

never

"never" オプションを使用したこのルールの不正なコードの例

js
/*eslint @stylistic/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/spaced-comment: ["error", "never", { "block": { "balanced": true } }]*/
/*This is a comment with whitespace at the end */
不正

"never" オプションを使用したこのルールの正しいコードの例

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

/*This is a comment with no whitespace at the beginning */
正しい
js
/*eslint @stylistic/spaced-comment: ["error", "never"]*/

/**
* I am jsdoc
*/
正しい

exceptions

"always" オプションと "exceptions" を組み合わせたこのルールの不正なコードの例

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

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

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

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

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

/******** COMMENT *******/
不正

"always" オプションと "exceptions" を組み合わせたこのルールの正しいコードの例

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

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

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

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

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

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

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

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

/********
COMMENT
*******/
正しい

markers

"always" オプションと "markers" を組み合わせたこのルールの不正なコードの例

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

///This is a comment with a marker but without whitespace
不正
js
/*eslint @stylistic/spaced-comment: ["error", "always", { "block": { "markers": ["!"], "balanced": true } }]*/
/*! This is a comment with a marker but without whitespace at the end*/
不正
js
/*eslint @stylistic/spaced-comment: ["error", "never", { "block": { "markers": ["!"], "balanced": true } }]*/
/*!This is a comment with a marker but with whitespace at the end */
不正

"always" オプションと "markers" を組み合わせたこのルールの正しいコードの例

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

/// This is a comment with a marker
正しい
js
/*eslint @stylistic/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/spaced-comment: ["error", "always", { "markers": ["global"] }] */

/*global ABC*/
正しい

MITライセンスの下でリリースされました。