版本

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)指定制表符的字符宽度
  • "comments" 强制执行注释的最大行长度;默认为 code 的值
  • "ignorePattern" 忽略与正则表达式匹配的行;只能匹配单行,并且在 YAML 或 JSON 中编写时需要双重转义
  • "ignoreComments": true 忽略所有尾随注释和独占一行的注释
  • "ignoreTrailingComments": true 仅忽略尾随注释
  • "ignoreUrls": true 忽略包含 URL 的行
  • "ignoreStrings": true 忽略包含双引号或单引号字符串的行
  • "ignoreTemplateLiterals": true 忽略包含模板字面量的行
  • "ignoreRegExpLiterals": true 忽略包含 RegExp 字面量的行

code

此规则在默认 { "code": 80 } 选项下错误代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "code": 80 }]*/

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

此规则在默认 { "code": 80 } 选项下正确代码示例

在 Playground 中打开
/*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 } 选项下错误代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/

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

此规则在默认 { "tabWidth": 4 } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "code": 80, "tabWidth": 4 }]*/

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

comments

此规则在 { "comments": 65 } 选项下错误代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "comments": 65 }]*/

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

ignoreComments

此规则在 { "ignoreComments": true } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "ignoreComments": true }]*/

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

ignoreTrailingComments

此规则在 { "ignoreTrailingComments": true } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/

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

ignoreUrls

此规则在 { "ignoreUrls": true } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "ignoreUrls": true }]*/

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

ignoreStrings

此规则在 { "ignoreStrings": true } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "ignoreStrings": true }]*/

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

ignoreTemplateLiterals

此规则在 { "ignoreTemplateLiterals": true } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/

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

ignoreRegExpLiterals

此规则在 { "ignoreRegExpLiterals": true } 选项下正确代码示例

在 Playground 中打开
/*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/

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

ignorePattern

此规则在 ignorePattern 选项下正确代码示例

在 Playground 中打开
/*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 中引入。

资源

更改语言