Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -183,7 +183,7 @@ export class ERBNoTrailingWhitespaceRule extends ParserRule<ERBNoTrailingWhitesp
const offenseIsAtEndOfContent = this.isOffenseAtEndOfContent(offense, node)

if (offenseIsAtEndOfContent) {
if (this.hasTrailingWhitespaceNotIndentation(fixedContent)) {
if (this.hasTrailingWhitespaceNotIndentation(fixedContent) || this.isNodeAtEndOfDocument(node, result)) {
fixedContent = trimTrailingWhitespace(fixedContent)
}

Expand Down Expand Up @@ -219,6 +219,12 @@ export class ERBNoTrailingWhitespaceRule extends ParserRule<ERBNoTrailingWhitesp
return offense.location.end.line === node.location.end.line && offense.location.end.column === node.location.end.column
}

private isNodeAtEndOfDocument(node: Mutable<HTMLTextNode> | Mutable<LiteralNode>, 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

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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 = "<div>\n</div>\n "
const expected = "<div>\n</div>\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 = "<!-- comment with trailing space \n and more content -->"
Expand Down
Loading