rubocop 0.47.1 → 0.48.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/README.md +28 -16
- data/config/default.yml +203 -115
- data/config/disabled.yml +0 -5
- data/config/enabled.yml +92 -8
- data/lib/rubocop.rb +24 -1
- data/lib/rubocop/ast/builder.rb +7 -2
- data/lib/rubocop/ast/node.rb +23 -20
- data/lib/rubocop/ast/node/and_node.rb +37 -0
- data/lib/rubocop/ast/node/array_node.rb +4 -1
- data/lib/rubocop/ast/node/case_node.rb +1 -0
- data/lib/rubocop/ast/node/ensure_node.rb +25 -0
- data/lib/rubocop/ast/node/hash_node.rb +10 -3
- data/lib/rubocop/ast/node/if_node.rb +2 -0
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +23 -0
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +35 -0
- data/lib/rubocop/ast/node/or_node.rb +37 -0
- data/lib/rubocop/ast/node/resbody_node.rb +25 -0
- data/lib/rubocop/ast/node/send_node.rb +190 -0
- data/lib/rubocop/ast/node/when_node.rb +1 -1
- data/lib/rubocop/ast/traversal.rb +15 -15
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +39 -15
- data/lib/rubocop/config_loader.rb +34 -13
- data/lib/rubocop/cop/bundler/ordered_gems.rb +23 -4
- data/lib/rubocop/cop/commissioner.rb +4 -0
- data/lib/rubocop/cop/cop.rb +5 -0
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +53 -0
- data/lib/rubocop/cop/lint/debugger.rb +8 -1
- data/lib/rubocop/cop/lint/def_end_alignment.rb +2 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +2 -4
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +6 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +20 -18
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -3
- data/lib/rubocop/cop/lint/literal_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/multiple_compare.rb +5 -3
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +26 -18
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +7 -8
- data/lib/rubocop/cop/lint/require_parentheses.rb +7 -13
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +7 -3
- data/lib/rubocop/cop/lint/shadowed_exception.rb +2 -6
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +7 -8
- data/lib/rubocop/cop/lint/unneeded_disable.rb +35 -11
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +5 -2
- data/lib/rubocop/cop/lint/unused_block_argument.rb +6 -6
- data/lib/rubocop/cop/lint/useless_assignment.rb +2 -1
- data/lib/rubocop/cop/lint/useless_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/metrics/abc_size.rb +1 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +4 -4
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +2 -2
- data/lib/rubocop/cop/metrics/line_length.rb +2 -9
- data/lib/rubocop/cop/metrics/parameter_lists.rb +4 -3
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +2 -2
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +1 -1
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/check_assignment.rb +6 -6
- data/lib/rubocop/cop/mixin/duplication.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +1 -1
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +27 -0
- data/lib/rubocop/cop/mixin/method_preference.rb +2 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +17 -29
- data/lib/rubocop/cop/mixin/on_method_def.rb +3 -3
- data/lib/rubocop/cop/mixin/percent_literal.rb +27 -0
- data/lib/rubocop/cop/mixin/rescue_node.rb +21 -0
- data/lib/rubocop/cop/mixin/safe_mode.rb +1 -1
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +2 -1
- data/lib/rubocop/cop/mixin/target_rails_version.rb +16 -0
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -1
- data/lib/rubocop/cop/offense.rb +3 -3
- data/lib/rubocop/cop/performance/casecmp.rb +1 -1
- data/lib/rubocop/cop/performance/detect.rb +2 -1
- data/lib/rubocop/cop/performance/double_start_end_with.rb +35 -1
- data/lib/rubocop/cop/performance/end_with.rb +3 -1
- data/lib/rubocop/cop/performance/flat_map.rb +6 -6
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +3 -1
- data/lib/rubocop/cop/performance/redundant_match.rb +6 -5
- data/lib/rubocop/cop/performance/regexp_match.rb +10 -3
- data/lib/rubocop/cop/performance/reverse_each.rb +2 -1
- data/lib/rubocop/cop/performance/size.rb +6 -11
- data/lib/rubocop/cop/performance/start_with.rb +3 -1
- data/lib/rubocop/cop/performance/string_replacement.rb +13 -18
- data/lib/rubocop/cop/performance/times_map.rb +4 -4
- data/lib/rubocop/cop/rails/action_filter.rb +42 -42
- data/lib/rubocop/cop/rails/active_support_aliases.rb +68 -0
- data/lib/rubocop/cop/rails/blank.rb +131 -0
- data/lib/rubocop/cop/rails/date.rb +25 -28
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +5 -7
- data/lib/rubocop/cop/rails/dynamic_find_by.rb +7 -3
- data/lib/rubocop/cop/rails/exit.rb +9 -9
- data/lib/rubocop/cop/rails/file_path.rb +5 -14
- data/lib/rubocop/cop/rails/find_by.rb +8 -10
- data/lib/rubocop/cop/rails/find_each.rb +6 -9
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +1 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +15 -7
- data/lib/rubocop/cop/rails/output.rb +3 -5
- data/lib/rubocop/cop/rails/output_safety.rb +4 -8
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +25 -24
- data/lib/rubocop/cop/rails/present.rb +137 -0
- data/lib/rubocop/cop/rails/read_write_attribute.rb +9 -18
- data/lib/rubocop/cop/rails/relative_date_constant.rb +53 -0
- data/lib/rubocop/cop/rails/request_referer.rb +7 -4
- data/lib/rubocop/cop/rails/reversible_migration.rb +1 -2
- data/lib/rubocop/cop/rails/safe_navigation.rb +2 -1
- data/lib/rubocop/cop/rails/save_bang.rb +10 -10
- data/lib/rubocop/cop/rails/skips_model_validations.rb +23 -6
- data/lib/rubocop/cop/rails/time_zone.rb +20 -18
- data/lib/rubocop/cop/rails/uniq_before_pluck.rb +3 -2
- data/lib/rubocop/cop/rails/validation.rb +8 -11
- data/lib/rubocop/cop/registry.rb +3 -3
- data/lib/rubocop/cop/security/json_load.rb +1 -1
- data/lib/rubocop/cop/security/marshal_load.rb +5 -1
- data/lib/rubocop/cop/security/yaml_load.rb +3 -3
- data/lib/rubocop/cop/severity.rb +1 -1
- data/lib/rubocop/cop/style/alias.rb +5 -5
- data/lib/rubocop/cop/style/align_hash.rb +1 -1
- data/lib/rubocop/cop/style/align_parameters.rb +5 -5
- data/lib/rubocop/cop/style/and_or.rb +16 -31
- data/lib/rubocop/cop/style/attr.rb +14 -8
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +8 -11
- data/lib/rubocop/cop/style/block_delimiters.rb +11 -13
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +19 -23
- data/lib/rubocop/cop/style/case_indentation.rb +2 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_check.rb +5 -7
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +5 -4
- data/lib/rubocop/cop/style/collection_methods.rb +8 -8
- data/lib/rubocop/cop/style/colon_method_call.rb +2 -9
- data/lib/rubocop/cop/style/conditional_assignment.rb +38 -45
- data/lib/rubocop/cop/style/constant_name.rb +1 -1
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/dot_position.rb +3 -7
- data/lib/rubocop/cop/style/double_negation.rb +2 -1
- data/lib/rubocop/cop/style/each_with_object.rb +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +2 -2
- data/lib/rubocop/cop/style/empty_line_after_magic_comment.rb +63 -0
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +74 -4
- data/lib/rubocop/cop/style/empty_lines_around_begin_body.rb +42 -0
- data/lib/rubocop/cop/style/empty_lines_around_exception_handling_keywords.rb +127 -0
- data/lib/rubocop/cop/style/empty_literal.rb +17 -9
- data/lib/rubocop/cop/style/end_of_line.rb +25 -3
- data/lib/rubocop/cop/style/file_name.rb +1 -1
- data/lib/rubocop/cop/style/first_method_argument_line_break.rb +1 -1
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +17 -19
- data/lib/rubocop/cop/style/for.rb +2 -4
- data/lib/rubocop/cop/style/format_string.rb +5 -4
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +27 -1
- data/lib/rubocop/cop/style/if_unless_modifier.rb +2 -2
- data/lib/rubocop/cop/style/indent_assignment.rb +2 -2
- data/lib/rubocop/cop/style/indent_hash.rb +2 -1
- data/lib/rubocop/cop/style/indent_heredoc.rb +173 -0
- data/lib/rubocop/cop/style/indentation_width.rb +61 -29
- data/lib/rubocop/cop/style/inverse_methods.rb +130 -0
- data/lib/rubocop/cop/style/lambda_call.rb +15 -11
- data/lib/rubocop/cop/style/line_end_concatenation.rb +4 -4
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +26 -14
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -16
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +4 -1
- data/lib/rubocop/cop/style/missing_else.rb +4 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +97 -0
- data/lib/rubocop/cop/style/multiline_memoization.rb +38 -5
- data/lib/rubocop/cop/style/multiline_method_call_brace_layout.rb +2 -3
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +38 -19
- data/lib/rubocop/cop/style/mutable_constant.rb +5 -1
- data/lib/rubocop/cop/style/negated_if.rb +73 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +21 -19
- data/lib/rubocop/cop/style/next.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +7 -10
- data/lib/rubocop/cop/style/not.rb +3 -4
- data/lib/rubocop/cop/style/numeric_literals.rb +25 -3
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -2
- data/lib/rubocop/cop/style/op_method.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -3
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +52 -6
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -9
- data/lib/rubocop/cop/style/raise_args.rb +28 -24
- data/lib/rubocop/cop/style/redundant_freeze.rb +5 -7
- data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self.rb +17 -35
- data/lib/rubocop/cop/style/rescue_modifier.rb +2 -14
- data/lib/rubocop/cop/style/self_assignment.rb +3 -3
- data/lib/rubocop/cop/style/send.rb +4 -5
- data/lib/rubocop/cop/style/space_after_not.rb +7 -8
- data/lib/rubocop/cop/style/space_around_keyword.rb +8 -9
- data/lib/rubocop/cop/style/space_around_operators.rb +19 -15
- data/lib/rubocop/cop/style/space_before_first_arg.rb +17 -14
- data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -1
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +3 -3
- data/lib/rubocop/cop/style/space_inside_parens.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +14 -14
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/string_literals.rb +1 -1
- data/lib/rubocop/cop/style/string_methods.rb +10 -5
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -15
- data/lib/rubocop/cop/style/symbol_array.rb +31 -35
- data/lib/rubocop/cop/style/symbol_proc.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +41 -13
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +6 -9
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -2
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +12 -34
- data/lib/rubocop/cop/style/zero_length_predicate.rb +11 -4
- data/lib/rubocop/cop/team.rb +4 -1
- data/lib/rubocop/cop/util.rb +33 -26
- data/lib/rubocop/cop/variable_force.rb +13 -13
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -8
- data/lib/rubocop/cop/variable_force/branch.rb +318 -0
- data/lib/rubocop/cop/variable_force/branchable.rb +21 -0
- data/lib/rubocop/cop/variable_force/reference.rb +1 -3
- data/lib/rubocop/cop/variable_force/scope.rb +36 -20
- data/lib/rubocop/cop/variable_force/variable.rb +9 -8
- data/lib/rubocop/formatter/colorizable.rb +10 -10
- data/lib/rubocop/formatter/formatter_set.rb +1 -1
- data/lib/rubocop/formatter/html_formatter.rb +2 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +4 -2
- data/lib/rubocop/magic_comment.rb +20 -6
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/platform.rb +11 -0
- data/lib/rubocop/processed_source.rb +1 -1
- data/lib/rubocop/remote_config.rb +18 -6
- data/lib/rubocop/result_cache.rb +8 -8
- data/lib/rubocop/rspec/cop_helper.rb +2 -0
- data/lib/rubocop/rspec/shared_contexts.rb +20 -0
- data/lib/rubocop/rspec/shared_examples.rb +1 -1
- data/lib/rubocop/runner.rb +2 -2
- data/lib/rubocop/target_finder.rb +64 -6
- data/lib/rubocop/version.rb +2 -4
- metadata +27 -4
- data/lib/rubocop/cop/variable_force/locatable.rb +0 -200
@@ -19,12 +19,12 @@ module RuboCop
|
|
19
19
|
MSG = 'Prefer using `YAML.safe_load` over `YAML.load`.'.freeze
|
20
20
|
|
21
21
|
def_node_matcher :yaml_load, <<-END
|
22
|
-
(send (const nil :YAML) :load ...)
|
22
|
+
(send (const {nil cbase} :YAML) :load ...)
|
23
23
|
END
|
24
24
|
|
25
25
|
def on_send(node)
|
26
|
-
yaml_load(node) do
|
27
|
-
add_offense(node, :selector
|
26
|
+
yaml_load(node) do
|
27
|
+
add_offense(node, :selector)
|
28
28
|
end
|
29
29
|
end
|
30
30
|
|
data/lib/rubocop/cop/severity.rb
CHANGED
@@ -15,7 +15,7 @@ module RuboCop
|
|
15
15
|
MSG_SYMBOL_ARGS = 'Use `alias %s` instead of `alias %s`.'.freeze
|
16
16
|
|
17
17
|
def on_send(node)
|
18
|
-
return unless node.
|
18
|
+
return unless node.command?(:alias_method)
|
19
19
|
return if style == :prefer_alias_method
|
20
20
|
return if scope_type(node) == :dynamic
|
21
21
|
|
@@ -86,14 +86,14 @@ module RuboCop
|
|
86
86
|
end
|
87
87
|
|
88
88
|
def bareword?(sym_node)
|
89
|
-
sym_node.source
|
89
|
+
!sym_node.source.start_with?(':')
|
90
90
|
end
|
91
91
|
|
92
|
-
def correct_alias_method_to_alias(
|
92
|
+
def correct_alias_method_to_alias(send_node)
|
93
93
|
lambda do |corrector|
|
94
|
-
new, old = *
|
94
|
+
new, old = *send_node.arguments
|
95
95
|
replacement = "alias #{new.children.first} #{old.children.first}"
|
96
|
-
corrector.replace(
|
96
|
+
corrector.replace(send_node.source_range, replacement)
|
97
97
|
end
|
98
98
|
end
|
99
99
|
|
@@ -16,23 +16,23 @@ module RuboCop
|
|
16
16
|
'following the first line of a multi-line method %s.'.freeze
|
17
17
|
|
18
18
|
def on_send(node)
|
19
|
-
|
19
|
+
return if node.arguments.size < 2 || node.method?(:[]=)
|
20
20
|
|
21
|
-
|
22
|
-
return if args.size < 2
|
23
|
-
|
24
|
-
check_alignment(args, base_column(node, args))
|
21
|
+
check_alignment(node.arguments, base_column(node, node.arguments))
|
25
22
|
end
|
26
23
|
|
27
24
|
def on_method_def(node, _method_name, args, _body)
|
28
25
|
args = args.children
|
26
|
+
|
29
27
|
return if args.size < 2
|
28
|
+
|
30
29
|
check_alignment(args, base_column(node, args))
|
31
30
|
end
|
32
31
|
|
33
32
|
def message(node)
|
34
33
|
type = node && node.parent.send_type? ? 'call' : 'definition'
|
35
34
|
msg = fixed_indentation? ? FIXED_INDENT_MSG : ALIGN_PARAMS_MSG
|
35
|
+
|
36
36
|
format(msg, type)
|
37
37
|
end
|
38
38
|
|
@@ -42,26 +42,21 @@ module RuboCop
|
|
42
42
|
private
|
43
43
|
|
44
44
|
def on_conditionals(node)
|
45
|
-
|
46
|
-
|
47
|
-
condition_node.each_node(*LOGICAL_OPERATOR_NODES) do |logical_node|
|
45
|
+
node.condition.each_node(*LOGICAL_OPERATOR_NODES) do |logical_node|
|
48
46
|
process_logical_op(logical_node)
|
49
47
|
end
|
50
48
|
end
|
51
49
|
|
52
50
|
def process_logical_op(node)
|
53
|
-
|
54
|
-
op_type = node.type.to_s
|
55
|
-
return unless op == op_type
|
51
|
+
return if node.logical_operator?
|
56
52
|
|
57
|
-
add_offense(node, :operator,
|
53
|
+
add_offense(node, :operator,
|
54
|
+
format(MSG, node.alternate_operator, node.operator))
|
58
55
|
end
|
59
56
|
|
60
57
|
def autocorrect(node)
|
61
|
-
expr1, expr2 = *node
|
62
|
-
replacement = (node.and_type? ? '&&' : '||')
|
63
58
|
lambda do |corrector|
|
64
|
-
[
|
59
|
+
[*node].each do |expr|
|
65
60
|
if expr.send_type?
|
66
61
|
correct_send(expr, corrector)
|
67
62
|
elsif expr.return_type?
|
@@ -70,24 +65,23 @@ module RuboCop
|
|
70
65
|
correct_other(expr, corrector)
|
71
66
|
end
|
72
67
|
end
|
73
|
-
|
68
|
+
|
69
|
+
corrector.replace(node.loc.operator, node.alternate_operator)
|
74
70
|
end
|
75
71
|
end
|
76
72
|
|
77
73
|
def correct_send(node, corrector)
|
78
|
-
|
79
|
-
return
|
80
|
-
return correct_setter(node, corrector) if setter_method?(method_name)
|
74
|
+
return correct_not(node, node.receiver, corrector) if node.method?(:!)
|
75
|
+
return correct_setter(node, corrector) if node.setter_method?
|
81
76
|
return unless correctable_send?(node)
|
82
77
|
|
83
78
|
corrector.replace(whitespace_before_arg(node), '('.freeze)
|
84
|
-
corrector.insert_after(
|
79
|
+
corrector.insert_after(node.last_argument.source_range, ')'.freeze)
|
85
80
|
end
|
86
81
|
|
87
82
|
def correct_setter(node, corrector)
|
88
|
-
receiver,
|
89
|
-
corrector.
|
90
|
-
corrector.insert_after(args.last.source_range, ')'.freeze)
|
83
|
+
corrector.insert_before(node.receiver.source_range, '('.freeze)
|
84
|
+
corrector.insert_after(node.last_argument.source_range, ')'.freeze)
|
91
85
|
end
|
92
86
|
|
93
87
|
# ! is a special case:
|
@@ -95,7 +89,7 @@ module RuboCop
|
|
95
89
|
# recurse down a level and add parens to 'obj.method arg'
|
96
90
|
# however, 'not x' also parses as (send x :!)
|
97
91
|
def correct_not(node, receiver, corrector)
|
98
|
-
if node.
|
92
|
+
if node.keyword_bang?
|
99
93
|
return unless receiver.send_type?
|
100
94
|
|
101
95
|
correct_send(receiver, corrector)
|
@@ -107,23 +101,14 @@ module RuboCop
|
|
107
101
|
end
|
108
102
|
|
109
103
|
def correct_other(node, corrector)
|
110
|
-
return
|
104
|
+
return if node.source_range.begin.is?('(')
|
105
|
+
|
111
106
|
corrector.insert_before(node.source_range, '(')
|
112
107
|
corrector.insert_after(node.source_range, ')')
|
113
108
|
end
|
114
109
|
|
115
|
-
def setter_method?(method_name)
|
116
|
-
method_name.to_s.end_with?('=')
|
117
|
-
end
|
118
|
-
|
119
110
|
def correctable_send?(node)
|
120
|
-
|
121
|
-
# don't clobber if we already have a starting paren
|
122
|
-
return false unless !node.loc.begin || node.loc.begin.source != '('
|
123
|
-
# don't touch anything unless we are sure it is a method call.
|
124
|
-
return false unless args.last && method_name.to_s =~ /[a-z]/
|
125
|
-
|
126
|
-
true
|
111
|
+
!node.parenthesized? && node.arguments?
|
127
112
|
end
|
128
113
|
|
129
114
|
def whitespace_before_arg(node)
|
@@ -5,16 +5,19 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for uses of Module#attr.
|
7
7
|
class Attr < Cop
|
8
|
+
MSG = 'Do not use `attr`. Use `%s` instead.'.freeze
|
9
|
+
|
8
10
|
def on_send(node)
|
9
|
-
return unless node.command?(:attr)
|
10
|
-
_receiver, _method_name, *args = *node
|
11
|
-
msg = "Do not use `attr`. Use `#{replacement_method(node)}` instead."
|
11
|
+
return unless node.command?(:attr) && node.arguments?
|
12
12
|
|
13
|
-
add_offense(node, :selector
|
13
|
+
add_offense(node, :selector)
|
14
14
|
end
|
15
15
|
|
16
|
+
private
|
17
|
+
|
16
18
|
def autocorrect(node)
|
17
|
-
|
19
|
+
attr_name, setter = *node.arguments
|
20
|
+
|
18
21
|
node_expr = node.source_range
|
19
22
|
attr_expr = attr_name.source_range
|
20
23
|
|
@@ -24,14 +27,17 @@ module RuboCop
|
|
24
27
|
|
25
28
|
lambda do |corrector|
|
26
29
|
corrector.replace(node.loc.selector, replacement_method(node))
|
27
|
-
corrector.
|
30
|
+
corrector.remove(remove) if remove
|
28
31
|
end
|
29
32
|
end
|
30
33
|
|
31
|
-
|
34
|
+
def message(node)
|
35
|
+
format(MSG, replacement_method(node))
|
36
|
+
end
|
32
37
|
|
33
38
|
def replacement_method(node)
|
34
|
-
|
39
|
+
setter = node.last_argument
|
40
|
+
|
35
41
|
if setter && (setter.true_type? || setter.false_type?)
|
36
42
|
setter.true_type? ? 'attr_accessor' : 'attr_reader'
|
37
43
|
else
|
@@ -19,23 +19,20 @@ module RuboCop
|
|
19
19
|
class AutoResourceCleanup < Cop
|
20
20
|
MSG = 'Use the block version of `%s.%s`.'.freeze
|
21
21
|
|
22
|
-
TARGET_METHODS =
|
23
|
-
|
24
|
-
|
22
|
+
TARGET_METHODS = {
|
23
|
+
File: :open
|
24
|
+
}.freeze
|
25
25
|
|
26
26
|
def on_send(node)
|
27
|
-
|
28
|
-
|
29
|
-
TARGET_METHODS.each do |(target_class, target_method)|
|
27
|
+
TARGET_METHODS.each do |target_class, target_method|
|
30
28
|
target_receiver = s(:const, nil, target_class)
|
31
29
|
|
32
|
-
next if
|
33
|
-
next if method_name != target_method
|
30
|
+
next if node.receiver != target_receiver
|
31
|
+
next if node.method_name != target_method
|
34
32
|
next if node.parent && node.parent.block_type?
|
35
|
-
next if
|
33
|
+
next if node.block_argument?
|
36
34
|
|
37
|
-
add_offense(node,
|
38
|
-
:expression,
|
35
|
+
add_offense(node, :expression,
|
39
36
|
format(MSG, target_class, target_method))
|
40
37
|
end
|
41
38
|
end
|
@@ -9,11 +9,10 @@ module RuboCop
|
|
9
9
|
include ConfigurableEnforcedStyle
|
10
10
|
|
11
11
|
def on_send(node)
|
12
|
-
|
13
|
-
return if
|
14
|
-
return if parentheses?(node) || operator?(method_name)
|
12
|
+
return unless node.arguments?
|
13
|
+
return if node.parenthesized? || node.operator_method?
|
15
14
|
|
16
|
-
|
15
|
+
node.arguments.each do |arg|
|
17
16
|
get_blocks(arg) do |block|
|
18
17
|
# If there are no parentheses around the arguments, then braces
|
19
18
|
# and do-end have different meaning due to how they bind, so we
|
@@ -116,21 +115,22 @@ module RuboCop
|
|
116
115
|
when :block
|
117
116
|
yield node
|
118
117
|
when :send
|
119
|
-
receiver,
|
120
|
-
get_blocks(receiver, &block) if receiver
|
118
|
+
get_blocks(node.receiver, &block) if node.receiver
|
121
119
|
when :hash
|
122
120
|
# A hash which is passed as method argument may have no braces
|
123
121
|
# In that case, one of the K/V pairs could contain a block node
|
124
122
|
# which could change in meaning if do...end replaced {...}
|
125
123
|
return if node.loc.begin
|
126
|
-
node.
|
124
|
+
node.each_child_node { |child| get_blocks(child, &block) }
|
127
125
|
when :pair
|
128
|
-
node.
|
126
|
+
node.each_child_node { |child| get_blocks(child, &block) }
|
129
127
|
end
|
130
128
|
nil
|
131
129
|
end
|
132
130
|
|
133
131
|
def proper_block_style?(node)
|
132
|
+
return true if ignored_method?(node.method_name)
|
133
|
+
|
134
134
|
case style
|
135
135
|
when :line_count_based then line_count_based_block_style?(node)
|
136
136
|
when :semantic then semantic_block_style?(node)
|
@@ -146,8 +146,6 @@ module RuboCop
|
|
146
146
|
|
147
147
|
def semantic_block_style?(node)
|
148
148
|
method_name = node.method_name
|
149
|
-
return true if ignored_method?(method_name)
|
150
|
-
|
151
149
|
block_begin = node.loc.begin.source
|
152
150
|
|
153
151
|
if block_begin == '{'
|
@@ -169,7 +167,7 @@ module RuboCop
|
|
169
167
|
end
|
170
168
|
|
171
169
|
def return_value_chaining?(node)
|
172
|
-
node.parent && node.parent.send_type? && node.parent.
|
170
|
+
node.parent && node.parent.send_type? && node.parent.dot?
|
173
171
|
end
|
174
172
|
|
175
173
|
def correction_would_break_code?(node)
|
@@ -178,8 +176,8 @@ module RuboCop
|
|
178
176
|
# Converting `obj.method arg do |x| end` to use `{}` would cause
|
179
177
|
# a syntax error.
|
180
178
|
send = node.children.first
|
181
|
-
|
182
|
-
|
179
|
+
|
180
|
+
send.arguments? && !send.parenthesized?
|
183
181
|
end
|
184
182
|
|
185
183
|
def ignored_method?(method_name)
|
@@ -11,26 +11,21 @@ module RuboCop
|
|
11
11
|
MSG = '%s curly braces around a hash parameter.'.freeze
|
12
12
|
|
13
13
|
def on_send(node)
|
14
|
-
return if node.asgn_method_call?
|
14
|
+
return if node.asgn_method_call? || node.operator_method?
|
15
15
|
|
16
|
-
|
16
|
+
return unless node.arguments? && node.last_argument.hash_type? &&
|
17
|
+
!node.last_argument.empty?
|
17
18
|
|
18
|
-
|
19
|
-
return if args.empty?
|
20
|
-
|
21
|
-
return unless args.last.hash_type? && !args.last.pairs.empty?
|
22
|
-
|
23
|
-
check(args.last, args)
|
19
|
+
check(node.last_argument, node.arguments)
|
24
20
|
end
|
25
21
|
|
26
22
|
private
|
27
23
|
|
28
24
|
def check(arg, args)
|
29
25
|
if style == :braces && !arg.braces?
|
30
|
-
|
26
|
+
add_arg_offense(arg, :missing)
|
31
27
|
elsif style == :no_braces && arg.braces?
|
32
|
-
|
33
|
-
format(MSG, 'Redundant'))
|
28
|
+
add_arg_offense(arg, :redundant)
|
34
29
|
elsif style == :context_dependent
|
35
30
|
check_context_dependent(arg, args)
|
36
31
|
end
|
@@ -38,28 +33,33 @@ module RuboCop
|
|
38
33
|
|
39
34
|
def check_context_dependent(arg, args)
|
40
35
|
braces_around_second_from_end = args.size > 1 && args[-2].hash_type?
|
36
|
+
|
41
37
|
if arg.braces?
|
42
38
|
unless braces_around_second_from_end
|
43
|
-
|
44
|
-
format(MSG, 'Redundant'))
|
39
|
+
add_arg_offense(arg, :redundant)
|
45
40
|
end
|
46
41
|
elsif braces_around_second_from_end
|
47
|
-
|
42
|
+
add_arg_offense(arg, :missing)
|
48
43
|
end
|
49
44
|
end
|
50
45
|
|
46
|
+
def add_arg_offense(arg, type)
|
47
|
+
add_offense(arg.parent, arg.source_range,
|
48
|
+
format(MSG, type.to_s.capitalize))
|
49
|
+
end
|
50
|
+
|
51
51
|
# We let AutocorrectUnlessChangingAST#autocorrect work with the send
|
52
52
|
# node, because that context is needed. When parsing the code to see if
|
53
53
|
# the AST has changed, a braceless hash would not be parsed as a hash
|
54
54
|
# otherwise.
|
55
55
|
def autocorrect(send_node)
|
56
|
-
|
57
|
-
|
56
|
+
hash_node = send_node.last_argument
|
57
|
+
|
58
58
|
lambda do |corrector|
|
59
|
-
if
|
60
|
-
remove_braces_with_whitespace(corrector,
|
59
|
+
if hash_node.braces?
|
60
|
+
remove_braces_with_whitespace(corrector, hash_node)
|
61
61
|
else
|
62
|
-
add_braces(corrector,
|
62
|
+
add_braces(corrector, hash_node)
|
63
63
|
end
|
64
64
|
end
|
65
65
|
end
|
@@ -98,10 +98,6 @@ module RuboCop
|
|
98
98
|
corrector.insert_before(node.source_range, '{')
|
99
99
|
corrector.insert_after(node.source_range, '}')
|
100
100
|
end
|
101
|
-
|
102
|
-
def braces?(arg)
|
103
|
-
arg.loc.begin
|
104
|
-
end
|
105
101
|
end
|
106
102
|
end
|
107
103
|
end
|