版本

max-len

強制執行最大行長度

此規則已在 ESLint v8.53.0 中棄用。請使用 @stylistic/eslint-plugin-js 中對應的規則。

任何語言中過長的程式碼行都可能難以閱讀。為了幫助提高可讀性和可維護性,許多程式設計師制定了一項慣例,將程式碼行限制為 X 個字元(傳統上為 80 個字元)。

var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long

規則詳情

此規則強制執行最大行長度,以提高程式碼的可讀性和可維護性。行的長度定義為該行中的 Unicode 字元數。

選項

此規則最多可以有兩個數字作為位置參數(用於 codetabWidth 選項),後跟一個物件選項(位置參數具有優先權)

  • "code"(預設值 80)強制執行最大行長度
  • "tabWidth"(預設值 4)指定 tab 字元的字元寬度
  • "comments" 強制執行註解的最大行長度;預設值為 code 的值
  • "ignorePattern" 忽略符合正規表示式的行;只能匹配單行,並且在 YAML 或 JSON 中編寫時需要雙重跳脫
  • "ignoreComments": true 忽略所有尾隨註解和單獨一行的註解
  • "ignoreTrailingComments": true 僅忽略尾隨註解
  • "ignoreUrls": true 忽略包含 URL 的行
  • "ignoreStrings": true 忽略包含雙引號或單引號字串的行
  • "ignoreTemplateLiterals": true 忽略包含樣板字串的行
  • "ignoreRegExpLiterals": true 忽略包含 RegExp 字面值的行

code

使用預設 { "code": 80 } 選項時,此規則的錯誤程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "code": 80 }]*/

var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };

使用預設 { "code": 80 } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "code": 80 }]*/

var foo = {
  "bar": "This is a bar.",
  "baz": { "qux": "This is a qux" },
  "easier": "to read"
};

tabWidth

使用預設 { "tabWidth": 4 } 選項時,此規則的錯誤程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/

		var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };

使用預設 { "tabWidth": 4 } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/

		var foo = {
				"bar": "This is a bar.",
				"baz": { "qux": "This is a qux" }
		};

comments

使用 { "comments": 65 } 選項時,此規則的錯誤程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "comments": 65 }]*/

/**
 * This is a comment that violates the maximum line length we have specified
**/

ignoreComments

使用 { "ignoreComments": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "ignoreComments": true }]*/

/**
 * This is a really really really really really really really really really long comment
**/

ignoreTrailingComments

使用 { "ignoreTrailingComments": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/

var foo = 'bar'; // This is a really really really really really really really long comment

ignoreUrls

使用 { "ignoreUrls": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "ignoreUrls": true }]*/

var url = 'https://www.example.com/really/really/really/really/really/really/really/long';

ignoreStrings

使用 { "ignoreStrings": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "ignoreStrings": true }]*/

var longString = 'this is a really really really really really long string!';

ignoreTemplateLiterals

使用 { "ignoreTemplateLiterals": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/

var longTemplateLiteral = `this is a really really really really really long template literal!`;

ignoreRegExpLiterals

使用 { "ignoreRegExpLiterals": true } 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/

var longRegExpLiteral = /this is a really really really really really long regular expression!/;

ignorePattern

使用 ignorePattern 選項時,此規則的正確程式碼範例

在遊樂場中開啟
/*eslint max-len:
["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/

var dep = require('really/really/really/really/really/really/really/really/long/module');

版本

此規則於 ESLint v0.0.9 中引入。

資源

變更語言