Skip to content

Commit

Permalink
Add at_least_one_space option to SpaceAroundOperator
Browse files Browse the repository at this point in the history
  • Loading branch information
sds committed Nov 23, 2015
1 parent 596b96d commit 3515907
Show file tree
Hide file tree
Showing 5 changed files with 77 additions and 9 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
references
* Fix `SingleLinePerSelector` to report correct line number for sequences
spread over multiple lines
* Add `at_least_one_space` option to `SpaceAroundOperator` linter

## 0.42.2

Expand Down
2 changes: 1 addition & 1 deletion config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -186,7 +186,7 @@ linters:

SpaceAroundOperator:
enabled: true
style: one_space # or 'no_space'
style: one_space # or 'at_least_one_space', or 'no_space'

SpaceBeforeBrace:
enabled: true
Expand Down
2 changes: 1 addition & 1 deletion lib/scss_lint/linter/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -1413,7 +1413,7 @@ The `style` option allows you to specify a different preferred style.

Configuration Option | Description
---------------------|---------------------------------------------------------
`style` | `one_space`, `no_space` (default **one_space**)
`style` | `one_space`, `at_least_one_space`, `no_space` (default **one_space**)

## SpaceBeforeBrace

Expand Down
30 changes: 23 additions & 7 deletions lib/scss_lint/linter/space_around_operator.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ def source_fm_range(range)

private

def check(node, operation_sources) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity, Metrics/LineLength
def check(node, operation_sources) # rubocop:disable Metrics/AbcSize, Metrics/LineLength, Metrics/MethodLength
match = operation_sources.operator_source.match(/
(?<left_space>\s*)
(?<operator>\S+)
Expand All @@ -41,17 +41,33 @@ def check(node, operation_sources) # rubocop:disable Metrics/AbcSize, Metrics/Cy
# just don't worry about space with a newline.
left_newline = match[:left_space].include?("\n")
right_newline = match[:right_space].include?("\n")
if config['style'] == 'one_space'
if (match[:left_space] != ' ' && !left_newline) ||
(match[:right_space] != ' ' && !right_newline)

case config['style']
when 'one_space'
if one_space_exists?(match, left_newline, right_newline)
add_lint(node, operation_sources.space_msg(match[:operator]))
end
elsif (match[:left_space] != '' && !left_newline) ||
(match[:right_space] != '' && !right_newline)
add_lint(node, operation_sources.no_space_msg(match[:operator]))
when 'at_least_one_space'
unless spaces_exist?(match, left_newline, right_newline)
add_lint(node, operation_sources.space_msg(match[:operator]))
end
else
if spaces_exist?(match, left_newline, right_newline)
add_lint(node, operation_sources.no_space_msg(match[:operator]))
end
end
end

def one_space_exists?(match, left_newline, right_newline)
(match[:left_space] != ' ' && !left_newline) ||
(match[:right_space] != ' ' && !right_newline)
end

def spaces_exist?(match, left_newline, right_newline)
(match[:left_space] != '' && !left_newline) ||
(match[:right_space] != '' && !right_newline)
end

# A helper class for storing and adjusting the sources of the different
# components of an Operation node.
class OperationSources
Expand Down
51 changes: 51 additions & 0 deletions spec/scss_lint/linter/space_around_operator_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -242,6 +242,57 @@
end
end

context 'when at least one space is preferred' do
let(:style) { 'at_least_one_space' }

context 'when values with single-spaced infix operators exist' do
let(:scss) { <<-SCSS }
$x: 2px + 2px;
p {
margin: 5px + 5px;
}
SCSS

it { should_not report_lint }
end

context 'when numeric values with infix operators exist' do
let(:scss) { <<-SCSS }
p {
margin: 5px+5px;
margin: 5px + 5px;
margin: 4px*2;
margin: 20px%3;
font-family: sans-+serif;
}
$x: 10px+10px;
$x: 20px-10px;
SCSS

it { should report_lint line: 2 }
it { should_not report_lint line: 3 }
it { should report_lint line: 4 }
it { should report_lint line: 5 }
it { should report_lint line: 6 }
it { should_not report_lint line: 7 }
it { should report_lint line: 9 }
it { should report_lint line: 10 }
end

context 'when expression spread over two lines' do
let(:scss) { <<-SCSS }
p {
margin: 7px +
7px;
}
SCSS

it { should_not report_lint }
end
end

context 'when no space is preferred' do
let(:style) { 'no_space' }

Expand Down

0 comments on commit 3515907

Please sign in to comment.