no-inline-comments
禁止在代码后添加行内注释
❄️ 冻结
此规则目前处于冻结状态,不接受功能请求。
一些风格指南不允许在与代码相同的行上添加注释。如果注释紧跟在同一行代码之后,代码可能会变得难以阅读。另一方面,有时将注释紧跟在代码之后会更快更明显。
规则详情
此规则禁止在与代码相同的行上添加注释。
此规则的错误代码示例
在 Playground 中打开
/*eslint no-inline-comments: "error"*/
const a = 1;
function getRandomNumber(){
return 4;
// guaranteed to be random.
}
const b = 2;
const c = 3;
此规则的正确代码示例
在 Playground 中打开
/*eslint no-inline-comments: "error"*/
// This is a comment above a line of code
const foo = 5;
const bar = 5;
//This is a comment below a line of code
JSX 异常
JSX 中花括号内的注释允许与花括号在同一行,但前提是它们不与同一行的其他代码在同一行,并且花括号不包含实际的表达式。
此规则的错误代码示例
在 Playground 中打开
/*eslint no-inline-comments: "error"*/
const foo = <div>{ }<h1>Some heading</h1></div>;
const bar = (
<div>
{
baz
}
</div>
);
此规则的正确代码示例
在 Playground 中打开
/*eslint no-inline-comments: "error"*/
const foo = (
<div>
{/* These braces are just for this comment and there is nothing else on this line */}
<h1>Some heading</h1>
</div>
)
const bar = (
<div>
{
// There is nothing else on this line
baz
}
</div>
);
const quux = (
<div>
{/*
Multiline
comment
*/}
<h1>Some heading</h1>
</div>
)
选项
ignorePattern
要使此规则忽略特定的注释,请将 ignorePattern
选项设置为一个字符串模式,该模式将传递给 RegExp
构造函数。
ignorePattern
选项的正确代码示例
在 Playground 中打开
/*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/
import(/* webpackChunkName: "my-chunk-name" */ './locale/en');
ignorePattern
选项的错误代码示例
在 Playground 中打开
/*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */
const foo = 4;
版本
此规则在 ESLint v0.10.0 中引入。