版本

max-lines

強制每個檔案的最大行數

有些人認為大型檔案是一種程式碼壞味道。大型檔案往往做很多事情,而且會讓人難以理解正在發生的事情。雖然對於檔案中可接受的最大行數沒有客觀標準,但大多數人會同意不應該達到數千行。建議通常在 100 到 500 行之間。

規則詳細資訊

此規則強制每個檔案的最大行數,以幫助維護並降低複雜性。

請注意,如果檔案以換行符號結尾,大多數編輯器會在結尾顯示一個額外的空行。此規則不計算該額外行。

選項

此規則具有數字或物件選項

  • "max"(預設值 300)強制檔案中的最大行數

  • "skipBlankLines": true 忽略完全由空白組成的行。

  • "skipComments": true 忽略僅包含註解的行

max

此規則在 max 值為 3 時的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines: ["error", 3]*/
let a,
    b,
    c;
在 Playground 中開啟
/*eslint max-lines: ["error", 3]*/

let a,
    b,c;
在 Playground 中開啟
/*eslint max-lines: ["error", 3]*/
// a comment
let a,
    b,c;

此規則在 max 值為 3 時的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines: ["error", 3]*/
let a,
    b, c;
在 Playground 中開啟
/*eslint max-lines: ["error", 3]*/

let a, b, c;
在 Playground 中開啟
/*eslint max-lines: ["error", 3]*/
// a comment
let a, b, c;

skipBlankLines

此規則在 { "skipBlankLines": true } 選項下的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines: ["error", {"max": 3, "skipBlankLines": true}]*/

let a,
    b,
    c;

此規則在 { "skipBlankLines": true } 選項下的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines: ["error", {"max": 3, "skipBlankLines": true}]*/

let a,
    b, c;

skipComments

此規則在 { "skipComments": true } 選項下的錯誤程式碼範例

在 Playground 中開啟
/*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/
// a comment
let a,
    b,
    c;

此規則在 { "skipComments": true } 選項下的正確程式碼範例

在 Playground 中開啟
/*eslint max-lines: ["error", {"max": 2, "skipComments": true}]*/
// a comment
let a,
    b, c;

何時不該使用

如果您不關心檔案中的行數,則可以關閉此規則。

相容性

版本

此規則在 ESLint v2.12.0 中引入。

延伸閱讀

資源

變更語言