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 1e5d3ebf291..7474b019e02 100644 --- a/tests/lib/rules/eol-last.js +++ b/tests/lib/rules/eol-last.js @@ -55,48 +55,104 @@ ruleTester.run("eol-last", rule, { { code: "var a = 123;", output: "var a = 123;\n", - errors: [{ messageId: "missing", type: "Program" }] + errors: [{ + messageId: "missing", + type: "Program", + line: 1, + column: 13, + endLine: void 0, + endColumn: void 0 + }] }, { code: "var a = 123;\n ", output: "var a = 123;\n \n", - errors: [{ messageId: "missing", type: "Program" }] + errors: [{ + messageId: "missing", + type: "Program", + line: 2, + column: 4, + endLine: void 0, + endColumn: void 0 + }] }, { code: "var a = 123;\n", output: "var a = 123;", options: ["never"], - errors: [{ messageId: "unexpected", type: "Program" }] + errors: [{ + messageId: "unexpected", + type: "Program", + line: 1, + column: 13, + endLine: 2, + endColumn: 1 + }] }, { code: "var a = 123;\r\n", output: "var a = 123;", options: ["never"], - errors: [{ messageId: "unexpected", type: "Program" }] + errors: [{ + messageId: "unexpected", + type: "Program", + line: 1, + column: 13, + endLine: 2, + endColumn: 1 + }] }, { code: "var a = 123;\r\n\r\n", output: "var a = 123;", options: ["never"], - errors: [{ messageId: "unexpected", type: "Program" }] + errors: [{ + messageId: "unexpected", + type: "Program", + line: 2, + column: 1, + endLine: 3, + endColumn: 1 + }] }, { code: "var a = 123;\nvar b = 456;\n", output: "var a = 123;\nvar b = 456;", options: ["never"], - errors: [{ messageId: "unexpected", type: "Program" }] + errors: [{ + messageId: "unexpected", + type: "Program", + line: 2, + column: 13, + endLine: 3, + endColumn: 1 + }] }, { code: "var a = 123;\r\nvar b = 456;\r\n", output: "var a = 123;\r\nvar b = 456;", options: ["never"], - errors: [{ messageId: "unexpected", type: "Program" }] + errors: [{ + messageId: "unexpected", + type: "Program", + line: 2, + column: 13, + endLine: 3, + endColumn: 1 + }] }, { code: "var a = 123;\n\n", output: "var a = 123;", options: ["never"], - errors: [{ messageId: "unexpected", type: "Program" }] + errors: [{ + messageId: "unexpected", + type: "Program", + line: 2, + column: 1, + endLine: 3, + endColumn: 1 + }] }, // Deprecated: `"unix"` parameter @@ -104,13 +160,27 @@ ruleTester.run("eol-last", rule, { code: "var a = 123;", output: "var a = 123;\n", options: ["unix"], - errors: [{ messageId: "missing", type: "Program" }] + errors: [{ + messageId: "missing", + type: "Program", + line: 1, + column: 13, + endLine: void 0, + endColumn: void 0 + }] }, { code: "var a = 123;\n ", output: "var a = 123;\n \n", options: ["unix"], - errors: [{ messageId: "missing", type: "Program" }] + errors: [{ + messageId: "missing", + type: "Program", + line: 2, + column: 4, + endLine: void 0, + endColumn: void 0 + }] }, // Deprecated: `"windows"` parameter @@ -118,13 +188,27 @@ ruleTester.run("eol-last", rule, { code: "var a = 123;", output: "var a = 123;\r\n", options: ["windows"], - errors: [{ messageId: "missing", type: "Program" }] + errors: [{ + messageId: "missing", + type: "Program", + line: 1, + column: 13, + endLine: void 0, + endColumn: void 0 + }] }, { code: "var a = 123;\r\n ", output: "var a = 123;\r\n \r\n", options: ["windows"], - errors: [{ messageId: "missing", type: "Program" }] + errors: [{ + messageId: "missing", + type: "Program", + line: 2, + column: 4, + endLine: void 0, + endColumn: void 0 + }] } ] });