Skip to content

Commit

Permalink
Handles empty documents and leading new lines (#57)
Browse files Browse the repository at this point in the history
  • Loading branch information
davidwessman authored Aug 22, 2023
1 parent cf23af0 commit 0cd3195
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,10 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/) a

## [Unreleased]

### Fixes

- Handles formatting empty documents and removing leading new-linews in files with content.

## [0.10.1] - 2023-08-20

### Added
Expand Down
4 changes: 4 additions & 0 deletions lib/syntax_tree/erb/parser.rb
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,10 @@ def parse_any_tag
@found_doctype = true
end
end

# Ignore new lines in beginning of document
next if tag.is_a?(NewLine)

# Allow skipping empty CharData
return tag unless tag.skip?
end
Expand Down
7 changes: 7 additions & 0 deletions test/erb_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -101,5 +101,12 @@ def test_erb_group_blank_line

assert_formatting(source, source)
end

def test_erb_empty_first_line
source = "\n\n<%= what %>\n"
expected = "<%= what %>\n"

assert_formatting(source, expected)
end
end
end
5 changes: 5 additions & 0 deletions test/html_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,11 @@ def test_html_unmatched_single_quote
end
end

def test_empty_file
source = ""
assert_formatting(source, "\n")
end

def test_html_doctype
parsed = ERB.parse("<!DOCTYPE html>")
assert_instance_of(SyntaxTree::ERB::Doctype, parsed.elements.first)
Expand Down

0 comments on commit 0cd3195

Please sign in to comment.