rubocop 0.42.0 → 0.43.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/assets/output.html.erb +21 -10
- data/config/default.yml +32 -2
- data/config/disabled.yml +8 -1
- data/config/enabled.yml +40 -12
- data/lib/rubocop.rb +14 -2
- data/lib/rubocop/ast_node.rb +2 -0
- data/lib/rubocop/cached_data.rb +13 -11
- data/lib/rubocop/cli.rb +5 -5
- data/lib/rubocop/config.rb +68 -24
- data/lib/rubocop/config_loader.rb +13 -11
- data/lib/rubocop/config_loader_resolver.rb +4 -2
- data/lib/rubocop/cop/cop.rb +16 -5
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +21 -20
- data/lib/rubocop/cop/lint/block_alignment.rb +3 -4
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -3
- data/lib/rubocop/cop/lint/duplicate_methods.rb +16 -6
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +4 -6
- data/lib/rubocop/cop/lint/eval.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -1
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +8 -8
- data/lib/rubocop/cop/lint/inherit_exception.rb +22 -7
- data/lib/rubocop/cop/lint/literal_in_condition.rb +5 -5
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +3 -5
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +9 -8
- data/lib/rubocop/cop/lint/percent_string_array.rb +17 -6
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +4 -4
- data/lib/rubocop/cop/lint/rand_one.rb +3 -3
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +39 -44
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +2 -2
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +1 -2
- data/lib/rubocop/cop/lint/unified_integer.rb +38 -0
- data/lib/rubocop/cop/lint/unneeded_disable.rb +51 -38
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +114 -0
- data/lib/rubocop/cop/lint/useless_assignment.rb +25 -12
- data/lib/rubocop/cop/lint/useless_setter_call.rb +27 -28
- data/lib/rubocop/cop/lint/void.rb +2 -4
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +5 -5
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +19 -17
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +3 -5
- data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -5
- data/lib/rubocop/cop/mixin/configurable_numbering.rb +52 -0
- data/lib/rubocop/cop/mixin/def_node.rb +28 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +41 -0
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +18 -13
- data/lib/rubocop/cop/mixin/if_node.rb +6 -0
- data/lib/rubocop/cop/mixin/match_range.rb +2 -5
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +40 -28
- data/lib/rubocop/cop/mixin/negative_conditional.rb +6 -6
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -5
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +14 -4
- data/lib/rubocop/cop/mixin/safe_mode.rb +23 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -4
- data/lib/rubocop/cop/mixin/space_inside.rb +1 -3
- data/lib/rubocop/cop/mixin/statement_modifier.rb +30 -20
- data/lib/rubocop/cop/mixin/trailing_comma.rb +19 -17
- data/lib/rubocop/cop/performance/case_when_splat.rb +16 -41
- data/lib/rubocop/cop/performance/casecmp.rb +28 -16
- data/lib/rubocop/cop/performance/count.rb +58 -34
- data/lib/rubocop/cop/performance/detect.rb +3 -7
- data/lib/rubocop/cop/performance/double_start_end_with.rb +17 -13
- data/lib/rubocop/cop/performance/fixed_size.rb +19 -14
- data/lib/rubocop/cop/performance/flat_map.rb +16 -9
- data/lib/rubocop/cop/performance/hash_each.rb +2 -3
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +4 -6
- data/lib/rubocop/cop/performance/redundant_match.rb +4 -1
- data/lib/rubocop/cop/performance/redundant_merge.rb +63 -32
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +8 -7
- data/lib/rubocop/cop/performance/reverse_each.rb +1 -4
- data/lib/rubocop/cop/performance/size.rb +21 -8
- data/lib/rubocop/cop/performance/sort_with_block.rb +54 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +3 -7
- data/lib/rubocop/cop/rails/delegate.rb +2 -3
- data/lib/rubocop/cop/rails/find_by.rb +4 -8
- data/lib/rubocop/cop/rails/not_null_column.rb +45 -0
- data/lib/rubocop/cop/rails/request_referer.rb +3 -3
- data/lib/rubocop/cop/rails/safe_navigation.rb +89 -0
- data/lib/rubocop/cop/rails/save_bang.rb +78 -9
- data/lib/rubocop/cop/rails/scope_args.rb +3 -1
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +2 -3
- data/lib/rubocop/cop/rails/validation.rb +1 -1
- data/lib/rubocop/cop/security/json_load.rb +36 -0
- data/lib/rubocop/cop/style/alias.rb +1 -1
- data/lib/rubocop/cop/style/align_hash.rb +25 -14
- data/lib/rubocop/cop/style/and_or.rb +13 -3
- data/lib/rubocop/cop/style/array_join.rb +3 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -2
- data/lib/rubocop/cop/style/ascii_identifiers.rb +1 -2
- data/lib/rubocop/cop/style/attr.rb +1 -3
- data/lib/rubocop/cop/style/block_comments.rb +2 -6
- data/lib/rubocop/cop/style/block_delimiters.rb +35 -21
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +4 -4
- data/lib/rubocop/cop/style/case_indentation.rb +1 -3
- data/lib/rubocop/cop/style/class_methods.rb +3 -4
- data/lib/rubocop/cop/style/collection_methods.rb +1 -1
- data/lib/rubocop/cop/style/command_literal.rb +15 -8
- data/lib/rubocop/cop/style/comment_annotation.rb +1 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +68 -36
- data/lib/rubocop/cop/style/copyright.rb +1 -5
- data/lib/rubocop/cop/style/def_with_parentheses.rb +3 -5
- data/lib/rubocop/cop/style/documentation.rb +28 -56
- data/lib/rubocop/cop/style/documentation_method.rb +80 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +6 -5
- data/lib/rubocop/cop/style/each_with_object.rb +2 -2
- data/lib/rubocop/cop/style/else_alignment.rb +10 -9
- data/lib/rubocop/cop/style/empty_case_condition.rb +2 -4
- data/lib/rubocop/cop/style/empty_else.rb +1 -4
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +1 -3
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +2 -5
- data/lib/rubocop/cop/style/encoding.rb +28 -14
- data/lib/rubocop/cop/style/even_odd.rb +28 -17
- data/lib/rubocop/cop/style/extra_spacing.rb +36 -25
- data/lib/rubocop/cop/style/file_name.rb +19 -10
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +2 -3
- data/lib/rubocop/cop/style/for.rb +12 -8
- data/lib/rubocop/cop/style/format_string.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +22 -56
- data/lib/rubocop/cop/style/hash_syntax.rb +72 -7
- data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -19
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +3 -3
- data/lib/rubocop/cop/style/indentation_width.rb +30 -16
- data/lib/rubocop/cop/style/infinite_loop.rb +16 -13
- data/lib/rubocop/cop/style/initial_indentation.rb +23 -18
- data/lib/rubocop/cop/style/inline_comment.rb +16 -3
- data/lib/rubocop/cop/style/lambda.rb +22 -10
- data/lib/rubocop/cop/style/leading_comment_space.rb +12 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +24 -6
- data/lib/rubocop/cop/style/method_call_parentheses.rb +18 -9
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +3 -4
- data/lib/rubocop/cop/style/method_missing.rb +10 -2
- data/lib/rubocop/cop/style/module_function.rb +14 -6
- data/lib/rubocop/cop/style/multiline_assignment_layout.rb +2 -5
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -5
- data/lib/rubocop/cop/style/multiline_block_layout.rb +22 -15
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +9 -0
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +41 -20
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +6 -6
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +3 -5
- data/lib/rubocop/cop/style/mutable_constant.rb +21 -13
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_while.rb +3 -3
- data/lib/rubocop/cop/style/nested_modifier.rb +2 -4
- data/lib/rubocop/cop/style/next.rb +4 -4
- data/lib/rubocop/cop/style/non_nil_check.rb +18 -10
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +8 -0
- data/lib/rubocop/cop/style/numeric_predicate.rb +9 -9
- data/lib/rubocop/cop/style/one_line_conditional.rb +11 -1
- data/lib/rubocop/cop/style/op_method.rb +1 -1
- data/lib/rubocop/cop/style/option_hash.rb +8 -8
- data/lib/rubocop/cop/style/optional_arguments.rb +21 -8
- data/lib/rubocop/cop/style/parallel_assignment.rb +51 -35
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +2 -2
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +2 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +26 -15
- data/lib/rubocop/cop/style/redundant_return.rb +5 -5
- data/lib/rubocop/cop/style/redundant_self.rb +20 -11
- data/lib/rubocop/cop/style/regexp_literal.rb +16 -10
- data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +8 -6
- data/lib/rubocop/cop/style/safe_navigation.rb +125 -0
- data/lib/rubocop/cop/style/self_assignment.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +9 -10
- data/lib/rubocop/cop/style/signal_exception.rb +2 -4
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +18 -11
- data/lib/rubocop/cop/style/space_after_method_name.rb +2 -3
- data/lib/rubocop/cop/style/space_after_not.rb +4 -6
- data/lib/rubocop/cop/style/space_around_block_parameters.rb +1 -2
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -3
- data/lib/rubocop/cop/style/space_around_operators.rb +21 -16
- data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -12
- data/lib/rubocop/cop/style/space_before_first_arg.rb +1 -3
- data/lib/rubocop/cop/style/space_inside_array_percent_literal.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +33 -40
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +38 -23
- data/lib/rubocop/cop/style/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +26 -12
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/symbol_array.rb +10 -10
- data/lib/rubocop/cop/style/symbol_proc.rb +28 -13
- data/lib/rubocop/cop/style/ternary_parentheses.rb +35 -5
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +2 -4
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +29 -17
- data/lib/rubocop/cop/style/trivial_accessors.rb +6 -6
- data/lib/rubocop/cop/style/unless_else.rb +2 -6
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +8 -4
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +4 -5
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +13 -7
- data/lib/rubocop/cop/style/variable_number.rb +79 -0
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +25 -15
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -0
- data/lib/rubocop/cop/util.rb +23 -4
- data/lib/rubocop/cop/variable_force.rb +59 -25
- data/lib/rubocop/cop/variable_force/locatable.rb +8 -6
- data/lib/rubocop/cop/variable_force/variable.rb +2 -2
- data/lib/rubocop/cop/variable_force/variable_table.rb +3 -3
- data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -11
- data/lib/rubocop/formatter/formatter_set.rb +12 -10
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +4 -4
- data/lib/rubocop/node_pattern.rb +79 -35
- data/lib/rubocop/options.rb +4 -4
- data/lib/rubocop/processed_source.rb +9 -5
- data/lib/rubocop/remote_config.rb +14 -10
- data/lib/rubocop/result_cache.rb +14 -6
- data/lib/rubocop/runner.rb +55 -34
- data/lib/rubocop/string_util.rb +9 -5
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/token.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- metadata +15 -4
- data/lib/rubocop/cop/lint/useless_array_splat.rb +0 -56
- data/lib/rubocop/cop/performance/push_splat.rb +0 -47
@@ -29,12 +29,11 @@ module RuboCop
|
|
29
29
|
def on_send(node)
|
30
30
|
return if ignored_node?(node)
|
31
31
|
receiver, _method_name, *_args = *node
|
32
|
-
return unless receiver && receiver.
|
32
|
+
return unless receiver && receiver.block_type? &&
|
33
33
|
receiver.loc.end.is?('end')
|
34
34
|
|
35
|
-
range =
|
36
|
-
|
37
|
-
node.source_range.end_pos)
|
35
|
+
range = range_between(receiver.loc.end.begin_pos,
|
36
|
+
node.source_range.end_pos)
|
38
37
|
add_offense(nil, range)
|
39
38
|
end
|
40
39
|
end
|
@@ -33,9 +33,8 @@ module RuboCop
|
|
33
33
|
else
|
34
34
|
args_expr = args_node(node).source_range
|
35
35
|
args_with_space = range_with_surrounding_space(args_expr, :left)
|
36
|
-
just_space =
|
37
|
-
|
38
|
-
args_expr.begin_pos)
|
36
|
+
just_space = range_between(args_with_space.begin_pos,
|
37
|
+
args_expr.begin_pos)
|
39
38
|
corrector.replace(just_space, '(')
|
40
39
|
corrector.insert_after(args_expr, ')')
|
41
40
|
end
|
@@ -68,7 +67,7 @@ module RuboCop
|
|
68
67
|
end
|
69
68
|
|
70
69
|
def args_node(def_node)
|
71
|
-
if def_node.
|
70
|
+
if def_node.def_type?
|
72
71
|
_method_name, args, _body = *def_node
|
73
72
|
else
|
74
73
|
_scope, _method_name, args, _body = *def_node
|
@@ -60,14 +60,22 @@ module RuboCop
|
|
60
60
|
end
|
61
61
|
|
62
62
|
def implements_respond_to_missing?(node)
|
63
|
-
node.parent.
|
64
|
-
|
63
|
+
node.parent.each_child_node(node.type).any? do |sibling|
|
64
|
+
if node.def_type?
|
65
|
+
respond_to_missing_def?(sibling)
|
66
|
+
elsif node.defs_type?
|
67
|
+
respond_to_missing_defs?(sibling)
|
68
|
+
end
|
65
69
|
end
|
66
70
|
end
|
67
71
|
|
68
72
|
def_node_matcher :respond_to_missing_def?, <<-PATTERN
|
69
73
|
(def :respond_to_missing? (...) ...)
|
70
74
|
PATTERN
|
75
|
+
|
76
|
+
def_node_matcher :respond_to_missing_defs?, <<-PATTERN
|
77
|
+
(defs (self) :respond_to_missing? (...) ...)
|
78
|
+
PATTERN
|
71
79
|
end
|
72
80
|
end
|
73
81
|
end
|
@@ -38,13 +38,21 @@ module RuboCop
|
|
38
38
|
|
39
39
|
def on_module(node)
|
40
40
|
_name, body = *node
|
41
|
-
return unless body && body.
|
41
|
+
return unless body && body.begin_type?
|
42
42
|
|
43
|
-
body.children
|
44
|
-
|
45
|
-
|
46
|
-
|
47
|
-
|
43
|
+
each_wrong_style(body.children) do |child_node, msg|
|
44
|
+
add_offense(child_node, :expression, msg)
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
private
|
49
|
+
|
50
|
+
def each_wrong_style(nodes)
|
51
|
+
nodes.each do |node|
|
52
|
+
if style == :module_function && node == EXTEND_SELF_NODE
|
53
|
+
yield node, MODULE_FUNCTION_MSG
|
54
|
+
elsif style == :extend_self && node == MODULE_FUNCTION_NODE
|
55
|
+
yield node, EXTEND_SELF_MSG
|
48
56
|
end
|
49
57
|
end
|
50
58
|
end
|
@@ -71,11 +71,8 @@ module RuboCop
|
|
71
71
|
when :new_line
|
72
72
|
->(corrector) { corrector.insert_after(node.loc.operator, "\n") }
|
73
73
|
when :same_line
|
74
|
-
range =
|
75
|
-
|
76
|
-
node.loc.operator.end_pos,
|
77
|
-
extract_rhs(node).source_range.begin_pos
|
78
|
-
)
|
74
|
+
range = range_between(node.loc.operator.end_pos,
|
75
|
+
extract_rhs(node).source_range.begin_pos)
|
79
76
|
|
80
77
|
->(corrector) { corrector.replace(range, ' ') }
|
81
78
|
end
|
@@ -21,7 +21,7 @@ module RuboCop
|
|
21
21
|
method, _args, _body = *node
|
22
22
|
method.each_node(:send) do |send_node|
|
23
23
|
receiver, _method_name, *_args = *send_node
|
24
|
-
next unless receiver && receiver.
|
24
|
+
next unless receiver && receiver.block_type?
|
25
25
|
|
26
26
|
# The begin and end could also be braces, but we call the
|
27
27
|
# variables do... and end...
|
@@ -29,10 +29,8 @@ module RuboCop
|
|
29
29
|
end_kw_loc = receiver.loc.end
|
30
30
|
next if do_kw_loc.line == end_kw_loc.line
|
31
31
|
|
32
|
-
range =
|
33
|
-
|
34
|
-
end_kw_loc.begin_pos,
|
35
|
-
method.source_range.end_pos)
|
32
|
+
range = range_between(end_kw_loc.begin_pos,
|
33
|
+
method.source_range.end_pos)
|
36
34
|
add_offense(nil, range)
|
37
35
|
# Done. If there are more blocks in the chain, they will be
|
38
36
|
# found by subsequent calls to on_block.
|
@@ -43,19 +43,16 @@ module RuboCop
|
|
43
43
|
'block start.'.freeze
|
44
44
|
|
45
45
|
def on_block(node)
|
46
|
-
|
47
|
-
do_loc = node.loc.begin # Actually it's either do or {.
|
48
|
-
return if do_loc.line == end_loc.line # One-liner, no newline needed.
|
46
|
+
return if oneliner?(node)
|
49
47
|
|
50
48
|
# A block node has three children: the block start,
|
51
49
|
# the arguments, and the expression. We care if the block start
|
52
50
|
# with arguments and the expression start on the same line.
|
53
51
|
_block_start, args, last_expression = node.children
|
52
|
+
do_loc = node.loc.begin # Actually it's either do or {.
|
54
53
|
|
55
|
-
|
56
|
-
|
57
|
-
add_offense_for_expression(node, args, ARG_MSG)
|
58
|
-
end
|
54
|
+
if args_on_different_line?(do_loc.line, args)
|
55
|
+
add_offense_for_expression(node, args, ARG_MSG)
|
59
56
|
end
|
60
57
|
|
61
58
|
return unless last_expression
|
@@ -64,11 +61,19 @@ module RuboCop
|
|
64
61
|
add_offense_for_expression(node, last_expression, MSG)
|
65
62
|
end
|
66
63
|
|
64
|
+
def oneliner?(node)
|
65
|
+
node.loc.begin.line == node.loc.end.line
|
66
|
+
end
|
67
|
+
|
68
|
+
def args_on_different_line?(do_line, args)
|
69
|
+
return false if args.children.empty?
|
70
|
+
|
71
|
+
do_line != args.loc.last_line
|
72
|
+
end
|
73
|
+
|
67
74
|
def add_offense_for_expression(node, expr, msg)
|
68
75
|
expression = expr.source_range
|
69
|
-
range =
|
70
|
-
expression.begin_pos,
|
71
|
-
expression.end_pos)
|
76
|
+
range = range_between(expression.begin_pos, expression.end_pos)
|
72
77
|
|
73
78
|
add_offense(node, range, msg)
|
74
79
|
end
|
@@ -76,7 +81,7 @@ module RuboCop
|
|
76
81
|
def autocorrect(node)
|
77
82
|
lambda do |corrector|
|
78
83
|
_method, args, block_body = *node
|
79
|
-
unless
|
84
|
+
unless arguments_on_different_line?(node, args)
|
80
85
|
autocorrect_arguments(corrector, node, args)
|
81
86
|
expr_before_body = args.source_range.end
|
82
87
|
end
|
@@ -90,18 +95,20 @@ module RuboCop
|
|
90
95
|
end
|
91
96
|
end
|
92
97
|
|
98
|
+
def arguments_on_different_line?(node, args)
|
99
|
+
args.children.empty? || args.loc.last_line == node.loc.line
|
100
|
+
end
|
101
|
+
|
93
102
|
def autocorrect_arguments(corrector, node, args)
|
94
103
|
end_pos =
|
95
104
|
range_with_surrounding_space(args.source_range, :right, false)
|
96
105
|
.end_pos
|
97
|
-
range =
|
98
|
-
node.loc.begin.end.begin_pos,
|
99
|
-
end_pos)
|
106
|
+
range = range_between(node.loc.begin.end.begin_pos, end_pos)
|
100
107
|
corrector.replace(range, " |#{block_arg_string(args)}|")
|
101
108
|
end
|
102
109
|
|
103
110
|
def autocorrect_body(corrector, node, block_body)
|
104
|
-
first_node = if block_body.
|
111
|
+
first_node = if block_body.begin_type?
|
105
112
|
block_body.children.first
|
106
113
|
else
|
107
114
|
block_body
|
@@ -84,6 +84,15 @@ module RuboCop
|
|
84
84
|
|
85
85
|
args
|
86
86
|
end
|
87
|
+
|
88
|
+
def ignored_literal?(node)
|
89
|
+
single_line_ignoring_receiver?(node) || super
|
90
|
+
end
|
91
|
+
|
92
|
+
def single_line_ignoring_receiver?(node)
|
93
|
+
return false unless node.loc.begin && node.loc.end
|
94
|
+
node.loc.begin.line == node.loc.end.line
|
95
|
+
end
|
87
96
|
end
|
88
97
|
end
|
89
98
|
end
|
@@ -36,13 +36,13 @@ module RuboCop
|
|
36
36
|
include MultilineExpressionIndentation
|
37
37
|
|
38
38
|
def validate_config
|
39
|
-
|
40
|
-
|
41
|
-
|
42
|
-
|
43
|
-
|
44
|
-
|
45
|
-
|
39
|
+
return unless style == :aligned && cop_config['IndentationWidth']
|
40
|
+
|
41
|
+
raise ValidationError,
|
42
|
+
'The `Style/MultilineMethodCallIndentation`' \
|
43
|
+
' cop only accepts an `IndentationWidth` ' \
|
44
|
+
'configuration parameter when ' \
|
45
|
+
'`EnforcedStyle` is `indented`.'
|
46
46
|
end
|
47
47
|
|
48
48
|
private
|
@@ -74,23 +74,44 @@ module RuboCop
|
|
74
74
|
end
|
75
75
|
|
76
76
|
def message(node, lhs, rhs)
|
77
|
-
if
|
78
|
-
|
79
|
-
|
80
|
-
|
81
|
-
"more than `#{base_source}` on line #{@base.line}."
|
82
|
-
else
|
83
|
-
"Align `#{rhs.source}` with `#{base_source}` on " \
|
84
|
-
"line #{@base.line}."
|
85
|
-
end
|
77
|
+
if should_indent_relative_to_receiver?
|
78
|
+
relative_to_receiver_message(rhs)
|
79
|
+
elsif should_align_with_base?
|
80
|
+
align_with_base_message(rhs)
|
86
81
|
else
|
87
|
-
|
88
|
-
what = operation_description(node, rhs)
|
89
|
-
"Use #{correct_indentation(node)} (not #{used_indentation}) " \
|
90
|
-
"spaces for indenting #{what} spanning multiple lines."
|
82
|
+
no_base_message(lhs, rhs, node)
|
91
83
|
end
|
92
84
|
end
|
93
85
|
|
86
|
+
def should_indent_relative_to_receiver?
|
87
|
+
@base && style == :indented_relative_to_receiver
|
88
|
+
end
|
89
|
+
|
90
|
+
def should_align_with_base?
|
91
|
+
@base && style != :indented_relative_to_receiver
|
92
|
+
end
|
93
|
+
|
94
|
+
def relative_to_receiver_message(rhs)
|
95
|
+
"Indent `#{rhs.source}` #{configured_indentation_width} spaces " \
|
96
|
+
"more than `#{base_source}` on line #{@base.line}."
|
97
|
+
end
|
98
|
+
|
99
|
+
def align_with_base_message(rhs)
|
100
|
+
"Align `#{rhs.source}` with `#{base_source}` on line #{@base.line}."
|
101
|
+
end
|
102
|
+
|
103
|
+
def base_source
|
104
|
+
@base.source[/[^\n]*/]
|
105
|
+
end
|
106
|
+
|
107
|
+
def no_base_message(lhs, rhs, node)
|
108
|
+
used_indentation = rhs.column - indentation(lhs)
|
109
|
+
what = operation_description(node, rhs)
|
110
|
+
|
111
|
+
"Use #{correct_indentation(node)} (not #{used_indentation}) " \
|
112
|
+
"spaces for indenting #{what} spanning multiple lines."
|
113
|
+
end
|
114
|
+
|
94
115
|
def alignment_base(node, rhs, given_style)
|
95
116
|
return nil if given_style == :indented
|
96
117
|
|
@@ -27,12 +27,12 @@ module RuboCop
|
|
27
27
|
end
|
28
28
|
|
29
29
|
def validate_config
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
30
|
+
return unless style == :aligned && cop_config['IndentationWidth']
|
31
|
+
|
32
|
+
raise ValidationError, 'The `Style/MultilineOperationIndentation`' \
|
33
|
+
' cop only accepts an `IndentationWidth` ' \
|
34
|
+
'configuration parameter when ' \
|
35
|
+
'`EnforcedStyle` is `indented`.'
|
36
36
|
end
|
37
37
|
|
38
38
|
private
|
@@ -12,13 +12,11 @@ module RuboCop
|
|
12
12
|
'use `if` or `unless` instead.'.freeze
|
13
13
|
|
14
14
|
def on_if(node)
|
15
|
-
_condition, _if_branch,
|
15
|
+
_condition, _if_branch, = *node
|
16
16
|
|
17
|
-
return unless ternary?(node)
|
17
|
+
return unless ternary?(node) && node.multiline?
|
18
18
|
|
19
|
-
|
20
|
-
add_offense(node, :expression)
|
21
|
-
end
|
19
|
+
add_offense(node, :expression)
|
22
20
|
end
|
23
21
|
end
|
24
22
|
end
|
@@ -14,10 +14,10 @@ module RuboCop
|
|
14
14
|
# # good
|
15
15
|
# CONST = [1, 2, 3].freeze
|
16
16
|
class MutableConstant < Cop
|
17
|
-
MSG = 'Freeze mutable objects assigned to constants.'.freeze
|
18
|
-
|
19
17
|
include FrozenStringLiteral
|
20
18
|
|
19
|
+
MSG = 'Freeze mutable objects assigned to constants.'.freeze
|
20
|
+
|
21
21
|
def on_casgn(node)
|
22
22
|
_scope, _const_name, value = *node
|
23
23
|
on_assignment(value)
|
@@ -25,7 +25,22 @@ module RuboCop
|
|
25
25
|
|
26
26
|
def on_or_asgn(node)
|
27
27
|
lhs, value = *node
|
28
|
-
|
28
|
+
|
29
|
+
return unless lhs && lhs.casgn_type?
|
30
|
+
|
31
|
+
on_assignment(value)
|
32
|
+
end
|
33
|
+
|
34
|
+
private
|
35
|
+
|
36
|
+
def on_assignment(value)
|
37
|
+
value = splat_value(value) if splat_value(value)
|
38
|
+
|
39
|
+
return unless value && value.mutable_literal?
|
40
|
+
return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) &&
|
41
|
+
frozen_string_literals_enabled?(processed_source)
|
42
|
+
|
43
|
+
add_offense(value, :expression)
|
29
44
|
end
|
30
45
|
|
31
46
|
def autocorrect(node)
|
@@ -40,16 +55,9 @@ module RuboCop
|
|
40
55
|
end
|
41
56
|
end
|
42
57
|
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
return unless value
|
47
|
-
return unless value.mutable_literal?
|
48
|
-
return if FROZEN_STRING_LITERAL_TYPES.include?(value.type) &&
|
49
|
-
frozen_string_literals_enabled?(processed_source)
|
50
|
-
|
51
|
-
add_offense(value, :expression)
|
52
|
-
end
|
58
|
+
def_node_matcher :splat_value, <<-PATTERN
|
59
|
+
(array (splat $_))
|
60
|
+
PATTERN
|
53
61
|
end
|
54
62
|
end
|
55
63
|
end
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
lambda do |corrector|
|
33
33
|
condition, _body, _rest = *node
|
34
34
|
# look inside parentheses around the condition
|
35
|
-
condition = condition.children.first while condition.
|
35
|
+
condition = condition.children.first while condition.begin_type?
|
36
36
|
# unwrap the negated portion of the condition (a send node)
|
37
37
|
pos_condition, _method, = *condition
|
38
38
|
corrector.replace(
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
end
|
20
20
|
|
21
21
|
def message(node)
|
22
|
-
if node.
|
22
|
+
if node.while_type?
|
23
23
|
format(MSG, 'until', 'while')
|
24
24
|
else
|
25
25
|
format(MSG, 'while', 'until')
|
@@ -32,12 +32,12 @@ module RuboCop
|
|
32
32
|
lambda do |corrector|
|
33
33
|
condition, _body, _rest = *node
|
34
34
|
# Look inside parentheses around the condition, if any.
|
35
|
-
condition, = *condition while condition.
|
35
|
+
condition, = *condition while condition.begin_type?
|
36
36
|
# Unwrap the negated portion of the condition (a send node).
|
37
37
|
pos_condition, _method, = *condition
|
38
38
|
corrector.replace(
|
39
39
|
node.loc.keyword,
|
40
|
-
node.
|
40
|
+
node.while_type? ? 'until' : 'while'
|
41
41
|
)
|
42
42
|
corrector.replace(condition.source_range, pos_condition.source)
|
43
43
|
end
|
@@ -66,10 +66,8 @@ module RuboCop
|
|
66
66
|
def autocorrect_if_unless(outer_node, inner_node)
|
67
67
|
outer_cond, = *outer_node
|
68
68
|
|
69
|
-
range =
|
70
|
-
|
71
|
-
inner_node.loc.keyword.begin_pos,
|
72
|
-
outer_cond.source_range.end_pos)
|
69
|
+
range = range_between(inner_node.loc.keyword.begin_pos,
|
70
|
+
outer_cond.source_range.end_pos)
|
73
71
|
|
74
72
|
lambda do |corrector|
|
75
73
|
corrector.replace(range, new_expression(outer_node, inner_node))
|