版本

no-inline-comments

禁止程式碼後面的行內註解

❄️ 已凍結

此規則目前已凍結,且不接受功能請求。

某些風格指南不允許註解與程式碼在同一行。如果註解緊接在同一行程式碼之後,程式碼可能會變得難以閱讀。另一方面,有時將註解緊接在程式碼之後會更快且更明顯。

規則詳情

此規則禁止註解與程式碼在同一行。

此規則的 錯誤 程式碼範例

在 Playground 中開啟
/*eslint no-inline-comments: "error"*/

const a = 1; // declaring a to 1

function getRandomNumber(){
    return 4; // chosen by fair dice roll.
              // guaranteed to be random.
}

/* A block comment before code */ const b = 2;

const c = 3; /* A block comment after code */

此規則的 正確 程式碼範例

在 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>{ /* On the same line with other code */ }<h1>Some heading</h1></div>;

const bar = (
    <div>
    {   // These braces are not just for the comment, so it can't be on the same line
        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; // other thing

版本

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

資源

變更語言