Skip to content

Commit

Permalink
Fix Linter ControlStatementSpacing raise incorrect = (#164)
Browse files Browse the repository at this point in the history
This commit is to resolve issue
#101 .

I'm not familiar with OSS, so I apologize if I'm being impolite.

I'm also using machine translation for English.
  • Loading branch information
rotelstift authored Jan 10, 2024
1 parent 0302b22 commit ff1ef25
Show file tree
Hide file tree
Showing 2 changed files with 130 additions and 2 deletions.
34 changes: 32 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,14 @@ 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 +26,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?(/\\$/)
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
98 changes: 98 additions & 0 deletions spec/slim_lint/linter/control_statement_spacing_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,104 @@
it { should_not report_lint }
end

context 'when an element has a multi-line attribute' do
# OK
context 'when it is simple' do
let(:slim) { <<-'SLIM' }
div class='one \
two'
div = some_method
SLIM

it { should_not report_lint }
end

context 'when it is more than two lines' do
let(:slim) { <<-'SLIM' }
div class='one \
two three four \
five six seven \
eight nine ten eleven twelve'
div = some_method
SLIM

it { should_not report_lint }
end

context 'when it has more than two locations' do
let(:slim) { <<-'SLIM' }
div class='one \
two'
div class='one \
two three four five six seven \
eight nine ten eleven twelve'
div class='one \
two three four five six seven \
eight nine ten eleven twelve'
div = some_method
SLIM

it { should_not report_lint }
end

# NG
context 'when it is simple' do
let(:slim) { <<-'SLIM' }
div class='one \
two'
div= some_method
SLIM

it { should report_lint line: 3 }
end

context 'when it is more than two lines' do
let(:slim) { <<-'SLIM' }
div class='one \
two three four \
five six seven \
eight nine ten eleven twelve'
div =some_method
SLIM

it { should report_lint line: 5 }
end

context 'when it has more than two locations' do
let(:slim) { <<-'SLIM' }
div class='one \
two'
div class='one \
two three four five six seven \
eight nine ten eleven twelve'
div class='one \
two three four five six seven \
eight nine ten eleven twelve'
div=some_method
SLIM

it { should report_lint line: 9 }
end

context 'when it has more than two locations and verify the correctness of the line count.' do
let(:slim) { <<-'SLIM' }
div class='one \
two'
div class='one \
two three four five six seven \
eight nine ten eleven twelve'
div=some_method_one
div class='one \
two three four five six seven \
eight nine ten eleven twelve'
div=some_method_two
SLIM

it { should report_lint line: 6 }
it { should report_lint line: 10 }
end
end

context 'when leading whitespace (=<) is used' do
context 'and it has appropriate spacing' do
let(:slim) { 'title =< "Something"' }
Expand Down

0 comments on commit ff1ef25

Please sign in to comment.