跳至内容

@stylistic/js/

带空格注释

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

规则详情

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

选项

此规则接受两个选项。

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

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

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

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

    • "exceptions" 值是一个字符串模式数组,这些模式被视为规则的例外。如果模式从注释的开头开始并重复到行尾或 */(如果注释是单行注释),则该规则不会发出警告。请注意,如果第一个参数为 "never",则例外将被忽略。
    js
    "spaced-comment": ["error", "always", { "exceptions": ["-", "+"] }]
    • "markers" 值是一个字符串模式数组,这些模式被视为 docblock 风格注释的标记,例如额外的 /,用于表示由 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/js/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/js/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/js/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/js/spaced-comment: ["error", "always"] */

/**
* I am jsdoc
*/
correct

never

使用 "never" 选项时,此规则的错误代码示例

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

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

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

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

/**
* I am jsdoc
*/
correct

exceptions

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

markers

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

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

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

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

/*global ABC*/
correct