Skip to content

Commit

Permalink
Merge pull request #795 from agrimm/if_modifier
Browse files Browse the repository at this point in the history
Add MaxLength parameter to IfUnlessModifier and WhileUntilModifier
  • Loading branch information
bbatsov committed Feb 17, 2014
2 parents b76c5c6 + 734aaba commit cff52c6
Show file tree
Hide file tree
Showing 7 changed files with 46 additions and 0 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
* [#797](https://github.com/bbatsov/rubocop/issues/797): New cop `IndentHash` checks the indentation of the first key in multi-line hash literals. ([@jonas054][])
* [#797](https://github.com/bbatsov/rubocop/issues/797): New cop `IndentArray` checks the indentation of the first element in multi-line array literals. ([@jonas054][])
* [#806](https://github.com/bbatsov/rubocop/issues/806): Now excludes files in `vendor/**` by default. ([@jeremyolliver][])
* [#795](https://github.com/bbatsov/rubocop/issues/795): `IfUnlessModifier` and `WhileUntilModifier` supports `MaxLineLength`, which is independent of `LineLength` parameter `Max`. ([@agrimm][])

### Changes

Expand Down
6 changes: 6 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -148,6 +148,9 @@ HashSyntax:
- ruby19
- hash_rockets

IfUnlessModifier:
MaxLineLength: 79

# Checks the indentation of the first key in a hash literal.
IndentHash:
# The value `special_inside_parentheses` means that hash literals with braces
Expand Down Expand Up @@ -301,6 +304,9 @@ VariableName:
- snake_case
- camelCase

WhileUntilModifier:
MaxLineLength: 79

WordArray:
MinSize: 0

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/mixin/statement_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ def check(sexp, comments)
end

def max_line_length
cop_config && cop_config['MaxLineLength'] ||
config.for_cop('LineLength')['Max']
end

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/if_unless_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Cop
module Style
# Checks for if and unless statements that would fit on one line
# if written as a modifier if/unless.
# The maximum line length is configurable.
class IfUnlessModifier < Cop
include StatementModifier

Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/style/while_until_modifier.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ module Cop
module Style
# Checks for while and until statements that would fit on one line
# if written as a modifier while/until.
# The maximum line length is configurable.
class WhileUntilModifier < Cop
include StatementModifier

Expand Down
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/if_unless_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -124,5 +124,23 @@
expect(cop.offenses).to be_empty
end
end

context 'when the maximum line length is specified by the cop itself' do
let(:config) do
hash = {
'LineLength' => { 'Max' => 100 },
'IfUnlessModifier' => { 'MaxLineLength' => 79 }
}
Rubocop::Config.new(hash)
end

it "accepts multiline if that doesn't fit on one line" do
check_too_long(cop, 'if')
end

it "accepts multiline unless that doesn't fit on one line" do
check_too_long(cop, 'unless')
end
end
end
end
18 changes: 18 additions & 0 deletions spec/rubocop/cop/style/while_until_modifier_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -72,4 +72,22 @@
inspect_source(cop, ['ala until bala'])
expect(cop.offenses).to be_empty
end

context 'when the maximum line length is specified by the cop itself' do
let(:config) do
hash = {
'LineLength' => { 'Max' => 100 },
'WhileUntilModifier' => { 'MaxLineLength' => 79 }
}
Rubocop::Config.new(hash)
end

it "accepts multiline while that doesn't fit on one line" do
check_too_long(cop, 'while')
end

it "accepts multiline until that doesn't fit on one line" do
check_too_long(cop, 'until')
end
end
end

0 comments on commit cff52c6

Please sign in to comment.