diff --git a/changelog/change_update_uses_of_ignoredmethods_to.md b/changelog/change_update_uses_of_ignoredmethods_to.md
new file mode 100644
index 00000000000..b90513789b0
--- /dev/null
+++ b/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][])
diff --git a/config/default.yml b/config/default.yml
index ab0a4d5f31d..c253fa04979 100644
--- a/config/default.yml
+++ b/config/default.yml
@@ -1502,7 +1502,9 @@ Lint/AmbiguousBlockAssociation:
Enabled: true
VersionAdded: '0.48'
VersionChanged: '1.13'
- IgnoredMethods: []
+ AllowedMethods: []
+ AllowedPatterns: []
+ IgnoredMethods: [] # deprecated
Lint/AmbiguousOperator:
Description: >-
@@ -1991,7 +1993,9 @@ Lint/NumberConversion:
VersionAdded: '0.53'
VersionChanged: '1.1'
SafeAutoCorrect: false
- IgnoredMethods: []
+ AllowedMethods: []
+ AllowedPatterns: []
+ IgnoredMethods: [] # deprecated
IgnoredClasses:
- Time
- DateTime
@@ -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
@@ -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'
@@ -2489,7 +2497,9 @@ Metrics/CyclomaticComplexity:
Enabled: true
VersionAdded: '0.25'
VersionChanged: '0.81'
- IgnoredMethods: []
+ AllowedMethods: []
+ AllowedPatterns: []
+ IgnoredMethods: [] # deprecated
Max: 7
Metrics/MethodLength:
@@ -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.'
@@ -2530,7 +2542,9 @@ Metrics/PerceivedComplexity:
Enabled: true
VersionAdded: '0.25'
VersionChanged: '0.81'
- IgnoredMethods: []
+ AllowedMethods: []
+ AllowedPatterns: []
+ IgnoredMethods: [] # deprecated
Max: 8
################## Migration #############################
@@ -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
@@ -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.
#
@@ -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:
#
@@ -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.'
@@ -3687,7 +3705,9 @@ Style/FormatStringToken:
MaxUnannotatedPlaceholdersAllowed: 1
VersionAdded: '0.49'
VersionChanged: '1.0'
- IgnoredMethods: []
+ AllowedMethods: []
+ AllowedPatterns: []
+ IgnoredMethods: [] # deprecated
Style/FrozenStringLiteralComment:
Description: >-
@@ -4006,7 +4026,8 @@ Style/MethodCallWithArgsParentheses:
VersionAdded: '0.47'
VersionChanged: '1.7'
IgnoreMacros: true
- IgnoredMethods: []
+ AllowedMethods: []
+ IgnoredMethods: [] # deprecated
AllowedPatterns: []
IgnoredPatterns: [] # deprecated
IncludedMacros: []
@@ -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'
@@ -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:
@@ -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:
diff --git a/config/obsoletion.yml b/config/obsoletion.yml
index 141f5a3c9a8..80f7b20ace6 100644
--- a/config/obsoletion.yml
+++ b/config/obsoletion.yml
@@ -187,7 +187,9 @@ changed_parameters:
- Metrics/BlockLength
- Metrics/MethodLength
parameters: ExcludedMethods
- alternative: IgnoredMethods
+ alternatives:
+ - AllowedMethods
+ - AllowedPatterns
severity: warning
- cops: Lint/Debugger
parameters: DebuggerReceivers
@@ -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:
diff --git a/lib/rubocop.rb b/lib/rubocop.rb
index eaac41af88e..55ef735b0f1 100644
--- a/lib/rubocop.rb
+++ b/lib/rubocop.rb
@@ -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'
diff --git a/lib/rubocop/config_obsoletion/changed_parameter.rb b/lib/rubocop/config_obsoletion/changed_parameter.rb
index 48cd576ea0e..49905717001 100644
--- a/lib/rubocop/config_obsoletion/changed_parameter.rb
+++ b/lib/rubocop/config_obsoletion/changed_parameter.rb
@@ -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
diff --git a/lib/rubocop/config_obsoletion/parameter_rule.rb b/lib/rubocop/config_obsoletion/parameter_rule.rb
index 1f1ca1c4a5c..f2d742a1a8c 100644
--- a/lib/rubocop/config_obsoletion/parameter_rule.rb
+++ b/lib/rubocop/config_obsoletion/parameter_rule.rb
@@ -32,6 +32,10 @@ def alternative
metadata['alternative']
end
+ def alternatives
+ metadata['alternatives']
+ end
+
def reason
metadata['reason']
end
diff --git a/lib/rubocop/cop/lint/ambiguous_block_association.rb b/lib/rubocop/cop/lint/ambiguous_block_association.rb
index 30d12f184eb..8ea895fb0ec 100644
--- a/lib/rubocop/cop/lint/ambiguous_block_association.rb
+++ b/lib/rubocop/cop/lint/ambiguous_block_association.rb
@@ -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
#
@@ -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 `%s` to make sure that the ' \
'block will be associated with the `%s` method ' \
@@ -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)
@@ -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)
diff --git a/lib/rubocop/cop/lint/number_conversion.rb b/lib/rubocop/cop/lint/number_conversion.rb
index 59a068de5ac..068bbe9ec3c 100644
--- a/lib/rubocop/cop/lint/number_conversion.rb
+++ b/lib/rubocop/cop/lint/number_conversion.rb
@@ -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
@@ -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
@@ -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}(%s, 10)",
@@ -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,
@@ -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)
@@ -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?
diff --git a/lib/rubocop/cop/metrics/abc_size.rb b/lib/rubocop/cop/metrics/abc_size.rb
index 9a09511cf7b..b52f5d3b821 100644
--- a/lib/rubocop/cop/metrics/abc_size.rb
+++ b/lib/rubocop/cop/metrics/abc_size.rb
@@ -33,7 +33,9 @@ module Metrics
# render 'pages/search/page'
# end
#
- # This cop also takes into account `IgnoredMethods` (defaults to `[]`)
+ # This cop also takes into account `AllowedMethods` (defaults to `[]`)
+ # And `AllowedPatterns` (defaults to `[]`)
+ #
class AbcSize < Base
include MethodComplexity
diff --git a/lib/rubocop/cop/metrics/block_length.rb b/lib/rubocop/cop/metrics/block_length.rb
index 9ae7239e7ae..0072e919c7c 100644
--- a/lib/rubocop/cop/metrics/block_length.rb
+++ b/lib/rubocop/cop/metrics/block_length.rb
@@ -14,8 +14,8 @@ module Metrics
#
#
# NOTE: The `ExcludedMethods` configuration is deprecated and only kept
- # for backwards compatibility. Please use `IgnoredMethods` instead.
- # By default, there are no methods to ignored.
+ # for backwards compatibility. Please use `AllowedMethods` and `AllowedPatterns`
+ # instead. By default, there are no methods to allowed.
#
# @example CountAsOne: ['array', 'heredoc']
#
@@ -38,14 +38,13 @@ module Metrics
# NOTE: This cop does not apply for `Struct` definitions.
class BlockLength < Base
include CodeLength
- include IgnoredMethods
-
- ignored_methods deprecated_key: 'ExcludedMethods'
+ include AllowedMethods
+ include AllowedPattern
LABEL = 'Block'
def on_block(node)
- return if ignored_method?(node.method_name)
+ return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)
return if method_receiver_excluded?(node)
return if node.class_constructor? || node.struct_constructor?
@@ -59,7 +58,7 @@ def method_receiver_excluded?(node)
node_receiver = node.receiver&.source&.gsub(/\s+/, '')
node_method = String(node.method_name)
- ignored_methods.any? do |config|
+ allowed_methods.any? do |config|
next unless config.is_a?(String)
receiver, method = config.split('.')
diff --git a/lib/rubocop/cop/metrics/method_length.rb b/lib/rubocop/cop/metrics/method_length.rb
index 205d2c24fd0..96da880e171 100644
--- a/lib/rubocop/cop/metrics/method_length.rb
+++ b/lib/rubocop/cop/metrics/method_length.rb
@@ -4,16 +4,17 @@ module RuboCop
module Cop
module Metrics
# Checks if the length of a method exceeds some maximum value.
- # Comment lines can optionally be ignored.
+ # Comment lines can optionally be allowed.
# The maximum allowed length is configurable.
#
# You can set literals you want to fold with `CountAsOne`.
# Available are: 'array', 'hash', and 'heredoc'. Each literal
# will be counted as one line regardless of its actual size.
#
- # NOTE: The `ExcludedMethods` configuration is deprecated and only kept
- # for backwards compatibility. Please use `IgnoredMethods` instead.
- # By default, there are no methods to ignored.
+ # NOTE: The `ExcludedMethods` and `IgnoredMethods` configuration is
+ # deprecated and only kept for backwards compatibility.
+ # Please use `AllowedMethods` and `AllowedPatterns` instead.
+ # By default, there are no methods to allowed.
#
# @example CountAsOne: ['array', 'heredoc']
#
@@ -35,14 +36,13 @@ module Metrics
#
class MethodLength < Base
include CodeLength
- include IgnoredMethods
-
- ignored_methods deprecated_key: 'ExcludedMethods'
+ include AllowedMethods
+ include AllowedPattern
LABEL = 'Method'
def on_def(node)
- return if ignored_method?(node.method_name)
+ return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)
check_code_length(node)
end
diff --git a/lib/rubocop/cop/mixin/allowed_methods.rb b/lib/rubocop/cop/mixin/allowed_methods.rb
index 8be7a048c09..64829d79c90 100644
--- a/lib/rubocop/cop/mixin/allowed_methods.rb
+++ b/lib/rubocop/cop/mixin/allowed_methods.rb
@@ -12,10 +12,24 @@ def allowed_method?(name)
allowed_methods.include?(name.to_s)
end
+ # @deprecated Use allowed_method? instead
+ alias ignored_method? allowed_method?
+
# @api public
def allowed_methods
- cop_config.fetch('AllowedMethods', [])
+ deprecated_values = cop_config_deprecated_values
+ if deprecated_values.any?(Regexp)
+ cop_config.fetch('AllowedMethods', [])
+ else
+ Array(cop_config['AllowedMethods']).concat(deprecated_values)
+ end
+ end
+
+ def cop_config_deprecated_values
+ Array(cop_config['IgnoredMethods']).concat(Array(cop_config['ExcludedMethods']))
end
end
+ # @deprecated IgnoredMethods class has been replaced with AllowedMethods.
+ IgnoredMethods = AllowedMethods
end
end
diff --git a/lib/rubocop/cop/mixin/allowed_pattern.rb b/lib/rubocop/cop/mixin/allowed_pattern.rb
index 0df4ebbae45..f3d47fb3ce9 100644
--- a/lib/rubocop/cop/mixin/allowed_pattern.rb
+++ b/lib/rubocop/cop/mixin/allowed_pattern.rb
@@ -30,7 +30,15 @@ def matches_allowed_pattern?(line)
def allowed_patterns
# Since there could be a pattern specified in the default config, merge the two
# arrays together.
- Array(cop_config['AllowedPatterns']).concat(Array(cop_config['IgnoredPatterns']))
+ patterns = Array(cop_config['AllowedPatterns']).concat(Array(cop_config['IgnoredPatterns']))
+ deprecated_values = cop_config_deprecated_methods_values
+ return patterns unless deprecated_values.any?(Regexp)
+
+ Array(patterns.concat(deprecated_values))
+ end
+
+ def cop_config_deprecated_methods_values
+ Array(cop_config['IgnoredMethods']).concat(Array(cop_config['ExcludedMethods']))
end
end
diff --git a/lib/rubocop/cop/mixin/ignored_methods.rb b/lib/rubocop/cop/mixin/ignored_methods.rb
deleted file mode 100644
index e6b13a989a0..00000000000
--- a/lib/rubocop/cop/mixin/ignored_methods.rb
+++ /dev/null
@@ -1,52 +0,0 @@
-# frozen_string_literal: true
-
-module RuboCop
- module Cop
- # This module encapsulates the ability to ignore certain methods when
- # parsing.
- # Cops that use `IgnoredMethods` can accept either strings or regexes to match
- # against.
- module IgnoredMethods
- # Configuration for IgnoredMethods. It is added to classes that include
- # the module so that configuration can be set using the `ignored_methods`
- # class macro.
- module Config
- attr_accessor :deprecated_key
-
- def ignored_methods(**config)
- self.deprecated_key = config[:deprecated_key]
- end
- end
-
- def self.included(base)
- base.extend(Config)
- end
-
- def ignored_method?(name)
- ignored_methods.any? do |value|
- case value
- when Regexp
- value.match? String(name)
- else
- value == String(name)
- end
- end
- end
-
- def ignored_methods
- keys = %w[IgnoredMethods]
- keys << deprecated_key if deprecated_key
-
- cop_config.slice(*keys).values.reduce(&:concat)
- end
-
- private
-
- def deprecated_key
- return unless self.class.respond_to?(:deprecated_key)
-
- self.class.deprecated_key&.to_s
- end
- end
- end
-end
diff --git a/lib/rubocop/cop/mixin/method_complexity.rb b/lib/rubocop/cop/mixin/method_complexity.rb
index 2e07b41c744..92be750b239 100644
--- a/lib/rubocop/cop/mixin/method_complexity.rb
+++ b/lib/rubocop/cop/mixin/method_complexity.rb
@@ -6,21 +6,16 @@ module Cop
#
# This module handles measurement and reporting of complexity in methods.
module MethodComplexity
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
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.
- def self.included(base)
- base.extend(IgnoredMethods::Config)
- end
-
def on_def(node)
- return if ignored_method?(node.method_name)
+ return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)
check_complexity(node, node.method_name)
end
@@ -28,7 +23,7 @@ def on_def(node)
def on_block(node)
define_method?(node) do |name|
- return if ignored_method?(name)
+ return if allowed_method?(name) || matches_allowed_pattern?(name)
check_complexity(node, name)
end
diff --git a/lib/rubocop/cop/style/block_delimiters.rb b/lib/rubocop/cop/style/block_delimiters.rb
index ee8154f47ac..3ebd740b34e 100644
--- a/lib/rubocop/cop/style/block_delimiters.rb
+++ b/lib/rubocop/cop/style/block_delimiters.rb
@@ -10,7 +10,7 @@ module Style
# Methods that can be either procedural or functional and cannot be
# categorised from their usage alone is ignored.
# `lambda`, `proc`, and `it` are their defaults.
- # Additional methods can be added to the `IgnoredMethods`.
+ # Additional methods can be added to the `AllowedMethods`.
#
# @example EnforcedStyle: line_count_based (default)
# # bad - single line block
@@ -66,7 +66,7 @@ module Style
# x
# }.inspect
#
- # # The AllowBracesOnProceduralOneLiners option is ignored unless the
+ # # The AllowBracesOnProceduralOneLiners option is allowed unless the
# # EnforcedStyle is set to `semantic`. If so:
#
# # If the AllowBracesOnProceduralOneLiners option is unspecified, or
@@ -116,7 +116,7 @@ module Style
#
# # Methods listed in the BracesRequiredMethods list, such as 'sig'
# # in this example, will require `{...}` braces. This option takes
- # # precedence over all other configurations except IgnoredMethods.
+ # # precedence over all other configurations except AllowedMethods.
#
# # bad
# sig do
@@ -138,7 +138,7 @@ module Style
# puts foo
# end
#
- # @example IgnoredMethods: ['lambda', 'proc', 'it' ] (default)
+ # @example AllowedMethods: ['lambda', 'proc', 'it' ] (default)
#
# # good
# foo = lambda do |x|
@@ -149,9 +149,26 @@ module Style
# x * 100
# end
#
+ # @example AllowedPatterns: [] (default)
+ #
+ # # bad
+ # things.map { |thing|
+ # something = thing.some_method
+ # process(something)
+ # }
+ #
+ # @example AllowedPatterns: [/map/]
+ #
+ # # good
+ # things.map { |thing|
+ # something = thing.some_method
+ # process(something)
+ # }
+ #
class BlockDelimiters < Base
include ConfigurableEnforcedStyle
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
include RangeHelp
extend AutoCorrector
@@ -330,12 +347,14 @@ def proper_block_style?(node)
end
def special_method?(method_name)
- ignored_method?(method_name) || braces_required_method?(method_name)
+ allowed_method?(method_name) ||
+ matches_allowed_pattern?(method_name) ||
+ braces_required_method?(method_name)
end
def special_method_proper_block_style?(node)
method_name = node.method_name
- return true if ignored_method?(method_name)
+ return true if allowed_method?(method_name) || matches_allowed_pattern?(method_name)
return node.braces? if braces_required_method?(method_name)
end
diff --git a/lib/rubocop/cop/style/class_equality_comparison.rb b/lib/rubocop/cop/style/class_equality_comparison.rb
index 6adfa0d70f8..febe49b5d67 100644
--- a/lib/rubocop/cop/style/class_equality_comparison.rb
+++ b/lib/rubocop/cop/style/class_equality_comparison.rb
@@ -5,8 +5,8 @@ module Cop
module Style
# Enforces the use of `Object#instance_of?` instead of class comparison
# for equality.
- # `==`, `equal?`, and `eql?` methods are ignored by default.
- # These are customizable with `IgnoredMethods` option.
+ # `==`, `equal?`, and `eql?` methods are allowed by default.
+ # These are customizable with `AllowedMethods` option.
#
# @example
# # bad
@@ -18,7 +18,7 @@ module Style
# # good
# var.instance_of?(Date)
#
- # @example IgnoreMethods: [] (default)
+ # @example AllowedMethods: [] (default)
# # good
# var.instance_of?(Date)
#
@@ -28,7 +28,7 @@ module Style
# var.class.eql?(Date)
# var.class.name == 'Date'
#
- # @example IgnoreMethods: [`==`]
+ # @example AllowedMethods: [`==`]
# # good
# var.instance_of?(Date)
# var.class == Date
@@ -38,9 +38,30 @@ module Style
# var.class.equal?(Date)
# var.class.eql?(Date)
#
+ # @example AllowedPatterns: [] (default)
+ # # good
+ # var.instance_of?(Date)
+ #
+ # # bad
+ # var.class == Date
+ # var.class.equal?(Date)
+ # var.class.eql?(Date)
+ # var.class.name == 'Date'
+ #
+ # @example AllowedPatterns: [`/eq/`]
+ # # good
+ # var.instance_of?(Date)
+ # var.class.equal?(Date)
+ # var.class.eql?(Date)
+ #
+ # # bad
+ # var.class == Date
+ # var.class.name == 'Date'
+ #
class ClassEqualityComparison < Base
include RangeHelp
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
extend AutoCorrector
MSG = 'Use `instance_of?(%s)` instead of comparing classes.'
@@ -56,7 +77,9 @@ class ClassEqualityComparison < Base
def on_send(node)
def_node = node.each_ancestor(:def, :defs).first
- return if def_node && ignored_method?(def_node.method_name)
+ return if def_node &&
+ (allowed_method?(def_node.method_name) ||
+ matches_allowed_pattern?(def_node.method_name))
class_comparison_candidate?(node) do |receiver_node, class_node|
range = offense_range(receiver_node, node)
diff --git a/lib/rubocop/cop/style/format_string_token.rb b/lib/rubocop/cop/style/format_string_token.rb
index 522f7f42329..e8bb538c886 100644
--- a/lib/rubocop/cop/style/format_string_token.rb
+++ b/lib/rubocop/cop/style/format_string_token.rb
@@ -11,8 +11,8 @@ module Style
# The reason is that _unannotated_ format is very similar
# to encoded URLs or Date/Time formatting strings.
#
- # This cop can be customized ignored methods with `IgnoredMethods`.
- # By default, there are no methods to ignored.
+ # This cop can be customized allowed methods with `AllowedMethods`.
+ # By default, there are no methods to allowed.
#
# @example EnforcedStyle: annotated (default)
#
@@ -62,23 +62,34 @@ module Style
# # good
# format('%06d', 10)
#
- # @example IgnoredMethods: [] (default)
+ # @example AllowedMethods: [] (default)
#
# # bad
# redirect('foo/%{bar_id}')
#
- # @example IgnoredMethods: [redirect]
+ # @example AllowedMethods: [redirect]
+ #
+ # # good
+ # redirect('foo/%{bar_id}')
+ #
+ # @example AllowedPatterns: [] (default)
+ #
+ # # bad
+ # redirect('foo/%{bar_id}')
+ #
+ # @example AllowedPatterns: [/redirect/]
#
# # good
# redirect('foo/%{bar_id}')
#
class FormatStringToken < Base
include ConfigurableEnforcedStyle
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
extend AutoCorrector
def on_str(node)
- return if format_string_token?(node) || use_ignored_method?(node)
+ return if format_string_token?(node) || use_allowed_method?(node)
detections = collect_detections(node)
return if detections.empty?
@@ -103,9 +114,11 @@ def format_string_token?(node)
!node.value.include?('%') || node.each_ancestor(:xstr, :regexp).any?
end
- def use_ignored_method?(node)
+ def use_allowed_method?(node)
send_parent = node.each_ancestor(:send).first
- send_parent && ignored_method?(send_parent.method_name)
+ send_parent &&
+ (allowed_method?(send_parent.method_name) ||
+ matches_allowed_pattern?(send_parent.method_name))
end
def check_sequence(detected_sequence, token_range)
diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses.rb
index ccf87d7bda3..70ccda1e2d7 100644
--- a/lib/rubocop/cop/style/method_call_with_args_parentheses.rb
+++ b/lib/rubocop/cop/style/method_call_with_args_parentheses.rb
@@ -6,8 +6,8 @@ module Style
# Enforces the presence (default) or absence of parentheses in
# method calls containing parameters.
#
- # In the default style (require_parentheses), macro methods are ignored.
- # Additional methods can be added to the `IgnoredMethods`
+ # In the default style (require_parentheses), macro methods are allowed.
+ # Additional methods can be added to the `AllowedMethods`
# or `AllowedPatterns` list. These options are
# valid only in the default style. Macros can be included by
# either setting `IgnoreMacros` to false or adding specific macros to
@@ -15,13 +15,13 @@ module Style
#
# Precedence of options is all follows:
#
- # 1. `IgnoredMethods`
+ # 1. `AllowedMethods`
# 2. `AllowedPatterns`
# 3. `IncludedMacros`
#
# eg. If a method is listed in both
- # `IncludedMacros` and `IgnoredMethods`, then the latter takes
- # precedence (that is, the method is ignored).
+ # `IncludedMacros` and `AllowedMethods`, then the latter takes
+ # precedence (that is, the method is allowed).
#
# In the alternative style (omit_parentheses), there are three additional
# options.
@@ -65,7 +65,7 @@ module Style
# # Setter methods don't need parens
# foo.bar = baz
#
- # # okay with `puts` listed in `IgnoredMethods`
+ # # okay with `puts` listed in `AllowedMethods`
# puts 'test'
#
# # okay with `^assert` listed in `AllowedPatterns`
@@ -197,7 +197,7 @@ class MethodCallWithArgsParentheses < Base
require_relative 'method_call_with_args_parentheses/require_parentheses'
include ConfigurableEnforcedStyle
- include IgnoredMethods
+ include AllowedMethods
include AllowedPattern
include RequireParentheses
include OmitParentheses
diff --git a/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb b/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb
index 6e77197f8c9..e5b392ba715 100644
--- a/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb
+++ b/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb
@@ -12,7 +12,7 @@ module RequireParentheses
private
def require_parentheses(node)
- return if ignored_method?(node.method_name)
+ return if allowed_method_name?(node.method_name)
return if matches_allowed_pattern?(node.method_name)
return if eligible_for_parentheses_omission?(node)
return unless node.arguments? && !node.parenthesized?
@@ -24,6 +24,10 @@ def require_parentheses(node)
end
end
+ def allowed_method_name?(name)
+ allowed_method?(name) || matches_allowed_pattern?(name)
+ end
+
def eligible_for_parentheses_omission?(node)
node.operator_method? || node.setter_method? || ignored_macro?(node)
end
diff --git a/lib/rubocop/cop/style/method_call_without_args_parentheses.rb b/lib/rubocop/cop/style/method_call_without_args_parentheses.rb
index d176baa990b..174ba826691 100644
--- a/lib/rubocop/cop/style/method_call_without_args_parentheses.rb
+++ b/lib/rubocop/cop/style/method_call_without_args_parentheses.rb
@@ -5,8 +5,8 @@ module Cop
module Style
# Checks for unwanted parentheses in parameterless method calls.
#
- # This cop can be customized ignored methods with `IgnoredMethods`.
- # By default, there are no methods to ignored.
+ # This cop can be customized allowed methods with `AllowedMethods`.
+ # By default, there are no methods to allowed.
#
# @example
# # bad
@@ -15,16 +15,17 @@ module Style
# # good
# object.some_method
#
- # @example IgnoredMethods: [] (default)
+ # @example AllowedMethods: [] (default)
# # bad
# object.foo()
#
- # @example IgnoredMethods: [foo]
+ # @example AllowedMethods: [foo]
# # good
# object.foo()
#
class MethodCallWithoutArgsParentheses < Base
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
extend AutoCorrector
MSG = 'Do not use parentheses for method calls with no arguments.'
@@ -33,7 +34,7 @@ def on_send(node)
return unless !node.arguments? && node.parenthesized?
return if ineligible_node?(node)
return if default_argument?(node)
- return if ignored_method?(node.method_name)
+ return if allowed_method_name?(node.method_name)
return if same_name_assignment?(node)
register_offense(node)
@@ -56,6 +57,10 @@ def default_argument?(node)
node.parent&.optarg_type?
end
+ def allowed_method_name?(name)
+ allowed_method?(name) || matches_allowed_pattern?(name)
+ end
+
def same_name_assignment?(node)
any_assignment?(node) do |asgn_node|
next variable_in_mass_assignment?(node.method_name, asgn_node) if asgn_node.masgn_type?
diff --git a/lib/rubocop/cop/style/numeric_predicate.rb b/lib/rubocop/cop/style/numeric_predicate.rb
index a71b72c1735..7e78dd16e38 100644
--- a/lib/rubocop/cop/style/numeric_predicate.rb
+++ b/lib/rubocop/cop/style/numeric_predicate.rb
@@ -8,14 +8,14 @@ module Style
# These can be replaced by their respective predicate methods.
# This cop can also be configured to do the reverse.
#
- # This cop can be customized ignored methods with `IgnoredMethods`.
- # By default, there are no methods to ignored.
+ # This cop can be customized allowed methods with `AllowedMethods`.
+ # By default, there are no methods to allowed.
#
# This cop disregards `#nonzero?` as its value is truthy or falsey,
# but not `true` and `false`, and thus not always interchangeable with
# `!= 0`.
#
- # This cop ignores comparisons to global variables, since they are often
+ # This cop allows comparisons to global variables, since they are often
# populated with objects which can be compared with integers, but are
# not themselves `Integer` polymorphic.
#
@@ -46,13 +46,13 @@ module Style
# 0 > foo
# bar.baz > 0
#
- # @example IgnoredMethods: [] (default) with EnforcedStyle: predicate
+ # @example AllowedMethods: [] (default) with EnforcedStyle: predicate
# # bad
# foo == 0
# 0 > foo
# bar.baz > 0
#
- # @example IgnoredMethods: [==] with EnforcedStyle: predicate
+ # @example AllowedMethods: [==] with EnforcedStyle: predicate
# # good
# foo == 0
#
@@ -60,9 +60,25 @@ module Style
# 0 > foo
# bar.baz > 0
#
+ # @example AllowedPatterns: [] (default) with EnforcedStyle: comparison
+ # # bad
+ # foo.zero?
+ # foo.negative?
+ # bar.baz.positive?
+ #
+ # @example AllowedPatterns: [/zero/] with EnforcedStyle: predicate
+ # # good
+ # # bad
+ # foo.zero?
+ #
+ # # bad
+ # foo.negative?
+ # bar.baz.positive?
+ #
class NumericPredicate < Base
include ConfigurableEnforcedStyle
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
extend AutoCorrector
MSG = 'Use `%s` instead of `%s`.'
@@ -75,9 +91,9 @@ def on_send(node)
numeric, replacement = check(node)
return unless numeric
- return if ignored_method?(node.method_name) ||
+ return if allowed_method_name?(node.method_name) ||
node.each_ancestor(:send, :block).any? do |ancestor|
- ignored_method?(ancestor.method_name)
+ allowed_method_name?(ancestor.method_name)
end
message = format(MSG, prefer: replacement, current: node.source)
@@ -88,6 +104,10 @@ def on_send(node)
private
+ def allowed_method_name?(name)
+ allowed_method?(name) || matches_allowed_pattern?(name)
+ end
+
def check(node)
numeric, operator =
if style == :predicate
diff --git a/lib/rubocop/cop/style/symbol_proc.rb b/lib/rubocop/cop/style/symbol_proc.rb
index 099cb480893..dc56c3ca981 100644
--- a/lib/rubocop/cop/style/symbol_proc.rb
+++ b/lib/rubocop/cop/style/symbol_proc.rb
@@ -7,13 +7,13 @@ module Style
#
# If you prefer a style that allows block for method with arguments,
# please set `true` to `AllowMethodsWithArguments`.
- # respond_to , and `define_method?` methods are ignored by default.
- # These are customizable with `IgnoredMethods` option.
+ # respond_to , and `define_method?` methods are allowed by default.
+ # These are customizable with `AllowedMethods` option.
#
# @safety
# This cop is unsafe because `proc`s and blocks work differently
# when additional arguments are passed in. A block will silently
- # ignore additional arguments, but a `proc` will raise
+ # allow additional arguments, but a `proc` will raise
# an `ArgumentError`.
#
# For example:
@@ -71,15 +71,25 @@ module Style
# # some comment
# end
#
- # @example IgnoredMethods: [respond_to, define_method] (default)
+ # @example AllowedMethods: [respond_to, define_method] (default)
# # good
# respond_to { |foo| foo.bar }
# define_method(:foo) { |foo| foo.bar }
#
+ #
+ # @example AllowedPatterns: [] (default)
+ # # bad
+ # something.map { |s| s.upcase }
+ #
+ # @example AllowedPatterns: [/map/] (default)
+ # # good
+ # something.map { |s| s.upcase }
+ #
class SymbolProc < Base
include CommentsHelp
include RangeHelp
- include IgnoredMethods
+ include AllowedMethods
+ include AllowedPattern
extend AutoCorrector
MSG = 'Pass `&:%s` as an argument to `%s` instead of a block.'
@@ -108,10 +118,10 @@ def on_block(node)
symbol_proc?(node) do |dispatch_node, arguments_node, method_name|
# TODO: Rails-specific handling that we should probably make
# configurable - https://github.com/rubocop/rubocop/issues/1485
- # we should ignore lambdas & procs
+ # we should allow lambdas & procs
return if proc_node?(dispatch_node)
return if %i[lambda proc].include?(dispatch_node.method_name)
- return if ignored_method?(dispatch_node.method_name)
+ return if allowed_method_name?(dispatch_node.method_name)
return if allow_if_method_has_argument?(node.send_node)
return if node.block_type? && destructuring_block_argument?(arguments_node)
return if allow_comments? && contains_comments?(node)
@@ -128,6 +138,10 @@ def destructuring_block_argument?(argument_node)
private
+ def allowed_method_name?(name)
+ allowed_method?(name) || matches_allowed_pattern?(name)
+ end
+
def register_offense(node, method_name, block_method_name)
block_start = node.loc.begin.begin_pos
block_end = node.loc.end.end_pos
diff --git a/spec/rubocop/config_loader_spec.rb b/spec/rubocop/config_loader_spec.rb
index 2d508d6c887..3975d4335c3 100644
--- a/spec/rubocop/config_loader_spec.rb
+++ b/spec/rubocop/config_loader_spec.rb
@@ -903,7 +903,9 @@ class Loop < Base
'CountComments' => false,
'Max' => 5,
'CountAsOne' => [],
+ 'AllowedMethods' => [],
'IgnoredMethods' => [],
+ 'AllowedPatterns' => [],
'ExcludedMethods' => []
}
)
diff --git a/spec/rubocop/config_obsoletion_spec.rb b/spec/rubocop/config_obsoletion_spec.rb
index 885e7cf68e2..3f544283a8a 100644
--- a/spec/rubocop/config_obsoletion_spec.rb
+++ b/spec/rubocop/config_obsoletion_spec.rb
@@ -399,11 +399,49 @@
context 'when the configuration includes any deprecated parameters' do
let(:hash) do
{
+ 'Lint/AmbiguousBlockAssociation' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Lint/NumberConversion' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Metrics/AbcSize' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
'Metrics/BlockLength' => {
- 'ExcludedMethods' => %w[foo bar]
+ 'ExcludedMethods' => %w[foo bar],
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Metrics/CyclomaticComplexity' => {
+ 'IgnoredMethods' => %w[foo bar]
},
'Metrics/MethodLength' => {
- 'ExcludedMethods' => %w[foo bar]
+ 'ExcludedMethods' => %w[foo bar],
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Metrics/PerceivedComplexity' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/BlockDelimiters' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/ClassEqualityComparison' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/FormatStringToken' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/MethodCallWithArgsParentheses' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/MethodCallWithoutArgsParentheses' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/NumericPredicate' => {
+ 'IgnoredMethods' => %w[foo bar]
+ },
+ 'Style/SymbolLiteral' => {
+ 'IgnoredMethods' => %w[foo bar]
}
}
end
@@ -413,9 +451,37 @@
let(:expected_message) do
<<~OUTPUT.chomp
obsolete parameter `ExcludedMethods` (for `Metrics/BlockLength`) found in example/.rubocop.yml
- `ExcludedMethods` has been renamed to `IgnoredMethods`.
+ `ExcludedMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
obsolete parameter `ExcludedMethods` (for `Metrics/MethodLength`) found in example/.rubocop.yml
- `ExcludedMethods` has been renamed to `IgnoredMethods`.
+ `ExcludedMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Lint/AmbiguousBlockAssociation`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Lint/NumberConversion`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Metrics/AbcSize`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Metrics/BlockLength`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Metrics/CyclomaticComplexity`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Metrics/MethodLength`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Metrics/PerceivedComplexity`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/BlockDelimiters`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/ClassEqualityComparison`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/FormatStringToken`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/MethodCallWithArgsParentheses`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/MethodCallWithoutArgsParentheses`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/NumericPredicate`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
+ obsolete parameter `IgnoredMethods` (for `Style/SymbolLiteral`) found in example/.rubocop.yml
+ `IgnoredMethods` has been renamed to `AllowedMethods` and/or `AllowedPatterns`.
OUTPUT
end
diff --git a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb
index ec498a63b59..d77adcff4da 100644
--- a/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb
+++ b/spec/rubocop/cop/lint/ambiguous_block_association_spec.rb
@@ -86,10 +86,10 @@
end
end
- context 'IgnoredMethods' do
- let(:cop_config) { { 'IgnoredMethods' => %w[change] } }
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'AllowedMethods' => %w[change] } }
- it 'does not register an offense for an ignored method' do
+ it 'does not register an offense for an allowed method' do
expect_no_offenses(<<~RUBY)
expect { order.expire }.to change { order.events }
RUBY
@@ -102,4 +102,22 @@
RUBY
end
end
+
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'AllowedPatterns' => [/change/] } }
+
+ it 'does not register an offense for an allowed method' do
+ expect_no_offenses(<<~RUBY)
+ expect { order.expire }.to change { order.events }
+ expect { order.expire }.to not_change { order.events }
+ RUBY
+ end
+
+ it 'registers an offense for other methods' do
+ expect_offense(<<~RUBY)
+ expect { order.expire }.to update { order.events }
+ ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Parenthesize the param `update { order.events }` to make sure that the block will be associated with the `update` method call.
+ RUBY
+ end
+ end
end
diff --git a/spec/rubocop/cop/lint/number_conversion_spec.rb b/spec/rubocop/cop/lint/number_conversion_spec.rb
index 1becedbd0be..8cafc23fdae 100644
--- a/spec/rubocop/cop/lint/number_conversion_spec.rb
+++ b/spec/rubocop/cop/lint/number_conversion_spec.rb
@@ -266,39 +266,37 @@
end
end
- context 'IgnoredMethods' do
- context 'with a string' do
- let(:cop_config) { { 'IgnoredMethods' => %w[minutes] } }
-
- it 'does not register an offense for an ignored method' do
- expect_no_offenses(<<~RUBY)
- 10.minutes.to_i
- RUBY
- end
-
- it 'registers an offense for other methods' do
- expect_offense(<<~RUBY)
- 10.hours.to_i
- ^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `10.hours.to_i`, use stricter `Integer(10.hours, 10)`.
- RUBY
- end
- end
-
- context 'with a regex' do
- let(:cop_config) { { 'IgnoredMethods' => [/minutes/] } }
-
- it 'does not register an offense for an ignored method' do
- expect_no_offenses(<<~RUBY)
- 10.minutes.to_i
- RUBY
- end
-
- it 'registers an offense for other methods' do
- expect_offense(<<~RUBY)
- 10.hours.to_i
- ^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `10.hours.to_i`, use stricter `Integer(10.hours, 10)`.
- RUBY
- end
+ context 'AllowedMethods' do
+ let(:cop_config) { { 'AllowedMethods' => %w[minutes] } }
+
+ it 'does not register an offense for an allowed method' do
+ expect_no_offenses(<<~RUBY)
+ 10.minutes.to_i
+ RUBY
+ end
+
+ it 'registers an offense for other methods' do
+ expect_offense(<<~RUBY)
+ 10.hours.to_i
+ ^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `10.hours.to_i`, use stricter `Integer(10.hours, 10)`.
+ RUBY
+ end
+ end
+
+ context 'AllowedPatterns' do
+ let(:cop_config) { { 'AllowedPatterns' => [/min/] } }
+
+ it 'does not register an offense for an allowed method' do
+ expect_no_offenses(<<~RUBY)
+ 10.minutes.to_i
+ RUBY
+ end
+
+ it 'registers an offense for other methods' do
+ expect_offense(<<~RUBY)
+ 10.hours.to_i
+ ^^^^^^^^^^^^^ Replace unsafe number conversion with number class parsing, instead of using `10.hours.to_i`, use stricter `Integer(10.hours, 10)`.
+ RUBY
end
end
end
diff --git a/spec/rubocop/cop/metrics/abc_size_spec.rb b/spec/rubocop/cop/metrics/abc_size_spec.rb
index 4f39b75fb39..24de31eb20d 100644
--- a/spec/rubocop/cop/metrics/abc_size_spec.rb
+++ b/spec/rubocop/cop/metrics/abc_size_spec.rb
@@ -76,9 +76,9 @@ def method_name
RUBY
end
- context 'when method is in list of ignored methods' do
- context 'when given a string' do
- let(:cop_config) { { 'Max' => 0, 'IgnoredMethods' => ['foo'] } }
+ context 'when method is in list of allowed methods' do
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'Max' => 0, 'AllowedMethods' => ['foo'] } }
it 'does not register an offense when defining an instance method' do
expect_no_offenses(<<~RUBY)
@@ -105,8 +105,8 @@ def self.foo
end
end
- context 'when given a regex' do
- let(:cop_config) { { 'Max' => 0, 'IgnoredMethods' => [/foo/] } }
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'Max' => 0, 'AllowedPatterns' => [/foo/] } }
it 'does not register an offense when defining an instance method' do
expect_no_offenses(<<~RUBY)
diff --git a/spec/rubocop/cop/metrics/block_length_spec.rb b/spec/rubocop/cop/metrics/block_length_spec.rb
index 33d9bca4ff8..edddc1107b0 100644
--- a/spec/rubocop/cop/metrics/block_length_spec.rb
+++ b/spec/rubocop/cop/metrics/block_length_spec.rb
@@ -3,8 +3,8 @@
RSpec.describe RuboCop::Cop::Metrics::BlockLength, :config do
let(:cop_config) { { 'Max' => 2, 'CountComments' => false } }
- shared_examples 'ignoring an offense on an ignored method' do |ignored, config_key|
- before { cop_config[config_key] = [ignored] }
+ shared_examples 'allow an offense on an allowed method' do |allowed, config_key|
+ before { cop_config[config_key] = [allowed] }
it 'still rejects other methods with long blocks' do
expect_offense(<<~RUBY)
@@ -19,7 +19,7 @@
it 'accepts the foo method with a long block' do
expect_no_offenses(<<~RUBY)
- #{ignored} do
+ #{allowed} do
a = 1
a = 2
a = 3
@@ -257,20 +257,89 @@ def foo
end
end
- context 'when methods to ignore are defined' do
- %w[IgnoredMethods ExcludedMethods].each do |key|
- context 'when IgnoredMethods is enabled' do
- it_behaves_like('ignoring an offense on an ignored method', 'foo', key)
+ context 'when methods to allow are defined' do
+ context 'when AllowedMethods is enabled' do
+ it_behaves_like('allow an offense on an allowed method', 'foo', 'AllowedMethods')
- it_behaves_like('ignoring an offense on an ignored method', 'Gem::Specification.new', key)
+ it_behaves_like('allow an offense on an allowed method', 'Gem::Specification.new',
+ 'AllowedMethods')
- context 'when receiver contains whitespaces' do
- before { cop_config[key] = ['Foo::Bar.baz'] }
+ context 'when receiver contains whitespaces' do
+ before { cop_config['AllowedMethods'] = ['Foo::Bar.baz'] }
- it 'ignores whitespaces' do
- expect_no_offenses(<<~RUBY)
- Foo::
- Bar.baz do
+ it 'allows whitespaces' do
+ expect_no_offenses(<<~RUBY)
+ Foo::
+ Bar.baz do
+ a = 1
+ a = 2
+ a = 3
+ end
+ RUBY
+ end
+ end
+
+ context 'when a method is allowed, but receiver is a module' do
+ before { cop_config['AllowedMethods'] = ['baz'] }
+
+ it 'does not report an offense' do
+ expect_no_offenses(<<~RUBY)
+ Foo::Bar.baz do
+ a = 1
+ a = 2
+ a = 3
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'when AllowedPatterns is enabled' do
+ before { cop_config['AllowedPatterns'] = [/baz/] }
+
+ it 'does not report an offense' do
+ expect_no_offenses(<<~RUBY)
+ Foo::Bar.baz do
+ a = 1
+ a = 2
+ a = 3
+ end
+ RUBY
+ end
+
+ context 'that does not match' do
+ it 'reports an offense' do
+ expect_offense(<<~RUBY)
+ Foo::Bar.bar do
+ ^^^^^^^^^^^^^^^ Block has too many lines. [3/2]
+ a = 1
+ a = 2
+ a = 3
+ end
+ RUBY
+ end
+ end
+ end
+
+ context 'when IgnoredMethods is enabled' do
+ context 'when string' do
+ before { cop_config['IgnoredMethods'] = ['Foo::Bar.baz'] }
+
+ it 'does not report an offense' do
+ expect_no_offenses(<<~RUBY)
+ Foo::Bar.baz do
+ a = 1
+ a = 2
+ a = 3
+ end
+ RUBY
+ end
+
+ context 'that does not match' do
+ it 'reports an offense' do
+ expect_offense(<<~RUBY)
+ Foo::Bar.bar do
+ ^^^^^^^^^^^^^^^ Block has too many lines. [3/2]
a = 1
a = 2
a = 3
@@ -278,13 +347,26 @@ def foo
RUBY
end
end
+ end
- context 'when a method is ignored, but receiver is a module' do
- before { cop_config[key] = ['baz'] }
+ context 'when regex' do
+ before { cop_config['IgnoredMethods'] = [/baz/] }
+
+ it 'does not report an offense' do
+ expect_no_offenses(<<~RUBY)
+ Foo::Bar.baz do
+ a = 1
+ a = 2
+ a = 3
+ end
+ RUBY
+ end
- it 'does not report an offense' do
- expect_no_offenses(<<~RUBY)
- Foo::Bar.baz do
+ context 'that does not match' do
+ it 'reports an offense' do
+ expect_offense(<<~RUBY)
+ Foo::Bar.bar do
+ ^^^^^^^^^^^^^^^ Block has too many lines. [3/2]
a = 1
a = 2
a = 3
@@ -295,8 +377,8 @@ def foo
end
end
- context 'when given a regex' do
- before { cop_config['IgnoredMethods'] = [/baz/] }
+ context 'when ExcludedMethods is enabled' do
+ before { cop_config['ExcludedMethods'] = ['Foo::Bar.baz'] }
it 'does not report an offense' do
expect_no_offenses(<<~RUBY)
diff --git a/spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb b/spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb
index 4f9a18a6445..1b147f213e2 100644
--- a/spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb
+++ b/spec/rubocop/cop/metrics/cyclomatic_complexity_spec.rb
@@ -292,61 +292,59 @@ def method_name_2
end
end
- context 'when IgnoredMethods is set' do
- context 'with a string' do
- let(:cop_config) { { 'Max' => 0, 'IgnoredMethods' => ['foo'] } }
-
- it 'does not register an offense when defining an instance method' do
- expect_no_offenses(<<~RUBY)
- def foo
- bar.baz(:qux)
- end
- RUBY
- end
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'Max' => 0, 'AllowedMethods' => ['foo'] } }
- it 'does not register an offense when defining a class method' do
- expect_no_offenses(<<~RUBY)
- def self.foo
- bar.baz(:qux)
- end
- RUBY
- end
+ it 'does not register an offense when defining an instance method' do
+ expect_no_offenses(<<~RUBY)
+ def foo
+ bar.baz(:qux)
+ end
+ RUBY
+ end
- it 'does not register an offense when using `define_method`' do
- expect_no_offenses(<<~RUBY)
- define_method :foo do
- bar.baz(:qux)
- end
- RUBY
- end
+ it 'does not register an offense when defining a class method' do
+ expect_no_offenses(<<~RUBY)
+ def self.foo
+ bar.baz(:qux)
+ end
+ RUBY
+ end
+
+ it 'does not register an offense when using `define_method`' do
+ expect_no_offenses(<<~RUBY)
+ define_method :foo do
+ bar.baz(:qux)
+ end
+ RUBY
end
+ end
- context 'with a regex' do
- let(:cop_config) { { 'Max' => 0, 'IgnoredMethods' => [/foo/] } }
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'Max' => 0, 'AllowedPatterns' => [/foo/] } }
- it 'does not register an offense when defining an instance method' do
- expect_no_offenses(<<~RUBY)
- def foo
- bar.baz(:qux)
- end
- RUBY
- end
+ it 'does not register an offense when defining an instance method' do
+ expect_no_offenses(<<~RUBY)
+ def foo
+ bar.baz(:qux)
+ end
+ RUBY
+ end
- it 'does not register an offense when defining a class method' do
- expect_no_offenses(<<~RUBY)
- def self.foo
- bar.baz(:qux)
- end
- RUBY
- end
+ it 'does not register an offense when defining a class method' do
+ expect_no_offenses(<<~RUBY)
+ def self.foo
+ bar.baz(:qux)
+ end
+ RUBY
+ end
- it 'does not register an offense when using `define_method`' do
- expect_no_offenses(<<~RUBY)
- define_method :foo do
- bar.baz(:qux)
- end
- RUBY
- end
+ it 'does not register an offense when using `define_method`' do
+ expect_no_offenses(<<~RUBY)
+ define_method :foo do
+ bar.baz(:qux)
+ end
+ RUBY
end
end
diff --git a/spec/rubocop/cop/metrics/method_length_spec.rb b/spec/rubocop/cop/metrics/method_length_spec.rb
index 709430bc2cc..a1b534d4c7b 100644
--- a/spec/rubocop/cop/metrics/method_length_spec.rb
+++ b/spec/rubocop/cop/metrics/method_length_spec.rb
@@ -205,82 +205,58 @@ def m
end
end
- context 'when methods to ignore are defined' do
- %w[IgnoredMethods ExcludedMethods].each do |key|
- context "with #{key} config" do
- context 'with a string' do
- before { cop_config[key] = ['foo'] }
+ context 'when methods to allow are defined' do
+ context 'AllowedMethods is enabled' do
+ before { cop_config['AllowedMethods'] = ['foo'] }
- it 'still rejects other methods with more than 5 lines' do
- expect_offense(<<~RUBY)
- def m
- ^^^^^ Method has too many lines. [6/5]
- a = 1
- a = 2
- a = 3
- a = 4
- a = 5
- a = 6
- end
- RUBY
- end
-
- it 'accepts the foo method with more than 5 lines' do
- expect_no_offenses(<<~RUBY)
- def foo
- a = 1
- a = 2
- a = 3
- a = 4
- a = 5
- a = 6
- end
- RUBY
- end
- end
-
- context 'with a regex' do
- before { cop_config[key] = [/_name$/] }
-
- it 'accepts the user_name method' do
- expect_no_offenses(<<~RUBY)
- def user_name
- a = 1
- a = 2
- a = 3
- a = 4
- a = 5
- a = 6
- end
- RUBY
+ it 'still rejects other methods with more than 5 lines' do
+ expect_offense(<<~RUBY)
+ def m
+ ^^^^^ Method has too many lines. [6/5]
+ a = 1
+ a = 2
+ a = 3
+ a = 4
+ a = 5
+ a = 6
end
+ RUBY
+ end
- it 'raises offense for firstname' do
- expect_offense(<<~RUBY)
- def firstname
- ^^^^^^^^^^^^^ Method has too many lines. [6/5]
- a = 1
- a = 2
- a = 3
- a = 4
- a = 5
- a = 6
- end
- RUBY
+ it 'accepts the foo method with more than 5 lines' do
+ expect_no_offenses(<<~RUBY)
+ def foo
+ a = 1
+ a = 2
+ a = 3
+ a = 4
+ a = 5
+ a = 6
end
- end
+ RUBY
end
end
- context 'if both IgnoredMethods and ExcludedMethods are given' do
- before do
- cop_config['IgnoredMethods'] = ['foo']
- cop_config['ExcludedMethods'] = ['m']
- end
+ context 'AllowedPatterns is enabled' do
+ before { cop_config['AllowedPatterns'] = [/_name/] }
- it 'uses both configs' do
+ it 'accepts the user_name method' do
expect_no_offenses(<<~RUBY)
- def m
+ def user_name
+ a = 1
+ a = 2
+ a = 3
+ a = 4
+ a = 5
+ a = 6
+ end
+ RUBY
+ end
+
+ it 'raises offense for firstname' do
+ expect_offense(<<~RUBY)
+ def firstname
+ ^^^^^^^^^^^^^ Method has too many lines. [6/5]
a = 1
a = 2
a = 3
diff --git a/spec/rubocop/cop/metrics/perceived_complexity_spec.rb b/spec/rubocop/cop/metrics/perceived_complexity_spec.rb
index 15bd528d5b3..59485662c75 100644
--- a/spec/rubocop/cop/metrics/perceived_complexity_spec.rb
+++ b/spec/rubocop/cop/metrics/perceived_complexity_spec.rb
@@ -264,8 +264,36 @@ def method_name
end
end
- context 'when method is in list of ignored methods' do
- let(:cop_config) { { 'Max' => 0, 'IgnoredMethods' => ['foo'] } }
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'Max' => 0, 'AllowedMethods' => ['foo'] } }
+
+ it 'does not register an offense when defining an instance method' do
+ expect_no_offenses(<<~RUBY)
+ def foo
+ bar.baz(:qux)
+ end
+ RUBY
+ end
+
+ it 'does not register an offense when defining a class method' do
+ expect_no_offenses(<<~RUBY)
+ def self.foo
+ bar.baz(:qux)
+ end
+ RUBY
+ end
+
+ it 'does not register an offense when using `define_method`' do
+ expect_no_offenses(<<~RUBY)
+ define_method :foo do
+ bar.baz(:qux)
+ end
+ RUBY
+ end
+ end
+
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'Max' => 0, 'AllowedPatterns' => [/foo/] } }
it 'does not register an offense when defining an instance method' do
expect_no_offenses(<<~RUBY)
diff --git a/spec/rubocop/cop/style/block_delimiters_spec.rb b/spec/rubocop/cop/style/block_delimiters_spec.rb
index a98ad02f4f6..6f4a9ac713f 100644
--- a/spec/rubocop/cop/style/block_delimiters_spec.rb
+++ b/spec/rubocop/cop/style/block_delimiters_spec.rb
@@ -63,7 +63,8 @@
'EnforcedStyle' => 'semantic',
'ProceduralMethods' => %w[tap],
'FunctionalMethods' => %w[let],
- 'IgnoredMethods' => ['lambda', /test/]
+ 'AllowedMethods' => ['lambda'],
+ 'AllowedPatterns' => [/test/]
}
let(:cop_config) { cop_config }
@@ -374,7 +375,11 @@
end
context 'EnforcedStyle: line_count_based' do
- cop_config = { 'EnforcedStyle' => 'line_count_based', 'IgnoredMethods' => ['proc', /test/] }
+ cop_config = {
+ 'EnforcedStyle' => 'line_count_based',
+ 'AllowedMethods' => ['proc'],
+ 'AllowedPatterns' => [/test/]
+ }
let(:cop_config) { cop_config }
@@ -603,7 +608,11 @@
end
context 'EnforcedStyle: braces_for_chaining' do
- cop_config = { 'EnforcedStyle' => 'braces_for_chaining', 'IgnoredMethods' => ['proc', /test/] }
+ cop_config = {
+ 'EnforcedStyle' => 'braces_for_chaining',
+ 'AllowedMethods' => ['proc'],
+ 'AllowedPatterns' => [/test/]
+ }
let(:cop_config) { cop_config }
@@ -768,7 +777,11 @@
end
context 'EnforcedStyle: always_braces' do
- cop_config = { 'EnforcedStyle' => 'always_braces', 'IgnoredMethods' => ['proc', /test/] }
+ cop_config = {
+ 'EnforcedStyle' => 'always_braces',
+ 'AllowedMethods' => ['proc'],
+ 'AllowedPatterns' => [/test/]
+ }
let(:cop_config) { cop_config }
diff --git a/spec/rubocop/cop/style/class_equality_comparison_spec.rb b/spec/rubocop/cop/style/class_equality_comparison_spec.rb
index 5c279be6e3a..dfe59c81151 100644
--- a/spec/rubocop/cop/style/class_equality_comparison_spec.rb
+++ b/spec/rubocop/cop/style/class_equality_comparison_spec.rb
@@ -1,7 +1,7 @@
# frozen_string_literal: true
RSpec.describe RuboCop::Cop::Style::ClassEqualityComparison, :config do
- let(:cop_config) { { 'IgnoredMethods' => [] } }
+ let(:cop_config) { { 'AllowedMethods' => [] } }
it 'registers an offense and corrects when comparing class using `==` for equality' do
expect_offense(<<~RUBY)
@@ -75,31 +75,29 @@
RUBY
end
- context 'when IgnoredMethods is specified' do
- context 'with a string' do
- let(:cop_config) { { 'IgnoredMethods' => ['=='] } }
-
- it 'does not register an offense when comparing class for equality' do
- expect_no_offenses(<<~RUBY)
- def ==(other)
- self.class == other.class &&
- name == other.name
- end
- RUBY
- end
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'AllowedMethods' => ['=='] } }
+
+ it 'does not register an offense when comparing class for equality' do
+ expect_no_offenses(<<~RUBY)
+ def ==(other)
+ self.class == other.class &&
+ name == other.name
+ end
+ RUBY
end
+ end
+
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'AllowedPatterns' => [/equal/] } }
- context 'with a regex' do
- let(:cop_config) { { 'IgnoredMethods' => [/equal/] } }
-
- it 'does not register an offense when comparing class for equality' do
- expect_no_offenses(<<~RUBY)
- def equal?(other)
- self.class == other.class &&
- name == other.name
- end
- RUBY
- end
+ it 'does not register an offense when comparing class for equality' do
+ expect_no_offenses(<<~RUBY)
+ def equal?(other)
+ self.class == other.class &&
+ name == other.name
+ end
+ RUBY
end
end
end
diff --git a/spec/rubocop/cop/style/format_string_token_spec.rb b/spec/rubocop/cop/style/format_string_token_spec.rb
index a298763a1be..f8d6c902d1e 100644
--- a/spec/rubocop/cop/style/format_string_token_spec.rb
+++ b/spec/rubocop/cop/style/format_string_token_spec.rb
@@ -2,14 +2,16 @@
RSpec.describe RuboCop::Cop::Style::FormatStringToken, :config do
let(:enforced_style) { :annotated }
- let(:ignored_methods) { [] }
+ let(:allowed_methods) { [] }
+ let(:allowed_patterns) { [] }
let(:cop_config) do
{
'EnforcedStyle' => enforced_style,
'SupportedStyles' => %i[annotated template unannotated],
'MaxUnannotatedPlaceholdersAllowed' => 0,
- 'IgnoredMethods' => ignored_methods
+ 'AllowedMethods' => allowed_methods,
+ 'AllowedPatterns' => allowed_patterns
}
end
@@ -318,8 +320,8 @@
RUBY
end
- context 'when `IgnoredMethods: redirect`' do
- let(:ignored_methods) { ['redirect'] }
+ context 'when AllowedMethods is enabled' do
+ let(:allowed_methods) { ['redirect'] }
it 'does not register an offense' do
expect_no_offenses(<<~RUBY)
@@ -341,8 +343,42 @@
end
end
- context 'when `IgnoredMethods: []`' do
- let(:ignored_methods) { [] }
+ context 'when AllowedMethods is disabled' do
+ let(:allowed_methods) { [] }
+
+ it 'registers an offense' do
+ expect_offense(<<~RUBY)
+ redirect("%{foo}")
+ ^^^^^^ Prefer annotated tokens (like `%s`) over template tokens (like `%{foo}`).
+ RUBY
+ end
+ end
+
+ context 'when AllowedPatterns is enabled' do
+ let(:allowed_patterns) { [/redirect/] }
+
+ it 'does not register an offense' do
+ expect_no_offenses(<<~RUBY)
+ redirect("%{foo}")
+ RUBY
+ end
+
+ it 'does not register an offense for value in nested structure' do
+ expect_no_offenses(<<~RUBY)
+ redirect("%{foo}", bye: "%{foo}")
+ RUBY
+ end
+
+ it 'registers an offense for different method call within ignored method' do
+ expect_offense(<<~RUBY)
+ redirect("%{foo}", bye: foo("%{foo}"))
+ ^^^^^^ Prefer annotated tokens (like `%s`) over template tokens (like `%{foo}`).
+ RUBY
+ end
+ end
+
+ context 'when AllowedPatterns is disabled' do
+ let(:allowed_patterns) { [] }
it 'registers an offense' do
expect_offense(<<~RUBY)
diff --git a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
index 14ebf3678b7..8c03b8f3820 100644
--- a/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
+++ b/spec/rubocop/cop/style/method_call_with_args_parentheses_spec.rb
@@ -322,21 +322,11 @@ def my_method
RUBY
end
- context 'with IgnoredMethods' do
- context 'with a string' do
- let(:cop_config) { { 'IgnoredMethods' => %w[puts] } }
+ context 'with AllowedMethods' do
+ let(:cop_config) { { 'AllowedMethods' => %w[puts] } }
- it 'ignores method listed in IgnoredMethods' do
- expect_no_offenses('puts :test')
- end
- end
-
- context 'with a regex' do
- let(:cop_config) { { 'IgnoredMethods' => [/puts/] } }
-
- it 'ignores method listed in IgnoredMethods' do
- expect_no_offenses('puts :test')
- end
+ it 'allow method listed in AllowedMethods' do
+ expect_no_offenses('puts :test')
end
end
@@ -1039,12 +1029,12 @@ module Foo
end
end
- context 'for a macro in both IncludedMacros and IgnoredMethods' do
+ context 'for a macro in both IncludedMacros and AllowedMethods' do
let(:cop_config) do
{
'IgnoreMacros' => 'true',
'IncludedMacros' => ['bar'],
- 'IgnoredMethods' => ['bar']
+ 'AllowedMethods' => ['bar']
}
end
diff --git a/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb b/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb
index 5f64956cfc6..524157515d4 100644
--- a/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb
+++ b/spec/rubocop/cop/style/method_call_without_args_parentheses_spec.rb
@@ -29,21 +29,19 @@
expect_no_offenses('not(something)')
end
- context 'with IgnoredMethods' do
- context 'with a string' do
- let(:cop_config) { { 'IgnoredMethods' => %w[s] } }
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'AllowedMethods' => %w[s] } }
- it 'ignores method listed in IgnoredMethods' do
- expect_no_offenses('s()')
- end
+ it 'ignores method listed in AllowedMethods' do
+ expect_no_offenses('s()')
end
+ end
- context 'with a regex' do
- let(:cop_config) { { 'IgnoredMethods' => [/test/] } }
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'AllowedPatterns' => [/test/] } }
- it 'ignores method listed in IgnoredMethods' do
- expect_no_offenses('my_test()')
- end
+ it 'ignores method listed in AllowedMethods' do
+ expect_no_offenses('my_test()')
end
end
diff --git a/spec/rubocop/cop/style/numeric_predicate_spec.rb b/spec/rubocop/cop/style/numeric_predicate_spec.rb
index eabc977fbd7..7224a52b7d0 100644
--- a/spec/rubocop/cop/style/numeric_predicate_spec.rb
+++ b/spec/rubocop/cop/style/numeric_predicate_spec.rb
@@ -242,18 +242,25 @@ def m(foo)
end
end
- context 'when there are ignored methods' do
+ context 'when there are allowed methods' do
let(:cop_config) do
{
'EnforcedStyle' => 'predicate',
'AutoCorrect' => true,
- 'IgnoredMethods' => ['where', /order/]
+ 'AllowedMethods' => ['where'],
+ 'AllowedPatterns' => [/order/]
}
end
context 'simple method call' do
context '`EnforcedStyle` is `predicate`' do
- let(:cop_config) { { 'EnforcedStyle' => 'predicate', 'IgnoredMethods' => %w[==] } }
+ let(:cop_config) do
+ {
+ 'EnforcedStyle' => 'predicate',
+ 'AllowedMethods' => %w[==],
+ 'AllowedPatterns' => []
+ }
+ end
it 'allows checking if a number is zero' do
expect_no_offenses(<<~RUBY)
@@ -265,7 +272,13 @@ def m(foo)
end
context '`EnforcedStyle` is `comparison`' do
- let(:cop_config) { { 'EnforcedStyle' => 'comparison', 'IgnoredMethods' => %w[zero?] } }
+ let(:cop_config) do
+ {
+ 'EnforcedStyle' => 'comparison',
+ 'AllowedMethods' => [],
+ 'AllowedPatterns' => [/zero/]
+ }
+ end
it 'allows checking if a number is zero' do
expect_no_offenses(<<~RUBY)
diff --git a/spec/rubocop/cop/style/symbol_proc_spec.rb b/spec/rubocop/cop/style/symbol_proc_spec.rb
index 9ce4bcf8bd8..e1dc91d3b01 100644
--- a/spec/rubocop/cop/style/symbol_proc_spec.rb
+++ b/spec/rubocop/cop/style/symbol_proc_spec.rb
@@ -54,21 +54,19 @@
expect_no_offenses('::Proc.new { |x| x.method }')
end
- context 'with IgnoredMethods' do
- context 'when given a string' do
- let(:cop_config) { { 'IgnoredMethods' => %w[respond_to] } }
+ context 'when AllowedMethods is enabled' do
+ let(:cop_config) { { 'AllowedMethods' => %w[respond_to] } }
- it 'accepts ignored method' do
- expect_no_offenses('respond_to { |format| format.xml }')
- end
+ it 'accepts ignored method' do
+ expect_no_offenses('respond_to { |format| format.xml }')
end
+ end
- context 'when given a regex' do
- let(:cop_config) { { 'IgnoredMethods' => [/respond_/] } }
+ context 'when AllowedPatterns is enabled' do
+ let(:cop_config) { { 'AllowedPatterns' => [/respond_/] } }
- it 'accepts ignored method' do
- expect_no_offenses('respond_to { |format| format.xml }')
- end
+ it 'accepts ignored method' do
+ expect_no_offenses('respond_to { |format| format.xml }')
end
end