Skip to content

Commit

Permalink
[Fix rubocop#3479] Add IgnoredPatterns config option to LineLength
Browse files Browse the repository at this point in the history
A list of regular expressions. The cop ignores lines matching
any of them. Empty list by default.
  • Loading branch information
jonas054 committed Nov 26, 2016
1 parent 3d842bd commit e79811a
Show file tree
Hide file tree
Showing 7 changed files with 59 additions and 16 deletions.
18 changes: 11 additions & 7 deletions .rubocop_todo.yml
Original file line number Diff line number Diff line change
@@ -1,30 +1,34 @@
# This configuration was generated by
# `rubocop --auto-gen-config`
# on 2016-09-07 17:08:09 +0300 using RuboCop version 0.42.0.
# on 2016-11-06 21:38:48 +0100 using RuboCop version 0.45.0.
# The point is for the user to remove these configuration records
# one by one as the offenses are removed from the code base.
# Note that changes in the inspected code, or installation of new
# versions of RuboCop, may require this file to be generated again.

# Offense count: 75
# Offense count: 83
Metrics/AbcSize:
Max: 19

# Offense count: 32
# Offense count: 33
# Configuration parameters: CountComments.
Metrics/ClassLength:
Max: 172

# Offense count: 28
# Offense count: 31
Metrics/CyclomaticComplexity:
Max: 7
Max: 8

# Offense count: 143
# Offense count: 147
# Configuration parameters: CountComments.
Metrics/MethodLength:
Max: 14

# Offense count: 11
# Offense count: 12
# Configuration parameters: CountComments.
Metrics/ModuleLength:
Max: 156

# Offense count: 1
Metrics/PerceivedComplexity:
Max: 8
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
* New cop `Rails/EnumUniqueness` checks for duplicate values defined in enum config hash. ([@olliebennett][])
* [#3451](https://github.com/bbatsov/rubocop/issues/3451): Add new `require_parentheses_when_complex` style to `Style/TernaryParentheses` cop. ([@swcraig][])
* [#3600](https://github.com/bbatsov/rubocop/issues/3600): Add new `Bundler/OrderedGems` cop. ([@tdeo][])
* [#3479](https://github.com/bbatsov/rubocop/issues/3479): Add new configuration option `IgnoredPatterns` to `Metrics/LineLength`. ([@jonas054][])

### Changes

Expand Down
4 changes: 4 additions & 0 deletions config/default.yml
Original file line number Diff line number Diff line change
Expand Up @@ -1144,6 +1144,10 @@ Metrics/LineLength:
# The IgnoreCopDirectives option causes the LineLength rule to ignore cop
# directives like '# rubocop: enable ...' when calculating a line's length.
IgnoreCopDirectives: false
# The IgnoredPatterns option is a list of !ruby/regexp and/or string
# elements. Strings will be converted to Regexp objects. A line that matches
# any regular expression listed in this option will be ignored by LineLength.
IgnoredPatterns: []

Metrics/MethodLength:
CountComments: false # count full line comments?
Expand Down
9 changes: 9 additions & 0 deletions lib/rubocop/cop/metrics/line_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ def investigate(processed_source)

def check_line(line, index, heredocs)
return if line.length <= max
return if matches_ignored_pattern?(line)
if ignore_cop_directives? && directive_on_source_line?(index)
return check_directive_line(line, index)
end
Expand Down Expand Up @@ -81,6 +82,14 @@ def line_in_whitelisted_heredoc?(heredocs, line_number)
end
end

def matches_ignored_pattern?(line)
ignored_patterns.any? { |pattern| Regexp.new(pattern).match(line) }
end

def ignored_patterns
cop_config['IgnoredPatterns'] || []
end

def allow_uri?
cop_config['AllowURI']
end
Expand Down
10 changes: 5 additions & 5 deletions spec/rubocop/cli/cli_auto_gen_config_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ def abs(path)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].map(&:chomp))
.to eq(['# Offense count: 1',
'# Configuration parameters: AllowHeredoc, AllowURI, ' \
'URISchemes, IgnoreCopDirectives.',
'URISchemes, IgnoreCopDirectives, IgnoredPatterns.',
'# URISchemes: http, https',
'Metrics/LineLength:',
' Max: 85',
Expand Down Expand Up @@ -81,7 +81,7 @@ def abs(path)
expect(IO.readlines('.rubocop_todo.yml')[8..-1].join)
.to eq(['# Offense count: 1',
'# Configuration parameters: AllowHeredoc, AllowURI, ' \
'URISchemes, IgnoreCopDirectives.',
'URISchemes, IgnoreCopDirectives, IgnoredPatterns.',
'# URISchemes: http, https',
'Metrics/LineLength:',
' Max: 81',
Expand Down Expand Up @@ -136,7 +136,7 @@ def abs(path)
'',
'# Offense count: 2',
'# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, '\
'IgnoreCopDirectives.',
'IgnoreCopDirectives, IgnoredPatterns.',
'# URISchemes: http, https',
'Metrics/LineLength:',
' Max: 90',
Expand Down Expand Up @@ -225,7 +225,7 @@ def abs(path)
'',
'# Offense count: 3',
'# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, '\
'IgnoreCopDirectives.',
'IgnoreCopDirectives, IgnoredPatterns.',
'# URISchemes: http, https',
'Metrics/LineLength:',
' Max: 90', # Offense occurs in 2 files, limit is 1, so no Exclude.
Expand Down Expand Up @@ -413,7 +413,7 @@ def abs(path)
'again.',
'',
'# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, '\
'IgnoreCopDirectives.',
'IgnoreCopDirectives, IgnoredPatterns.',
'# URISchemes: http, https',
'Metrics/LineLength:',
' Max: 90',
Expand Down
6 changes: 4 additions & 2 deletions spec/rubocop/config_loader_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -217,7 +217,8 @@
'AllowHeredoc' => true,
'AllowURI' => true,
'URISchemes' => %w(http https),
'IgnoreCopDirectives' => false
'IgnoreCopDirectives' => false,
'IgnoredPatterns' => []
},
'Metrics/MethodLength' => {
'Description' =>
Expand Down Expand Up @@ -297,7 +298,8 @@
'AllowHeredoc' => false, # overridden in rubocop.yml
'AllowURI' => true,
'URISchemes' => %w(http https),
'IgnoreCopDirectives' => false
'IgnoreCopDirectives' => false,
'IgnoredPatterns' => []
}
)

Expand Down
27 changes: 25 additions & 2 deletions spec/rubocop/cop/metrics/line_length_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

describe RuboCop::Cop::Metrics::LineLength, :config do
subject(:cop) { described_class.new(config) }
let(:cop_config) { { 'Max' => 80 } }
let(:cop_config) { { 'Max' => 80, 'IgnoredPatterns' => nil } }

it "registers an offense for a line that's 81 characters wide" do
inspect_source(cop, '#' * 81)
Expand Down Expand Up @@ -118,6 +118,29 @@
end
end

context 'when IgnoredPatterns option is set' do
let(:cop_config) do
{
'Max' => 18,
'IgnoredPatterns' => ['^\s*test\s', /^\s*def\s+test_/]
}
end

let(:source) do
['class ExampleTest < TestCase',
" test 'some really long test description which exceeds length' do",
' end',
' def test_some_other_long_test_description_which_exceeds_length',
' end',
'end']
end

it 'accepts long lines matching a pattern but not other long lines' do
inspect_source(cop, source)
expect(cop.highlights).to eq(['< TestCase'])
end
end

context 'when AllowHeredoc option is enabled' do
let(:cop_config) { { 'Max' => 80, 'AllowHeredoc' => true } }

Expand All @@ -142,7 +165,7 @@

context 'and only certain heredoc delimiters are whitelisted' do
let(:cop_config) do
{ 'Max' => 80, 'AllowHeredoc' => %w(SQL OK) }
{ 'Max' => 80, 'AllowHeredoc' => %w(SQL OK), 'IgnoredPatterns' => [] }
end

let(:source) { <<-END }
Expand Down

0 comments on commit e79811a

Please sign in to comment.