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

0UzW5qZvZRhpd4yfdcqJqWOYJSJXCEg6xR+jiBMP5xQDgBtLCyDtRgpzqa+M27Dfe98g637HS0O4RocD97VJ87N98CYoWrUhyvgfqy0Bds2ghqM9FM6lZYEgGK4fpPbJ

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

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

0UzW5qZvZRhpd4yfdcqJqWOYJSJXCEg6xR+jiBMP5xQDgBtLCyDtRgpzqa+M27DfreZGKnC3E7Ef+Ybmq02eHMya/GgoGa8rIhSQ2vQs4YuWYRpeMadGc6pN2DQha2QP

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

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

tabWidth

0UzW5qZvZRhpd4yfdcqJqTulcRm32708xu9dyID8DJEgZ7NzlaYlD1VfmHca54o816MhXDUHFeMuS7QVZQUnW4caKO+mXzF6JqMGjLeD6wXYG1D/WKVwx9y4Ja0tYf2x

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

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

0UzW5qZvZRhpd4yfdcqJqTulcRm32708xu9dyID8DJEgZ7NzlaYlD1VfmHca54o8OBTICsrHzHO47noFY315zsrpuGHOHssgFk4IAEic01E+8+kDJDqgWBZ/G9tXPq1N

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

vLn68i7rFOv8nwd/Nio843oNGIB6muEfKIfQvgFekszr1TULtyd/LgDg6soA2Rki5o0QuDfMLTJQSRRkKi/6o7aOQSWHQv/kfZdTtWBIPMU=

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

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

ignoreComments

vLn68i7rFOv8nwd/Nio846XCPpI7Kc1SJGXrmMv6mugkCQICg0Nub/G+VH/xnTkGwGiFLK3bX8y+m186tPiPm5D2CtZtYwg4zOtyRIk/CLfm/Fv4QPdvcdiril14I2qs

/*eslint max-len: ["error", { "ignoreComments": true }]*/

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

ignoreTrailingComments

vLn68i7rFOv8nwd/Nio846OSEyVexHuhVyVl+sev/LVwIC3osMjuYetkCDfGACpaftq3eWNU5CsF1gHpZIJtKmWpR9f14z/co7Q+I89VNYqx8PAZGBrFcYjpcthg1ehV

/*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/

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

ignoreUrls

vLn68i7rFOv8nwd/Nio84/mHP3zk8tD4L//ncjsQrhLYCsqeH/Lc/Vs2U9NsEjr/ke1wZOH27a1K/sPR4CV6ugxQuD0E11cBNd5If/tA4AYMbm6WqBLGTsSLVaw1c4Uu

/*eslint max-len: ["error", { "ignoreUrls": true }]*/

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

ignoreStrings

vLn68i7rFOv8nwd/Nio840vo0x1nkNPnHWQJEuP8UZoCP4yUCxZwswaTSo5plpWP0/qm/z/voVMTcYrToLOgA6ch5p/uN6ULRNG28SjV3/yu7bO9QmtN6Uk02yDBr3vK

/*eslint max-len: ["error", { "ignoreStrings": true }]*/

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

ignoreTemplateLiterals

vLn68i7rFOv8nwd/Nio846OSEyVexHuhVyVl+sev/LVRkktyd2RecoXW8ip3paGkfec/8x8pXf4kVsSoaXmnsZ33pQ46bqy00HLVtPHqlemIHt48Ozno5KkpPfTS4JeQ

/*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/

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

ignoreRegExpLiterals

vLn68i7rFOv8nwd/Nio84xWSahKnIAMt2SNPrKziT8Ksn5ZWzffKOXdulBtmZS+MgOD0Or4RIgnmax+OIzXm3apRmVCuqn1znEXzoHDOBOi56SP5NOfxyiYacNy3TWzh

/*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/

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

ignorePattern

vLn68i7rFOv8nwd/Nio8427lAnO5yYjdXfR3x/pq7955swHDBf9LP81voCRxXXMxIEGd6VUWP6YyRIhGsB5b7ichnBqdH6vfIb/UFmKlWPc=

/*eslint max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/

var dep = require('really/really/really/really/really/really/really/really/long/module');