Skip to content

Commit

Permalink
Check !default, !global, and !optional flags in BangFormat
Browse files Browse the repository at this point in the history
This linter was not visiting variable declarations or `@extend`
directives, so it was not checking for `!default`, `!global`, and
`!optional`.
  • Loading branch information
lencioni committed Sep 25, 2015
1 parent beaadcd commit 4571966
Show file tree
Hide file tree
Showing 4 changed files with 83 additions and 7 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@
## master (unreleased)

* Fix `TrailingSemicolon` for variables with `!default` and `!global`.
* Check `!default` and `!global` variable declarations and `!optional` `@extend`
directives in `BangFormat`

## 0.42.0

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 @@ -62,7 +62,7 @@ Below is a list of linters supported by `scss-lint`, ordered alphabetically.

## BangFormat

Reports when you use improper spacing around `!` (the "bang") in `!important` and `!default` declarations.
Reports when you use improper spacing around `!` (the "bang") in `!default`, `!global`, `!important`, and `!optional` flags.

You can prefer a single space or no space both before and after the `!`.

Expand Down
28 changes: 22 additions & 6 deletions lib/scss_lint/linter/bang_format.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,28 @@ class Linter::BangFormat < Linter

STOPPING_CHARACTERS = ['!', "'", '"', nil]

def visit_extend(node)
check_bang(node)
end

def visit_prop(node)
return unless source_from_range(node.source_range).include?('!')
return unless check_spacing(node)
check_bang(node)
end

def visit_variable(node)
check_bang(node)
end

private

def check_bang(node)
range = if node.respond_to?(:value_source_range)
node.value_source_range
else
node.source_range
end
return unless source_from_range(range).include?('!')
return unless check_spacing(range)

before_qualifier = config['space_before_bang'] ? '' : 'not '
after_qualifier = config['space_after_bang'] ? '' : 'not '
Expand All @@ -16,8 +35,6 @@ def visit_prop(node)
"and should #{after_qualifier}be followed by a space")
end

private

# Start from the back and move towards the front so that any !important or
# !default !'s will be found *before* quotation marks. Then we can
# stop at quotation marks to protect against linting !'s within strings
Expand All @@ -40,8 +57,7 @@ def is_after_wrong?(range, offset)
(after_actual =~ after_expected).nil?
end

def check_spacing(node)
range = node.value_source_range
def check_spacing(range)
offset = find_bang_offset(range)

return if character_at(range.end_pos, offset) != '!'
Expand Down
58 changes: 58 additions & 0 deletions spec/scss_lint/linter/bang_format_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,64 @@
it { should_not report_lint }
end

context 'with a !default variable declaration' do
context 'and !default is used correctly' do
let(:scss) { <<-SCSS }
$foo: bar !default;
SCSS

it { should_not report_lint }
end

context 'and there is no space before' do
let(:scss) { <<-SCSS }
$foo: bar!default;
SCSS

it { should report_lint line: 1 }
end
end

context 'with a !global variable declaration' do
context 'and !global is used correctly' do
let(:scss) { <<-SCSS }
$foo: bar !global;
SCSS

it { should_not report_lint }
end

context 'and there is no space before' do
let(:scss) { <<-SCSS }
$foo: bar!global;
SCSS

it { should report_lint line: 1 }
end
end

context 'with an !optional @extend directive' do
context 'and !optional is used correctly' do
let(:scss) { <<-SCSS }
.foo {
@extend .bar !optional;
}
SCSS

it { should_not report_lint }
end

context 'and there is no space before' do
let(:scss) { <<-SCSS }
.foo {
@extend .bar!optional;
}
SCSS

it { should report_lint line: 2 }
end
end

context 'when ! appears within a string' do
let(:scss) { <<-SCSS }
p:before { content: "!important"; }
Expand Down

0 comments on commit 4571966

Please sign in to comment.