-
Notifications
You must be signed in to change notification settings - Fork 101
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Enforces the presence of a final trailing newline in files.
- Loading branch information
Showing
6 changed files
with
91 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters