diff --git a/lib/slim_lint/linter/control_statement_spacing.rb b/lib/slim_lint/linter/control_statement_spacing.rb index 964ff4d..05d33da 100644 --- a/lib/slim_lint/linter/control_statement_spacing.rb +++ b/lib/slim_lint/linter/control_statement_spacing.rb @@ -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] @@ -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