diff --git a/javascript/packages/linter/src/rules/erb-no-trailing-whitespace.ts b/javascript/packages/linter/src/rules/erb-no-trailing-whitespace.ts index 1813b0215..df7748698 100644 --- a/javascript/packages/linter/src/rules/erb-no-trailing-whitespace.ts +++ b/javascript/packages/linter/src/rules/erb-no-trailing-whitespace.ts @@ -183,7 +183,7 @@ export class ERBNoTrailingWhitespaceRule extends ParserRule | Mutable, result: ParseResult): boolean { + const documentEnd = result.value.location.end + + return node.location.end.line === documentEnd.line && node.location.end.column === documentEnd.column + } + private hasTrailingWhitespaceNotIndentation(content: string): boolean { if (content.endsWith("\n")) return false diff --git a/javascript/packages/linter/test/autofix/erb-no-trailing-whitespace.autofix.test.ts b/javascript/packages/linter/test/autofix/erb-no-trailing-whitespace.autofix.test.ts index 2522ad49b..349da62b1 100644 --- a/javascript/packages/linter/test/autofix/erb-no-trailing-whitespace.autofix.test.ts +++ b/javascript/packages/linter/test/autofix/erb-no-trailing-whitespace.autofix.test.ts @@ -328,6 +328,30 @@ describe("erb-no-trailing-whitespace autofix", () => { }) }) + describe("end of file", () => { + test("removes a whitespace-only last line without a trailing newline", () => { + const input = "
\n
\n " + const expected = "
\n
\n" + + const linter = new Linter(Herb, [ERBNoTrailingWhitespaceRule]) + const result = linter.autofix(input, { fileName: "test.html.erb" }) + + expect(result.source).toBe(expected) + expect(result.fixed).toHaveLength(1) + expect(result.unfixed).toHaveLength(0) + expect(linter.lint(result.source, { fileName: "test.html.erb" }).offenses).toHaveLength(0) + }) + + test("removes a whitespace-only file", () => { + const linter = new Linter(Herb, [ERBNoTrailingWhitespaceRule]) + const result = linter.autofix(" ", { fileName: "test.html.erb" }) + + expect(result.source).toBe("") + expect(result.fixed).toHaveLength(1) + expect(result.unfixed).toHaveLength(0) + }) + }) + describe("HTML comments", () => { test("removes trailing whitespace in multiline HTML comments", () => { const input = ""