max-len
任何语言中过长的代码行都很难阅读。为了提高可读性和可维护性,许多程序员制定了将代码行限制为 X 个字符(传统上为 80 个字符)的约定。
js
var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" }; // very long
规则详情
此规则强制执行最大行长以提高代码可读性和可维护性。行长定义为行中 Unicode 字符的数量。
选项
此规则最多可以接受两个数字作为位置参数(用于 code
和 tabWidth
选项),然后是一个对象选项(如果提供了位置参数,则优先使用位置参数)。
"code"
(默认值为80
)强制执行最大行长。"tabWidth"
(默认值为4
)指定制表符的字符宽度。"comments"
强制执行注释的最大行长;默认为code
的值。"ignorePattern"
忽略与正则表达式匹配的行;只能匹配单行,并且在 YAML 或 JSON 中编写时需要双重转义。"ignoreComments": true
忽略所有尾随注释和独立行的注释。"ignoreTrailingComments": true
仅忽略尾随注释。"ignoreUrls": true
忽略包含 URL 的行。"ignoreStrings": true
忽略包含双引号或单引号字符串的行。"ignoreTemplateLiterals": true
忽略包含模板字面量的行。"ignoreRegExpLiterals": true
忽略包含正则表达式字面量的行。
代码
使用默认选项 { "code": 80 }
时,此规则的错误代码示例
js
/*eslint @stylistic/max-len: ["error", { "code": 80 }]*/
var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" }, "difficult": "to read" };
错误
使用默认选项 { "code": 80 }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "code": 80 }]*/
var foo = {
"bar": "This is a bar.",
"baz": { "qux": "This is a qux" },
"easier": "to read"
};
正确
tabWidth
使用默认选项 { "tabWidth": 4 }
时,此规则的错误代码示例
js
/*eslint @stylistic/max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
\t \t var foo = { "bar": "This is a bar.", "baz": { "qux": "This is a qux" } };
错误
使用默认选项 { "tabWidth": 4 }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "code": 80, "tabWidth": 4 }]*/
\t \t var foo = {
\t \t \t \t "bar": "This is a bar.",
\t \t \t \t "baz": { "qux": "This is a qux" }
\t \t };
正确
注释
使用选项 { "comments": 65 }
时,此规则的错误代码示例
js
/*eslint @stylistic/max-len: ["error", { "comments": 65 }]*/
/**
* This is a comment that violates the maximum line length we have specified
**/
错误
ignoreComments
使用选项 { "ignoreComments": true }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignoreComments": true }]*/
/**
* This is a really really really really really really really really really long comment
**/
正确
ignoreTrailingComments
使用选项 { "ignoreTrailingComments": true }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignoreTrailingComments": true }]*/
var foo = 'bar'; // This is a really really really really really really really long comment
正确
ignoreUrls
使用选项 { "ignoreUrls": true }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignoreUrls": true }]*/
var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
正确
ignoreStrings
使用选项 { "ignoreStrings": true }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignoreStrings": true }]*/
var longString = 'this is a really really really really really long string!';
正确
ignoreTemplateLiterals
使用选项 { "ignoreTemplateLiterals": true }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignoreTemplateLiterals": true }]*/
var longTemplateLiteral = `this is a really really really really really long template literal!`;
正确
ignoreRegExpLiterals
使用选项 { "ignoreRegExpLiterals": true }
时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignoreRegExpLiterals": true }]*/
var longRegExpLiteral = /this is a really really really really really long regular expression!/;
正确
ignorePattern
使用 ignorePattern
选项时,此规则的正确代码示例
js
/*eslint @stylistic/max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/
var dep = require('really/really/really/really/really/really/really/really/long/module');
正确