版本

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

版本

此規則在 ESLint v0.10.0 中引入。

資源

變更語言