max-len

强制执行最大行长度

此规则在 ESLint v8.53.0 中已弃用。请在 @stylistic/eslint-plugin-js 中使用 相应的规则

¥This rule was deprecated in ESLint v8.53.0. Please use the corresponding rule in @stylistic/eslint-plugin-js.

任何语言的非常长的代码行都可能难以阅读。为了提高可读性和可维护性,许多编码人员制定了一种约定,将代码行限制为 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

此规则最多可以有两个数字作为位置参数(对于 codetabWidth 选项),后跟一个对象选项(前提是位置参数具有优先级):

¥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)强制最大行长度

    ¥"code" (default 80) enforces a maximum line length

  • "tabWidth"(默认 4)指定制表符的字符宽度

    ¥"tabWidth" (default 4) specifies the character width for tab characters

  • "comments" 强制注释的最大行长度;默认值为 code

    ¥"comments" enforces a maximum line length for comments; defaults to value of code

  • "ignorePattern" 忽略与正则表达式匹配的行;只能匹配单行,用 YAML 或 JSON 写的时候需要双重转义

    ¥"ignorePattern" ignores lines matching a regular expression; can only match a single line and need to be double escaped when written in YAML or JSON

  • "ignoreComments": true 忽略所有尾随注释和自己行上的注释

    ¥"ignoreComments": true ignores all trailing comments and comments on their own line

  • "ignoreTrailingComments": true 仅忽略尾随注释

    ¥"ignoreTrailingComments": true ignores only trailing comments

  • "ignoreUrls": true 忽略包含 URL 的行

    ¥"ignoreUrls": true ignores lines that contain a URL

  • "ignoreStrings": true 忽略包含双引号或单引号字符串的行

    ¥"ignoreStrings": true ignores lines that contain a double-quoted or single-quoted string

  • "ignoreTemplateLiterals": true 忽略包含模板字面量的行

    ¥"ignoreTemplateLiterals": true ignores lines that contain a template literal

  • "ignoreRegExpLiterals": true 忽略包含 RegExp 字面量的行

    ¥"ignoreRegExpLiterals": true ignores lines that contain a RegExp literal

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 中引入。

资源