规则名 | 规则类型 | 关联规则 | |||||
---|---|---|---|---|---|---|---|
max-len |
layout |
|
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
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.
This rule has a number or object option:
"code"
(default80
) enforces a maximum line length"tabWidth"
(default4
) specifies the character width for tab characters"comments"
enforces a maximum line length for comments; defaults to value ofcode
"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
ignores all trailing comments and comments on their own line"ignoreTrailingComments": true
ignores only trailing comments"ignoreUrls": true
ignores lines that contain a URL"ignoreStrings": true
ignores lines that contain a double-quoted or single-quoted string"ignoreTemplateLiterals": true
ignores lines that contain a template literal"ignoreRegExpLiterals": true
ignores lines that contain a RegExp literal
选项 { "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 }
默认值的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "code": 80 }]*/
var foo = {
"bar": "This is a bar.",
"baz": { "qux": "This is a qux" },
"easier": "to read"
};
选项 { "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 }
默认值的 正确 代码示例:
::: correct
/*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": 65 }
的 错误 代码示例:
/*eslint max-len: ["error", { "comments": 65 }]*/
/**
* This is a comment that violates the maximum line length we have specified
**/
选项 { "ignoreComments": true }
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignoreComments": true }]*/
/**
* This is a really really really really really really really really really long comment
**/
选项 { "ignoreTrailingComments": true }
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignoreTrailingComments": true }]*/
var foo = 'bar'; // This is a really really really really really really really long comment
选项 { "ignoreUrls": true }
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignoreUrls": true }]*/
var url = 'https://www.example.com/really/really/really/really/really/really/really/long';
选项 { "ignoreStrings": true }
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignoreStrings": true }]*/
var longString = 'this is a really really really really really long string!';
选项 { "ignoreTemplateLiterals": true }
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignoreTemplateLiterals": true }]*/
var longTemplateLiteral = `this is a really really really really really long template literal!`;
选项 { "ignoreRegExpLiterals": true }
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignoreRegExpLiterals": true }]*/
var longRegExpLiteral = /this is a really really really really really long regular expression!/;
选项 ignorePattern
的 正确 代码示例:
::: correct
/*eslint max-len: ["error", { "ignorePattern": "^\\s*var\\s.+=\\s*require\\s*\\(" }]*/
var dep = require('really/really/really/really/really/really/really/really/long/module');