Skip to content
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion lib/bashly.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ module Bashly
]

autoloads 'bashly/concerns', %i[
AssetHelper Completions Renderable ValidationHelpers
AssetHelper Completions LintHelper Renderable ValidationHelpers
]

module Script
Expand Down
79 changes: 79 additions & 0 deletions lib/bashly/concerns/lint_helper.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,79 @@
module Bashly
# A helper class, used by the `String#lint` extension.
# It will remove consecutive newlines and hidden comments from a string
# unless the line is within a heredoc block.
class LintHelper
attr_reader :lines, :output, :marker

def initialize(script)
@lines = script.lines
@output = []
@marker = nil
@previous_blank = false
end

def lint
lines.each { |line| process_line line }
output.join
end

private

def process_line(line)
if inside_heredoc?
handle_heredoc_line line
else
handle_regular_line line
end
end

def handle_heredoc_line(line)
output << line
reset_marker if heredoc_closed? line
end

def handle_regular_line(line)
set_heredoc_state line
return handle_blank_line if blank? line
return if comment? line

output << line
@previous_blank = false
end

def handle_blank_line
return if @previous_blank

output << "\n"
@previous_blank = true
end

def blank?(line)
line.strip.empty?
end

def comment?(line)
line =~ /^\s*##/
end

def set_heredoc_state(line)
@marker = extract_heredoc_marker line unless inside_heredoc?
end

def extract_heredoc_marker(line)
line =~ /<<-?\s*['"]?(\w+)['"]?/ ? $1 : nil
end

def inside_heredoc?
!!marker
end

def heredoc_closed?(line)
line.strip == marker
end

def reset_marker
@marker = nil
end
end
end
2 changes: 1 addition & 1 deletion lib/bashly/extensions/string.rb
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ def wrap(length = 80)
end

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

def remove_front_matter
Expand Down
4 changes: 2 additions & 2 deletions spec/approvals/examples/stacktrace
Original file line number Diff line number Diff line change
Expand Up @@ -51,5 +51,5 @@ Examples:

Stack trace:
from ./download:15 in `root_command`
from ./download:260 in `run`
from ./download:266 in `main`
from ./download:259 in `run`
from ./download:265 in `main`
Loading