Skip to content

Latest commit

 

History

History
127 lines (84 loc) · 2.59 KB

no-inline-comments.md

File metadata and controls

127 lines (84 loc) · 2.59 KB
规则名 规则类型
no-inline-comments
suggestion

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.

规则详解

This rule disallows comments on the same line as code.

此规则的 错误 代码实例:

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

此规则的 正确 代码实例:

::: correct

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

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.

此规则的 错误 代码实例:

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

此规则的 正确 代码实例:

::: correct

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

To make this rule ignore specific comments, set the ignorePattern option to a string pattern that will be passed to the RegExp constructor.

选项 ignorePattern正确 代码示例:

::: correct

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