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"
選項下,此規則的不正確程式碼範例
/* eslint spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
在 "always"
選項下,此規則的正確程式碼範例
/* 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
*/
/* eslint spaced-comment: ["error", "always"] */
/**
* I am jsdoc
*/
never
在 "never"
選項下,此規則的不正確程式碼範例
/*eslint spaced-comment: ["error", "never"]*/
/*eslint spaced-comment: ["error", "never", { "block": { "balanced": true } }]*/
在 "never"
選項下,此規則的正確程式碼範例
/*eslint spaced-comment: ["error", "never"]*/
/*This is a comment with no whitespace at the beginning */
/*eslint spaced-comment: ["error", "never"]*/
/**
* I am jsdoc
*/
例外
在 "always"
選項與 "exceptions"
結合下,此規則的不正確程式碼範例
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-"] } }] */
// Comment block
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */
// Comment block
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */
/* Comment block */
/* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-+"] } }] */
// Comment block
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */
在 "always"
選項與 "exceptions"
結合下,此規則的正確程式碼範例
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-"] }] */
//--------------
// Comment block
//--------------
/* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-"] } }] */
//--------------
// Comment block
//--------------
/* eslint spaced-comment: ["error", "always", { "exceptions": ["*"] }] */
/****************
* Comment block
****************/
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-+"] }] */
//-+-+-+-+-+-+-+
// Comment block
//-+-+-+-+-+-+-+
/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-+"] } }] */
/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */
/***************/
/********
COMMENT
*******/
標記
在 "always"
選項與 "markers"
結合下,此規則的不正確程式碼範例
/* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */
/*eslint spaced-comment: ["error", "never", { "block": { "markers": ["!"], "balanced": true } }]*/
在 "always"
選項與 "markers"
結合下,此規則的正確程式碼範例
/* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */
/// This is a comment with a marker
/*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
*/
/* eslint spaced-comment: ["error", "always", { "markers": ["global"] }] */
/*global ABC*/
相關規則
版本
此規則在 ESLint v0.23.0 中引入。