spaced-comment
强制在注释中的 //
或 /*
后保持一致的间距
此规则报告的某些问题可以通过 --fix
命令行 选项自动修复
一些风格指南要求或禁止在注释的初始 //
或 /*
后立即添加空格。//
或 /*
后的空格使阅读注释中的文本更容易。另一方面,注释掉代码更容易,无需在 //
或 /*
后放置空格。
规则详情
此规则将强制注释 //
或 /*
开头后的间距一致性。它还为各种文档风格提供了几个例外。
选项
该规则接受两个选项。
-
第一个是一个字符串,可以是
"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
*/
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 中引入。