diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 6b53b11a58d5..5a9bab206602 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -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 diff --git a/CHANGELOG.md b/CHANGELOG.md index dfbef2f0ffa6..1e5073d4812f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -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 diff --git a/config/default.yml b/config/default.yml index bcd07d09543b..df7ef621973f 100644 --- a/config/default.yml +++ b/config/default.yml @@ -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? diff --git a/lib/rubocop/cop/metrics/line_length.rb b/lib/rubocop/cop/metrics/line_length.rb index f0d2f0cef9ea..064a6a32afb8 100644 --- a/lib/rubocop/cop/metrics/line_length.rb +++ b/lib/rubocop/cop/metrics/line_length.rb @@ -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 @@ -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 diff --git a/spec/rubocop/cli/cli_auto_gen_config_spec.rb b/spec/rubocop/cli/cli_auto_gen_config_spec.rb index b5317cda0e71..34a2a355c55d 100644 --- a/spec/rubocop/cli/cli_auto_gen_config_spec.rb +++ b/spec/rubocop/cli/cli_auto_gen_config_spec.rb @@ -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', @@ -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', @@ -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', @@ -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. @@ -413,7 +413,7 @@ def abs(path) 'again.', '', '# Configuration parameters: AllowHeredoc, AllowURI, URISchemes, '\ - 'IgnoreCopDirectives.', + 'IgnoreCopDirectives, IgnoredPatterns.', '# URISchemes: http, https', 'Metrics/LineLength:', ' Max: 90', diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb index 19f0fc73b414..08301470c080 100644 --- a/spec/rubocop/config_loader_spec.rb +++ b/spec/rubocop/config_loader_spec.rb @@ -217,7 +217,8 @@ 'AllowHeredoc' => true, 'AllowURI' => true, 'URISchemes' => %w(http https), - 'IgnoreCopDirectives' => false + 'IgnoreCopDirectives' => false, + 'IgnoredPatterns' => [] }, 'Metrics/MethodLength' => { 'Description' => @@ -297,7 +298,8 @@ 'AllowHeredoc' => false, # overridden in rubocop.yml 'AllowURI' => true, 'URISchemes' => %w(http https), - 'IgnoreCopDirectives' => false + 'IgnoreCopDirectives' => false, + 'IgnoredPatterns' => [] } ) diff --git a/spec/rubocop/cop/metrics/line_length_spec.rb b/spec/rubocop/cop/metrics/line_length_spec.rb index 300c331dbe67..7d038cb064a9 100644 --- a/spec/rubocop/cop/metrics/line_length_spec.rb +++ b/spec/rubocop/cop/metrics/line_length_spec.rb @@ -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) @@ -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 } } @@ -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 }