lib/rules/max-len.js
function checkProgramForMaxLength(node) {

            // split (honors line-ending)
            const lines = sourceCode.lines,

                // list of comments to ignore
                comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode.getAllComments() : [];

            // we iterate over comments in parallel with the lines
            let commentsIndex = 0;

            const strings = getAllStrings();
            const stringsByLine = strings.reduce(groupByLineNumber, {});

            const templateLiterals = getAllTemplateLiterals();
            const templateLiteralsByLine = templateLiterals.reduce(groupByLineNumber, {});

            const regExpLiterals = getAllRegExpLiterals();
            const regExpLiteralsByLine = regExpLiterals.reduce(groupByLineNumber, {});

            lines.forEach((line, i) => {

                // i is zero-indexed, line numbers are one-indexed
                const lineNumber = i + 1;

                /*
                 * if we're checking comment length; we need to know whether this
                 * line is a comment
                 */
                let lineIsComment = false;
                let textToMeasure;

                /*
                 * We can short-circuit the comment checks if we're already out of
                 * comments to check.
                 */
                if (commentsIndex < comments.length) {
                    let comment = null;

                    // iterate over comments until we find one past the current line
                    do {
                        comment = comments[++commentsIndex];
                    } while (comment && comment.loc.start.line <= lineNumber);

                    // and step back by one
                    comment = comments[--commentsIndex];

                    if (isFullLineComment(line, lineNumber, comment)) {
                        lineIsComment = true;
                        textToMeasure = line;
                    } else if (ignoreTrailingComments && isTrailingComment(line, lineNumber, comment)) {
                        textToMeasure = stripTrailingComment(line, comment);
                    } else {
                        textToMeasure = line;
                    }
                } else {
                    textToMeasure = line;
                }
                if (ignorePattern && ignorePattern.test(textToMeasure) ||
                    ignoreUrls && URL_REGEXP.test(textToMeasure) ||
                    ignoreStrings && stringsByLine[lineNumber] ||
                    ignoreTemplateLiterals && templateLiteralsByLine[lineNumber] ||
                    ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber]
                ) {

                    // ignore this line
                    return;
                }

                const lineLength = computeLineLength(textToMeasure, tabWidth);
                const commentLengthApplies = lineIsComment && maxCommentLength;

                if (lineIsComment && ignoreComments) {
                    return;
                }

                if (commentLengthApplies) {
                    if (lineLength > maxCommentLength) {
                        context.report({
                            node,
                            loc: { line: lineNumber, column: 0 },
                            messageId: "maxComment",
                            data: {
                                lineNumber: i + 1,
                                maxCommentLength
                            }
                        });
                    }
                } else if (lineLength > maxLength) {
                    context.report({
                        node,
                        loc: { line: lineNumber, column: 0 },
                        messageId: "max",
                        data: {
                            lineNumber: i + 1,
                            maxLength
                        }
                    });
                }
            });
        }
function checkProgramForMaxLength(node) {

            // split (honors line-ending)
            const lines = sourceCode.lines,

                // list of comments to ignore
                comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode.getAllComments() : [];

            // we iterate over comments in parallel with the lines
            let commentsIndex = 0;

            const strings = getAllStrings();
            const stringsByLine = strings.reduce(groupByLineNumber, {});

            const templateLiterals = getAllTemplateLiterals();
            const templateLiteralsByLine = templateLiterals.reduce(groupByLineNumber, {});

            const regExpLiterals = getAllRegExpLiterals();
            const regExpLiteralsByLine = regExpLiterals.reduce(groupByLineNumber, {});

            lines.forEach((line, i) => {

                // i is zero-indexed, line numbers are one-indexed
                const lineNumber = i + 1;

                /*
                 * if we're checking comment length; we need to know whether this
                 * line is a comment
                 */
                let lineIsComment = false;
                let textToMeasure;

                /*
                 * We can short-circuit the comment checks if we're already out of
                 * comments to check.
                 */
                if (commentsIndex < comments.length) {
                    let comment = null;

                    // iterate over comments until we find one past the current line
                    do {
                        comment = comments[++commentsIndex];
                    } while (comment && comment.loc.start.line <= lineNumber);

                    // and step back by one
                    comment = comments[--commentsIndex];

                    if (isFullLineComment(line, lineNumber, comment)) {
                        lineIsComment = true;
                        textToMeasure = line;
                    } else if (ignoreTrailingComments && isTrailingComment(line, lineNumber, comment)) {
                        textToMeasure = stripTrailingComment(line, comment);
                    } else {
                        textToMeasure = line;
                    }
                } else {
                    textToMeasure = line;
                }
                if (ignorePattern && ignorePattern.test(textToMeasure) ||
                    ignoreUrls && URL_REGEXP.test(textToMeasure) ||
                    ignoreStrings && stringsByLine[lineNumber] ||
                    ignoreTemplateLiterals && templateLiteralsByLine[lineNumber] ||
                    ignoreRegExpLiterals && regExpLiteralsByLine[lineNumber]
                ) {

                    // ignore this line
                    return;
                }

                const lineLength = computeLineLength(textToMeasure, tabWidth);
                const commentLengthApplies = lineIsComment && maxCommentLength;

                if (lineIsComment && ignoreComments) {
                    return;
                }

                if (commentLengthApplies) {
                    if (lineLength > maxCommentLength) {
                        context.report({
                            node,
                            loc: { line: lineNumber, column: 0 },
                            messageId: "maxComment",
                            data: {
                                lineNumber: i + 1,
                                maxCommentLength
                            }
                        });
                    }
                } else if (lineLength > maxLength) {
                    context.report({
                        node,
                        loc: { line: lineNumber, column: 0 },
                        messageId: "max",
                        data: {
                            lineNumber: i + 1,
                            maxLength
                        }
                    });
                }
            });
        }
function checkProgramForMaxLength ( node ) { // split (honors line-ending) const lines = sourceCode . lines , // list of comments to ignore comments = ignoreComments || maxCommentLength || ignoreTrailingComments ? sourceCode . getAllComments ( ) : [ ] ; // we iterate over comments in parallel with the lines let commentsIndex = 0 ; const strings = getAllStrings ( ) ; const stringsByLine = strings . reduce ( groupByLineNumber , { } ) ; const templateLiterals = getAllTemplateLiterals ( ) ; const templateLiteralsByLine = templateLiterals . reduce ( groupByLineNumber , { } ) ; const regExpLiterals = getAllRegExpLiterals ( ) ; const regExpLiteralsByLine = regExpLiterals . reduce ( groupByLineNumber , { } ) ; lines . forEach ( ( line , i ) => { // i is zero-indexed, line numbers are one-indexed const lineNumber = i + 1 ; /*
                 * if we're checking comment length; we need to know whether this
                 * line is a comment
                 */ let lineIsComment = false ; let textToMeasure ; /*
                 * We can short-circuit the comment checks if we're already out of
                 * comments to check.
                 */ if ( commentsIndex < comments . length ) { let comment = null ; // iterate over comments until we find one past the current line do { comment = comments [ ++ commentsIndex ] ; } while ( comment && comment . loc . start . line <= lineNumber ) ; // and step back by one comment = comments [ -- commentsIndex ] ; if ( isFullLineComment ( line , lineNumber , comment ) ) { lineIsComment = true ; textToMeasure = line ; } else if ( ignoreTrailingComments && isTrailingComment ( line , lineNumber , comment ) ) { textToMeasure = stripTrailingComment ( line , comment ) ; } else { textToMeasure = line ; } } else { textToMeasure = line ; } if ( ignorePattern && ignorePattern . test ( textToMeasure ) || ignoreUrls && URL_REGEXP . test ( textToMeasure ) || ignoreStrings && stringsByLine [ lineNumber ] || ignoreTemplateLiterals && templateLiteralsByLine [ lineNumber ] || ignoreRegExpLiterals && regExpLiteralsByLine [ lineNumber ] ) { // ignore this line return ; } const lineLength = computeLineLength ( textToMeasure , tabWidth ) ; const commentLengthApplies = lineIsComment && maxCommentLength ; if ( lineIsComment && ignoreComments ) { return ; } if ( commentLengthApplies ) { if ( lineLength > maxCommentLength ) { context . report ( { node , loc : { line : lineNumber , column : 0 } , messageId : "maxComment" , data : { lineNumber : i + 1 , maxCommentLength } } ) ; } } else if ( lineLength > maxLength ) { context . report ( { node , loc : { line : lineNumber , column : 0 } , messageId : "max" , data : { lineNumber : i + 1 , maxLength } } ) ; } } ) ; }
Check the program for max length
@param {ASTNode} node Node to examine
@returns {void}
@private
Check the program for max length