Skip to content

Commit

Permalink
Add FinalNewline linter
Browse files Browse the repository at this point in the history
Enforces the presence of a final trailing newline in files.
  • Loading branch information
sds committed Jun 23, 2015
1 parent 134c414 commit 1635c07
Show file tree
Hide file tree
Showing 6 changed files with 91 additions and 1 deletion.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@
* Improve bug reporting instructions in error message
* Add `Indentation` linter to enforce that spaces or tabs are used for
indentation (enabled by default and defaults to spaces)
* Add `FinalNewline` linter to enforce the presence of a final newline
in files

## 0.14.1

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ linters:
EmptyScript:
enabled: true

FinalNewline:
enabled: true
present: true

HtmlAttributes:
enabled: true

Expand Down
14 changes: 14 additions & 0 deletions lib/haml_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ Below is a list of linters supported by `haml-lint`, ordered alphabetically.
* [ConsecutiveComments](#consecutivecomments)
* [ConsecutiveSilentScripts](#consecutivesilentscripts)
* [EmptyScript](#emptyscript)
* [FinalNewline](#finalnewline)
* [HtmlAttributes](#htmlattributes)
* [ImplicitDiv](#implicitdiv)
* [Indentation](#indentation)
Expand Down Expand Up @@ -144,6 +145,19 @@ Don't write empty scripts.

These serve no purpose and are usually left behind by mistake.

## FinalNewline

Files should always have a final newline. This results in better diffs when
adding lines to the file, since SCM systems such as `git` won't think that you
touched the last line if you append to the end of a file.

You can customize whether or not a final newline exists with the `present`
option.

Configuration Option | Description
---------------------|---------------------------------------------------------
`present` | Whether a final newline should be present (default `true`)

## HtmlAttributes

Don't use the
Expand Down
21 changes: 21 additions & 0 deletions lib/haml_lint/linter/final_newline.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
module HamlLint
# Checks for final newlines at the end of a file.
class Linter::FinalNewline < Linter
include LinterRegistry

def visit_root(_node)
return if document.source.empty?

dummy_node = Struct.new(:line).new(document.source_lines.count)
ends_with_newline = document.source.end_with?("\n")

if config['present']
record_lint(dummy_node,
'Files should end with a trailing newline') unless ends_with_newline
else
record_lint(dummy_node,
'Files should not end with a trailing newline') if ends_with_newline
end
end
end
end
49 changes: 49 additions & 0 deletions spec/haml_lint/linter/final_newline_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
require 'spec_helper'

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

context 'when trailing newline is preferred' do
let(:config) { super().merge('present' => true) }

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

it { should_not report_lint }
end

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

it { should_not report_lint }
end

context 'when the file does not end with a newline' do
let(:haml) { '%span' }

it { should report_lint line: 1 }
end
end

context 'when no trailing newline is preferred' do
let(:config) { super().merge('present' => false) }

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

it { should_not report_lint }
end

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

it { should report_lint line: 1 }
end

context 'when the file does not end with a newline' do
let(:haml) { '%span' }

it { should_not report_lint }
end
end
end
2 changes: 1 addition & 1 deletion spec/haml_lint/rake_task_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ def run_task
end

context 'when Haml document is valid' do
let(:haml) { '%p Hello world' }
let(:haml) { "%p Hello world\n" }

it 'executes without error' do
expect { run_task }.not_to raise_error
Expand Down

0 comments on commit 1635c07

Please sign in to comment.