max-len
强制执行最大行长度
This rule was deprecated in ESLint v8.53.0. It will be removed in v11.0.0. Please use the corresponding rule in @stylistic/eslint-plugin.
任何语言中非常长的代码行都可能难以阅读。为了提高可读性和可维护性,许多程序员已经制定了一种规范,将代码行限制在X个字符以内(传统上是80个字符)。
🌐 Very long lines of code in any language can be difficult to read. In order to aid in readability and maintainability many coders have developed a convention to limit lines of code to X number of characters (traditionally 80 characters).
var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long
规则详情
🌐 Rule Details
此规则强制执行最大行长度,以提高代码的可读性和可维护性。行的长度定义为行中的 Unicode 字符数。
🌐 This rule enforces a maximum line length to increase code readability and maintainability. The length of a line is defined as the number of Unicode characters in the line.
选项
🌐 Options
此规则最多可以有两个数字作为位置参数(用于 code 和 tabWidth 选项),然后是一个对象选项(提供的位置参数具有优先权):
🌐 This rule can have up to two numbers as positional arguments (for code and tabWidth options), followed by an object option (provided positional arguments have priority):
"code"(默认80)强制执行最大行长度"tabWidth"(默认值4)指定制表符的字符宽度"comments"对评论的最大行长度进行强制限制;默认值为code"ignorePattern"忽略与正则表达式匹配的行;只能匹配单行,在 YAML 或 JSON 中书写时需要双重转义"ignoreComments": true会忽略所有尾随注释以及单独一行的注释"ignoreTrailingComments": true仅忽略行尾注释"ignoreUrls": true会忽略包含 URL 的行"ignoreStrings": true忽略包含双引号或单引号字符串的行"ignoreTemplateLiterals": true忽略包含模板字符串的行"ignoreRegExpLiterals": true忽略包含正则表达式字面量的行
code
使用默认 { "code": 80 } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "code": 80 } option:
/*eslint max-len: ["error", { "code": 80 }]*/
var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };
使用默认 { "code": 80 } 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default { "code": 80 } option:
/*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 } 选项时,该规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the default { "tabWidth": 4 } option:
::: 错误
/*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };
:::
使用默认 { "tabWidth": 4 } 选项时,该规则的正确代码示例:
🌐 Examples of correct code for this rule with the default { "tabWidth": 4 } option:
::: 正确
/*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
var foo = {
"bar": "This is a bar.",
"baz": { "qux": "This is a qux" }
};
:::
comments
使用 { "comments": 65 } 选项时违反此规则的错误代码示例:
🌐 Examples of incorrect code for this rule with the { "comments": 65 } option:
/*eslint max-len: ["error", { "comments": 65 }]*/
/**
* This is a comment that violates the maximum line length we have specified
**/
ignoreComments
使用 { "ignoreComments": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreComments": true } option:
/*eslint max-len: ["error", { "ignoreComments": true }]*/
/**
* This is a really really really really really really really really really long comment
**/
ignoreTrailingComments
使用 { "ignoreTrailingComments": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreTrailingComments": true } option:
/*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/
var foo = 'bar'; // This is a really really really really really really really long comment
ignoreUrls
使用 { "ignoreUrls": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreUrls": true } option:
/*eslint max-len: ["error", { "ignoreUrls": true }]*/
var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
ignoreStrings
使用 { "ignoreStrings": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreStrings": true } option:
/*eslint max-len: ["error", { "ignoreStrings": true }]*/
var longString = 'this is a really really really really really long string!';
ignoreTemplateLiterals
使用 { "ignoreTemplateLiterals": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreTemplateLiterals": true } option:
/*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/
var longTemplateLiteral = `this is a really really really really really long template literal!`;
ignoreRegExpLiterals
使用 { "ignoreRegExpLiterals": true } 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the { "ignoreRegExpLiterals": true } option:
/*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/
var longRegExpLiteral = /this is a really really really really really long regular expression!/;
ignorePattern
使用 ignorePattern 选项时,此规则的正确代码示例:
🌐 Examples of correct code for this rule with the ignorePattern option:
/*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 中引入。