From 3ddb815f0818b6b28f6706c067521d2b2a61267d Mon Sep 17 00:00:00 2001 From: Nitin Kumar Date: Mon, 2 Aug 2021 07:52:00 +0530 Subject: [PATCH] Update: improve end report location for `never` option in eol-last --- lib/rules/eol-last.js | 7 +++++- tests/lib/rules/eol-last.js | 44 ++++++++++++++++++------------------- 2 files changed, 28 insertions(+), 23 deletions(-) diff --git a/lib/rules/eol-last.js b/lib/rules/eol-last.js index 24b0c9279c7..c141d9befd2 100644 --- a/lib/rules/eol-last.js +++ b/lib/rules/eol-last.js @@ -86,10 +86,15 @@ module.exports = { }); } else if (mode === "never" && endsWithNewline) { + const secondLastLine = sourceCode.lines[sourceCode.lines.length - 2]; + // File is newline-terminated, but shouldn't be context.report({ node, - loc: location, + loc: { + start: { line: sourceCode.lines.length - 1, column: secondLastLine.length }, + end: { line: sourceCode.lines.length, column: 0 } + }, messageId: "unexpected", fix(fixer) { const finalEOLs = /(?:\r?\n)+$/u, diff --git a/tests/lib/rules/eol-last.js b/tests/lib/rules/eol-last.js index 0600c0daba8..7474b019e02 100644 --- a/tests/lib/rules/eol-last.js +++ b/tests/lib/rules/eol-last.js @@ -83,10 +83,10 @@ ruleTester.run("eol-last", rule, { errors: [{ messageId: "unexpected", type: "Program", - line: 2, - column: 1, - endLine: void 0, - endColumn: void 0 + line: 1, + column: 13, + endLine: 2, + endColumn: 1 }] }, { @@ -96,10 +96,10 @@ ruleTester.run("eol-last", rule, { errors: [{ messageId: "unexpected", type: "Program", - line: 2, - column: 1, - endLine: void 0, - endColumn: void 0 + line: 1, + column: 13, + endLine: 2, + endColumn: 1 }] }, { @@ -109,10 +109,10 @@ ruleTester.run("eol-last", rule, { errors: [{ messageId: "unexpected", type: "Program", - line: 3, + line: 2, column: 1, - endLine: void 0, - endColumn: void 0 + endLine: 3, + endColumn: 1 }] }, { @@ -122,10 +122,10 @@ ruleTester.run("eol-last", rule, { errors: [{ messageId: "unexpected", type: "Program", - line: 3, - column: 1, - endLine: void 0, - endColumn: void 0 + line: 2, + column: 13, + endLine: 3, + endColumn: 1 }] }, { @@ -135,10 +135,10 @@ ruleTester.run("eol-last", rule, { errors: [{ messageId: "unexpected", type: "Program", - line: 3, - column: 1, - endLine: void 0, - endColumn: void 0 + line: 2, + column: 13, + endLine: 3, + endColumn: 1 }] }, { @@ -148,10 +148,10 @@ ruleTester.run("eol-last", rule, { errors: [{ messageId: "unexpected", type: "Program", - line: 3, + line: 2, column: 1, - endLine: void 0, - endColumn: void 0 + endLine: 3, + endColumn: 1 }] },