Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add SpaceBetweenParens linter #1

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
12 changes: 12 additions & 0 deletions README.md
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
Expand Up @@ -68,6 +68,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
@@ -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 /(\( *[^ ]|[^ ] *\))/ 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 parenthesis instead of #{spaces}")
end
end
end
end
87 changes: 87 additions & 0 deletions spec/scss_lint/linter/space_between_parens_spec.rb
@@ -0,0 +1,87 @@
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 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 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
end
end