From 5963187edb5422958c145df2add67f274549e4d3 Mon Sep 17 00:00:00 2001 From: Geremia Taglialatela Date: Fri, 14 Jul 2023 12:41:48 +0200 Subject: [PATCH] Add Trailing Empty Lines linter HAML documents should not contain empty lines at the end of the file. Inspired by Slim lint's `TrailingBlankLines` Close #422 --- CHANGELOG.md | 4 +++ config/default.yml | 3 ++ lib/haml_lint/linter/README.md | 5 ++++ lib/haml_lint/linter/trailing_empty_lines.rb | 22 ++++++++++++++ .../linter/trailing_empty_lines_spec.rb | 29 +++++++++++++++++++ test.haml | 1 + 6 files changed, 64 insertions(+) create mode 100644 lib/haml_lint/linter/trailing_empty_lines.rb create mode 100644 spec/haml_lint/linter/trailing_empty_lines_spec.rb create mode 100644 test.haml diff --git a/CHANGELOG.md b/CHANGELOG.md index 8e4eec4..2f2903a 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.yml b/config/default.yml index 31558d7..7b7cf37 100644 --- a/config/default.yml +++ b/config/default.yml @@ -105,6 +105,9 @@ linters: TagName: enabled: true + TrailingEmptyLines: + enabled: true + TrailingWhitespace: enabled: true diff --git a/lib/haml_lint/linter/README.md b/lib/haml_lint/linter/README.md index d9d8e60..f315f89 100644 --- a/lib/haml_lint/linter/README.md +++ b/lib/haml_lint/linter/README.md @@ -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) @@ -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 diff --git a/lib/haml_lint/linter/trailing_empty_lines.rb b/lib/haml_lint/linter/trailing_empty_lines.rb new file mode 100644 index 0000000..0bd16dc --- /dev/null +++ b/lib/haml_lint/linter/trailing_empty_lines.rb @@ -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 diff --git a/spec/haml_lint/linter/trailing_empty_lines_spec.rb b/spec/haml_lint/linter/trailing_empty_lines_spec.rb new file mode 100644 index 0000000..faae2ed --- /dev/null +++ b/spec/haml_lint/linter/trailing_empty_lines_spec.rb @@ -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 diff --git a/test.haml b/test.haml new file mode 100644 index 0000000..ca44ab2 --- /dev/null +++ b/test.haml @@ -0,0 +1 @@ +%h1 Hello