line-comment-position
强制行注释的位置
This rule was deprecated in ESLint v9.3.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
行注释可以放在代码上方或旁边。这条规则帮助团队保持一致的风格。
🌐 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 属性的设置相同(如下所述)。对象选项具有以下属性:
🌐 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(默认)只在代码上方、单独一行中强制使用行注释。beside仅在代码行末强制使用行注释。
位置:上方
🌐 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
位置:旁边
🌐 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。
何时不使用
🌐 When Not To Use It
如果你不关心有不同的行注释样式,那么你可以关闭此规则。
🌐 If you aren’t concerned about having different line comment styles, then you can turn off this rule.
兼容性
🌐 Compatibility
JSCS: validateCommentPosition
版本
此规则是在 ESLint v3.5.0 中引入。