版本

spaced-comment

強制在註解中的 ///* 後面保持一致的間距

🔧 可修正

此規則報告的一些問題可透過 --fix 命令列 選項自動修正

此規則已在 ESLint v8.53.0 中棄用。請使用 @stylistic/eslint-plugin-js 中的對應規則

一些風格指南要求或禁止在註解的初始 ///* 之後立即使用空格。///* 之後的空格可以使註解中的文字更容易閱讀。另一方面,註解程式碼在 ///* 之後沒有空格的情況下更容易。

規則詳情

此規則將強制在註解 ///* 的開始之後保持間距的一致性。它還為各種文件樣式提供了幾個例外情況。

選項

該規則有兩個選項。

  • 第一個選項是一個字串,可以是 "always""never"。預設值是 "always"

    • 如果為 "always",則 ///* 後面必須至少跟隨一個空格。

    • 如果為 "never",則後面不應有空格。

  • 此規則還可以接受第二個選項,一個具有以下任何鍵的物件:"exceptions""markers"

    • "exceptions" 值是一個字串模式陣列,這些模式被視為規則的例外。當模式從註解的開頭開始並重複到行尾或 */(如果註解是單行註解)時,該規則不會發出警告。請注意,如果第一個引數是 "never",則會忽略例外。
    "spaced-comment": ["error", "always", { "exceptions": ["-", "+"] }]
    
    • "markers" 值是一個字串模式陣列,這些模式被視為 docblock 樣式的註解的標記,例如額外的 /,用於表示 doxygen、vsdoc 等讀取的文件,這些文件必須有其他字元。"markers" 陣列將適用,而不考慮第一個引數的值,例如 "always""never"
    "spaced-comment": ["error", "always", { "markers": ["/"] }]
    

標記和例外之間的區別在於,標記僅出現在註解的開頭,而例外可以出現在註解字串中的任何位置。

您也可以為區塊和行註解定義單獨的例外和標記。"block" 物件可以有一個額外的鍵 "balanced",一個布林值,指定內嵌區塊註解是否應具有平衡的間距。預設值為 false

  • 如果 "balanced": true"always",則 /* 後面必須至少跟隨一個空格,並且 */ 前面必須至少有一個空格。

  • 如果 "balanced": true"never",則 /* 後面或 */ 前面不應有空格。

  • 如果 "balanced": false,則不強制執行平衡的空格。

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

always

"always" 選項下,此規則的不正確程式碼範例

在 Playground 中開啟
/*eslint spaced-comment: ["error", "always"]*/

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

/*This is a comment with no whitespace at the beginning */
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
/* This is a comment with whitespace at the beginning but not the end*/

"always" 選項下,此規則的正確程式碼範例

在 Playground 中開啟
/* eslint 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
*/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always"] */

/**
* I am jsdoc
*/

never

"never" 選項下,此規則的不正確程式碼範例

在 Playground 中開啟
/*eslint 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 */
在 Playground 中開啟
/*eslint spaced-comment: ["error", "never", { "block": { "balanced": true } }]*/
/*This is a comment with whitespace at the end */

"never" 選項下,此規則的正確程式碼範例

在 Playground 中開啟
/*eslint spaced-comment: ["error", "never"]*/

/*This is a comment with no whitespace at the beginning */
在 Playground 中開啟
/*eslint spaced-comment: ["error", "never"]*/

/**
* I am jsdoc
*/

例外

"always" 選項與 "exceptions" 結合下,此規則的不正確程式碼範例

在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-"] } }] */

//--------------
// Comment block
//--------------
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */

//------++++++++
// Comment block
//------++++++++
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */

/*------++++++++*/
/* Comment block */
/*------++++++++*/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-+"] } }] */

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */

/******** COMMENT *******/

"always" 選項與 "exceptions" 結合下,此規則的正確程式碼範例

在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-"] }] */

//--------------
// Comment block
//--------------
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-"] } }] */

//--------------
// Comment block
//--------------
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "exceptions": ["*"] }] */

/****************
 * Comment block
 ****************/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-+"] }] */

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

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-+"] } }] */

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */

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

/********
COMMENT
*******/

標記

"always" 選項與 "markers" 結合下,此規則的不正確程式碼範例

在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */

///This is a comment with a marker but without whitespace
在 Playground 中開啟
/*eslint spaced-comment: ["error", "always", { "block": { "markers": ["!"], "balanced": true } }]*/
/*! This is a comment with a marker but without whitespace at the end*/
在 Playground 中開啟
/*eslint spaced-comment: ["error", "never", { "block": { "markers": ["!"], "balanced": true } }]*/
/*!This is a comment with a marker but with whitespace at the end */

"always" 選項與 "markers" 結合下,此規則的正確程式碼範例

在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */

/// This is a comment with a marker
在 Playground 中開啟
/*eslint 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
*/
在 Playground 中開啟
/* eslint spaced-comment: ["error", "always", { "markers": ["global"] }] */

/*global ABC*/

版本

此規則在 ESLint v0.23.0 中引入。

資源

變更語言