Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add Trailing Empty Lines linter #424

Merged
merged 1 commit into from
Jul 15, 2023
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
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