rubocop 0.73.0 → 0.77.0
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +3 -2
- data/bin/console +1 -0
- data/config/default.yml +332 -295
- data/lib/rubocop.rb +46 -30
- data/lib/rubocop/ast/builder.rb +1 -0
- data/lib/rubocop/ast/node.rb +6 -8
- data/lib/rubocop/ast/node/block_node.rb +2 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +1 -12
- data/lib/rubocop/ast/node/return_node.rb +24 -0
- data/lib/rubocop/cli.rb +11 -227
- data/lib/rubocop/cli/command.rb +21 -0
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +105 -0
- data/lib/rubocop/cli/command/base.rb +33 -0
- data/lib/rubocop/cli/command/execute_runner.rb +76 -0
- data/lib/rubocop/cli/command/init_dotfile.rb +45 -0
- data/lib/rubocop/cli/command/show_cops.rb +73 -0
- data/lib/rubocop/cli/command/version.rb +17 -0
- data/lib/rubocop/cli/environment.rb +21 -0
- data/lib/rubocop/comment_config.rb +5 -4
- data/lib/rubocop/config.rb +28 -537
- data/lib/rubocop/config_loader.rb +21 -3
- data/lib/rubocop/config_loader_resolver.rb +4 -3
- data/lib/rubocop/config_obsoletion.rb +275 -0
- data/lib/rubocop/config_validator.rb +246 -0
- data/lib/rubocop/cop/autocorrect_logic.rb +2 -2
- data/lib/rubocop/cop/bundler/gem_comment.rb +4 -4
- data/lib/rubocop/cop/commissioner.rb +15 -7
- data/lib/rubocop/cop/cop.rb +33 -9
- data/lib/rubocop/cop/corrector.rb +8 -7
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +43 -17
- data/lib/rubocop/cop/correctors/multiline_literal_brace_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -2
- data/lib/rubocop/cop/generator.rb +3 -3
- data/lib/rubocop/cop/generator/configuration_injector.rb +9 -4
- data/lib/rubocop/cop/generator/require_file_injector.rb +1 -1
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +59 -0
- data/lib/rubocop/cop/layout/{align_arguments.rb → argument_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/{align_array.rb → array_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/{indent_assignment.rb → assignment_indentation.rb} +11 -2
- data/lib/rubocop/cop/layout/block_alignment.rb +2 -2
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +10 -13
- data/lib/rubocop/cop/layout/empty_comment.rb +7 -16
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +22 -7
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_class_body.rb +2 -2
- data/lib/rubocop/cop/layout/end_of_line.rb +8 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +15 -60
- data/lib/rubocop/cop/layout/{indent_first_argument.rb → first_argument_indentation.rb} +12 -10
- data/lib/rubocop/cop/layout/{indent_first_array_element.rb → first_array_element_indentation.rb} +4 -4
- data/lib/rubocop/cop/layout/{indent_first_hash_element.rb → first_hash_element_indentation.rb} +4 -4
- data/lib/rubocop/cop/layout/{indent_first_parameter.rb → first_parameter_indentation.rb} +3 -3
- data/lib/rubocop/cop/layout/{align_hash.rb → hash_alignment.rb} +8 -4
- data/lib/rubocop/cop/layout/{indent_heredoc.rb → heredoc_indentation.rb} +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +19 -5
- data/lib/rubocop/cop/layout/{leading_blank_lines.rb → leading_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +24 -2
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -1
- data/lib/rubocop/cop/layout/{align_parameters.rb → parameter_alignment.rb} +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -0
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +5 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +12 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +43 -24
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +9 -7
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +8 -5
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +7 -0
- data/lib/rubocop/cop/layout/space_inside_parens.rb +6 -6
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +24 -40
- data/lib/rubocop/cop/layout/{trailing_blank_lines.rb → trailing_empty_lines.rb} +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +18 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +17 -4
- data/lib/rubocop/cop/lint/debugger.rb +1 -3
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +1 -1
- data/lib/rubocop/cop/lint/{duplicated_key.rb → duplicate_hash_key.rb} +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +4 -4
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +61 -4
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +10 -36
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +7 -8
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +2 -2
- data/lib/rubocop/cop/lint/{multiple_compare.rb → multiple_comparison.rb} +1 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/{unneeded_cop_disable_directive.rb → redundant_cop_disable_directive.rb} +24 -24
- data/lib/rubocop/cop/lint/{unneeded_cop_enable_directive.rb → redundant_cop_enable_directive.rb} +6 -8
- data/lib/rubocop/cop/lint/{unneeded_require_statement.rb → redundant_require_statement.rb} +1 -1
- data/lib/rubocop/cop/lint/{unneeded_splat_expansion.rb → redundant_splat_expansion.rb} +12 -7
- data/lib/rubocop/cop/lint/{string_conversion_in_interpolation.rb → redundant_string_coercion.rb} +7 -7
- data/lib/rubocop/cop/lint/redundant_with_index.rb +2 -2
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +5 -6
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +91 -0
- data/lib/rubocop/cop/lint/{handle_exceptions.rb → suppressed_exception.rb} +1 -1
- data/lib/rubocop/cop/lint/unused_block_argument.rb +22 -6
- data/lib/rubocop/cop/lint/unused_method_argument.rb +23 -5
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +57 -23
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/lint/void.rb +7 -26
- data/lib/rubocop/cop/message_annotator.rb +16 -7
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/line_length.rb +48 -42
- data/lib/rubocop/cop/metrics/method_length.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +23 -6
- data/lib/rubocop/cop/migration/department_name.rb +44 -0
- data/lib/rubocop/cop/mixin/alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +0 -2
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/{hash_alignment.rb → hash_alignment_styles.rb} +1 -1
- data/lib/rubocop/cop/mixin/interpolation.rb +27 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +2 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +4 -4
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +87 -0
- data/lib/rubocop/cop/mixin/statement_modifier.rb +5 -2
- data/lib/rubocop/cop/mixin/surrounding_space.rb +7 -5
- data/lib/rubocop/cop/mixin/trailing_comma.rb +8 -6
- data/lib/rubocop/cop/naming/{uncommunicative_block_param_name.rb → block_parameter_name.rb} +3 -3
- data/lib/rubocop/cop/naming/file_name.rb +12 -5
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +5 -5
- data/lib/rubocop/cop/naming/method_name.rb +12 -1
- data/lib/rubocop/cop/naming/{uncommunicative_method_param_name.rb → method_parameter_name.rb} +3 -3
- data/lib/rubocop/cop/naming/predicate_name.rb +6 -6
- data/lib/rubocop/cop/naming/variable_name.rb +1 -0
- data/lib/rubocop/cop/offense.rb +18 -7
- data/lib/rubocop/cop/registry.rb +22 -1
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +2 -2
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +35 -16
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/comment_annotation.rb +5 -5
- data/lib/rubocop/cop/style/commented_keyword.rb +16 -30
- data/lib/rubocop/cop/style/conditional_assignment.rb +5 -7
- data/lib/rubocop/cop/style/constant_visibility.rb +13 -2
- data/lib/rubocop/cop/style/copyright.rb +11 -7
- data/lib/rubocop/cop/style/documentation_method.rb +44 -0
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +10 -4
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +2 -2
- data/lib/rubocop/cop/style/empty_method.rb +5 -5
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +1 -1
- data/lib/rubocop/cop/style/format_string.rb +10 -7
- data/lib/rubocop/cop/style/format_string_token.rb +19 -68
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +28 -33
- data/lib/rubocop/cop/style/guard_clause.rb +39 -10
- data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/style/if_unless_modifier.rb +58 -15
- data/lib/rubocop/cop/style/infinite_loop.rb +5 -4
- data/lib/rubocop/cop/style/inverse_methods.rb +19 -13
- data/lib/rubocop/cop/style/ip_addresses.rb +4 -4
- data/lib/rubocop/cop/style/lambda.rb +0 -2
- data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -10
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +25 -25
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -9
- data/lib/rubocop/cop/style/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/mixin_usage.rb +11 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
- data/lib/rubocop/cop/style/nested_modifier.rb +22 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +5 -5
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +21 -9
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -3
- data/lib/rubocop/cop/style/option_hash.rb +3 -3
- data/lib/rubocop/cop/style/or_assignment.rb +6 -1
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +14 -0
- data/lib/rubocop/cop/style/{unneeded_capital_w.rb → redundant_capital_w.rb} +1 -1
- data/lib/rubocop/cop/style/{unneeded_condition.rb → redundant_condition.rb} +3 -3
- data/lib/rubocop/cop/style/{unneeded_interpolation.rb → redundant_interpolation.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +16 -7
- data/lib/rubocop/cop/style/{unneeded_percent_q.rb → redundant_percent_q.rb} +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +39 -29
- data/lib/rubocop/cop/style/redundant_self.rb +18 -1
- data/lib/rubocop/cop/style/{unneeded_sort.rb → redundant_sort.rb} +5 -5
- data/lib/rubocop/cop/style/rescue_modifier.rb +24 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +23 -3
- data/lib/rubocop/cop/style/semicolon.rb +13 -2
- data/lib/rubocop/cop/style/single_line_methods.rb +8 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +5 -7
- data/lib/rubocop/cop/style/ternary_parentheses.rb +19 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +4 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +5 -5
- data/lib/rubocop/cop/style/variable_interpolation.rb +6 -16
- data/lib/rubocop/cop/team.rb +5 -0
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/utils/format_string.rb +120 -0
- data/lib/rubocop/cop/variable_force.rb +7 -5
- data/lib/rubocop/cop/variable_force/variable.rb +15 -2
- data/lib/rubocop/core_ext/string.rb +0 -24
- data/lib/rubocop/formatter/clang_style_formatter.rb +9 -6
- data/lib/rubocop/formatter/emacs_style_formatter.rb +22 -9
- data/lib/rubocop/formatter/file_list_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +16 -15
- data/lib/rubocop/formatter/pacman_formatter.rb +80 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +16 -4
- data/lib/rubocop/formatter/tap_formatter.rb +18 -7
- data/lib/rubocop/magic_comment.rb +4 -0
- data/lib/rubocop/node_pattern.rb +3 -1
- data/lib/rubocop/options.rb +17 -22
- data/lib/rubocop/path_util.rb +1 -1
- data/lib/rubocop/processed_source.rb +5 -1
- data/lib/rubocop/rake_task.rb +1 -0
- data/lib/rubocop/result_cache.rb +22 -8
- data/lib/rubocop/rspec/expect_offense.rb +4 -1
- data/lib/rubocop/runner.rb +55 -32
- data/lib/rubocop/target_finder.rb +12 -6
- data/lib/rubocop/version.rb +1 -1
- metadata +47 -32
- data/lib/rubocop/cop/mixin/ignored_method_patterns.rb +0 -19
- data/lib/rubocop/cop/mixin/safe_mode.rb +0 -22
@@ -37,9 +37,10 @@ module RuboCop
|
|
37
37
|
# ok
|
38
38
|
class GuardClause < Cop
|
39
39
|
include MinBodyLength
|
40
|
+
include StatementModifier
|
40
41
|
|
41
|
-
MSG = 'Use a guard clause instead of wrapping the
|
42
|
-
'conditional expression.'
|
42
|
+
MSG = 'Use a guard clause (`%<example>s`) instead of wrapping the ' \
|
43
|
+
'code inside a conditional expression.'
|
43
44
|
|
44
45
|
def on_def(node)
|
45
46
|
body = node.body
|
@@ -55,9 +56,19 @@ module RuboCop
|
|
55
56
|
alias on_defs on_def
|
56
57
|
|
57
58
|
def on_if(node)
|
58
|
-
return if accepted_form?(node)
|
59
|
+
return if accepted_form?(node)
|
59
60
|
|
60
|
-
|
61
|
+
guard_clause_in_if = node.if_branch&.guard_clause?
|
62
|
+
guard_clause_in_else = node.else_branch&.guard_clause?
|
63
|
+
guard_clause = guard_clause_in_if || guard_clause_in_else
|
64
|
+
return unless guard_clause
|
65
|
+
|
66
|
+
kw = if guard_clause_in_if
|
67
|
+
node.loc.keyword.source
|
68
|
+
else
|
69
|
+
opposite_keyword(node)
|
70
|
+
end
|
71
|
+
register_offense(node, guard_clause.source, kw)
|
61
72
|
end
|
62
73
|
|
63
74
|
private
|
@@ -65,7 +76,30 @@ module RuboCop
|
|
65
76
|
def check_ending_if(node)
|
66
77
|
return if accepted_form?(node, true) || !min_body_length?(node)
|
67
78
|
|
68
|
-
|
79
|
+
register_offense(node, 'return', opposite_keyword(node))
|
80
|
+
end
|
81
|
+
|
82
|
+
def opposite_keyword(node)
|
83
|
+
node.if? ? 'unless' : 'if'
|
84
|
+
end
|
85
|
+
|
86
|
+
def register_offense(node, scope_exiting_keyword, conditional_keyword)
|
87
|
+
condition, = node.node_parts
|
88
|
+
example = [scope_exiting_keyword,
|
89
|
+
conditional_keyword,
|
90
|
+
condition.source].join(' ')
|
91
|
+
if too_long_for_single_line?(node, example)
|
92
|
+
example = "#{conditional_keyword} #{condition.source}; " \
|
93
|
+
"#{scope_exiting_keyword}; end"
|
94
|
+
end
|
95
|
+
add_offense(node,
|
96
|
+
location: :keyword,
|
97
|
+
message: format(MSG, example: example))
|
98
|
+
end
|
99
|
+
|
100
|
+
def too_long_for_single_line?(node, example)
|
101
|
+
max = max_line_length
|
102
|
+
max && node.source_range.column + example.length > max
|
69
103
|
end
|
70
104
|
|
71
105
|
def accepted_form?(node, ending = false)
|
@@ -81,11 +115,6 @@ module RuboCop
|
|
81
115
|
!node.else? || node.elsif?
|
82
116
|
end
|
83
117
|
end
|
84
|
-
|
85
|
-
def contains_guard_clause?(node)
|
86
|
-
node.if_branch&.guard_clause? ||
|
87
|
-
node.else_branch&.guard_clause?
|
88
|
-
end
|
89
118
|
end
|
90
119
|
end
|
91
120
|
end
|
@@ -168,10 +168,10 @@ module RuboCop
|
|
168
168
|
end
|
169
169
|
|
170
170
|
def autocorrect_ruby19(corrector, pair_node)
|
171
|
-
key = pair_node.key
|
171
|
+
key = pair_node.key.source_range
|
172
172
|
op = pair_node.loc.operator
|
173
173
|
|
174
|
-
range =
|
174
|
+
range = key.join(op)
|
175
175
|
range = range_with_surrounding_space(range: range, side: :right)
|
176
176
|
|
177
177
|
space = argument_without_space?(pair_node.parent) ? ' ' : ''
|
@@ -3,10 +3,13 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# Checks for if and unless statements that would fit on one line
|
7
|
-
#
|
8
|
-
#
|
9
|
-
#
|
6
|
+
# Checks for `if` and `unless` statements that would fit on one line if
|
7
|
+
# written as modifier `if`/`unless`. The cop also checks for modifier
|
8
|
+
# `if`/`unless` lines that exceed the maximum line length.
|
9
|
+
#
|
10
|
+
# The maximum line length is configured in the `Metrics/LineLength`
|
11
|
+
# cop. The tab size is configured in the `IndentationWidth` of the
|
12
|
+
# `Layout/Tab` cop.
|
10
13
|
#
|
11
14
|
# @example
|
12
15
|
# # bad
|
@@ -18,35 +21,66 @@ module RuboCop
|
|
18
21
|
# Foo.do_something
|
19
22
|
# end
|
20
23
|
#
|
24
|
+
# do_something_in_a_method_with_a_long_name(arg) if long_condition
|
25
|
+
#
|
21
26
|
# # good
|
22
27
|
# do_stuff(bar) if condition
|
23
28
|
# Foo.do_something unless qux.empty?
|
29
|
+
#
|
30
|
+
# if long_condition
|
31
|
+
# do_something_in_a_method_with_a_long_name(arg)
|
32
|
+
# end
|
24
33
|
class IfUnlessModifier < Cop
|
25
34
|
include StatementModifier
|
26
35
|
|
27
|
-
|
28
|
-
|
29
|
-
|
36
|
+
MSG_USE_MODIFIER = 'Favor modifier `%<keyword>s` usage when having a ' \
|
37
|
+
'single-line body. Another good alternative is ' \
|
38
|
+
'the usage of control flow `&&`/`||`.'
|
39
|
+
MSG_USE_NORMAL =
|
40
|
+
'Modifier form of `%<keyword>s` makes the line too long.'
|
30
41
|
|
31
42
|
ASSIGNMENT_TYPES = %i[lvasgn casgn cvasgn
|
32
43
|
gvasgn ivasgn masgn].freeze
|
33
44
|
|
34
45
|
def on_if(node)
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
-
|
46
|
+
msg = if eligible_node?(node)
|
47
|
+
MSG_USE_MODIFIER unless named_capture_in_condition?(node)
|
48
|
+
elsif node.modifier_form? && too_long_single_line?(node)
|
49
|
+
MSG_USE_NORMAL
|
50
|
+
end
|
51
|
+
return unless msg
|
52
|
+
|
53
|
+
add_offense(node,
|
54
|
+
location: :keyword,
|
55
|
+
message: format(msg, keyword: node.keyword))
|
40
56
|
end
|
41
57
|
|
42
58
|
def autocorrect(node)
|
43
|
-
|
44
|
-
|
45
|
-
|
59
|
+
replacement = if node.modifier_form?
|
60
|
+
to_normal_form(node)
|
61
|
+
else
|
62
|
+
to_modifier_form(node)
|
63
|
+
end
|
64
|
+
->(corrector) { corrector.replace(node.source_range, replacement) }
|
46
65
|
end
|
47
66
|
|
48
67
|
private
|
49
68
|
|
69
|
+
def too_long_single_line?(node)
|
70
|
+
return false unless max_line_length
|
71
|
+
|
72
|
+
range = node.source_range
|
73
|
+
return false unless range.first_line == range.last_line
|
74
|
+
return false unless line_length_enabled_at_line?(range.first_line)
|
75
|
+
|
76
|
+
range.last_column > max_line_length
|
77
|
+
end
|
78
|
+
|
79
|
+
def line_length_enabled_at_line?(line)
|
80
|
+
processed_source.comment_config
|
81
|
+
.cop_enabled_at_line?('Metrics/LineLength', line)
|
82
|
+
end
|
83
|
+
|
50
84
|
def named_capture_in_condition?(node)
|
51
85
|
node.condition.match_with_lvasgn_type?
|
52
86
|
end
|
@@ -79,6 +113,15 @@ module RuboCop
|
|
79
113
|
parenthesize?(node) ? "(#{expression})" : expression
|
80
114
|
end
|
81
115
|
|
116
|
+
def to_normal_form(node)
|
117
|
+
indentation = ' ' * node.source_range.column
|
118
|
+
<<~RUBY.chomp
|
119
|
+
#{node.keyword} #{node.condition.source}
|
120
|
+
#{indentation} #{node.body.source}
|
121
|
+
#{indentation}end
|
122
|
+
RUBY
|
123
|
+
end
|
124
|
+
|
82
125
|
def first_line_comment(node)
|
83
126
|
comment =
|
84
127
|
processed_source.find_comment { |c| c.loc.line == node.loc.line }
|
@@ -97,12 +97,13 @@ module RuboCop
|
|
97
97
|
end
|
98
98
|
|
99
99
|
def modifier_replacement(node)
|
100
|
+
body = node.body
|
100
101
|
if node.single_line?
|
101
|
-
'loop { ' +
|
102
|
+
'loop { ' + body.source + ' }'
|
102
103
|
else
|
103
|
-
indentation =
|
104
|
+
indentation = body.source_range.source_line[LEADING_SPACE]
|
104
105
|
|
105
|
-
['loop do',
|
106
|
+
['loop do', body.source.gsub(/^/, configured_indent),
|
106
107
|
'end'].join("\n#{indentation}")
|
107
108
|
end
|
108
109
|
end
|
@@ -119,7 +120,7 @@ module RuboCop
|
|
119
120
|
end
|
120
121
|
|
121
122
|
def configured_indent
|
122
|
-
' ' * config.for_cop('IndentationWidth')['Width']
|
123
|
+
' ' * config.for_cop('Layout/IndentationWidth')['Width']
|
123
124
|
end
|
124
125
|
end
|
125
126
|
end
|
@@ -94,20 +94,25 @@ module RuboCop
|
|
94
94
|
end
|
95
95
|
|
96
96
|
def autocorrect(node)
|
97
|
+
if node.block_type?
|
98
|
+
correct_inverse_block(node)
|
99
|
+
elsif node.send_type?
|
100
|
+
correct_inverse_method(node)
|
101
|
+
end
|
102
|
+
end
|
103
|
+
|
104
|
+
def correct_inverse_method(node)
|
97
105
|
method_call, _lhs, method, _rhs = inverse_candidate?(node)
|
106
|
+
return unless method_call && method
|
98
107
|
|
99
|
-
|
100
|
-
|
101
|
-
|
102
|
-
|
103
|
-
inverse_methods[method].to_s)
|
108
|
+
lambda do |corrector|
|
109
|
+
corrector.remove(not_to_receiver(node, method_call))
|
110
|
+
corrector.replace(method_call.loc.selector,
|
111
|
+
inverse_methods[method].to_s)
|
104
112
|
|
105
|
-
|
106
|
-
|
107
|
-
end
|
113
|
+
if EQUALITY_METHODS.include?(method)
|
114
|
+
corrector.remove(end_parentheses(node, method_call))
|
108
115
|
end
|
109
|
-
else
|
110
|
-
correct_inverse_block(node)
|
111
116
|
end
|
112
117
|
end
|
113
118
|
|
@@ -122,18 +127,19 @@ module RuboCop
|
|
122
127
|
end
|
123
128
|
|
124
129
|
def correct_inverse_selector(block, corrector)
|
125
|
-
|
130
|
+
selector_loc = block.loc.selector
|
131
|
+
selector = selector_loc.source
|
126
132
|
|
127
133
|
if NEGATED_EQUALITY_METHODS.include?(selector.to_sym)
|
128
134
|
selector[0] = '='
|
129
|
-
corrector.replace(
|
135
|
+
corrector.replace(selector_loc, selector)
|
130
136
|
else
|
131
137
|
if block.loc.dot
|
132
138
|
range = dot_range(block.loc)
|
133
139
|
corrector.remove(range)
|
134
140
|
end
|
135
141
|
|
136
|
-
corrector.remove(
|
142
|
+
corrector.remove(selector_loc)
|
137
143
|
end
|
138
144
|
end
|
139
145
|
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
28
28
|
contents = node.source[1...-1]
|
29
29
|
return false if contents.empty?
|
30
30
|
|
31
|
-
return false if
|
31
|
+
return false if allowed_addresses.include?(contents.downcase)
|
32
32
|
|
33
33
|
# To try to avoid doing two regex checks on every string,
|
34
34
|
# shortcut out if the string does not look like an IP address
|
@@ -47,9 +47,9 @@ module RuboCop
|
|
47
47
|
|
48
48
|
private
|
49
49
|
|
50
|
-
def
|
51
|
-
|
52
|
-
Array(
|
50
|
+
def allowed_addresses
|
51
|
+
allowed_addresses = cop_config['AllowedAddresses']
|
52
|
+
Array(allowed_addresses).map(&:downcase)
|
53
53
|
end
|
54
54
|
|
55
55
|
def could_be_ip?(str)
|
@@ -26,13 +26,14 @@ module RuboCop
|
|
26
26
|
'those strings.'
|
27
27
|
CONCAT_TOKEN_TYPES = %i[tPLUS tLSHFT].freeze
|
28
28
|
SIMPLE_STRING_TOKEN_TYPE = :tSTRING
|
29
|
-
|
29
|
+
COMPLEX_STRING_BEGIN_TOKEN = :tSTRING_BEG
|
30
|
+
COMPLEX_STRING_END_TOKEN = :tSTRING_END
|
30
31
|
HIGH_PRECEDENCE_OP_TOKEN_TYPES = %i[tSTAR2 tPERCENT tDOT
|
31
32
|
tLBRACK2].freeze
|
32
33
|
QUOTE_DELIMITERS = %w[' "].freeze
|
33
34
|
|
34
35
|
def self.autocorrect_incompatible_with
|
35
|
-
[Style::
|
36
|
+
[Style::RedundantInterpolation]
|
36
37
|
end
|
37
38
|
|
38
39
|
def investigate(processed_source)
|
@@ -59,9 +60,7 @@ module RuboCop
|
|
59
60
|
def check_token_set(index)
|
60
61
|
predecessor, operator, successor = processed_source.tokens[index, 3]
|
61
62
|
|
62
|
-
return unless
|
63
|
-
eligible_operator?(operator) &&
|
64
|
-
eligible_predecessor?(predecessor)
|
63
|
+
return unless eligible_token_set?(predecessor, operator, successor)
|
65
64
|
|
66
65
|
return if operator.line == successor.line
|
67
66
|
|
@@ -72,6 +71,12 @@ module RuboCop
|
|
72
71
|
add_offense(operator.pos, location: operator.pos)
|
73
72
|
end
|
74
73
|
|
74
|
+
def eligible_token_set?(predecessor, operator, successor)
|
75
|
+
eligible_successor?(successor) &&
|
76
|
+
eligible_operator?(operator) &&
|
77
|
+
eligible_predecessor?(predecessor)
|
78
|
+
end
|
79
|
+
|
75
80
|
def eligible_successor?(successor)
|
76
81
|
successor && standard_string_literal?(successor)
|
77
82
|
end
|
@@ -91,13 +96,12 @@ module RuboCop
|
|
91
96
|
|
92
97
|
def token_after_last_string(successor, base_index)
|
93
98
|
index = base_index + 3
|
94
|
-
|
95
|
-
if successor.type == begin_token
|
99
|
+
if successor.type == COMPLEX_STRING_BEGIN_TOKEN
|
96
100
|
ends_to_find = 1
|
97
101
|
while ends_to_find.positive?
|
98
102
|
case processed_source.tokens[index].type
|
99
|
-
when
|
100
|
-
when
|
103
|
+
when COMPLEX_STRING_BEGIN_TOKEN then ends_to_find += 1
|
104
|
+
when COMPLEX_STRING_END_TOKEN then ends_to_find -= 1
|
101
105
|
end
|
102
106
|
index += 1
|
103
107
|
end
|
@@ -109,7 +113,7 @@ module RuboCop
|
|
109
113
|
case token.type
|
110
114
|
when SIMPLE_STRING_TOKEN_TYPE
|
111
115
|
true
|
112
|
-
when
|
116
|
+
when COMPLEX_STRING_BEGIN_TOKEN, COMPLEX_STRING_END_TOKEN
|
113
117
|
QUOTE_DELIMITERS.include?(token.text)
|
114
118
|
else
|
115
119
|
false
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
9
9
|
#
|
10
10
|
# In the default style (require_parentheses), macro methods are ignored.
|
11
11
|
# Additional methods can be added to the `IgnoredMethods`
|
12
|
-
# or `
|
12
|
+
# or `IgnoredPatterns` list. These options are
|
13
13
|
# valid only in the default style. Macros can be included by
|
14
14
|
# either setting `IgnoreMacros` to false or adding specific macros to
|
15
15
|
# the `IncludedMacros` list.
|
@@ -17,7 +17,7 @@ module RuboCop
|
|
17
17
|
# Precedence of options is all follows:
|
18
18
|
#
|
19
19
|
# 1. `IgnoredMethods`
|
20
|
-
# 2. `
|
20
|
+
# 2. `IgnoredPatterns`
|
21
21
|
# 3. `IncludedMacros`
|
22
22
|
#
|
23
23
|
# eg. If a method is listed in both
|
@@ -61,23 +61,9 @@ module RuboCop
|
|
61
61
|
# # okay with `puts` listed in `IgnoredMethods`
|
62
62
|
# puts 'test'
|
63
63
|
#
|
64
|
-
# # okay with `^assert` listed in `
|
64
|
+
# # okay with `^assert` listed in `IgnoredPatterns`
|
65
65
|
# assert_equal 'test', x
|
66
66
|
#
|
67
|
-
# # IgnoreMacros: true (default)
|
68
|
-
#
|
69
|
-
# # good
|
70
|
-
# class Foo
|
71
|
-
# bar :baz
|
72
|
-
# end
|
73
|
-
#
|
74
|
-
# # IgnoreMacros: false
|
75
|
-
#
|
76
|
-
# # bad
|
77
|
-
# class Foo
|
78
|
-
# bar :baz
|
79
|
-
# end
|
80
|
-
#
|
81
67
|
# @example EnforcedStyle: omit_parentheses
|
82
68
|
#
|
83
69
|
# # bad
|
@@ -92,7 +78,21 @@ module RuboCop
|
|
92
78
|
# # good
|
93
79
|
# foo.enforce strict: true
|
94
80
|
#
|
95
|
-
#
|
81
|
+
# @example IgnoreMacros: true (default)
|
82
|
+
#
|
83
|
+
# # good
|
84
|
+
# class Foo
|
85
|
+
# bar :baz
|
86
|
+
# end
|
87
|
+
#
|
88
|
+
# @example IgnoreMacros: false
|
89
|
+
#
|
90
|
+
# # bad
|
91
|
+
# class Foo
|
92
|
+
# bar :baz
|
93
|
+
# end
|
94
|
+
#
|
95
|
+
# @example AllowParenthesesInMultilineCall: false (default)
|
96
96
|
#
|
97
97
|
# # bad
|
98
98
|
# foo.enforce(
|
@@ -103,7 +103,7 @@ module RuboCop
|
|
103
103
|
# foo.enforce \
|
104
104
|
# strict: true
|
105
105
|
#
|
106
|
-
#
|
106
|
+
# @example AllowParenthesesInMultilineCall: true
|
107
107
|
#
|
108
108
|
# # good
|
109
109
|
# foo.enforce(
|
@@ -114,7 +114,7 @@ module RuboCop
|
|
114
114
|
# foo.enforce \
|
115
115
|
# strict: true
|
116
116
|
#
|
117
|
-
#
|
117
|
+
# @example AllowParenthesesInChaining: false (default)
|
118
118
|
#
|
119
119
|
# # bad
|
120
120
|
# foo().bar(1)
|
@@ -122,7 +122,7 @@ module RuboCop
|
|
122
122
|
# # good
|
123
123
|
# foo().bar 1
|
124
124
|
#
|
125
|
-
#
|
125
|
+
# @example AllowParenthesesInChaining: true
|
126
126
|
#
|
127
127
|
# # good
|
128
128
|
# foo().bar(1)
|
@@ -130,7 +130,7 @@ module RuboCop
|
|
130
130
|
# # good
|
131
131
|
# foo().bar 1
|
132
132
|
#
|
133
|
-
#
|
133
|
+
# @example AllowParenthesesInCamelCaseMethod: false (default)
|
134
134
|
#
|
135
135
|
# # bad
|
136
136
|
# Array(1)
|
@@ -138,7 +138,7 @@ module RuboCop
|
|
138
138
|
# # good
|
139
139
|
# Array 1
|
140
140
|
#
|
141
|
-
#
|
141
|
+
# @example AllowParenthesesInCamelCaseMethod: true
|
142
142
|
#
|
143
143
|
# # good
|
144
144
|
# Array(1)
|
@@ -148,7 +148,7 @@ module RuboCop
|
|
148
148
|
class MethodCallWithArgsParentheses < Cop
|
149
149
|
include ConfigurableEnforcedStyle
|
150
150
|
include IgnoredMethods
|
151
|
-
include
|
151
|
+
include IgnoredPattern
|
152
152
|
|
153
153
|
TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
|
154
154
|
|
@@ -186,7 +186,7 @@ module RuboCop
|
|
186
186
|
|
187
187
|
def add_offense_for_require_parentheses(node)
|
188
188
|
return if ignored_method?(node.method_name)
|
189
|
-
return if
|
189
|
+
return if matches_ignored_pattern?(node.method_name)
|
190
190
|
return if eligible_for_parentheses_omission?(node)
|
191
191
|
return unless node.arguments? && !node.parenthesized?
|
192
192
|
|