spaced-comment
強制註解中 //
或 /*
後面的一致空格
此規則回報的部分問題可透過 --fix
命令列 選項自動修正
某些風格指南要求或不允許在註解的初始 //
或 /*
後面立即加上空格。 //
或 /*
後面的空格使註解中的文字更易於閱讀。 另一方面,註解掉程式碼若在 //
或 /*
後面不加上空格會更容易。
規則詳情
此規則將強制註解 //
或 /*
開始後空格的一致性。 它也為各種文件風格提供了幾個例外。
選項
此規則接受兩個選項。
-
第一個是字串,可以是
"always"
或"never"
。 預設值為"always"
。-
如果是
"always"
,則//
或/*
後面必須至少跟隨一個空格。 -
如果是
"never"
,則後面不應有空格。
-
-
此規則還可以接受第二個選項,一個具有以下任何鍵的物件:
"exceptions"
和"markers"
。"exceptions"
值是一個字串模式陣列,這些模式被視為規則的例外。 當模式從註解的開頭開始並重複到行尾或*/
時(如果註解是單行註解),規則將不會發出警告。 請注意,如果第一個參數是"never"
,則會忽略例外。
"spaced-comment": ["error", "always", { "exceptions": ["-", "+"] }]
"markers"
值是一個字串模式陣列,這些模式被視為 docblock 風格註解的標記,例如額外的/
,用於表示由 doxygen、vsdoc 等讀取的文件,這些文件必須有額外的字元。 無論第一個參數的值(例如"always"
或"never"
)為何,"markers"
陣列都將適用。
"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
*/
exceptions
使用 "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
*******/
markers
使用 "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 中引入。