Skip to content

Commit 93ce17b

Browse files
committed
- Fix consecutive newlines in heredoc not being preserved
1 parent 406a86a commit 93ce17b

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

lib/bashly.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ module Bashly
1616
]
1717

1818
autoloads 'bashly/concerns', %i[
19-
AssetHelper Completions Renderable ValidationHelpers
19+
AssetHelper Completions LintHelper Renderable ValidationHelpers
2020
]
2121

2222
module Script

lib/bashly/concerns/lint_helper.rb

Lines changed: 79 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,79 @@
1+
module Bashly
2+
# A helper class, used by the `String#lint` extension.
3+
# It will remove consecutive newlines and hidden comments from a string
4+
# unless the line is within a heredoc block.
5+
class LintHelper
6+
attr_reader :lines, :output, :marker
7+
8+
def initialize(script)
9+
@lines = script.lines
10+
@output = []
11+
@marker = nil
12+
@previous_blank = false
13+
end
14+
15+
def lint
16+
lines.each { |line| process_line line }
17+
output.join
18+
end
19+
20+
private
21+
22+
def process_line(line)
23+
if inside_heredoc?
24+
handle_heredoc_line line
25+
else
26+
handle_regular_line line
27+
end
28+
end
29+
30+
def handle_heredoc_line(line)
31+
output << line
32+
reset_marker if heredoc_closed? line
33+
end
34+
35+
def handle_regular_line(line)
36+
set_heredoc_state line
37+
return handle_blank_line if blank? line
38+
return if comment? line
39+
40+
output << line
41+
@previous_blank = false
42+
end
43+
44+
def handle_blank_line
45+
return if @previous_blank
46+
47+
output << "\n"
48+
@previous_blank = true
49+
end
50+
51+
def blank?(line)
52+
line.strip.empty?
53+
end
54+
55+
def comment?(line)
56+
line =~ /^\s*##/
57+
end
58+
59+
def set_heredoc_state(line)
60+
@marker = extract_heredoc_marker line unless inside_heredoc?
61+
end
62+
63+
def extract_heredoc_marker(line)
64+
line =~ /<<-?\s*['"]?(\w+)['"]?/ ? $1 : nil
65+
end
66+
67+
def inside_heredoc?
68+
!!marker
69+
end
70+
71+
def heredoc_closed?(line)
72+
line.strip == marker
73+
end
74+
75+
def reset_marker
76+
@marker = nil
77+
end
78+
end
79+
end

lib/bashly/extensions/string.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ def wrap(length = 80)
4545
end
4646

4747
def lint
48-
gsub(/\s+\n/m, "\n\n").lines.grep_v(/^\s*##/).join
48+
Bashly::LintHelper.new(self).lint
4949
end
5050

5151
def remove_front_matter

spec/approvals/examples/stacktrace

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -51,5 +51,5 @@ Examples:
5151

5252
Stack trace:
5353
from ./download:15 in `root_command`
54-
from ./download:260 in `run`
55-
from ./download:266 in `main`
54+
from ./download:259 in `run`
55+
from ./download:265 in `main`

0 commit comments

Comments
 (0)