Skip to content

Commit

Permalink
fix Linter ControlStatementSpacing raise incorrect =
Browse files Browse the repository at this point in the history
  • Loading branch information
rotelstift committed Jan 6, 2024
1 parent e599bfb commit 5ba066a
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions lib/slim_lint/linter/control_statement_spacing.rb
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,15 @@ class Linter::ControlStatementSpacing < Linter

on [:html, :tag, anything, [],
[:slim, :output, anything, capture(:ruby, anything)]] do |sexp|
# Fetch original Slim code that contains an element with a control statement.
line = document.source_lines[sexp.line - 1]
# Process original slim code so that multi-line attributes become single line.
# And store the correction line count
source = merge_multiline_attributes(document.source_lines)

# Fetch processed Slim code that contains an element with a control statement.
line = source[sexp.line - 1][:line]
# Apply correction to the line count.
sexp.line += source[sexp.line - 1][:line_count]


# Remove any Ruby code, because our regexp below must not match inside Ruby.
ruby = captures[:ruby]
Expand All @@ -20,5 +27,29 @@ class Linter::ControlStatementSpacing < Linter

report_lint(sexp, MESSAGE)
end

private

def merge_multiline_attributes(source_lines)
result = []
memo = ''
correction_line_count = 0

source_lines.each do |line|
memo += line.chomp('\\')

# Lines ending in a backslash are concatenated with the next line
# And count the number of lines to correct the sexp line count.
if line.match?(/\\$/) then
correction_line_count += 1
next
end

# Add merged rows and correction line count to the result and reset the memo
result << {line: memo, line_count: correction_line_count}
memo = ''
end
result
end
end
end

0 comments on commit 5ba066a

Please sign in to comment.