From 09cc0a2bb5bcf3bcb0766a3c989871f268518437 Mon Sep 17 00:00:00 2001 From: Anix Date: Fri, 19 Jun 2020 21:04:45 +0530 Subject: [PATCH] Update: max-lines reporting loc improvement (refs #12334) (#13318) * Update: max-lines reporting loc refactore (refs #12334) * Update: changed loc position * Update: updated startline logic and end loc * Chore: refactor * Chore: tests error property ordering * Chore: tests re-ordering --- lib/rules/max-lines.js | 42 ++++- tests/lib/rules/max-lines.js | 320 +++++++++++++++++++++++++++++++++-- 2 files changed, 342 insertions(+), 20 deletions(-) diff --git a/lib/rules/max-lines.js b/lib/rules/max-lines.js index 299377bc2dd..f33adbdb5b6 100644 --- a/lib/rules/max-lines.js +++ b/lib/rules/max-lines.js @@ -53,7 +53,8 @@ module.exports = { } ], messages: { - exceed: "File has too many lines ({{actual}}). Maximum allowed is {{max}}." + exceed: + "File has too many lines ({{actual}}). Maximum allowed is {{max}}." } }, @@ -61,7 +62,10 @@ module.exports = { const option = context.options[0]; let max = 300; - if (typeof option === "object" && Object.prototype.hasOwnProperty.call(option, "max")) { + if ( + typeof option === "object" && + Object.prototype.hasOwnProperty.call(option, "max") + ) { max = option.max; } else if (typeof option === "number") { max = option; @@ -94,7 +98,9 @@ module.exports = { token = comment; do { - token = sourceCode.getTokenBefore(token, { includeComments: true }); + token = sourceCode.getTokenBefore(token, { + includeComments: true + }); } while (isCommentNodeType(token)); if (token && astUtils.isTokenOnSameLine(token, comment)) { @@ -103,7 +109,9 @@ module.exports = { token = comment; do { - token = sourceCode.getTokenAfter(token, { includeComments: true }); + token = sourceCode.getTokenAfter(token, { + includeComments: true + }); } while (isCommentNodeType(token)); if (token && astUtils.isTokenOnSameLine(comment, token)) { @@ -118,7 +126,10 @@ module.exports = { return { "Program:exit"() { - let lines = sourceCode.lines.map((text, i) => ({ lineNumber: i + 1, text })); + let lines = sourceCode.lines.map((text, i) => ({ + lineNumber: i + 1, + text + })); if (skipBlankLines) { lines = lines.filter(l => l.text.trim() !== ""); @@ -127,14 +138,29 @@ module.exports = { if (skipComments) { const comments = sourceCode.getAllComments(); - const commentLines = lodash.flatten(comments.map(comment => getLinesWithoutCode(comment))); + const commentLines = lodash.flatten( + comments.map(comment => getLinesWithoutCode(comment)) + ); - lines = lines.filter(l => !lodash.includes(commentLines, l.lineNumber)); + lines = lines.filter( + l => !lodash.includes(commentLines, l.lineNumber) + ); } if (lines.length > max) { + const loc = { + start: { + line: lines[max].lineNumber, + column: 0 + }, + end: { + line: sourceCode.lines.length, + column: lodash.last(sourceCode.lines).length + } + }; + context.report({ - loc: { line: 1, column: 0 }, + loc, messageId: "exceed", data: { max, diff --git a/tests/lib/rules/max-lines.js b/tests/lib/rules/max-lines.js index 2929551539a..b7c255e9322 100644 --- a/tests/lib/rules/max-lines.js +++ b/tests/lib/rules/max-lines.js @@ -9,10 +9,8 @@ //------------------------------------------------------------------------------ const rule = require("../../../lib/rules/max-lines"), - { RuleTester } = require("../../../lib/rule-tester"); - //------------------------------------------------------------------------------ // Tests //------------------------------------------------------------------------------ @@ -79,17 +77,44 @@ ruleTester.run("max-lines", rule, { { code: "var xyz;\nvar xyz;\nvar xyz;", options: [2], - errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 3 }, + line: 3, + column: 1, + endLine: 3, + endColumn: 9 + } + ] }, { code: "/* a multiline comment\n that goes to many lines*/\nvar xy;\nvar xy;", options: [2], - errors: [{ messageId: "exceed", data: { max: 2, actual: 4 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 4, + endColumn: 8 + } + ] }, { code: "//a single line comment\nvar xy;\nvar xy;", options: [2], - errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 3 }, + line: 3, + column: 1, + endLine: 3, + endColumn: 8 + } + ] }, { code: [ @@ -100,7 +125,16 @@ ruleTester.run("max-lines", rule, { "var y;" ].join("\n"), options: [{ max: 2 }], - errors: [{ messageId: "exceed", data: { max: 2, actual: 5 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 5 }, + line: 3, + column: 1, + endLine: 5, + endColumn: 7 + } + ] }, { code: [ @@ -114,7 +148,16 @@ ruleTester.run("max-lines", rule, { " long comment*/" ].join("\n"), options: [{ max: 2, skipComments: true }], - errors: [{ messageId: "exceed", data: { max: 2, actual: 4 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 4, + column: 1, + endLine: 8, + endColumn: 16 + } + ] }, { code: [ @@ -123,7 +166,16 @@ ruleTester.run("max-lines", rule, { "var z;" ].join("\n"), options: [{ max: 2, skipComments: true }], - errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 3 }, + line: 3, + column: 1, + endLine: 3, + endColumn: 7 + } + ] }, { code: [ @@ -133,7 +185,16 @@ ruleTester.run("max-lines", rule, { "var z;" ].join("\n"), options: [{ max: 2, skipComments: true }], - errors: [{ messageId: "exceed", data: { max: 2, actual: 3 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 3 }, + line: 4, + column: 1, + endLine: 4, + endColumn: 7 + } + ] }, { code: [ @@ -147,17 +208,252 @@ ruleTester.run("max-lines", rule, { " long comment*/" ].join("\n"), options: [{ max: 2, skipBlankLines: true }], - errors: [{ messageId: "exceed", data: { max: 2, actual: 6 } }] + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 6 }, + line: 4, + column: 1, + endLine: 8, + endColumn: 16 + } + ] }, { code: "AAAAAAAA\n".repeat(301).trim(), options: [{}], - errors: [{ messageId: "exceed", data: { max: 300, actual: 301 } }] + errors: [ + { + messageId: "exceed", + data: { max: 300, actual: 301 }, + line: 301, + column: 1, + endLine: 301, + endColumn: 9 + } + ] }, { code: "A", options: [{ max: 0 }], - errors: [{ messageId: "exceed", data: { max: 0, actual: 1 } }] + errors: [ + { + messageId: "exceed", + data: { max: 0, actual: 1 }, + line: 1, + column: 1, + endLine: 1, + endColumn: 2 + } + ] + }, + { + code: ["var a = 'a'; ", "var x", "var c;", "console.log"].join( + "\n" + ), + options: [{ max: 2 }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 4, + endColumn: 12 + } + ] + }, + { + code: "var a = 'a',\nc,\nx;\r", + options: [{ max: 2 }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 4, + endColumn: 1 + } + ] + }, + { + code: "var a = 'a',\nc,\nx;\n", + options: [{ max: 2 }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 4, + endColumn: 1 + } + ] + }, + { + code: "\n\nvar a = 'a',\nc,\nx;\n", + options: [{ max: 2, skipBlankLines: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 3 }, + line: 5, + column: 1, + endLine: 6, + endColumn: 1 + } + ] + }, + { + code: [ + "var a = 'a'; ", + "var x", + "var c;", + "console.log", + "// some block ", + "// comments" + ].join("\n"), + options: [{ max: 2, skipComments: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 6, + endColumn: 12 + } + ] + }, + { + code: [ + "var a = 'a'; ", + "var x", + "var c;", + "console.log", + "/* block comments */" + ].join("\n"), + options: [{ max: 2, skipComments: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 5, + endColumn: 21 + } + ] + }, + { + code: [ + "var a = 'a'; ", + "var x", + "var c;", + "console.log", + "/** block \n\n comments */" + ].join("\n"), + options: [{ max: 2, skipComments: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 3, + column: 1, + endLine: 7, + endColumn: 13 + } + ] + }, + { + code: [ + "var a = 'a'; ", + "var x", + "\n", + "var c;", + "console.log", + "\n" + ].join("\n"), + options: [{ max: 2, skipBlankLines: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 5, + column: 1, + endLine: 8, + endColumn: 1 + } + ] + }, + { + code: [ + "var a = 'a'; ", + "\n", + "var x", + "var c;", + "console.log", + "\n" + ].join("\n"), + options: [{ max: 2, skipBlankLines: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 5, + column: 1, + endLine: 8, + endColumn: 1 + } + ] + }, + { + code: [ + "var a = 'a'; ", + "//", + "var x", + "var c;", + "console.log", + "//" + ].join("\n"), + options: [{ max: 2, skipComments: true }], + errors: [ + { + messageId: "exceed", + data: { max: 2, actual: 4 }, + line: 4, + column: 1, + endLine: 6, + endColumn: 3 + } + ] + }, + { + code: ["// hello world", "/*hello", " world 2 */", "var a,", "b", "// hh", "c,", "e,", "f;"].join("\n"), + options: [{ max: 2, skipComments: true }], + errors: [{ + data: { max: 2, actual: 5 }, + messageId: "exceed", + line: 7, + column: 1, + endLine: 9, + endColumn: 3 + + }] + }, + { + code: ["", "var x = '';", "", "// comment", "", "var b = '',", "c,", "d,", "e", "", "// comment"].join("\n"), + options: [{ max: 2, skipComments: true, skipBlankLines: true }], + errors: [{ + data: { max: 2, actual: 5 }, + messageId: "exceed", + line: 7, + column: 1, + endLine: 11, + endColumn: 11 + }] } + ] });