Skip to content

Commit

Permalink
Add Trailing Empty Lines linter
Browse files Browse the repository at this point in the history
HAML documents should not contain empty lines at the end of the file.

Inspired by Slim lint's `TrailingBlankLines`


Close sds#422
  • Loading branch information
tagliala committed Jul 14, 2023
1 parent a8db651 commit 5963187
Show file tree
Hide file tree
Showing 6 changed files with 64 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,9 @@
# HAML-Lint Changelog

# unreleased

* Add `TrailingEmptyLines` linter

# 0.48.0

* Fix `Marshal.dump` error when using `--parallel` option and `RepeatedId` Linter
Expand Down
3 changes: 3 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ linters:
TagName:
enabled: true

TrailingEmptyLines:
enabled: true

TrailingWhitespace:
enabled: true

Expand Down
5 changes: 5 additions & 0 deletions lib/haml_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ Below is a list of linters supported by `haml-lint`, ordered alphabetically.
* [SpaceBeforeScript](#spacebeforescript)
* [SpaceInsideHashAttributes](#spaceinsidehashattributes)
* [TagName](#tagname)
* [TrailingEmptyLines](#trailingemptylines)
* [TrailingWhitespace](#trailingwhitespace)
* [UnnecessaryInterpolation](#unnecessaryinterpolation)
* [UnnecessaryStringOutput](#unnecessarystringoutput)
Expand Down Expand Up @@ -711,6 +712,10 @@ general, as it is easier to type and matches the convention of many developer
tools. If you are writing HAML to output XML documents, however, it is a strict
requirement.

## TrailingEmptyLines

HAML documents should not contain empty lines at the end of the file.

## TrailingWhitespace

HAML documents should not contain trailing whitespace (spaces or tabs) on any
Expand Down
22 changes: 22 additions & 0 deletions lib/haml_lint/linter/trailing_empty_lines.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# frozen_string_literal: true

module HamlLint
# Checks for trailing empty lines.
class Linter::TrailingEmptyLines < Linter
include LinterRegistry

DummyNode = Struct.new(:line)

def visit_root(root)
return if document.source.empty?
line_number = document.last_non_empty_line

node = root.node_for_line(line_number)
return if node.disabled?(self)

return unless document.source.end_with?("\n\n")

record_lint(line_number, 'Files should not end with trailing empty lines')
end
end
end
29 changes: 29 additions & 0 deletions spec/haml_lint/linter/trailing_empty_lines_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
# frozen_string_literal: true

describe HamlLint::Linter::TrailingEmptyLines do
include_context 'linter'

context 'when the file is empty' do
let(:haml) { '' }

it { should_not report_lint }
end

context 'when file ends with a single newline' do
let(:haml) { "%h1 Hello\n" }

it { should_not report_lint }
end

context 'when file contains multiple newlines' do
let(:haml) { "%h1 Hello\n\n" }

it { should report_lint line: 1 }

context 'but the linter is disabled in the file' do
let(:haml) { "-# haml-lint:disable TrailingEmptyLines\n" + super() }

it { should_not report_lint }
end
end
end
1 change: 1 addition & 0 deletions test.haml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
%h1 Hello

0 comments on commit 5963187

Please sign in to comment.