rubocop 0.78.0 → 0.82.0
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 +4 -4
- data/config/default.yml +158 -48
- data/lib/rubocop.rb +19 -4
- data/lib/rubocop/ast/builder.rb +45 -42
- data/lib/rubocop/ast/node.rb +12 -19
- data/lib/rubocop/ast/node/array_node.rb +13 -0
- data/lib/rubocop/ast/node/block_node.rb +5 -1
- data/lib/rubocop/ast/node/case_match_node.rb +56 -0
- data/lib/rubocop/ast/node/def_node.rb +11 -0
- data/lib/rubocop/ast/node/forward_args_node.rb +18 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +8 -0
- data/lib/rubocop/ast/node/regexp_node.rb +2 -4
- data/lib/rubocop/ast/traversal.rb +29 -10
- data/lib/rubocop/cli.rb +10 -4
- data/lib/rubocop/cli/command/show_cops.rb +11 -4
- data/lib/rubocop/comment_config.rb +6 -1
- data/lib/rubocop/config.rb +36 -10
- data/lib/rubocop/config_loader.rb +42 -33
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/config_obsoletion.rb +4 -1
- data/lib/rubocop/config_validator.rb +66 -92
- data/lib/rubocop/cop/autocorrect_logic.rb +6 -3
- data/lib/rubocop/cop/badge.rb +5 -5
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
- data/lib/rubocop/cop/corrector.rb +48 -24
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/generator.rb +3 -4
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
- data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -1
- data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +16 -10
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +8 -4
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
- data/lib/rubocop/cop/layout/leading_comment_space.rb +34 -3
- data/lib/rubocop/cop/layout/line_length.rb +32 -3
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +15 -6
- data/lib/rubocop/cop/layout/multiline_hash_brace_layout.rb +0 -4
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +133 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +19 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +8 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -9
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
- data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
- data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/loop.rb +6 -4
- data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +2 -2
- data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +12 -7
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
- data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +12 -22
- data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
- data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
- data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +4 -0
- data/lib/rubocop/cop/migration/department_name.rb +47 -6
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +4 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +7 -7
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +171 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +4 -3
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -10
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +2 -1
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +30 -0
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
- data/lib/rubocop/cop/registry.rb +15 -3
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
- data/lib/rubocop/cop/style/alias.rb +4 -4
- data/lib/rubocop/cop/style/and_or.rb +5 -6
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +60 -1
- data/lib/rubocop/cop/style/case_equality.rb +24 -1
- data/lib/rubocop/cop/style/character_literal.rb +2 -2
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +8 -8
- data/lib/rubocop/cop/style/copyright.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
- data/lib/rubocop/cop/style/documentation.rb +43 -5
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +3 -3
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/end_block.rb +6 -0
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -11
- data/lib/rubocop/cop/style/hash_each_methods.rb +89 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -5
- data/lib/rubocop/cop/style/hash_transform_keys.rb +83 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +80 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
- data/lib/rubocop/cop/style/lambda.rb +3 -2
- data/lib/rubocop/cop/style/lambda_call.rb +2 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +7 -205
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +169 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +54 -0
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/module_function.rb +58 -12
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +5 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +5 -4
- data/lib/rubocop/cop/style/one_line_conditional.rb +4 -3
- data/lib/rubocop/cop/style/or_assignment.rb +4 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +7 -7
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
- data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_return.rb +5 -7
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/return_nil.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -1
- data/lib/rubocop/cop/style/self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
- data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +3 -3
- data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -22
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unpack_first.rb +0 -4
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +16 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
- data/lib/rubocop/cop/variable_force.rb +4 -1
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +74 -0
- data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/node_pattern.rb +96 -10
- data/lib/rubocop/options.rb +7 -1
- data/lib/rubocop/processed_source.rb +1 -4
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +1 -1
- data/lib/rubocop/rspec/shared_contexts.rb +5 -4
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/target_ruby.rb +151 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +39 -12
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +0 -209
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
@@ -60,6 +60,10 @@ module RuboCop
|
|
60
60
|
alias conflicting_styles_detected no_acceptable_style!
|
61
61
|
alias unrecognized_style_detected no_acceptable_style!
|
62
62
|
|
63
|
+
def style_configured?
|
64
|
+
cop_config.key?(style_parameter_name)
|
65
|
+
end
|
66
|
+
|
63
67
|
def style
|
64
68
|
@style ||= begin
|
65
69
|
s = cop_config[style_parameter_name].to_sym
|
@@ -20,7 +20,7 @@ module RuboCop
|
|
20
20
|
return if ignored_node?(node)
|
21
21
|
|
22
22
|
end_loc = node.loc.end
|
23
|
-
return
|
23
|
+
return if accept_end_kw_alignment?(end_loc)
|
24
24
|
|
25
25
|
matching = matching_ranges(end_loc, align_ranges)
|
26
26
|
|
@@ -49,6 +49,11 @@ module RuboCop
|
|
49
49
|
add_offense(node, location: end_loc, message: msg)
|
50
50
|
end
|
51
51
|
|
52
|
+
def accept_end_kw_alignment?(end_loc)
|
53
|
+
end_loc.nil? || # Discard modifier forms of if/while/until.
|
54
|
+
processed_source.lines[end_loc.line - 1] !~ /\A[ \t]*end/
|
55
|
+
end
|
56
|
+
|
52
57
|
def style_parameter_name
|
53
58
|
'EnforcedStyleAlignWith'
|
54
59
|
end
|
@@ -39,15 +39,15 @@ module RuboCop
|
|
39
39
|
end
|
40
40
|
end
|
41
41
|
|
42
|
-
def
|
43
|
-
|
44
|
-
|
45
|
-
comments.each_with_object([]) do |comment, leading_comments|
|
46
|
-
next if comment.loc.line > 3
|
47
|
-
|
48
|
-
leading_comments << comment.text
|
42
|
+
def frozen_string_literal_specified?
|
43
|
+
leading_comment_lines.any? do |line|
|
44
|
+
MagicComment.parse(line).frozen_string_literal_specified?
|
49
45
|
end
|
50
46
|
end
|
47
|
+
|
48
|
+
def leading_comment_lines
|
49
|
+
processed_source.comments.first(3).map(&:text)
|
50
|
+
end
|
51
51
|
end
|
52
52
|
end
|
53
53
|
end
|
@@ -0,0 +1,171 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# Common functionality for Style/HashTransformKeys and
|
6
|
+
# Style/HashTransformValues
|
7
|
+
module HashTransformMethod
|
8
|
+
def on_block(node)
|
9
|
+
on_bad_each_with_object(node) do |*match|
|
10
|
+
handle_possible_offense(node, match, 'each_with_object')
|
11
|
+
end
|
12
|
+
end
|
13
|
+
|
14
|
+
def on_send(node)
|
15
|
+
on_bad_hash_brackets_map(node) do |*match|
|
16
|
+
handle_possible_offense(node, match, 'Hash[_.map {...}]')
|
17
|
+
end
|
18
|
+
on_bad_map_to_h(node) do |*match|
|
19
|
+
handle_possible_offense(node, match, 'map {...}.to_h')
|
20
|
+
end
|
21
|
+
end
|
22
|
+
|
23
|
+
def on_csend(node)
|
24
|
+
on_bad_map_to_h(node) do |*match|
|
25
|
+
handle_possible_offense(node, match, 'map {...}.to_h')
|
26
|
+
end
|
27
|
+
end
|
28
|
+
|
29
|
+
def autocorrect(node)
|
30
|
+
lambda do |corrector|
|
31
|
+
correction = prepare_correction(node)
|
32
|
+
execute_correction(corrector, node, correction)
|
33
|
+
end
|
34
|
+
end
|
35
|
+
|
36
|
+
private
|
37
|
+
|
38
|
+
# @abstract Implemented with `def_node_matcher`
|
39
|
+
def on_bad_each_with_object(_node)
|
40
|
+
raise NotImplementedError
|
41
|
+
end
|
42
|
+
|
43
|
+
# @abstract Implemented with `def_node_matcher`
|
44
|
+
def on_bad_hash_brackets_map(_node)
|
45
|
+
raise NotImplementedError
|
46
|
+
end
|
47
|
+
|
48
|
+
# @abstract Implemented with `def_node_matcher`
|
49
|
+
def on_bad_map_to_h(_node)
|
50
|
+
raise NotImplementedError
|
51
|
+
end
|
52
|
+
|
53
|
+
def handle_possible_offense(node, match, match_desc)
|
54
|
+
captures = extract_captures(match)
|
55
|
+
|
56
|
+
# If key didn't actually change either, this is most likely a false
|
57
|
+
# positive (receiver isn't a hash).
|
58
|
+
return if captures.noop_transformation?
|
59
|
+
|
60
|
+
# Can't `transform_keys` if key transformation uses value, or
|
61
|
+
# `transform_values` if value transformation uses key.
|
62
|
+
return if captures.transformation_uses_both_args?
|
63
|
+
|
64
|
+
add_offense(
|
65
|
+
node,
|
66
|
+
message: "Prefer `#{new_method_name}` over `#{match_desc}`."
|
67
|
+
)
|
68
|
+
end
|
69
|
+
|
70
|
+
# @abstract
|
71
|
+
#
|
72
|
+
# @return [Captures]
|
73
|
+
def extract_captures(_match)
|
74
|
+
raise NotImplementedError
|
75
|
+
end
|
76
|
+
|
77
|
+
# @abstract
|
78
|
+
#
|
79
|
+
# @return [String]
|
80
|
+
def new_method_name
|
81
|
+
raise NotImplementedError
|
82
|
+
end
|
83
|
+
|
84
|
+
def prepare_correction(node)
|
85
|
+
if (match = on_bad_each_with_object(node))
|
86
|
+
Autocorrection.from_each_with_object(node, match)
|
87
|
+
elsif (match = on_bad_hash_brackets_map(node))
|
88
|
+
Autocorrection.from_hash_brackets_map(node, match)
|
89
|
+
elsif (match = on_bad_map_to_h(node))
|
90
|
+
Autocorrection.from_map_to_h(node, match)
|
91
|
+
else
|
92
|
+
raise 'unreachable'
|
93
|
+
end
|
94
|
+
end
|
95
|
+
|
96
|
+
def execute_correction(corrector, node, correction)
|
97
|
+
correction.strip_prefix_and_suffix(node, corrector)
|
98
|
+
correction.set_new_method_name(new_method_name, corrector)
|
99
|
+
|
100
|
+
captures = extract_captures(correction.match)
|
101
|
+
correction.set_new_arg_name(captures.transformed_argname, corrector)
|
102
|
+
correction.set_new_body_expression(
|
103
|
+
captures.transforming_body_expr,
|
104
|
+
corrector
|
105
|
+
)
|
106
|
+
end
|
107
|
+
|
108
|
+
# Internal helper class to hold match data
|
109
|
+
Captures = Struct.new(
|
110
|
+
:transformed_argname,
|
111
|
+
:transforming_body_expr,
|
112
|
+
:unchanged_body_expr
|
113
|
+
) do
|
114
|
+
def noop_transformation?
|
115
|
+
transforming_body_expr.lvar_type? &&
|
116
|
+
transforming_body_expr.children == [transformed_argname]
|
117
|
+
end
|
118
|
+
|
119
|
+
def transformation_uses_both_args?
|
120
|
+
transforming_body_expr.descendants.include?(unchanged_body_expr)
|
121
|
+
end
|
122
|
+
end
|
123
|
+
|
124
|
+
# Internal helper class to hold autocorrect data
|
125
|
+
Autocorrection = Struct.new(:match, :block_node, :leading, :trailing) do # rubocop:disable Metrics/BlockLength
|
126
|
+
def self.from_each_with_object(node, match)
|
127
|
+
new(match, node, 0, 0)
|
128
|
+
end
|
129
|
+
|
130
|
+
def self.from_hash_brackets_map(node, match)
|
131
|
+
new(match, node.children.last, 'Hash['.length, ']'.length)
|
132
|
+
end
|
133
|
+
|
134
|
+
def self.from_map_to_h(node, match)
|
135
|
+
strip_trailing_chars = node.parent&.block_type? ? 0 : '.to_h'.length
|
136
|
+
new(match, node.children.first, 0, strip_trailing_chars)
|
137
|
+
end
|
138
|
+
|
139
|
+
def strip_prefix_and_suffix(node, corrector)
|
140
|
+
expression = node.loc.expression
|
141
|
+
corrector.remove_leading(expression, leading)
|
142
|
+
corrector.remove_trailing(expression, trailing)
|
143
|
+
end
|
144
|
+
|
145
|
+
def set_new_method_name(new_method_name, corrector)
|
146
|
+
range = block_node.send_node.loc.selector
|
147
|
+
if (send_end = block_node.send_node.loc.end)
|
148
|
+
# If there are arguments (only true in the `each_with_object`
|
149
|
+
# case)
|
150
|
+
range = range.begin.join(send_end)
|
151
|
+
end
|
152
|
+
corrector.replace(range, new_method_name)
|
153
|
+
end
|
154
|
+
|
155
|
+
def set_new_arg_name(transformed_argname, corrector)
|
156
|
+
corrector.replace(
|
157
|
+
block_node.arguments.loc.expression,
|
158
|
+
"|#{transformed_argname}|"
|
159
|
+
)
|
160
|
+
end
|
161
|
+
|
162
|
+
def set_new_body_expression(transforming_body_expr, corrector)
|
163
|
+
corrector.replace(
|
164
|
+
block_node.body,
|
165
|
+
transforming_body_expr.loc.expression.source
|
166
|
+
)
|
167
|
+
end
|
168
|
+
end
|
169
|
+
end
|
170
|
+
end
|
171
|
+
end
|
@@ -5,15 +5,20 @@ module RuboCop
|
|
5
5
|
# This module handles measurement and reporting of complexity in methods.
|
6
6
|
module MethodComplexity
|
7
7
|
include ConfigurableMax
|
8
|
+
include IgnoredMethods
|
8
9
|
extend NodePattern::Macros
|
9
10
|
|
10
11
|
def on_def(node)
|
12
|
+
return if ignored_method?(node.method_name)
|
13
|
+
|
11
14
|
check_complexity(node, node.method_name)
|
12
15
|
end
|
13
16
|
alias on_defs on_def
|
14
17
|
|
15
18
|
def on_block(node)
|
16
19
|
define_method?(node) do |name|
|
20
|
+
return if ignored_method?(name)
|
21
|
+
|
17
22
|
check_complexity(node, name)
|
18
23
|
end
|
19
24
|
end
|
@@ -58,12 +58,13 @@ module RuboCop
|
|
58
58
|
end
|
59
59
|
|
60
60
|
def indentation_multiplier
|
61
|
-
return 1 if config.for_cop('Layout/
|
61
|
+
return 1 if config.for_cop('Layout/IndentationStyle')['Enabled']
|
62
62
|
|
63
63
|
default_configuration = RuboCop::ConfigLoader.default_configuration
|
64
|
-
config.for_cop('Layout/
|
64
|
+
config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
|
65
65
|
config.for_cop('Layout/IndentationWidth')['Width'] ||
|
66
|
-
default_configuration
|
66
|
+
default_configuration
|
67
|
+
.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
|
67
68
|
default_configuration.for_cop('Layout/IndentationWidth')['Width']
|
68
69
|
end
|
69
70
|
end
|
@@ -23,7 +23,7 @@ module RuboCop
|
|
23
23
|
if comma_offset && !inside_comment?(after_last_item, comma_offset)
|
24
24
|
check_comma(node, kind, after_last_item.begin_pos + comma_offset)
|
25
25
|
elsif should_have_comma?(style, node)
|
26
|
-
put_comma(
|
26
|
+
put_comma(items, kind)
|
27
27
|
end
|
28
28
|
end
|
29
29
|
|
@@ -135,7 +135,7 @@ module RuboCop
|
|
135
135
|
|
136
136
|
def avoid_comma(kind, comma_begin_pos, extra_info)
|
137
137
|
range = range_between(comma_begin_pos, comma_begin_pos + 1)
|
138
|
-
article =
|
138
|
+
article = /array/.match?(kind) ? 'an' : 'a'
|
139
139
|
msg = format(
|
140
140
|
MSG,
|
141
141
|
command: 'Avoid',
|
@@ -145,9 +145,7 @@ module RuboCop
|
|
145
145
|
add_offense(range, location: range, message: msg)
|
146
146
|
end
|
147
147
|
|
148
|
-
def put_comma(
|
149
|
-
return if avoid_autocorrect?(elements(node))
|
150
|
-
|
148
|
+
def put_comma(items, kind)
|
151
149
|
last_item = items.last
|
152
150
|
return if last_item.block_pass_type?
|
153
151
|
|
@@ -169,11 +167,6 @@ module RuboCop
|
|
169
167
|
range_between(expr.begin_pos + ix, expr.end_pos)
|
170
168
|
end
|
171
169
|
|
172
|
-
# By default, there's no reason to avoid auto-correct.
|
173
|
-
def avoid_autocorrect?(_nodes)
|
174
|
-
false
|
175
|
-
end
|
176
|
-
|
177
170
|
def any_heredoc?(items)
|
178
171
|
items.any? { |item| heredoc?(item) }
|
179
172
|
end
|
@@ -45,8 +45,9 @@ module RuboCop
|
|
45
45
|
# SomeClass = Class.new(...)
|
46
46
|
# SomeClass = Struct.new(...)
|
47
47
|
return if allowed_assignment?(value)
|
48
|
+
return if SNAKE_CASE.match?(const_name)
|
48
49
|
|
49
|
-
add_offense(node, location: :name)
|
50
|
+
add_offense(node, location: :name)
|
50
51
|
end
|
51
52
|
|
52
53
|
private
|
@@ -39,7 +39,7 @@ module RuboCop
|
|
39
39
|
def meaningful_delimiters?(node)
|
40
40
|
delimiters = delimiter_string(node)
|
41
41
|
|
42
|
-
return false unless
|
42
|
+
return false unless /\w/.match?(delimiters)
|
43
43
|
|
44
44
|
forbidden_delimiters.none? do |forbidden_delimiter|
|
45
45
|
delimiters =~ Regexp.new(forbidden_delimiter)
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
# directive. It can be configured to allow for memoized instance variables
|
11
11
|
# prefixed with an underscore. Prefixing ivars with an underscore is a
|
12
12
|
# convention that is used to implicitly indicate that an ivar should not
|
13
|
-
# be set or
|
13
|
+
# be set or referenced outside of the memoization method.
|
14
14
|
#
|
15
15
|
# @example EnforcedStyleForLeadingUnderscores: disallowed (default)
|
16
16
|
# # bad
|
@@ -31,9 +31,28 @@ module RuboCop
|
|
31
31
|
class MethodName < Cop
|
32
32
|
include ConfigurableNaming
|
33
33
|
include IgnoredPattern
|
34
|
+
include RangeHelp
|
34
35
|
|
35
36
|
MSG = 'Use %<style>s for method names.'
|
36
37
|
|
38
|
+
def_node_matcher :attr?, <<~PATTERN
|
39
|
+
(send nil? ${:attr_reader :attr_writer :attr_accessor :attr} $...)
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def_node_matcher :sym_name, '(sym $_name)'
|
43
|
+
def_node_matcher :str_name, '(str $_name)'
|
44
|
+
|
45
|
+
def on_send(node)
|
46
|
+
return unless (attrs = attr?(node))
|
47
|
+
|
48
|
+
attrs.last.each do |name_item|
|
49
|
+
name = attr_name(name_item)
|
50
|
+
next if !name || matches_ignored_pattern?(name)
|
51
|
+
|
52
|
+
check_name(node, name, range_position(node))
|
53
|
+
end
|
54
|
+
end
|
55
|
+
|
37
56
|
def on_def(node)
|
38
57
|
return if node.operator_method? ||
|
39
58
|
matches_ignored_pattern?(node.method_name)
|
@@ -44,6 +63,17 @@ module RuboCop
|
|
44
63
|
|
45
64
|
private
|
46
65
|
|
66
|
+
def attr_name(name_item)
|
67
|
+
sym_name(name_item) || str_name(name_item)
|
68
|
+
end
|
69
|
+
|
70
|
+
def range_position(node)
|
71
|
+
selector_end_pos = node.loc.selector.end_pos + 1
|
72
|
+
expr_end_pos = node.loc.expression.end_pos
|
73
|
+
|
74
|
+
range_between(selector_end_pos, expr_end_pos)
|
75
|
+
end
|
76
|
+
|
47
77
|
def message(style)
|
48
78
|
format(MSG, style: style)
|
49
79
|
end
|
data/lib/rubocop/cop/registry.rb
CHANGED
@@ -22,12 +22,13 @@ module RuboCop
|
|
22
22
|
|
23
23
|
# Registry that tracks all cops by their badge and department.
|
24
24
|
class Registry
|
25
|
-
def initialize(cops = [])
|
25
|
+
def initialize(cops = [], options = {})
|
26
26
|
@registry = {}
|
27
27
|
@departments = {}
|
28
28
|
@cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }
|
29
29
|
|
30
30
|
cops.each { |cop| enlist(cop) }
|
31
|
+
@options = options
|
31
32
|
end
|
32
33
|
|
33
34
|
def enlist(cop)
|
@@ -146,13 +147,24 @@ module RuboCop
|
|
146
147
|
|
147
148
|
def enabled?(cop, config, only_safe)
|
148
149
|
cfg = config.for_cop(cop)
|
150
|
+
|
151
|
+
cop_enabled = cfg.fetch('Enabled') == true ||
|
152
|
+
enabled_pending_cop?(cfg, config)
|
153
|
+
|
149
154
|
if only_safe
|
150
|
-
|
155
|
+
cop_enabled && cfg.fetch('Safe', true)
|
151
156
|
else
|
152
|
-
|
157
|
+
cop_enabled
|
153
158
|
end
|
154
159
|
end
|
155
160
|
|
161
|
+
def enabled_pending_cop?(cop_cfg, config)
|
162
|
+
return false if @options[:disable_pending_cops]
|
163
|
+
|
164
|
+
cop_cfg.fetch('Enabled') == 'pending' &&
|
165
|
+
(@options[:enable_pending_cops] || config.enabled_new_cops?)
|
166
|
+
end
|
167
|
+
|
156
168
|
def names
|
157
169
|
cops.map(&:cop_name)
|
158
170
|
end
|