rubocop 1.41.1 → 1.45.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +2 -2
- data/config/default.yml +97 -31
- data/lib/rubocop/cli.rb +55 -9
- data/lib/rubocop/config.rb +7 -7
- data/lib/rubocop/config_loader.rb +12 -15
- data/lib/rubocop/config_loader_resolver.rb +8 -5
- data/lib/rubocop/cop/base.rb +89 -70
- data/lib/rubocop/cop/commissioner.rb +8 -2
- data/lib/rubocop/cop/cop.rb +51 -31
- data/lib/rubocop/cop/corrector.rb +30 -10
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +1 -6
- data/lib/rubocop/cop/gemspec/dependency_version.rb +16 -18
- data/lib/rubocop/cop/gemspec/development_dependencies.rb +107 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +11 -3
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/block_end_newline.rb +7 -1
- data/lib/rubocop/cop/layout/class_structure.rb +31 -23
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +2 -6
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -1
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +6 -9
- data/lib/rubocop/cop/layout/indentation_style.rb +4 -1
- data/lib/rubocop/cop/layout/line_continuation_spacing.rb +6 -6
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +11 -13
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +4 -4
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +5 -4
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +5 -2
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +4 -0
- data/lib/rubocop/cop/lint/debugger.rb +8 -27
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +62 -112
- data/lib/rubocop/cop/lint/else_layout.rb +2 -6
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -7
- data/lib/rubocop/cop/lint/heredoc_method_call_position.rb +15 -17
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +1 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +8 -5
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +19 -0
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +11 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +3 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +7 -4
- data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -3
- data/lib/rubocop/cop/lint/useless_rescue.rb +85 -0
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +14 -4
- data/lib/rubocop/cop/lint/void.rb +19 -10
- data/lib/rubocop/cop/metrics/block_length.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +1 -1
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +27 -0
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +3 -6
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/allowed_methods.rb +3 -1
- data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +5 -3
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +57 -23
- data/lib/rubocop/cop/mixin/line_length_help.rb +3 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -0
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +4 -0
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/registry.rb +34 -29
- data/lib/rubocop/cop/security/compound_hash.rb +2 -1
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -11
- data/lib/rubocop/cop/style/arguments_forwarding.rb +1 -0
- data/lib/rubocop/cop/style/block_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +8 -2
- data/lib/rubocop/cop/style/class_and_module_children.rb +3 -10
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/comparable_clamp.rb +125 -0
- data/lib/rubocop/cop/style/concat_array_literals.rb +22 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +0 -6
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/documentation_method.rb +6 -0
- data/lib/rubocop/cop/style/guard_clause.rb +11 -7
- data/lib/rubocop/cop/style/hash_each_methods.rb +13 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +11 -7
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +15 -0
- data/lib/rubocop/cop/style/infinite_loop.rb +2 -5
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +114 -0
- data/lib/rubocop/cop/style/map_to_set.rb +61 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +23 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +2 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +11 -4
- data/lib/rubocop/cop/style/min_max_comparison.rb +83 -0
- data/lib/rubocop/cop/style/missing_else.rb +13 -1
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +0 -4
- data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +18 -3
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +1 -5
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +11 -3
- data/lib/rubocop/cop/style/one_line_conditional.rb +3 -6
- data/lib/rubocop/cop/style/operator_method_call.rb +16 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +16 -1
- data/lib/rubocop/cop/style/redundant_conditional.rb +0 -4
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +16 -10
- data/lib/rubocop/cop/style/redundant_heredoc_delimiter_quotes.rb +58 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +2 -1
- data/lib/rubocop/cop/style/redundant_string_escape.rb +4 -2
- data/lib/rubocop/cop/style/require_order.rb +6 -11
- data/lib/rubocop/cop/style/select_by_regexp.rb +6 -2
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +24 -2
- data/lib/rubocop/cop/style/signal_exception.rb +8 -6
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -1
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +4 -4
- data/lib/rubocop/cop/style/word_array.rb +42 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +12 -5
- data/lib/rubocop/cop/style/yoda_expression.rb +90 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +31 -14
- data/lib/rubocop/cop/team.rb +48 -43
- data/lib/rubocop/cop/variable_force/scope.rb +3 -3
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -1
- data/lib/rubocop/cop/variable_force.rb +1 -4
- data/lib/rubocop/formatter.rb +0 -1
- data/lib/rubocop/options.rb +22 -1
- data/lib/rubocop/path_util.rb +17 -7
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +6 -4
- data/lib/rubocop/runner.rb +50 -7
- data/lib/rubocop/server/cache.rb +10 -3
- data/lib/rubocop/server/cli.rb +37 -18
- data/lib/rubocop/server/client_command/exec.rb +1 -1
- data/lib/rubocop/server/client_command/start.rb +6 -1
- data/lib/rubocop/server/core.rb +23 -8
- data/lib/rubocop/target_ruby.rb +0 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +8 -0
- metadata +21 -33
@@ -46,19 +46,23 @@ module RuboCop
|
|
46
46
|
LIT_MSG = 'Literal `%<lit>s` used in void context.'
|
47
47
|
SELF_MSG = '`self` used in void context.'
|
48
48
|
EXPRESSION_MSG = '`%<expression>s` used in void context.'
|
49
|
-
NONMUTATING_MSG = 'Method `#%<method>s` used in void context. Did you mean `#%<
|
49
|
+
NONMUTATING_MSG = 'Method `#%<method>s` used in void context. Did you mean `#%<suggest>s`?'
|
50
50
|
|
51
51
|
BINARY_OPERATORS = %i[* / % + - == === != < > <= >= <=>].freeze
|
52
52
|
UNARY_OPERATORS = %i[+@ -@ ~ !].freeze
|
53
53
|
OPERATORS = (BINARY_OPERATORS + UNARY_OPERATORS).freeze
|
54
54
|
VOID_CONTEXT_TYPES = %i[def for block].freeze
|
55
|
-
|
56
|
-
|
57
|
-
|
58
|
-
|
59
|
-
|
60
|
-
|
61
|
-
|
55
|
+
NONMUTATING_METHODS_WITH_BANG_VERSION = %i[capitalize chomp chop compact
|
56
|
+
delete_prefix delete_suffix downcase
|
57
|
+
encode flatten gsub lstrip merge next
|
58
|
+
reject reverse rotate rstrip scrub select
|
59
|
+
shuffle slice sort sort_by squeeze strip sub
|
60
|
+
succ swapcase tr tr_s transform_values
|
61
|
+
unicode_normalize uniq upcase].freeze
|
62
|
+
METHODS_REPLACABLE_BY_EACH = %i[collect map].freeze
|
63
|
+
|
64
|
+
NONMUTATING_METHODS = (NONMUTATING_METHODS_WITH_BANG_VERSION +
|
65
|
+
METHODS_REPLACABLE_BY_EACH).freeze
|
62
66
|
|
63
67
|
def on_block(node)
|
64
68
|
return unless node.body && !node.body.begin_type?
|
@@ -124,9 +128,14 @@ module RuboCop
|
|
124
128
|
end
|
125
129
|
|
126
130
|
def check_nonmutating(node)
|
127
|
-
return unless node.
|
131
|
+
return unless node.respond_to?(:method_name)
|
128
132
|
|
129
|
-
|
133
|
+
method_name = node.method_name
|
134
|
+
return unless NONMUTATING_METHODS.include?(method_name)
|
135
|
+
|
136
|
+
suggestion = METHODS_REPLACABLE_BY_EACH.include?(method_name) ? 'each' : "#{method_name}!"
|
137
|
+
add_offense(node,
|
138
|
+
message: format(NONMUTATING_MSG, method: method_name, suggest: suggestion))
|
130
139
|
end
|
131
140
|
|
132
141
|
def in_void_context?(node)
|
@@ -51,7 +51,7 @@ module RuboCop
|
|
51
51
|
def on_block(node)
|
52
52
|
return if allowed_method?(node.method_name) || matches_allowed_pattern?(node.method_name)
|
53
53
|
return if method_receiver_excluded?(node)
|
54
|
-
return if node.class_constructor?
|
54
|
+
return if node.class_constructor?
|
55
55
|
|
56
56
|
check_code_length(node)
|
57
57
|
end
|
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
#
|
13
13
|
# The maximum level of nesting allowed is configurable.
|
14
14
|
class BlockNesting < Base
|
15
|
-
NESTING_BLOCKS = %i[case if while while_post until until_post for resbody].freeze
|
15
|
+
NESTING_BLOCKS = %i[case case_match if while while_post until until_post for resbody].freeze
|
16
16
|
|
17
17
|
exclude_limit 'Max'
|
18
18
|
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
35
35
|
|
36
36
|
MSG = 'Cyclomatic complexity for %<method>s is too high. [%<complexity>d/%<max>d]'
|
37
37
|
COUNTED_NODES = %i[if while until for csend block block_pass
|
38
|
-
rescue when and or or_asgn and_asgn].freeze
|
38
|
+
rescue when in_pattern and or or_asgn and_asgn].freeze
|
39
39
|
|
40
40
|
private
|
41
41
|
|
@@ -9,6 +9,20 @@ module RuboCop
|
|
9
9
|
# Keyword arguments can optionally be excluded from the total count,
|
10
10
|
# as they add less complexity than positional or optional parameters.
|
11
11
|
#
|
12
|
+
# Any number of arguments for `initialize` method inside a block of
|
13
|
+
# `Struct.new` and `Data.define` like this is always allowed:
|
14
|
+
#
|
15
|
+
# [source,ruby]
|
16
|
+
# ----
|
17
|
+
# Struct.new(:one, :two, :three, :four, :five, keyword_init: true) do
|
18
|
+
# def initialize(one:, two:, three:, four:, five:)
|
19
|
+
# end
|
20
|
+
# end
|
21
|
+
# ----
|
22
|
+
#
|
23
|
+
# This is because checking the number of arguments of the `initialize` method
|
24
|
+
# does not make sense.
|
25
|
+
#
|
12
26
|
# NOTE: Explicit block argument `&block` is not counted to prevent
|
13
27
|
# erroneous change that is avoided by making block argument implicit.
|
14
28
|
#
|
@@ -63,6 +77,16 @@ module RuboCop
|
|
63
77
|
NAMED_KEYWORD_TYPES = %i[kwoptarg kwarg].freeze
|
64
78
|
private_constant :NAMED_KEYWORD_TYPES
|
65
79
|
|
80
|
+
# @!method struct_new_or_data_define_block?(node)
|
81
|
+
def_node_matcher :struct_new_or_data_define_block?, <<~PATTERN
|
82
|
+
(block
|
83
|
+
{
|
84
|
+
(send (const {nil? cbase} :Struct) :new ...)
|
85
|
+
(send (const {nil? cbase} :Data) :define ...)
|
86
|
+
}
|
87
|
+
(args) ...)
|
88
|
+
PATTERN
|
89
|
+
|
66
90
|
def on_def(node)
|
67
91
|
optargs = node.arguments.select(&:optarg_type?)
|
68
92
|
return if optargs.count <= max_optional_parameters
|
@@ -78,6 +102,9 @@ module RuboCop
|
|
78
102
|
alias on_defs on_def
|
79
103
|
|
80
104
|
def on_args(node)
|
105
|
+
parent = node.parent
|
106
|
+
return if parent.method?(:initialize) && struct_new_or_data_define_block?(parent.parent)
|
107
|
+
|
81
108
|
count = args_count(node)
|
82
109
|
return unless count > max_params
|
83
110
|
|
@@ -25,15 +25,12 @@ module RuboCop
|
|
25
25
|
# > http://c2.com/cgi/wiki?AbcMetric
|
26
26
|
CONDITION_NODES = CyclomaticComplexity::COUNTED_NODES.freeze
|
27
27
|
|
28
|
+
private_constant :BRANCH_NODES, :CONDITION_NODES
|
29
|
+
|
28
30
|
def self.calculate(node, discount_repeated_attributes: false)
|
29
31
|
new(node, discount_repeated_attributes: discount_repeated_attributes).calculate
|
30
32
|
end
|
31
33
|
|
32
|
-
# TODO: move to rubocop-ast
|
33
|
-
ARGUMENT_TYPES = %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg].freeze
|
34
|
-
|
35
|
-
private_constant :BRANCH_NODES, :CONDITION_NODES, :ARGUMENT_TYPES
|
36
|
-
|
37
34
|
def initialize(node)
|
38
35
|
@assignment = 0
|
39
36
|
@branch = 0
|
@@ -129,7 +126,7 @@ module RuboCop
|
|
129
126
|
end
|
130
127
|
|
131
128
|
def argument?(node)
|
132
|
-
|
129
|
+
node.argument_type? && capturing_variable?(node.children.first)
|
133
130
|
end
|
134
131
|
|
135
132
|
def condition?(node)
|
@@ -12,7 +12,7 @@ module RuboCop
|
|
12
12
|
attr_reader :column_delta
|
13
13
|
|
14
14
|
def configured_indentation_width
|
15
|
-
cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width']
|
15
|
+
cop_config['IndentationWidth'] || config.for_cop('Layout/IndentationWidth')['Width'] || 2
|
16
16
|
end
|
17
17
|
|
18
18
|
def indentation(node)
|
@@ -3,7 +3,9 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
# This module encapsulates the ability to allow certain methods when
|
6
|
-
# parsing.
|
6
|
+
# parsing. Even if the code is in offense, if it contains methods
|
7
|
+
# that are allowed. This module is equivalent to the IgnoredMethods module,
|
8
|
+
# which will be deprecated in RuboCop 2.0.
|
7
9
|
module AllowedMethods
|
8
10
|
private
|
9
11
|
|
@@ -47,7 +47,7 @@ module RuboCop
|
|
47
47
|
match.captures
|
48
48
|
end
|
49
49
|
|
50
|
-
KEYWORDS_REGEX_CACHE = {} # rubocop:disable
|
50
|
+
KEYWORDS_REGEX_CACHE = {} # rubocop:disable Style/MutableConstant
|
51
51
|
private_constant :KEYWORDS_REGEX_CACHE
|
52
52
|
|
53
53
|
def regex
|
@@ -62,10 +62,12 @@ module RuboCop
|
|
62
62
|
# Returns the end line of a node, which might be a comment and not part of the AST
|
63
63
|
# End line is considered either the line at which another node starts, or
|
64
64
|
# the line at which the parent node ends.
|
65
|
-
# rubocop:disable Metrics/AbcSize
|
65
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
66
66
|
def find_end_line(node)
|
67
|
-
if node.if_type? && node.
|
67
|
+
if node.if_type? && node.else?
|
68
68
|
node.loc.else.line
|
69
|
+
elsif node.if_type? && node.ternary?
|
70
|
+
node.else_branch.loc.line
|
69
71
|
elsif (next_sibling = node.right_sibling)
|
70
72
|
next_sibling.loc.line
|
71
73
|
elsif (parent = node.parent)
|
@@ -74,7 +76,7 @@ module RuboCop
|
|
74
76
|
node.loc.end.line
|
75
77
|
end
|
76
78
|
end
|
77
|
-
# rubocop:enable Metrics/AbcSize
|
79
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
78
80
|
end
|
79
81
|
end
|
80
82
|
end
|
@@ -3,6 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
# This module checks for Ruby 3.1's hash value omission syntax.
|
6
|
+
# rubocop:disable Metrics/ModuleLength
|
6
7
|
module HashShorthandSyntax
|
7
8
|
OMIT_HASH_VALUE_MSG = 'Omit the hash value.'
|
8
9
|
EXPLICIT_HASH_VALUE_MSG = 'Include the hash value.'
|
@@ -45,21 +46,22 @@ module RuboCop
|
|
45
46
|
|
46
47
|
private
|
47
48
|
|
48
|
-
# rubocop:disable Metrics/AbcSize
|
49
|
-
def register_offense(node, message, replacement)
|
49
|
+
def register_offense(node, message, replacement) # rubocop:disable Metrics/AbcSize
|
50
50
|
add_offense(node.value, message: message) do |corrector|
|
51
51
|
if (def_node = def_node_that_require_parentheses(node))
|
52
|
-
|
52
|
+
last_argument = def_node.last_argument
|
53
|
+
if last_argument.nil? || !last_argument.hash_type?
|
54
|
+
next corrector.replace(node, replacement)
|
55
|
+
end
|
56
|
+
|
57
|
+
white_spaces = range_between(def_node.selector.end_pos,
|
53
58
|
def_node.first_argument.source_range.begin_pos)
|
54
59
|
corrector.replace(white_spaces, '(')
|
55
|
-
|
56
|
-
last_argument = def_node.arguments.last
|
57
60
|
corrector.insert_after(last_argument, ')') if node == last_argument.pairs.last
|
58
61
|
end
|
59
62
|
corrector.replace(node, replacement)
|
60
63
|
end
|
61
64
|
end
|
62
|
-
# rubocop:enable Metrics/AbcSize
|
63
65
|
|
64
66
|
def ignore_mixed_hash_shorthand_syntax?(hash_node)
|
65
67
|
target_ruby_version <= 3.0 || enforced_shorthand_syntax != 'consistent' ||
|
@@ -86,25 +88,37 @@ module RuboCop
|
|
86
88
|
end
|
87
89
|
|
88
90
|
def require_hash_value_for_around_hash_literal?(node)
|
89
|
-
return false unless (
|
91
|
+
return false unless (method_dispatch_node = find_ancestor_method_dispatch_node(node))
|
90
92
|
|
91
|
-
!node.parent.braces? &&
|
92
|
-
|
93
|
+
!node.parent.braces? &&
|
94
|
+
!use_element_of_hash_literal_as_receiver?(method_dispatch_node, node.parent) &&
|
95
|
+
use_modifier_form_without_parenthesized_method_call?(method_dispatch_node)
|
93
96
|
end
|
94
97
|
|
98
|
+
# rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
95
99
|
def def_node_that_require_parentheses(node)
|
96
|
-
|
97
|
-
return unless
|
100
|
+
last_pair = node.parent.pairs.last
|
101
|
+
return unless last_pair.key.source == last_pair.value.source
|
102
|
+
return unless (dispatch_node = find_ancestor_method_dispatch_node(node))
|
103
|
+
return if node.respond_to?(:parenthesized?) && !node.parenthesized?
|
104
|
+
return unless last_expression?(dispatch_node) || method_dispatch_as_argument?(dispatch_node)
|
98
105
|
|
99
|
-
def_node = node.each_ancestor(:send, :csend).first
|
106
|
+
def_node = node.each_ancestor(:send, :csend, :super, :yield).first
|
100
107
|
|
101
|
-
def_node unless def_node && def_node.arguments.empty?
|
108
|
+
DefNode.new(def_node) unless def_node && def_node.arguments.empty?
|
102
109
|
end
|
110
|
+
# rubocop:enable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
103
111
|
|
104
|
-
def
|
105
|
-
ancestor = node.parent.parent
|
112
|
+
def find_ancestor_method_dispatch_node(node)
|
113
|
+
return unless (ancestor = node.parent.parent)
|
114
|
+
return unless ancestor.call_type? || ancestor.super_type? || ancestor.yield_type?
|
115
|
+
return if brackets?(ancestor)
|
116
|
+
|
117
|
+
ancestor
|
118
|
+
end
|
106
119
|
|
107
|
-
|
120
|
+
def brackets?(method_dispatch_node)
|
121
|
+
method_dispatch_node.method?(:[]) || method_dispatch_node.method?(:[]=)
|
108
122
|
end
|
109
123
|
|
110
124
|
def use_element_of_hash_literal_as_receiver?(ancestor, parent)
|
@@ -118,15 +132,16 @@ module RuboCop
|
|
118
132
|
ancestor.ancestors.any? { |node| node.respond_to?(:modifier_form?) && node.modifier_form? }
|
119
133
|
end
|
120
134
|
|
121
|
-
def
|
122
|
-
|
135
|
+
def last_expression?(ancestor)
|
136
|
+
ancestor.right_sibling ||
|
137
|
+
ancestor.each_ancestor.find { |node| node.assignment? || node.send_type? }&.right_sibling
|
138
|
+
end
|
123
139
|
|
124
|
-
|
125
|
-
|
126
|
-
|
127
|
-
end&.right_sibling
|
140
|
+
def method_dispatch_as_argument?(method_dispatch_node)
|
141
|
+
parent = method_dispatch_node.parent
|
142
|
+
return false unless parent
|
128
143
|
|
129
|
-
|
144
|
+
parent.call_type? || parent.super_type? || parent.yield_type?
|
130
145
|
end
|
131
146
|
|
132
147
|
def breakdown_value_types_of_hash(hash_node)
|
@@ -182,6 +197,25 @@ module RuboCop
|
|
182
197
|
register_offense(pair_node, OMIT_HASH_VALUE_MSG, replacement)
|
183
198
|
end
|
184
199
|
end
|
200
|
+
|
201
|
+
DefNode = Struct.new(:node) do
|
202
|
+
def selector
|
203
|
+
if node.loc.respond_to?(:selector)
|
204
|
+
node.loc.selector
|
205
|
+
else
|
206
|
+
node.loc.keyword
|
207
|
+
end
|
208
|
+
end
|
209
|
+
|
210
|
+
def first_argument
|
211
|
+
node.first_argument
|
212
|
+
end
|
213
|
+
|
214
|
+
def last_argument
|
215
|
+
node.last_argument
|
216
|
+
end
|
217
|
+
end
|
185
218
|
end
|
186
219
|
end
|
220
|
+
# rubocop:enable Metrics/ModuleLength
|
187
221
|
end
|
@@ -4,6 +4,8 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
# Help methods for determining if a line is too long.
|
6
6
|
module LineLengthHelp
|
7
|
+
include Alignment
|
8
|
+
|
7
9
|
private
|
8
10
|
|
9
11
|
def ignore_cop_directives?
|
@@ -85,7 +87,7 @@ module RuboCop
|
|
85
87
|
|
86
88
|
def tab_indentation_width
|
87
89
|
config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
|
88
|
-
|
90
|
+
configured_indentation_width
|
89
91
|
end
|
90
92
|
|
91
93
|
def uri_regexp
|
@@ -175,7 +175,7 @@ module RuboCop
|
|
175
175
|
|
176
176
|
def remove_optarg_equals(asgn_tokens, processed_source)
|
177
177
|
optargs = processed_source.ast.each_node(:optarg)
|
178
|
-
optarg_eql = optargs.
|
178
|
+
optarg_eql = optargs.to_set { |o| o.loc.operator.begin_pos }
|
179
179
|
asgn_tokens.reject { |t| optarg_eql.include?(t.begin_pos) }
|
180
180
|
end
|
181
181
|
end
|
@@ -107,9 +107,9 @@ module RuboCop
|
|
107
107
|
end
|
108
108
|
end
|
109
109
|
|
110
|
-
def empty_brackets?(left_bracket_token, right_bracket_token)
|
111
|
-
left_index =
|
112
|
-
right_index =
|
110
|
+
def empty_brackets?(left_bracket_token, right_bracket_token, tokens: processed_source.tokens)
|
111
|
+
left_index = tokens.index(left_bracket_token)
|
112
|
+
right_index = tokens.index(right_bracket_token)
|
113
113
|
right_index && left_index == right_index - 1
|
114
114
|
end
|
115
115
|
|
@@ -132,7 +132,7 @@ module RuboCop
|
|
132
132
|
|
133
133
|
def avoid_comma(kind, comma_begin_pos, extra_info)
|
134
134
|
range = range_between(comma_begin_pos, comma_begin_pos + 1)
|
135
|
-
article =
|
135
|
+
article = kind.include?('array') ? 'an' : 'a'
|
136
136
|
msg = format(
|
137
137
|
MSG,
|
138
138
|
command: 'Avoid',
|
data/lib/rubocop/cop/registry.rb
CHANGED
@@ -19,6 +19,33 @@ module RuboCop
|
|
19
19
|
class Registry
|
20
20
|
include Enumerable
|
21
21
|
|
22
|
+
def self.all
|
23
|
+
global.without_department(:Test).cops
|
24
|
+
end
|
25
|
+
|
26
|
+
def self.qualified_cop_name(name, origin)
|
27
|
+
global.qualified_cop_name(name, origin)
|
28
|
+
end
|
29
|
+
|
30
|
+
# Changes momentarily the global registry
|
31
|
+
# Intended for testing purposes
|
32
|
+
def self.with_temporary_global(temp_global = global.dup)
|
33
|
+
previous = @global
|
34
|
+
@global = temp_global
|
35
|
+
yield
|
36
|
+
ensure
|
37
|
+
@global = previous
|
38
|
+
end
|
39
|
+
|
40
|
+
def self.reset!
|
41
|
+
@global = new
|
42
|
+
end
|
43
|
+
|
44
|
+
def self.qualified_cop?(name)
|
45
|
+
badge = Badge.parse(name)
|
46
|
+
global.qualify_badge(badge).first == badge
|
47
|
+
end
|
48
|
+
|
22
49
|
attr_reader :options
|
23
50
|
|
24
51
|
def initialize(cops = [], options = {})
|
@@ -136,6 +163,13 @@ module RuboCop
|
|
136
163
|
'RedundantCopDisableDirective'
|
137
164
|
end
|
138
165
|
|
166
|
+
def qualify_badge(badge)
|
167
|
+
clear_enrollment_queue
|
168
|
+
@departments
|
169
|
+
.map { |department, _| badge.with_department(department) }
|
170
|
+
.select { |potential_badge| registered?(potential_badge) }
|
171
|
+
end
|
172
|
+
|
139
173
|
# @return [Hash{String => Array<Class>}]
|
140
174
|
def to_h
|
141
175
|
clear_enrollment_queue
|
@@ -238,28 +272,6 @@ module RuboCop
|
|
238
272
|
attr_reader :global
|
239
273
|
end
|
240
274
|
|
241
|
-
def self.all
|
242
|
-
global.without_department(:Test).cops
|
243
|
-
end
|
244
|
-
|
245
|
-
def self.qualified_cop_name(name, origin)
|
246
|
-
global.qualified_cop_name(name, origin)
|
247
|
-
end
|
248
|
-
|
249
|
-
# Changes momentarily the global registry
|
250
|
-
# Intended for testing purposes
|
251
|
-
def self.with_temporary_global(temp_global = global.dup)
|
252
|
-
previous = @global
|
253
|
-
@global = temp_global
|
254
|
-
yield
|
255
|
-
ensure
|
256
|
-
@global = previous
|
257
|
-
end
|
258
|
-
|
259
|
-
def self.reset!
|
260
|
-
@global = new
|
261
|
-
end
|
262
|
-
|
263
275
|
private
|
264
276
|
|
265
277
|
def initialize_copy(reg)
|
@@ -282,13 +294,6 @@ module RuboCop
|
|
282
294
|
self.class.new(cops)
|
283
295
|
end
|
284
296
|
|
285
|
-
def qualify_badge(badge)
|
286
|
-
clear_enrollment_queue
|
287
|
-
@departments
|
288
|
-
.map { |department, _| badge.with_department(department) }
|
289
|
-
.select { |potential_badge| registered?(potential_badge) }
|
290
|
-
end
|
291
|
-
|
292
297
|
def resolve_badge(given_badge, real_badge, source_path)
|
293
298
|
unless given_badge.match?(real_badge)
|
294
299
|
path = PathUtil.smart_path(source_path)
|
@@ -9,7 +9,8 @@ module RuboCop
|
|
9
9
|
# Manually combining hashes is error prone and hard to follow, especially
|
10
10
|
# when there are many values. Poor implementations may also introduce
|
11
11
|
# performance or security concerns if they are prone to collisions.
|
12
|
-
# Delegating to `Array#hash` is clearer
|
12
|
+
# Delegating to `Array#hash` is clearer and safer, although it might be slower
|
13
|
+
# depending on the use case.
|
13
14
|
#
|
14
15
|
# @safety
|
15
16
|
# This cop may be unsafe if the application logic depends on the hash
|
@@ -115,9 +115,7 @@ module RuboCop
|
|
115
115
|
def_node = find_corresponding_def_node(node)
|
116
116
|
return unless def_node
|
117
117
|
|
118
|
-
|
119
|
-
remove_node(corrector, node)
|
120
|
-
insert_def(corrector, node, def_node.source)
|
118
|
+
replace_def(corrector, node, def_node)
|
121
119
|
when :inline
|
122
120
|
remove_node(corrector, node)
|
123
121
|
select_grouped_def_nodes(node).each do |grouped_def_node|
|
@@ -131,7 +129,8 @@ module RuboCop
|
|
131
129
|
end
|
132
130
|
|
133
131
|
def offense?(node)
|
134
|
-
(group_style? && access_modifier_is_inlined?(node)
|
132
|
+
(group_style? && access_modifier_is_inlined?(node) &&
|
133
|
+
!right_siblings_same_inline_method?(node)) ||
|
135
134
|
(inline_style? && access_modifier_is_not_inlined?(node))
|
136
135
|
end
|
137
136
|
|
@@ -151,6 +150,12 @@ module RuboCop
|
|
151
150
|
!access_modifier_is_inlined?(node)
|
152
151
|
end
|
153
152
|
|
153
|
+
def right_siblings_same_inline_method?(node)
|
154
|
+
node.right_siblings.any? do |sibling|
|
155
|
+
sibling.send_type? && sibling.method?(node.method_name) && !sibling.arguments.empty?
|
156
|
+
end
|
157
|
+
end
|
158
|
+
|
154
159
|
def message(range)
|
155
160
|
access_modifier = range.source
|
156
161
|
|
@@ -173,7 +178,9 @@ module RuboCop
|
|
173
178
|
end
|
174
179
|
|
175
180
|
def find_argument_less_modifier_node(node)
|
176
|
-
node.parent
|
181
|
+
return unless (parent = node.parent)
|
182
|
+
|
183
|
+
parent.each_child_node(:send).find do |child|
|
177
184
|
child.method?(node.method_name) && child.arguments.empty?
|
178
185
|
end
|
179
186
|
end
|
@@ -184,17 +191,21 @@ module RuboCop
|
|
184
191
|
end.select(&:def_type?)
|
185
192
|
end
|
186
193
|
|
187
|
-
def
|
188
|
-
source =
|
194
|
+
def replace_def(corrector, node, def_node)
|
195
|
+
source = def_source(node, def_node)
|
189
196
|
argument_less_modifier_node = find_argument_less_modifier_node(node)
|
190
197
|
if argument_less_modifier_node
|
191
198
|
corrector.insert_after(argument_less_modifier_node, "\n\n#{source}")
|
199
|
+
elsif (ancestor = node.each_ancestor(:block, :class, :module).first)
|
200
|
+
|
201
|
+
corrector.insert_before(ancestor.loc.end, "#{node.method_name}\n\n#{source}\n")
|
192
202
|
else
|
193
|
-
corrector.
|
194
|
-
|
195
|
-
"#{node.method_name}\n\n#{source}\n"
|
196
|
-
)
|
203
|
+
corrector.replace(node, "#{node.method_name}\n\n#{source}")
|
204
|
+
return
|
197
205
|
end
|
206
|
+
|
207
|
+
remove_node(corrector, def_node)
|
208
|
+
remove_node(corrector, node)
|
198
209
|
end
|
199
210
|
|
200
211
|
def insert_inline_modifier(corrector, node, modifier_name)
|
@@ -204,6 +215,10 @@ module RuboCop
|
|
204
215
|
def remove_node(corrector, node)
|
205
216
|
corrector.remove(range_with_comments_and_lines(node))
|
206
217
|
end
|
218
|
+
|
219
|
+
def def_source(node, def_node)
|
220
|
+
[*processed_source.ast_with_comments[node].map(&:text), def_node.source].join("\n")
|
221
|
+
end
|
207
222
|
end
|
208
223
|
end
|
209
224
|
end
|
@@ -84,6 +84,7 @@ module RuboCop
|
|
84
84
|
def on_def(node)
|
85
85
|
return unless node.body
|
86
86
|
return unless (rest_args_name, args = use_rest_arguments?(node.arguments))
|
87
|
+
return if args.any?(&:default?)
|
87
88
|
|
88
89
|
node.each_descendant(:send) do |send_node|
|
89
90
|
kwargs_name, block_name = extract_argument_names_from(args)
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
eq_begin, eq_end, contents = parts(comment)
|
33
33
|
|
34
34
|
corrector.remove(eq_begin)
|
35
|
-
unless contents.
|
35
|
+
unless contents.empty?
|
36
36
|
corrector.replace(
|
37
37
|
contents,
|
38
38
|
contents.source.gsub(/\A/, '# ').gsub(/\n\n/, "\n#\n").gsub(/\n(?=[^#])/, "\n# ")
|