Skip to content

Commit

Permalink
Merge pull request #7449 from jonas054/fix_IfUnlessModifier_with_disa…
Browse files Browse the repository at this point in the history
…bled_LineLength

Make IfUnlessModifier respect rubocop:disable comments
  • Loading branch information
koic committed Oct 21, 2019
2 parents 4254691 + 4cccae1 commit d6ceaa3
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Expand Up @@ -6,6 +6,7 @@

* [#7439](https://github.com/rubocop-hq/rubocop/issues/7439): Make `Style/FormatStringToken` ignore percent escapes (`%%`). ([@buehmann][])
* [#7438](https://github.com/rubocop-hq/rubocop/issues/7438): Fix assignment edge-cases in `Layout/MultilineAssignmentLayout`. ([@gsamokovarov][])
* [#7449](https://github.com/rubocop-hq/rubocop/pull/7449): Make `Style/IfUnlessModifier` respect `rubocop:disable` comments for `Metrics/LineLength`. ([@jonas054][])

## 0.75.1 (2019-10-14)

Expand Down
11 changes: 9 additions & 2 deletions lib/rubocop/cop/style/if_unless_modifier.rb
Expand Up @@ -70,8 +70,15 @@ def too_long_single_line?(node)
return false unless max_line_length

range = node.source_range
range.first_line == range.last_line &&
range.last_column > max_line_length
return false unless range.first_line == range.last_line
return false unless line_length_enabled_at_line?(range.first_line)

range.last_column > max_line_length
end

def line_length_enabled_at_line?(line)
processed_source.comment_config
.cop_enabled_at_line?('Metrics/LineLength', line)
end

def named_capture_in_condition?(node)
Expand Down
14 changes: 13 additions & 1 deletion spec/rubocop/cop/style/if_unless_modifier_spec.rb
Expand Up @@ -36,7 +36,7 @@ def f
end
end

context 'when Metrics/LineLength is disabled' do
context 'when Metrics/LineLength is disabled in configuration' do
let(:line_length_config) { { 'Enabled' => false, 'Max' => 80 } }

it 'accepts' do
Expand All @@ -47,6 +47,18 @@ def f
RUBY
end
end

context 'when Metrics/LineLength is disabled with a comment' do
it 'accepts' do
expect_no_offenses(<<~RUBY)
def f
# rubocop:disable Metrics/LineLength
#{source}
# rubocop:enable Metrics/LineLength
end
RUBY
end
end
end

context 'multiline if that fits on one line' do
Expand Down

0 comments on commit d6ceaa3

Please sign in to comment.