Skip to content

Commit

Permalink
Deprecate IgnoredMethods option in integrate to AllowedMethods an…
Browse files Browse the repository at this point in the history
…d `AllowedPatterns` option

This PR is change deprecate `IgnoredMethods` option in
integrate to `AllowedMethods` and `AllowedPatterns` option.

In order to not break extensions, it adds aliases for the existing class,
methods and configuration.
Also adds obsoletion warnings for configuration files.
  • Loading branch information
ydah authored and bbatsov committed Jul 24, 2022
1 parent 743f6f7 commit 4537491
Show file tree
Hide file tree
Showing 39 changed files with 787 additions and 422 deletions.
1 change: 1 addition & 0 deletions changelog/change_update_uses_of_ignoredmethods_to.md
@@ -0,0 +1 @@
* [#10829](https://github.com/rubocop/rubocop/pull/10829): Deprecate `IgnoredMethods` option in integrate to `AllowedMethods` and `AllowedPatterns` option. ([@ydah][])
59 changes: 43 additions & 16 deletions config/default.yml
Expand Up @@ -1502,7 +1502,9 @@ Lint/AmbiguousBlockAssociation:
Enabled: true
VersionAdded: '0.48'
VersionChanged: '1.13'
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated

Lint/AmbiguousOperator:
Description: >-
Expand Down Expand Up @@ -1991,7 +1993,9 @@ Lint/NumberConversion:
VersionAdded: '0.53'
VersionChanged: '1.1'
SafeAutoCorrect: false
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated
IgnoredClasses:
- Time
- DateTime
Expand Down Expand Up @@ -2443,7 +2447,9 @@ Metrics/AbcSize:
VersionChanged: '1.5'
# The ABC size is a calculated magnitude, so this number can be an Integer or
# a Float.
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated
CountRepeatedAttributes: true
Max: 17

Expand All @@ -2456,10 +2462,12 @@ Metrics/BlockLength:
Max: 25
CountAsOne: []
ExcludedMethods: [] # deprecated, retained for backwards compatibility
IgnoredMethods:
AllowedMethods:
# By default, exclude the `#refine` method, as it tends to have larger
# associated blocks.
- refine
AllowedPatterns: []
IgnoredMethods: [] # deprecated
Exclude:
- '**/*.gemspec'

Expand Down Expand Up @@ -2489,7 +2497,9 @@ Metrics/CyclomaticComplexity:
Enabled: true
VersionAdded: '0.25'
VersionChanged: '0.81'
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated
Max: 7

Metrics/MethodLength:
Expand All @@ -2502,7 +2512,9 @@ Metrics/MethodLength:
Max: 10
CountAsOne: []
ExcludedMethods: [] # deprecated, retained for backwards compatibility
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated

Metrics/ModuleLength:
Description: 'Avoid modules longer than 100 lines of code.'
Expand Down Expand Up @@ -2530,7 +2542,9 @@ Metrics/PerceivedComplexity:
Enabled: true
VersionAdded: '0.25'
VersionChanged: '0.81'
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated
Max: 8

################## Migration #############################
Expand Down Expand Up @@ -3061,7 +3075,7 @@ Style/BlockDelimiters:
# This looks at the usage of a block's method to determine its type (e.g. is
# the result of a `map` assigned to a variable or passed to another
# method) but exceptions are permitted in the `ProceduralMethods`,
# `FunctionalMethods` and `IgnoredMethods` sections below.
# `FunctionalMethods` and `AllowedMethods` sections below.
- semantic
# The `braces_for_chaining` style enforces braces around single line blocks
# and do..end around multi-line blocks, except for multi-line blocks whose
Expand Down Expand Up @@ -3102,7 +3116,7 @@ Style/BlockDelimiters:
- let!
- subject
- watch
IgnoredMethods:
AllowedMethods:
# Methods that can be either procedural or functional and cannot be
# categorised from their usage alone, e.g.
#
Expand All @@ -3119,6 +3133,8 @@ Style/BlockDelimiters:
- lambda
- proc
- it
AllowedPatterns: []
IgnoredMethods: [] # deprecated
# The AllowBracesOnProceduralOneLiners option is ignored unless the
# EnforcedStyle is set to `semantic`. If so:
#
Expand Down Expand Up @@ -3222,10 +3238,12 @@ Style/ClassEqualityComparison:
StyleGuide: '#instance-of-vs-class-comparison'
Enabled: true
VersionAdded: '0.93'
IgnoredMethods:
AllowedMethods:
- ==
- equal?
- eql?
AllowedPatterns: []
IgnoredMethods: [] # deprecated

Style/ClassMethods:
Description: 'Use self when defining module/class methods.'
Expand Down Expand Up @@ -3687,7 +3705,9 @@ Style/FormatStringToken:
MaxUnannotatedPlaceholdersAllowed: 1
VersionAdded: '0.49'
VersionChanged: '1.0'
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated

Style/FrozenStringLiteralComment:
Description: >-
Expand Down Expand Up @@ -4006,7 +4026,8 @@ Style/MethodCallWithArgsParentheses:
VersionAdded: '0.47'
VersionChanged: '1.7'
IgnoreMacros: true
IgnoredMethods: []
AllowedMethods: []
IgnoredMethods: [] # deprecated
AllowedPatterns: []
IgnoredPatterns: [] # deprecated
IncludedMacros: []
Expand All @@ -4023,7 +4044,9 @@ Style/MethodCallWithoutArgsParentheses:
Description: 'Do not use parentheses for method calls with no arguments.'
StyleGuide: '#method-invocation-parens'
Enabled: true
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated
VersionAdded: '0.47'
VersionChanged: '0.55'

Expand Down Expand Up @@ -4393,7 +4416,9 @@ Style/NumericPredicate:
SupportedStyles:
- predicate
- comparison
IgnoredMethods: []
AllowedMethods: []
AllowedPatterns: []
IgnoredMethods: [] # deprecated
# Exclude RSpec specs because assertions like `expect(1).to be > 0` cause
# false positives.
Exclude:
Expand Down Expand Up @@ -5030,11 +5055,13 @@ Style/SymbolProc:
VersionAdded: '0.26'
VersionChanged: '1.28'
AllowMethodsWithArguments: false
# A list of method names to be ignored by the check.
# A list of method names to be always allowed by the check.
# The names should be fairly unique, otherwise you'll end up ignoring lots of code.
IgnoredMethods:
AllowedMethods:
- respond_to
- define_method
AllowedPatterns: []
IgnoredMethods: [] # deprecated
AllowComments: false

Style/TernaryParentheses:
Expand Down
24 changes: 23 additions & 1 deletion config/obsoletion.yml
Expand Up @@ -187,7 +187,9 @@ changed_parameters:
- Metrics/BlockLength
- Metrics/MethodLength
parameters: ExcludedMethods
alternative: IgnoredMethods
alternatives:
- AllowedMethods
- AllowedPatterns
severity: warning
- cops: Lint/Debugger
parameters: DebuggerReceivers
Expand All @@ -202,6 +204,26 @@ changed_parameters:
parameters: IgnoredPatterns
alternative: AllowedPatterns
severity: warning
- cops:
- Lint/AmbiguousBlockAssociation
- Lint/NumberConversion
- Metrics/AbcSize
- Metrics/BlockLength
- Metrics/CyclomaticComplexity
- Metrics/MethodLength
- Metrics/PerceivedComplexity
- Style/BlockDelimiters
- Style/ClassEqualityComparison
- Style/FormatStringToken
- Style/MethodCallWithArgsParentheses
- Style/MethodCallWithoutArgsParentheses
- Style/NumericPredicate
- Style/SymbolLiteral
parameters: IgnoredMethods
alternatives:
- AllowedMethods
- AllowedPatterns
severity: warning

# Enforced styles that have been removed or replaced
changed_enforced_styles:
Expand Down
1 change: 0 additions & 1 deletion lib/rubocop.rb
Expand Up @@ -86,7 +86,6 @@
require_relative 'rubocop/cop/mixin/gemspec_help'
require_relative 'rubocop/cop/mixin/hash_alignment_styles'
require_relative 'rubocop/cop/mixin/hash_transform_method'
require_relative 'rubocop/cop/mixin/ignored_methods'
require_relative 'rubocop/cop/mixin/integer_node'
require_relative 'rubocop/cop/mixin/interpolation'
require_relative 'rubocop/cop/mixin/line_length_help'
Expand Down
5 changes: 5 additions & 0 deletions lib/rubocop/config_obsoletion/changed_parameter.rb
Expand Up @@ -12,6 +12,11 @@ def message

if alternative
"#{base}\n`#{parameter}` has been renamed to `#{alternative.chomp}`."
elsif alternatives
"#{base}\n`#{parameter}` has been renamed to #{to_sentence(alternatives.map do |item|
"`#{item}`"
end,
connector: 'and/or')}."
else
"#{base}\n#{reason.chomp}"
end
Expand Down
4 changes: 4 additions & 0 deletions lib/rubocop/config_obsoletion/parameter_rule.rb
Expand Up @@ -32,6 +32,10 @@ def alternative
metadata['alternative']
end

def alternatives
metadata['alternatives']
end

def reason
metadata['reason']
end
Expand Down
29 changes: 21 additions & 8 deletions lib/rubocop/cop/lint/ambiguous_block_association.rb
Expand Up @@ -6,8 +6,8 @@ module Lint
# Checks for ambiguous block association with method
# when param passed without parentheses.
#
# This cop can customize ignored methods with `IgnoredMethods`.
# By default, there are no methods to ignored.
# This cop can customize allowed methods with `AllowedMethods`.
# By default, there are no methods to allowed.
#
# @example
#
Expand All @@ -30,18 +30,30 @@ module Lint
# # Lambda arguments require no disambiguation
# foo = ->(bar) { bar.baz }
#
# @example IgnoredMethods: [] (default)
# @example AllowedMethods: [] (default)
#
# # bad
# expect { do_something }.to change { object.attribute }
#
# @example IgnoredMethods: [change]
# @example AllowedMethods: [change]
#
# # good
# expect { do_something }.to change { object.attribute }
#
# @example AllowedPatterns: [] (default)
#
# # bad
# expect { do_something }.to change { object.attribute }
#
# @example AllowedPatterns: [/change/]
#
# # good
# expect { do_something }.to change { object.attribute }
# expect { do_something }.to not_change { object.attribute }
#
class AmbiguousBlockAssociation < Base
include IgnoredMethods
include AllowedMethods
include AllowedPattern

MSG = 'Parenthesize the param `%<param>s` to make sure that the ' \
'block will be associated with the `%<method>s` method ' \
Expand All @@ -52,7 +64,7 @@ def on_send(node)

return unless ambiguous_block_association?(node)
return if node.parenthesized? || node.last_argument.lambda? || node.last_argument.proc? ||
allowed_method?(node)
allowed_method_pattern?(node)

message = message(node)

Expand All @@ -66,9 +78,10 @@ def ambiguous_block_association?(send_node)
send_node.last_argument.block_type? && !send_node.last_argument.send_node.arguments?
end

def allowed_method?(node)
def allowed_method_pattern?(node)
node.assignment? || node.operator_method? || node.method?(:[]) ||
ignored_method?(node.last_argument.send_node.source)
allowed_method?(node.last_argument.method_name) ||
matches_allowed_pattern?(node.last_argument.method_name)
end

def message(send_node)
Expand Down
32 changes: 24 additions & 8 deletions lib/rubocop/cop/lint/number_conversion.rb
Expand Up @@ -16,8 +16,8 @@ module Lint
# NOTE: Some values cannot be converted properly using one of the `Kernel`
# method (for instance, `Time` and `DateTime` values are allowed by this
# cop by default). Similarly, Rails' duration methods do not work well
# with `Integer()` and can be ignored with `IgnoredMethods`. By default,
# there are no methods to ignored.
# with `Integer()` and can be allowed with `AllowedMethods`. By default,
# there are no methods to allowed.
#
# @safety
# Autocorrection is unsafe because it is not guaranteed that the
Expand Down Expand Up @@ -46,12 +46,22 @@ module Lint
# foo.try { |i| Float(i) }
# bar.send { |i| Complex(i) }
#
# @example IgnoredMethods: [] (default)
# @example AllowedMethods: [] (default)
#
# # bad
# 10.minutes.to_i
#
# @example IgnoredMethods: [minutes]
# @example AllowedMethods: [minutes]
#
# # good
# 10.minutes.to_i
#
# @example AllowedPatterns: [] (default)
#
# # bad
# 10.minutes.to_i
#
# @example AllowedPatterns: [/min*/]
#
# # good
# 10.minutes.to_i
Expand All @@ -62,7 +72,8 @@ module Lint
# Time.now.to_datetime.to_i
class NumberConversion < Base
extend AutoCorrector
include IgnoredMethods
include AllowedMethods
include AllowedPattern

CONVERSION_METHOD_CLASS_MAPPING = {
to_i: "#{Integer.name}(%<number_object>s, 10)",
Expand Down Expand Up @@ -97,7 +108,7 @@ def on_send(node)

def handle_conversion_method(node)
to_method(node) do |receiver, to_method|
next if receiver.nil? || ignore_receiver?(receiver)
next if receiver.nil? || allow_receiver?(receiver)

message = format(
MSG,
Expand Down Expand Up @@ -141,9 +152,10 @@ def remove_parentheses(corrector, node)
corrector.remove(node.loc.end)
end

def ignore_receiver?(receiver)
def allow_receiver?(receiver)
if receiver.numeric_type? || (receiver.send_type? &&
(conversion_method?(receiver.method_name) || ignored_method?(receiver.method_name)))
(conversion_method?(receiver.method_name) ||
allowed_method_name?(receiver.method_name)))
true
elsif (receiver = top_receiver(receiver))
receiver.const_type? && ignored_class?(receiver.const_name)
Expand All @@ -152,6 +164,10 @@ def ignore_receiver?(receiver)
end
end

def allowed_method_name?(name)
allowed_method?(name) || matches_allowed_pattern?(name)
end

def top_receiver(node)
receiver = node
receiver = receiver.receiver until receiver.receiver.nil?
Expand Down

0 comments on commit 4537491

Please sign in to comment.