Skip to content

Commit

Permalink
Avoid calling extend in initialize
Browse files Browse the repository at this point in the history
  • Loading branch information
marcandre authored and mergify[bot] committed Oct 16, 2020
1 parent 5f4d449 commit 5745318
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 34 deletions.
2 changes: 0 additions & 2 deletions lib/rubocop.rb
Expand Up @@ -471,8 +471,6 @@
require_relative 'rubocop/cop/style/redundant_file_extension_in_require'
require_relative 'rubocop/cop/style/redundant_self_assignment'
require_relative 'rubocop/cop/style/sole_nested_conditional'
require_relative 'rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses'
require_relative 'rubocop/cop/style/method_call_with_args_parentheses/require_parentheses'
require_relative 'rubocop/cop/style/method_called_on_do_end_block'
require_relative 'rubocop/cop/style/method_def_parentheses'
require_relative 'rubocop/cop/style/min_max'
Expand Down
20 changes: 10 additions & 10 deletions lib/rubocop/cop/style/method_call_with_args_parentheses.rb
Expand Up @@ -144,22 +144,22 @@ module Style
# # good
# Array 1
class MethodCallWithArgsParentheses < Base
require_relative 'method_call_with_args_parentheses/omit_parentheses'
require_relative 'method_call_with_args_parentheses/require_parentheses'

include ConfigurableEnforcedStyle
include IgnoredMethods
include IgnoredPattern
include RequireParentheses
include OmitParentheses
extend AutoCorrector

def initialize(*)
super
return unless style_configured?

case style
when :require_parentheses
extend RequireParentheses
when :omit_parentheses
extend OmitParentheses
end
def on_send(node)
send(style, node) # call require_parentheses or omit_parentheses
end
alias on_csend on_send
alias on_super on_send
alias on_yield on_send

private

Expand Down
Expand Up @@ -7,15 +7,19 @@ class MethodCallWithArgsParentheses
# Style omit_parentheses
module OmitParentheses
TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
OMIT_MSG = 'Omit parentheses for method calls with arguments.'
private_constant :OMIT_MSG

def on_send(node)
private

def omit_parentheses(node)
return unless node.parenthesized?
return if node.implicit_call?
return if super_call_without_arguments?(node)
return if allowed_camel_case_method_call?(node)
return if legitimate_call_with_parentheses?(node)

add_offense(offense_range(node)) do |corrector|
add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
if parentheses_at_the_end_of_multiline_call?(node)
corrector.replace(args_begin(node), ' \\')
else
Expand All @@ -24,20 +28,11 @@ def on_send(node)
corrector.remove(node.loc.end)
end
end
alias on_csend on_send
alias on_super on_send
alias on_yield on_send

private

def offense_range(node)
node.loc.begin.join(node.loc.end)
end

def message(_range = nil)
'Omit parentheses for method calls with arguments.'
end

def super_call_without_arguments?(node)
node.super_type? && node.arguments.none?
end
Expand Down
Expand Up @@ -6,27 +6,23 @@ module Style
class MethodCallWithArgsParentheses
# Style require_parentheses
module RequireParentheses
def on_send(node)
REQUIRE_MSG = 'Use parentheses for method calls with arguments.'
private_constant :REQUIRE_MSG

private

def require_parentheses(node)
return if ignored_method?(node.method_name)
return if matches_ignored_pattern?(node.method_name)
return if eligible_for_parentheses_omission?(node)
return unless node.arguments? && !node.parenthesized?

add_offense(node) do |corrector|
add_offense(node, message: REQUIRE_MSG) do |corrector|
corrector.replace(args_begin(node), '(')

corrector.insert_after(args_end(node), ')') unless args_parenthesized?(node)
end
end
alias on_csend on_send
alias on_super on_send
alias on_yield on_send

def message(_node = nil)
'Use parentheses for method calls with arguments.'
end

private

def eligible_for_parentheses_omission?(node)
node.operator_method? || node.setter_method? || ignored_macro?(node)
Expand Down

0 comments on commit 5745318

Please sign in to comment.