# no-inline-comments

禁止代码后的内联注释

一些样式指南不允许注释与代码在同一行。如果注释紧跟在同一行的代码后面,代码可能会变得难以阅读。另一方面,有时将注释紧跟在代码后面会更快、更明显。

# 规则详情

此规则不允许注释与代码位于同一行。

此规则的错误代码示例:

/*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 */

此规则的正确代码示例:

/*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 中大括号内的注释允许与大括号在同一行,但前提是它们与其他代码不在同一行,并且大括号不包含实际表达式。

此规则的错误代码示例:

/*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>
);

此规则的正确代码示例:

/*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>
)

# 选项

# ignorePattern

要使此规则忽略特定注释,请将 ignorePattern 选项设置为将传递给 RegExp 构造函数 的字符串模式。

ignorePattern 选项的正确代码示例:

/*eslint no-inline-comments: ["error", { "ignorePattern": "webpackChunkName:\\s.+" }]*/

import(/* webpackChunkName: "my-chunk-name" */ './locale/en');

ignorePattern 选项的错误代码示例:

/*eslint no-inline-comments: ["error", { "ignorePattern": "something" }] */

var foo = 4; // other thing
Last Updated: 5/13/2023, 8:55:38 PM