# max-len

强制执行最大行长

任何语言的非常长的代码行都可能难以阅读。为了提高可读性和可维护性,许多编码人员制定了一种约定,将代码行限制为 X 个字符(传统上为 80 个字符)。

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

# 规则详情

此规则强制执行最大行长度以提高代码的可读性和可维护性。行的长度定义为行中 Unicode 字符的数量。

# 选项

此规则有一个数字或对象选项:

  • "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 } 选项的错误代码示例:

/*eslint max-len: ["error", { "code": 80 }]*/

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

此规则使用默认 { "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 }]*/

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

此规则使用默认 { "tabWidth": 4 } 选项的正确代码示例:

/*eslint 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

此规则使用 { "comments": 65 } 选项的错误代码示例:

/*eslint max-len: ["error", { "comments": 65 }]*/

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

# 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');
Last Updated: 5/13/2023, 8:55:38 PM