max-len - ESLint - Pluggable JavaScript Linter (original) (raw)

Enforce a maximum line length

Table of Contents

  1. Rule Details
  2. Options
    1. code
    2. tabWidth
    3. comments
    4. ignoreComments
    5. ignoreTrailingComments
    6. ignoreUrls
    7. ignoreStrings
    8. ignoreTemplateLiterals
    9. ignoreRegExpLiterals
    10. ignorePattern
  3. Related Rules
  4. Version
  5. Resources

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

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

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

Examples of incorrect code for this rule with the default { "code": 80 } option:

Open in Playground

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

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

Examples of correct code for this rule with the default { "code": 80 } option:

Open in Playground

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

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

tabWidth

Examples of incorrect code for this rule with the default { "tabWidth": 4 } option:

Open in Playground

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

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

Examples of correct code for this rule with the default { "tabWidth": 4 } option:

Open in Playground

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

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

Examples of incorrect code for this rule with the { "comments": 65 } option:

Open in Playground

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

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

Examples of correct code for this rule with the { "ignoreComments": true } option:

Open in Playground

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

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

Examples of correct code for this rule with the { "ignoreTrailingComments": true } option:

Open in Playground

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

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

ignoreUrls

Examples of correct code for this rule with the { "ignoreUrls": true } option:

Open in Playground

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

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

ignoreStrings

Examples of correct code for this rule with the { "ignoreStrings": true } option:

Open in Playground

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

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

ignoreTemplateLiterals

Examples of correct code for this rule with the { "ignoreTemplateLiterals": true } option:

Open in Playground

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

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

ignoreRegExpLiterals

Examples of correct code for this rule with the { "ignoreRegExpLiterals": true } option:

Open in Playground

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

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

ignorePattern

Examples of correct code for this rule with the ignorePattern option:

Open in 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');

Version

This rule was introduced in ESLint v0.0.9.

Resources