line-comment-position
强制行注释的位置
该规则在 ESLint v9.3.0 中已弃用。请在 @stylistic/eslint-plugin-js
中使用 相应的规则。
¥This rule was deprecated in ESLint v9.3.0. Please use the corresponding rule in @stylistic/eslint-plugin-js
.
行注释可以位于代码上方或旁边。此规则有助于团队保持一致的风格。
¥Line comments can be positioned above or beside code. This rule helps teams maintain a consistent style.
// above comment
var foo = "bar"; // beside comment
规则详情
¥Rule Details
此规则强制行注释的位置一致。块注释不受此规则的影响。默认情况下,此规则会忽略以以下单词开头的注释:eslint
, jshint
, jslint
, istanbul
, global
, exported
, jscs
, falls through
.
¥This rule enforces consistent position of line comments. Block comments are not affected by this rule. By default, this rule ignores comments starting with the following words: eslint
, jshint
, jslint
, istanbul
, global
, exported
, jscs
, falls through
.
选项
¥Options
此规则接受一个参数,可以是字符串或对象。字符串设置与 position
属性的设置相同(解释如下)。object 选项具有以下属性:
¥This rule takes one argument, which can be a string or an object. The string settings are the same as those of the position
property (explained below). The object option has the following properties:
position
position
选项有两个设置:
¥The position
option has two settings:
-
above
(默认)仅在代码上方强制行注释,在它自己的行中。¥
above
(default) enforces line comments only above code, in its own line. -
beside
仅在代码行末尾强制行注释。¥
beside
enforces line comments only at the end of code lines.
位置:above
¥position: above
{ "position": "above" }
选项的正确代码示例:
¥Examples of correct code for the { "position": "above" }
option:
/*eslint line-comment-position: ["error", { "position": "above" }]*/
// valid comment
1 + 1;
{ "position": "above" }
选项的错误代码示例:
¥Examples of incorrect code for the { "position": "above" }
option:
/*eslint line-comment-position: ["error", { "position": "above" }]*/
1 + 1; // invalid comment
位置:beside
¥position: beside
{ "position": "beside" }
选项的正确代码示例:
¥Examples of correct code for the { "position": "beside" }
option:
/*eslint line-comment-position: ["error", { "position": "beside" }]*/
1 + 1; // valid comment
{ "position": "beside" }
选项的错误代码示例:
¥Examples of incorrect code for the { "position": "beside" }
option:
/*eslint line-comment-position: ["error", { "position": "beside" }]*/
// invalid comment
1 + 1;
ignorePattern
默认情况下,此规则会忽略以以下单词开头的注释:eslint
, jshint
, jslint
, istanbul
, global
, exported
, jscs
, falls through
.可以提供一个替代的正则表达式。
¥By default this rule ignores comments starting with the following words: eslint
, jshint
, jslint
, istanbul
, global
, exported
, jscs
, falls through
. An alternative regular expression can be provided.
ignorePattern
选项的正确代码示例:
¥Examples of correct code for the ignorePattern
option:
/*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/
1 + 1; // pragma valid comment
ignorePattern
选项的错误代码示例:
¥Examples of incorrect code for the ignorePattern
option:
/*eslint line-comment-position: ["error", { "ignorePattern": "pragma" }]*/
1 + 1; // invalid comment
applyDefaultIgnorePatterns
即使提供了 ignorePattern
,也会应用默认忽略模式。如果要省略默认模式,请将此选项设置为 false
。
¥Default ignore patterns are applied even when ignorePattern
is provided. If you want to omit default patterns, set this option to false
.
{ "applyDefaultIgnorePatterns": false }
选项的正确代码示例:
¥Examples of correct code for the { "applyDefaultIgnorePatterns": false }
option:
/*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/
1 + 1; // pragma valid comment
{ "applyDefaultIgnorePatterns": false }
选项的错误代码示例:
¥Examples of incorrect code for the { "applyDefaultIgnorePatterns": false }
option:
/*eslint line-comment-position: ["error", { "ignorePattern": "pragma", "applyDefaultIgnorePatterns": false }]*/
1 + 1; // falls through
已弃用:对象属性 applyDefaultPatterns
已弃用。请改用属性 applyDefaultIgnorePatterns
。
¥Deprecated: the object property applyDefaultPatterns
is deprecated. Please use the property applyDefaultIgnorePatterns
instead.
何时不使用
¥When Not To Use It
如果你不关心有不同的行注释样式,那么你可以关闭此规则。
¥If you aren’t concerned about having different line comment styles, then you can turn off this rule.
兼容性
¥Compatibility
版本
此规则是在 ESLint v3.5.0 中引入。