Skip to content

Commit

Permalink
Fix RuboCop to not report erroneous Style/EmptyElse warnings
Browse files Browse the repository at this point in the history
When writing code like the following:

  - if condition
    :filter
      ...
  - else
    :filter
      ...

...if the filters contained no executable Ruby code the `RuboCop` linter
would incorrectly report a `Style/EmptyRule` warning due to the
`ScriptExtractor` not representing the filter in the extracted code.

Fix by always inserting a `puts` statement for filters.

Change-Id: Ia9f46c03dc923bee65a49258b62365d337509b75
Reviewed-on: http://gerrit.causes.com/47622
Tested-by: jenkins <jenkins@brigade.com>
Reviewed-by: Shane da Silva <shane.dasilva@brigade.com>
  • Loading branch information
sds committed Mar 11, 2015
1 parent 9475505 commit ee07324
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 2 deletions.
6 changes: 4 additions & 2 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,10 @@
* Fix non-visible line number on light-colored terminal backgrounds
* Allow files without `.haml` extension to be linted when explicitly specified
* Ignore `Style/TrailingBlankLines` warnings from RuboCop by default
* Fix RuboCop linter to not report `Style/AlignHash` warnings for HAML code with
1.8-style hash rockets spanning multiple lines
* Fix `RuboCop` linter to not report `Style/AlignHash` warnings for HAML code
with 1.8-style hash rockets spanning multiple lines
* Fix `RuboCop` linter to not report `Style/EmptyElse` warnings for HAML code
containing `if`/`else` blocks containing only HAML filters

## 0.11.0

Expand Down
1 change: 1 addition & 0 deletions lib/haml_lint/script_extractor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@ def visit_filter(node)
add_line(line, node.line + index + 1)
end
else
add_line('puts', node)
HamlLint::Utils.extract_interpolated_values(node.text) do |interpolated_code|
add_line(interpolated_code, node)
end
Expand Down
22 changes: 22 additions & 0 deletions spec/haml_lint/script_extractor_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -301,6 +301,7 @@
HAML

it { should == normalize_indent(<<-RUBY).rstrip }
puts
some_method
some_other_method
RUBY
Expand All @@ -314,6 +315,7 @@
HAML

it { should == normalize_indent(<<-RUBY).rstrip }
puts
some_method("hello")
some_other_method('world')
RUBY
Expand All @@ -329,9 +331,29 @@
# TODO: Figure out if it's worth normalizing indentation for the generated
# code in this interpolated context
it { should == normalize_indent(<<-RUBY).rstrip }
puts
some_method('hello',
'world')
RUBY
end

context 'with an if/else block containing only filters' do
let(:haml) { <<-HAML }
- if condition
:filter
Some text
- else
:filter
Some other text
HAML

it { should == normalize_indent(<<-RUBY).rstrip }
if condition
puts
else
puts
end
RUBY
end
end
end

0 comments on commit ee07324

Please sign in to comment.