Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add MaxLength parameter to IfUnlessModifier and WhileUntilModifier #795

Merged
merged 1 commit into from
Feb 17, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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