no-inline-comments
禁止代码后的内联注释
一些风格指南不允许注释与代码在同一行。如果注释紧跟在同一行的代码后面,代码可能会变得难以阅读。另一方面,有时将注释紧跟在代码后面会更快、更明显。
¥Some style guides disallow comments on the same line as code. Code can become difficult to read if comments immediately follow the code on the same line. On the other hand, it is sometimes faster and more obvious to put comments immediately following code.
规则详情
¥Rule Details
此规则不允许注释与代码位于同一行。
¥This rule disallows comments on the same line as code.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-inline-comments: "error"*/
var a = 1; // declaring a to 1
function getRandomNumber(){
return 4; // chosen by fair dice roll.
// guaranteed to be random.
}
/* A block comment before code */ var b = 2;
var c = 3; /* A block comment after code */
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-inline-comments: "error"*/
// This is a comment above a line of code
var foo = 5;
var bar = 5;
//This is a comment below a line of code
JSX 异常
¥JSX exception
JSX 中大括号内的注释允许与大括号在同一行,但前提是它们与其他代码不在同一行,并且大括号不包含实际表达式。
¥Comments inside the curly braces in JSX are allowed to be on the same line as the braces, but only if they are not on the same line with other code, and the braces do not enclose an actual expression.
此规则的错误代码示例:
¥Examples of incorrect code for this rule:
/*eslint no-inline-comments: "error"*/
var foo = <div>{ /* On the same line with other code */ }<h1>Some heading</h1></div>;
var bar = (
<div>
{ // These braces are not just for the comment, so it can't be on the same line
baz
}
</div>
);
此规则的正确代码示例:
¥Examples of correct code for this rule:
/*eslint no-inline-comments: "error"*/
var foo = (
<div>
{/* These braces are just for this comment and there is nothing else on this line */}
<h1>Some heading</h1>
</div>
)
var bar = (
<div>
{
// There is nothing else on this line
baz
}
</div>
);
var quux = (
<div>
{/*
Multiline
comment
*/}
<h1>Some heading</h1>
</div>
)
选项
¥Options
ignorePattern
要使此规则忽略特定注释,请将 ignorePattern
选项设置为将传递给 RegExp
构造函数 的字符串模式。
¥To make this rule ignore specific comments, set the ignorePattern
option to a string pattern that will be passed to the RegExp
constructor.
ignorePattern
选项的正确代码示例:
¥Examples of correct code for the ignorePattern
option:
/*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/
import(/* webpackChunkName: "my-chunk-name" */ './locale/en');
ignorePattern
选项的错误代码示例:
¥Examples of incorrect code for the ignorePattern
option:
/*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */
var foo = 4; // other thing
版本
此规则是在 ESLint v0.10.0 中引入。