版本

spaced-comment

强制在注释中的 ///* 后保持一致的间距

🔧 可修复

此规则报告的某些问题可以通过 --fix 命令行 选项自动修复

重要提示

此规则在 ESLint v8.53.0 中已弃用。请使用 @stylistic/eslint-plugin-js 中的相应规则

了解更多

一些风格指南要求或禁止在注释的初始 ///* 后立即添加空格。///* 后的空格使阅读注释中的文本更容易。另一方面,注释掉代码更容易,无需在 ///* 后放置空格。

规则详情

此规则将强制注释 ///* 开头后的间距一致性。它还为各种文档风格提供了几个例外。

选项

该规则接受两个选项。

  • 第一个是一个字符串,可以是 "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" 选项的此规则的不正确代码示例

在 Playground 中打开
/*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 */
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "block": { "balanced": true } }] */
/* This is a comment with whitespace at the beginning but not the end*/

对于使用 "always" 选项的此规则的正确代码示例

在 Playground 中打开
/* 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
*/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always"] */

/**
* I am jsdoc
*/

never

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

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

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

在 Playground 中打开
/*eslint spaced-comment: ["error", "never"]*/

/*This is a comment with no whitespace at the beginning */
在 Playground 中打开
/*eslint spaced-comment: ["error", "never"]*/

/**
* I am jsdoc
*/

exceptions

对于使用 "always" 选项并结合 "exceptions" 的此规则的不正确代码示例

在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-"] } }] */

//--------------
// Comment block
//--------------
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */

//------++++++++
// Comment block
//------++++++++
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-", "+"] }] */

/*------++++++++*/
/* Comment block */
/*------++++++++*/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-+"] } }] */

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */

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

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

在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-"] }] */

//--------------
// Comment block
//--------------
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "line": { "exceptions": ["-"] } }] */

//--------------
// Comment block
//--------------
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "exceptions": ["*"] }] */

/****************
 * Comment block
 ****************/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "exceptions": ["-+"] }] */

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

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["-+"] } }] */

/*-+-+-+-+-+-+-+*/
// Comment block
/*-+-+-+-+-+-+-+*/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "block": { "exceptions": ["*"] } }] */

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

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

markers

对于使用 "always" 选项并结合 "markers" 的此规则的不正确代码示例

在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */

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

对于使用 "always" 选项并结合 "markers" 的此规则的正确代码示例

在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "markers": ["/"] }] */

/// This is a comment with a marker
在 Playground 中打开
/*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
*/
在 Playground 中打开
/* eslint spaced-comment: ["error", "always", { "markers": ["global"] }] */

/*global ABC*/

版本

此规则在 ESLint v0.23.0 中引入。

资源

更改语言