Skip to content
This repository has been archived by the owner on Sep 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request rubocop#2750 from Drenmi/bugfix/no-method-error-in…
Browse files Browse the repository at this point in the history
…-guard-clause-cop

[Fix rubocop#2723] Fix NoMethodError in Style/GuardClause cop
  • Loading branch information
bbatsov committed Jan 31, 2016
2 parents 01dce1e + 07e8e23 commit 0e4f86d
Show file tree
Hide file tree
Showing 3 changed files with 70 additions and 42 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@

### Bug fixes

* [#2723](https://github.com/bbatsov/rubocop/issues/2723): Fix NoMethodError in Style/GuardClause. ([@drenmi][])
* [#2674](https://github.com/bbatsov/rubocop/issues/2674): Also check for Hash#update alias in `Performance/RedundantMerge`. ([@drenmi][])
* [#2630](https://github.com/bbatsov/rubocop/issues/2630): Take frozen string literals into account in `Style/MutableConstant`. ([@segiddins][])
* [#2642](https://github.com/bbatsov/rubocop/issues/2642): Support assignment via `||=` in `Style/MutableConstant`. ([@segiddins][])
Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/style/guard_clause.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ def line_too_long_when_corrected?(node)
def line_too_long?(node, body, keyword, condition)
max = config.for_cop('Metrics/LineLength')['Max'] || 80
indent = node.loc.column
source = body && body.source || ''
# 2 is for spaces on left and right of keyword
indent + (body.source + keyword + condition.source).length + 2 > max
indent + (source + keyword + condition.source).length + 2 > max
end
end
end
Expand Down
108 changes: 67 additions & 41 deletions spec/rubocop/cop/style/guard_clause_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,50 +7,76 @@
let(:cop) { described_class.new(config) }
let(:cop_config) { {} }

it 'reports an offense if method body is if / unless without else' do
inspect_source(cop,
['def func',
' if something',
' work',
' end',
'end',
'',
'def func',
' unless something',
' work',
' end',
'end'])
expect(cop.offenses.size).to eq(2)
expect(cop.offenses.map(&:line).sort).to eq([2, 8])
expect(cop.messages)
.to eq(['Use a guard clause instead of wrapping ' \
'the code inside a conditional expression.'] * 2)
expect(cop.highlights).to eq(%w(if unless))
end
shared_examples 'reports offense' do |body|
it 'reports an offense if method body is if / unless without else' do
inspect_source(cop,
['def func',
' if something',
" #{body}",
' end',
'end',
'',
'def func',
' unless something',
" #{body}",
' end',
'end'])
expect(cop.offenses.size).to eq(2)
expect(cop.offenses.map(&:line).sort).to eq([2, 8])
expect(cop.messages)
.to eq(['Use a guard clause instead of wrapping ' \
'the code inside a conditional expression.'] * 2)
expect(cop.highlights).to eq(%w(if unless))
end

it 'reports an offense if method body ends with if / unless without else' do
inspect_source(cop,
['def func',
' test',
' if something',
' work',
' end',
'end',
'',
'def func',
' test',
' unless something',
' work',
' end',
'end'])
expect(cop.offenses.size).to eq(2)
expect(cop.offenses.map(&:line).sort).to eq([3, 10])
expect(cop.messages)
.to eq(['Use a guard clause instead of wrapping ' \
'the code inside a conditional expression.'] * 2)
expect(cop.highlights).to eq(%w(if unless))
it 'reports an offense if method body is if / unless without else' do
inspect_source(cop,
['def func',
' if something',
" #{body}",
' end',
'end',
'',
'def func',
' unless something',
" #{body}",
' end',
'end'])
expect(cop.offenses.size).to eq(2)
expect(cop.offenses.map(&:line).sort).to eq([2, 8])
expect(cop.messages)
.to eq(['Use a guard clause instead of wrapping ' \
'the code inside a conditional expression.'] * 2)
expect(cop.highlights).to eq(%w(if unless))
end

it 'reports an offense if method body ends with if / unless without else' do
inspect_source(cop,
['def func',
' test',
' if something',
" #{body}",
' end',
'end',
'',
'def func',
' test',
' unless something',
" #{body}",
' end',
'end'])
expect(cop.offenses.size).to eq(2)
expect(cop.offenses.map(&:line).sort).to eq([3, 10])
expect(cop.messages)
.to eq(['Use a guard clause instead of wrapping ' \
'the code inside a conditional expression.'] * 2)
expect(cop.highlights).to eq(%w(if unless))
end
end

it_behaves_like('reports offense', 'work')
it_behaves_like('reports offense', '# TODO')

it 'does not report an offense if corrected code would exceed line length' do
inspect_source(cop,
['def func',
Expand Down

0 comments on commit 0e4f86d

Please sign in to comment.