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 字符的数量。
选项
此规则最多可以有两个数字作为位置参数(用于code
和tabWidth
选项),后跟一个对象选项(提供的位置参数具有优先级)
"code"
(默认值为80
)强制执行最大行长度"tabWidth"
(默认值为4
)指定制表符字符的字符宽度"comments"
强制执行注释的最大行长度;默认为code
的值"ignorePattern"
忽略与正则表达式匹配的行;只能匹配单行,并且在 YAML 或 JSON 中编写时需要双重转义"ignoreComments": true
忽略所有尾随注释和单独一行的注释"ignoreTrailingComments": true
仅忽略尾随注释"ignoreUrls": true
忽略包含 URL 的行"ignoreStrings": true
忽略包含双引号或单引号字符串的行"ignoreTemplateLiterals": true
忽略包含模板字面量(template literal)的行"ignoreRegExpLiterals": true
忽略包含正则表达式字面量(RegExp literal)的行
code
使用默认{ "code": 80 }
选项时,此规则的**错误**代码示例
/*eslint max-len: ["error", { "code": 80 }]*/
使用默认{ "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 }]*/
使用默认{ "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 }]*/
/**
**/
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 中引入。