# 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"]*/

//This is a comment with no whitespace at the beginning

/*This is a comment with no whitespace at the beginning */
/* eslint spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
/* This is a comment with whitespace at the beginning but not the end*/

此规则使用 "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"]*/

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

此规则使用 "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": ["*"] } }] */

/******** COMMENT *******/

此规则的正确代码示例与 "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": ["/"] }] */

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

此规则的正确代码示例与 "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*/
Last Updated: 5/13/2023, 8:55:38 PM