Skip to content

Commit

Permalink
Deprecate ConfigurableMax in favour of ExcludeLimit which allows …
Browse files Browse the repository at this point in the history
…multiple configuration options to be added to the exclude limit for `--auto-gen-config`.
  • Loading branch information
dvandersluis authored and bbatsov committed Feb 1, 2021
1 parent 851741b commit 61627d6
Show file tree
Hide file tree
Showing 11 changed files with 48 additions and 15 deletions.
1 change: 1 addition & 0 deletions docs/modules/ROOT/pages/v1_upgrade_notes.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -254,3 +254,4 @@ Refactored for better separation of concern, being reusable, better result repor
** `Cop.registry` \=> `Registry.global`
** `Cop.all` \=> `Registry.all`
** `Cop.qualified_cop_name` \=> `Registry.qualified_cop_name`
* The `ConfigurableMax` mixin for tracking exclude limits of configuration options is deprecated. Use `exclude_limit ParameterName` instead.
1 change: 1 addition & 0 deletions lib/rubocop.rb
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
require_relative 'rubocop/cop/message_annotator'
require_relative 'rubocop/cop/ignored_node'
require_relative 'rubocop/cop/autocorrect_logic'
require_relative 'rubocop/cop/exclude_limit'
require_relative 'rubocop/cop/badge'
require_relative 'rubocop/cop/registry'
require_relative 'rubocop/cop/base'
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/base.rb
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ module Cop
class Base # rubocop:disable Metrics/ClassLength
extend RuboCop::AST::Sexp
extend NodePattern::Macros
extend ExcludeLimit
include RuboCop::AST::Sexp
include Util
include IgnoredNode
Expand Down
26 changes: 26 additions & 0 deletions lib/rubocop/cop/exclude_limit.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# frozen_string_literal: true

module RuboCop
# Allows specified configuration options to have an exclude limit
# ie. a maximum value tracked that it can be used by `--auto-gen-config`.
module ExcludeLimit
# Sets up a configuration option to have an exclude limit tracked.
# The parameter name given is transformed into a method name (eg. `Max`
# becomes `self.max=` and `MinDigits` becomes `self.min_digits=`).
def exclude_limit(parameter_name, method_name: transform(parameter_name))
define_method("#{method_name}=") do |value|
cfg = config_to_allow_offenses
cfg[:exclude_limit] ||= {}
current_max = cfg[:exclude_limit][parameter_name]
value = [current_max, value].max if current_max
cfg[:exclude_limit][parameter_name] = value
end
end

private

def transform(parameter_name)
parameter_name.gsub(/(?<!\A)(?=[A-Z])/, '_').downcase
end
end
end
3 changes: 2 additions & 1 deletion lib/rubocop/cop/layout/line_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -60,12 +60,13 @@ module Layout
# }
class LineLength < Base
include CheckLineBreakable
include ConfigurableMax
include IgnoredPattern
include RangeHelp
include LineLengthHelp
extend AutoCorrector

exclude_limit 'Max'

MSG = 'Line is too long. [%<length>d/%<max>d]'

def on_block(node)
Expand Down
4 changes: 2 additions & 2 deletions lib/rubocop/cop/metrics/block_nesting.rb
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,13 @@ module Metrics
#
# The maximum level of nesting allowed is configurable.
class BlockNesting < Base
include ConfigurableMax

NESTING_BLOCKS = %i[
case if while while_post
until until_post for resbody
].freeze

exclude_limit 'Max'

def on_new_investigation
return if processed_source.blank?

Expand Down
3 changes: 2 additions & 1 deletion lib/rubocop/cop/metrics/parameter_lists.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,8 @@ module Metrics
# end
#
class ParameterLists < Base
include ConfigurableMax
exclude_limit 'Max'


MSG = 'Avoid parameter lists longer than %<max>d parameters. ' \
'[%<count>d/%<max>d]'
Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/mixin/code_length.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ module RuboCop
module Cop
# Common functionality for checking length of code segments.
module CodeLength
include ConfigurableMax
extend ExcludeLimit

MSG = '%<label>s has too many lines. [%<length>d/%<max>d]'

exclude_limit 'Max'

private

def message(length, max_length)
Expand Down
1 change: 1 addition & 0 deletions lib/rubocop/cop/mixin/configurable_max.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ module RuboCop
module Cop
# Handles `Max` configuration parameters, especially setting them to an
# appropriate value with --auto-gen-config.
# @deprecated Use `exclude_limit ParameterName` instead.
module ConfigurableMax
private

Expand Down
4 changes: 3 additions & 1 deletion lib/rubocop/cop/mixin/method_complexity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,10 +6,12 @@ module Cop
#
# This module handles measurement and reporting of complexity in methods.
module MethodComplexity
include ConfigurableMax
include IgnoredMethods
include Metrics::Utils::RepeatedCsendDiscount
extend NodePattern::Macros
extend ExcludeLimit

exclude_limit 'Max'

# Ensure cops that include `MethodComplexity` have the config
# `attr_accessor`s that `ignored_method?` needs.
Expand Down
15 changes: 6 additions & 9 deletions lib/rubocop/cop/style/numeric_literals.rb
Original file line number Diff line number Diff line change
Expand Up @@ -28,17 +28,18 @@ module Style
# 10_000_00 # typical representation of $10,000 in cents
#
class NumericLiterals < Base
# The parameter is called MinDigits (meaning the minimum number of
# digits for which an offense can be registered), but essentially it's
# a Max parameter (the maximum number of something that's allowed).
include ConfigurableMax
include IntegerNode
extend AutoCorrector

MSG = 'Use underscores(_) as thousands separator and ' \
'separate every 3 digits with them.'
DELIMITER_REGEXP = /[eE.]/.freeze

# The parameter is called MinDigits (meaning the minimum number of
# digits for which an offense can be registered), but essentially it's
# a Max parameter (the maximum number of something that's allowed).
exclude_limit 'MinDigits'

def on_int(node)
check(node)
end
Expand All @@ -49,10 +50,6 @@ def on_float(node)

private

def max_parameter_name
'MinDigits'
end

def check(node)
int = integer_part(node)

Expand All @@ -62,7 +59,7 @@ def check(node)

case int
when /^\d+$/
return unless (self.max = int.size + 1)
return unless (self.min_digits = int.size + 1)

register_offense(node)
when /\d{4}/, short_group_regex
Expand Down

0 comments on commit 61627d6

Please sign in to comment.