Skip to content

Commit

Permalink
Examine all contiguous whitespace that follows a colon
Browse files Browse the repository at this point in the history
Fixes sds#434.
  • Loading branch information
srawlins authored and sds committed Apr 26, 2015
1 parent 279ff99 commit c150a00
Show file tree
Hide file tree
Showing 2 changed files with 68 additions and 16 deletions.
32 changes: 16 additions & 16 deletions lib/scss_lint/linter/space_after_property_colon.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,34 +13,34 @@ def visit_rule(node)
end

def visit_prop(node)
spaces = spaces_after_colon(node)
whitespace = whitespace_after_colon(node)

case config['style']
when 'no_space'
check_for_no_spaces(node, spaces)
check_for_no_spaces(node, whitespace)
when 'one_space'
check_for_one_space(node, spaces)
check_for_one_space(node, whitespace)
when 'at_least_one_space'
check_for_at_least_one_space(node, spaces)
check_for_at_least_one_space(node, whitespace)
end

yield # Continue linting children
end

private

def check_for_no_spaces(node, spaces)
return if spaces == 0
def check_for_no_spaces(node, whitespace)
return if whitespace == []
add_lint(node, 'Colon after property should not be followed by any spaces')
end

def check_for_one_space(node, spaces)
return if spaces == 1
def check_for_one_space(node, whitespace)
return if whitespace == [' ']
add_lint(node, 'Colon after property should be followed by one space')
end

def check_for_at_least_one_space(node, spaces)
return if spaces >= 1
def check_for_at_least_one_space(node, whitespace)
return if whitespace.uniq == [' ']
add_lint(node, 'Colon after property should be followed by at least one space')
end

Expand All @@ -60,11 +60,11 @@ def value_offset(prop)
src_range = prop.name_source_range
src_range.start_pos.offset +
(src_range.end_pos.offset - src_range.start_pos.offset) +
spaces_after_colon(prop)
whitespace_after_colon(prop).take_while { |w| w == ' ' }.size
end

def spaces_after_colon(node)
spaces = 0
def whitespace_after_colon(node)
whitespace = []
offset = 1

# Find the colon after the property name
Expand All @@ -73,12 +73,12 @@ def spaces_after_colon(node)
end

# Count spaces after the colon
while character_at(node.name_source_range.start_pos, offset) == ' '
spaces += 1
while [' ', "\t", "\n"].include? character_at(node.name_source_range.start_pos, offset)
whitespace << character_at(node.name_source_range.start_pos, offset)
offset += 1
end

spaces
whitespace
end
end
end
52 changes: 52 additions & 0 deletions spec/scss_lint/linter/space_after_property_colon_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -81,6 +81,27 @@

it { should report_lint line: 3 }
end

context 'when the colon after a property is followed by a space and a newline' do
let(:scss) { <<-SCSS }
p {
margin:
0;
}
SCSS

it { should report_lint line: 2 }
end

context 'when the colon after a property is followed by a tab' do
let(:scss) { <<-SCSS }
p {
margin: 0;
}
SCSS

it { should report_lint line: 2 }
end
end

context 'when no spaces are allowed' do
Expand Down Expand Up @@ -135,6 +156,27 @@

it { should report_lint line: 2 }
end

context 'when the colon after a property is followed by a newline' do
let(:scss) { <<-SCSS }
p {
margin:
0;
}
SCSS

it { should report_lint line: 2 }
end

context 'when the colon after a property is followed by a tab' do
let(:scss) { <<-SCSS }
p {
margin: 0;
}
SCSS

it { should report_lint line: 2 }
end
end

context 'when at least one space is preferred' do
Expand Down Expand Up @@ -189,6 +231,16 @@

it { should_not report_lint }
end

context 'when the colon after a property is followed by multiple spaces and a tab' do
let(:scss) { <<-SCSS }
p {
margin: bold;
}
SCSS

it { should report_lint line: 2 }
end
end

context 'when aligned property values are preferred' do
Expand Down

0 comments on commit c150a00

Please sign in to comment.