跳至内容

@stylistic/

空格注释

一些风格指南要求或禁止在注释的初始 ///* 后面立即添加空格。在 ///* 后面添加空格可以使注释中的文本更易读。另一方面,在 ///* 后面添加空格会使注释代码变得更加困难。

规则细节

此规则将强制执行注释开始处 ///* 后面的空格一致性。它还为各种文档样式提供了几个例外。

选项

此规则接受两个选项。

  • 第一个是一个字符串,可以是 "always""never"。默认值为 "always"

    • 如果为 "always",则 ///* 后面必须至少有一个空格。

    • 如果为 "never",则后面不应该有空格。

  • 此规则还可以接受第二个选项,一个包含以下任何键的对象:"exceptions""markers"

    • "exceptions" 值是一个字符串模式数组,这些模式被视为规则的例外。如果模式从注释的开头开始并重复到行尾或 */(如果注释是单行注释),则该规则不会发出警告。请注意,如果第一个参数是 "never",则会忽略例外。
    js
    "spaced-comment": ["error", "always", { "exceptions": ["-", "+"] }]
    • "markers" 值是一个字符串模式数组,这些模式被视为文档块样式注释的标记,例如额外的 /,用于表示由 doxygen、vsdoc 等读取的文档,这些文档必须具有额外的字符。"markers" 数组将应用于第一个参数的值,例如 "always""never"
    js
    "spaced-comment": ["error", "always", { "markers": ["/"] }]

标记和异常的区别在于,标记只出现在注释的开头,而异常可以出现在注释字符串中的任何位置。

您还可以为块注释和行注释定义单独的异常和标记。"block" 对象可以有一个额外的键 "balanced",这是一个布尔值,指定内联块注释是否应该具有平衡的间距。默认值为 false

  • 如果 "balanced": true"always",则 /* 后面必须至少有一个空格,并且 */ 前面必须至少有一个空格。

  • 如果 "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 */
incorrect
js
/* eslint @stylistic/spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
/* This is a comment with whitespace at the beginning but not the end*/
incorrect

此规则使用 "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
*/
correct
js
/* eslint @stylistic/spaced-comment: ["error", "always"] */

/**
* I am jsdoc
*/
correct

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

此规则使用 "never" 选项的正确代码示例

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

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

/**
* I am jsdoc
*/
correct

exceptions

此规则使用 "always" 选项与 "exceptions" 结合的错误代码示例

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

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

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

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

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

/******** COMMENT *******/
incorrect

此规则使用 "always" 选项与 "exceptions" 结合的正确代码示例

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

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

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

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

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

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

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

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

/********
COMMENT
*******/
correct

markers

此规则使用 "always" 选项与 "markers" 结合的错误代码示例

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

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

使用 "always" 选项结合 "markers" 的情况下,此规则的正确代码示例

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

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

/*global ABC*/
correct