Skip to content

Commit

Permalink
Add SpaceBetweenParens linter
Browse files Browse the repository at this point in the history
Change-Id: I9ad5e2fa2224044a499d60ddbc2b24406b7f2ac7
Reviewed-on: https://gerrit.causes.com/32214
Tested-by: jenkins <[email protected]>
Reviewed-by: Henric Trotzig <[email protected]>
  • Loading branch information
mikesherov authored and Shane da Silva committed Dec 31, 2013
1 parent c2d6b75 commit e716c7c
Show file tree
Hide file tree
Showing 4 changed files with 177 additions and 0 deletions.
12 changes: 12 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -387,6 +387,18 @@ Any lint can be disabled by using the `--exclude_linter` flag.
color: rgba(0, 0, 0, .1);
```

* Parentheses should not (or should) be padded with spaces.

```scss
// Incorrect
@include box-shadow( 0 2px 2px rgba( 0, 0, 0, .2 ) );
color: rgba( 0, 0, 0, .1 );
// Correct
@include box-shadow(0 2px 2px rgba(0, 0, 0, .2));
color: rgba(0, 0, 0, .1);
```

* Properties should be formatted with no space between the name and the colon,
and a single space separating the colon from the property's value.

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,10 @@ linters:
SpaceBeforeBrace:
enabled: true

SpaceBetweenParens:
enabled: true
spaces: 0

TrailingSemicolonAfterPropertyValue:
enabled: true

Expand Down
26 changes: 26 additions & 0 deletions lib/scss_lint/linter/space_between_parens.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
module SCSSLint
# Checks for the presence of spaces between parentheses.
class Linter::SpaceBetweenParens < Linter
include LinterRegistry

def visit_root(node)
@spaces = config['spaces']
engine.lines.each_with_index do |line, index|
line.scan /(\( *[^ ]|[^\s] *\))/ do |match|
match.each { |str| check(str, index, engine) }
end
end
end

private

def check(str, index, engine)
spaces = str.count ' '

if spaces != @spaces
@lints << Lint.new(engine.filename, index + 1, "Expected #{pluralize(@spaces, 'space')}" <<
" between parentheses instead of #{spaces}")
end
end
end
end
135 changes: 135 additions & 0 deletions spec/scss_lint/linter/space_between_parens_spec.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
require 'spec_helper'

describe SCSSLint::Linter::SpaceBetweenParens do
context 'when the opening parens is followed by a space' do
let(:css) { <<-CSS }
p {
property: ( value);
}
CSS

it { should report_lint line: 2 }
end

context 'when the closing parens is preceded by a space' do
let(:css) { <<-CSS }
p {
property: (value );
}
CSS

it { should report_lint line: 2 }
end

context 'when both parens are space padded' do
let(:css) { <<-CSS }
p {
property: ( value );
}
CSS

it { should report_lint line: 2, count: 2 }
end

context 'when neither parens are space padded' do
let(:css) { <<-CSS }
p {
property: (value);
}
CSS

it { should_not report_lint }
end

context 'when parens are multi-line' do
let(:css) { <<-CSS }
p {
property: (
value
);
}
CSS

it { should_not report_lint }
end

context 'when parens are multi-line with tabs' do
let(:css) { <<-CSS }
p {
property: (
value
);
}
CSS

it { should_not report_lint }
end

context 'when the number of spaces has been explicitly set' do
let(:linter_config) { { 'spaces' => 1 } }

context 'when the opening parens is followed by a space' do
let(:css) { <<-CSS }
p {
property: ( value);
}
CSS

it { should report_lint line: 2 }
end

context 'when the closing parens is preceded by a space' do
let(:css) { <<-CSS }
p {
property: (value );
}
CSS

it { should report_lint line: 2 }
end

context 'when neither parens are space padded' do
let(:css) { <<-CSS }
p {
property: (value);
}
CSS

it { should report_lint line: 2, count: 2 }
end

context 'when both parens are space padded' do
let(:css) { <<-CSS }
p {
property: ( value );
}
CSS

it { should_not report_lint }
end

context 'when parens are multi-line' do
let(:css) { <<-CSS }
p {
property: (
value
);
}
CSS

it { should_not report_lint }
end

context 'when parens are multi-line with tabs' do
let(:css) { <<-CSS }
p {
property: (
value
);
}
CSS

it { should_not report_lint }
end
end
end

0 comments on commit e716c7c

Please sign in to comment.