Skip to content

Commit

Permalink
Make Lint/LiteralInCondition cop aware of ! and not
Browse files Browse the repository at this point in the history
By this change, `Lint/LiteralInCondition` cop will register an offence for the following code.

```ruby
!'foo'

not 'foo'
```
  • Loading branch information
pocke authored and bbatsov committed Oct 12, 2017
1 parent 039ca54 commit 995315f
Show file tree
Hide file tree
Showing 5 changed files with 56 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
* [#4787](https://github.com/bbatsov/rubocop/pull/4787): Analyzing code that needs to support MRI 2.0 is no longer supported. ([@deivid-rodriguez][])
* [#4787](https://github.com/bbatsov/rubocop/pull/4787): RuboCop no longer installs on MRI 2.0. ([@deivid-rodriguez][])
* [#4266](https://github.com/bbatsov/rubocop/issues/4266): Download the inherited config files of a remote file from the same remote. ([@tdeo][])
* [#4853](https://github.com/bbatsov/rubocop/pull/4853): Make `Lint/LiteralInCondition` cop aware of `!` and `not`. ([@pocke][])

## 0.50.0 (2017-09-14)

Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/ast/node/send_node.rb
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ class SendNode < Node
def node_parts
to_a
end

def negation_method?
keyword_bang? || keyword_not?
end
end
end
end
20 changes: 17 additions & 3 deletions lib/rubocop/cop/lint/literal_in_condition.rb
Original file line number Diff line number Diff line change
Expand Up @@ -65,17 +65,23 @@ def on_case(case_node)
end
end

def on_send(node)
return unless node.negation_method?
check_for_literal(node)
end

def message(node)
format(MSG, node.source)
end

private

def check_for_literal(node)
if node.condition.literal?
add_offense(node.condition)
cond = condition(node)
if cond.literal?
add_offense(cond)
else
check_node(node.condition)
check_node(cond)
end
end

Expand Down Expand Up @@ -121,6 +127,14 @@ def check_case(case_node)

handle_node(condition)
end

def condition(node)
if node.send_type?
node.receiver
else
node.condition
end
end
end
end
end
Expand Down
20 changes: 20 additions & 0 deletions spec/rubocop/ast/send_node_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -858,4 +858,24 @@
it { expect(send_node.def_modifier?).to be_truthy }
end
end

describe '#negation_method?' do
context 'with keyword `not`' do
let(:source) { 'not foo' }

it { expect(send_node).to be_negation_method }
end

context 'with bang method' do
let(:source) { '!foo' }

it { expect(send_node).to be_negation_method }
end

context 'with normal method' do
let(:source) { 'foo.bar' }

it { expect(send_node).not_to be_negation_method }
end
end
end
14 changes: 14 additions & 0 deletions spec/rubocop/cop/lint/literal_in_condition_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,20 @@
RUBY
expect(cop.offenses).to be_empty
end

it "registers an offense for `!#{lit}`" do
inspect_source(<<-RUBY.strip_indent)
!#{lit}
RUBY
expect(cop.offenses.size).to eq(1)
end

it "registers an offense for `not #{lit}`" do
inspect_source(<<-RUBY.strip_indent)
!#{lit}
RUBY
expect(cop.offenses.size).to eq(1)
end
end

it 'accepts array literal in case, if it has non-literal elements' do
Expand Down

0 comments on commit 995315f

Please sign in to comment.