rubocop 1.9.1 → 1.12.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 +13 -13
- data/assets/output.html.erb +1 -1
- data/config/default.yml +67 -17
- data/config/obsoletion.yml +4 -0
- data/lib/rubocop.rb +4 -0
- data/lib/rubocop/cli/command/execute_runner.rb +1 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +3 -2
- data/lib/rubocop/comment_config.rb +43 -94
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +2 -1
- data/lib/rubocop/cop/bundler/gem_comment.rb +1 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -0
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +3 -6
- data/lib/rubocop/cop/gemspec/date_assignment.rb +57 -0
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -0
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +2 -0
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +2 -0
- data/lib/rubocop/cop/generator.rb +2 -2
- data/lib/rubocop/cop/internal_affairs.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/node_destructuring.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +151 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +2 -0
- data/lib/rubocop/cop/internal_affairs/redundant_described_class_as_subject.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +3 -0
- data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +4 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +2 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +11 -8
- data/lib/rubocop/cop/layout/argument_alignment.rb +6 -5
- data/lib/rubocop/cop/layout/array_alignment.rb +7 -6
- data/lib/rubocop/cop/layout/assignment_indentation.rb +6 -3
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/block_end_newline.rb +4 -8
- data/lib/rubocop/cop/layout/class_structure.rb +1 -0
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +14 -15
- data/lib/rubocop/cop/layout/comment_indentation.rb +16 -16
- data/lib/rubocop/cop/layout/else_alignment.rb +9 -6
- data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +12 -6
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +9 -6
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +22 -15
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/indentation_consistency.rb +9 -6
- data/lib/rubocop/cop/layout/indentation_style.rb +27 -30
- data/lib/rubocop/cop/layout/indentation_width.rb +20 -9
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -5
- data/lib/rubocop/cop/layout/parameter_alignment.rb +6 -5
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/lint/big_decimal_new.rb +1 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -0
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +2 -0
- data/lib/rubocop/cop/lint/constant_resolution.rb +1 -0
- data/lib/rubocop/cop/lint/debugger.rb +60 -14
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +14 -4
- data/lib/rubocop/cop/lint/duplicate_branch.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +3 -0
- data/lib/rubocop/cop/lint/duplicate_require.rb +3 -2
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -0
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -0
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +1 -0
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +1 -0
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +1 -0
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -0
- data/lib/rubocop/cop/lint/multiple_comparison.rb +5 -4
- data/lib/rubocop/cop/lint/nested_method_definition.rb +3 -0
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -0
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +7 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +3 -0
- data/lib/rubocop/cop/lint/number_conversion.rb +9 -0
- data/lib/rubocop/cop/lint/raise_exception.rb +2 -0
- data/lib/rubocop/cop/lint/rand_one.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -2
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +7 -3
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_with_index.rb +1 -0
- data/lib/rubocop/cop/lint/redundant_with_object.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -0
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +1 -0
- data/lib/rubocop/cop/lint/send_with_mixin_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -0
- data/lib/rubocop/cop/lint/struct_new_override.rb +1 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +44 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +89 -2
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
- data/lib/rubocop/cop/lint/unified_integer.rb +1 -0
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +5 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -0
- data/lib/rubocop/cop/lint/unreachable_loop.rb +1 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -0
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +1 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -0
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -0
- data/lib/rubocop/cop/lint/useless_times.rb +3 -0
- data/lib/rubocop/cop/metrics/module_length.rb +1 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -0
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +6 -4
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +2 -0
- data/lib/rubocop/cop/mixin/alignment.rb +10 -3
- data/lib/rubocop/cop/mixin/comments_help.rb +5 -1
- data/lib/rubocop/cop/mixin/def_node.rb +1 -0
- data/lib/rubocop/cop/mixin/documentation_comment.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +3 -0
- data/lib/rubocop/cop/mixin/empty_parameter.rb +1 -0
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -0
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +1 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +11 -6
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +3 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +4 -3
- data/lib/rubocop/cop/mixin/negative_conditional.rb +3 -0
- data/lib/rubocop/cop/mixin/preferred_delimiters.rb +3 -3
- data/lib/rubocop/cop/mixin/rational_literal.rb +1 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +5 -0
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +4 -6
- data/lib/rubocop/cop/mixin/visibility_help.rb +1 -0
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +1 -0
- data/lib/rubocop/cop/naming/constant_name.rb +2 -0
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +6 -0
- data/lib/rubocop/cop/naming/method_name.rb +3 -0
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +10 -0
- data/lib/rubocop/cop/registry.rb +10 -1
- data/lib/rubocop/cop/security/eval.rb +1 -0
- data/lib/rubocop/cop/security/json_load.rb +1 -0
- data/lib/rubocop/cop/security/marshal_load.rb +1 -0
- data/lib/rubocop/cop/security/open.rb +1 -0
- data/lib/rubocop/cop/security/yaml_load.rb +1 -0
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +2 -1
- data/lib/rubocop/cop/style/alias.rb +1 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +3 -0
- data/lib/rubocop/cop/style/array_coercion.rb +2 -0
- data/lib/rubocop/cop/style/array_join.rb +1 -0
- data/lib/rubocop/cop/style/attr.rb +1 -0
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +59 -71
- data/lib/rubocop/cop/style/bisected_attr_accessor/macro.rb +62 -0
- data/lib/rubocop/cop/style/case_equality.rb +2 -1
- data/lib/rubocop/cop/style/case_like_if.rb +15 -4
- data/lib/rubocop/cop/style/class_equality_comparison.rb +3 -0
- data/lib/rubocop/cop/style/collection_compact.rb +2 -0
- data/lib/rubocop/cop/style/colon_method_call.rb +1 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -0
- data/lib/rubocop/cop/style/constant_visibility.rb +28 -0
- data/lib/rubocop/cop/style/date_time.rb +3 -0
- data/lib/rubocop/cop/style/dir.rb +1 -0
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +2 -2
- data/lib/rubocop/cop/style/documentation.rb +30 -3
- data/lib/rubocop/cop/style/documentation_method.rb +1 -0
- data/lib/rubocop/cop/style/double_negation.rb +3 -2
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -0
- data/lib/rubocop/cop/style/each_with_object.rb +1 -0
- data/lib/rubocop/cop/style/empty_literal.rb +9 -0
- data/lib/rubocop/cop/style/endless_method.rb +1 -0
- data/lib/rubocop/cop/style/eval_with_location.rb +89 -27
- data/lib/rubocop/cop/style/even_odd.rb +1 -0
- data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
- data/lib/rubocop/cop/style/float_division.rb +4 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -0
- data/lib/rubocop/cop/style/format_string_token.rb +1 -0
- data/lib/rubocop/cop/style/global_std_stream.rb +1 -0
- data/lib/rubocop/cop/style/hash_conversion.rb +105 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -0
- data/lib/rubocop/cop/style/hash_except.rb +1 -0
- data/lib/rubocop/cop/style/hash_like_case.rb +1 -0
- data/lib/rubocop/cop/style/hash_syntax.rb +16 -15
- data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +2 -0
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +1 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +2 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +40 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -2
- data/lib/rubocop/cop/style/min_max.rb +1 -0
- data/lib/rubocop/cop/style/mixin_usage.rb +2 -0
- data/lib/rubocop/cop/style/module_function.rb +5 -0
- data/lib/rubocop/cop/style/multiple_comparison.rb +21 -2
- data/lib/rubocop/cop/style/mutable_constant.rb +3 -0
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +16 -2
- data/lib/rubocop/cop/style/nil_comparison.rb +3 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
- data/lib/rubocop/cop/style/non_nil_check.rb +7 -0
- data/lib/rubocop/cop/style/numeric_predicate.rb +3 -0
- data/lib/rubocop/cop/style/option_hash.rb +1 -0
- data/lib/rubocop/cop/style/or_assignment.rb +2 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +6 -0
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +1 -0
- data/lib/rubocop/cop/style/proc.rb +1 -0
- data/lib/rubocop/cop/style/random_with_offset.rb +5 -0
- data/lib/rubocop/cop/style/redundant_assignment.rb +1 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +33 -4
- data/lib/rubocop/cop/style/redundant_conditional.rb +2 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +2 -0
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +2 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -0
- data/lib/rubocop/cop/style/redundant_return.rb +4 -0
- data/lib/rubocop/cop/style/redundant_self.rb +7 -3
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +2 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -0
- data/lib/rubocop/cop/style/redundant_sort_by.rb +1 -0
- data/lib/rubocop/cop/style/regexp_literal.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +17 -14
- data/lib/rubocop/cop/style/rescue_standard_error.rb +2 -0
- data/lib/rubocop/cop/style/return_nil.rb +6 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -0
- data/lib/rubocop/cop/style/sample.rb +1 -0
- data/lib/rubocop/cop/style/signal_exception.rb +3 -0
- data/lib/rubocop/cop/style/single_argument_dig.rb +1 -0
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +16 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -0
- data/lib/rubocop/cop/style/string_chars.rb +38 -0
- data/lib/rubocop/cop/style/string_concatenation.rb +1 -0
- data/lib/rubocop/cop/style/string_hash_keys.rb +2 -0
- data/lib/rubocop/cop/style/strip.rb +1 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +1 -0
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +5 -0
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -0
- data/lib/rubocop/cop/style/unless_logical_operators.rb +105 -0
- data/lib/rubocop/cop/style/unpack_first.rb +1 -0
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
- data/lib/rubocop/directive_comment.rb +64 -9
- data/lib/rubocop/ext/regexp_parser.rb +3 -6
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/name_similarity.rb +1 -1
- data/lib/rubocop/target_finder.rb +1 -0
- data/lib/rubocop/target_ruby.rb +21 -13
- data/lib/rubocop/version.rb +1 -1
- metadata +13 -7
@@ -5,6 +5,7 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
class MethodCallWithArgsParentheses
|
7
7
|
# Style omit_parentheses
|
8
|
+
# rubocop:disable Metrics/ModuleLength
|
8
9
|
module OmitParentheses
|
9
10
|
TRAILING_WHITESPACE_REGEX = /\s+\Z/.freeze
|
10
11
|
OMIT_MSG = 'Omit parentheses for method calls with arguments.'
|
@@ -12,18 +13,21 @@ module RuboCop
|
|
12
13
|
|
13
14
|
private
|
14
15
|
|
16
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
15
17
|
def omit_parentheses(node)
|
16
18
|
return unless node.parenthesized?
|
17
19
|
return if inside_endless_method_def?(node)
|
18
|
-
return if node
|
20
|
+
return if syntax_like_method_call?(node)
|
19
21
|
return if super_call_without_arguments?(node)
|
20
22
|
return if allowed_camel_case_method_call?(node)
|
21
23
|
return if legitimate_call_with_parentheses?(node)
|
24
|
+
return if allowed_string_interpolation_method_call?(node)
|
22
25
|
|
23
26
|
add_offense(offense_range(node), message: OMIT_MSG) do |corrector|
|
24
27
|
auto_correct(corrector, node)
|
25
28
|
end
|
26
29
|
end
|
30
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
27
31
|
|
28
32
|
def auto_correct(corrector, node)
|
29
33
|
if parentheses_at_the_end_of_multiline_call?(node)
|
@@ -43,6 +47,10 @@ module RuboCop
|
|
43
47
|
node.each_ancestor(:def).any?(&:endless?) && node.arguments.any?
|
44
48
|
end
|
45
49
|
|
50
|
+
def syntax_like_method_call?(node)
|
51
|
+
node.implicit_call? || node.operator_method?
|
52
|
+
end
|
53
|
+
|
46
54
|
def super_call_without_arguments?(node)
|
47
55
|
node.super_type? && node.arguments.none?
|
48
56
|
end
|
@@ -53,6 +61,11 @@ module RuboCop
|
|
53
61
|
cop_config['AllowParenthesesInCamelCaseMethod'])
|
54
62
|
end
|
55
63
|
|
64
|
+
def allowed_string_interpolation_method_call?(node)
|
65
|
+
cop_config['AllowParenthesesInStringInterpolation'] &&
|
66
|
+
inside_string_interpolation?(node)
|
67
|
+
end
|
68
|
+
|
56
69
|
def parentheses_at_the_end_of_multiline_call?(node)
|
57
70
|
node.multiline? &&
|
58
71
|
node.loc.begin.source_line
|
@@ -114,7 +127,7 @@ module RuboCop
|
|
114
127
|
def call_as_argument_or_chain?(node)
|
115
128
|
node.parent &&
|
116
129
|
(node.parent.send_type? && !assigned_before?(node.parent, node) ||
|
117
|
-
node.parent.csend_type? || node.parent.super_type?)
|
130
|
+
node.parent.csend_type? || node.parent.super_type? || node.parent.yield_type?)
|
118
131
|
end
|
119
132
|
|
120
133
|
def hash_literal_in_arguments?(node)
|
@@ -172,7 +185,12 @@ module RuboCop
|
|
172
185
|
node.assignment? &&
|
173
186
|
node.loc.operator.begin < target.loc.begin
|
174
187
|
end
|
188
|
+
|
189
|
+
def inside_string_interpolation?(node)
|
190
|
+
node.ancestors.drop_while { |a| !a.begin_type? }.any?(&:dstr_type?)
|
191
|
+
end
|
175
192
|
end
|
193
|
+
# rubocop:enable Metrics/ModuleLength
|
176
194
|
end
|
177
195
|
end
|
178
196
|
end
|
@@ -45,11 +45,13 @@ module RuboCop
|
|
45
45
|
'or `module`.'
|
46
46
|
RESTRICT_ON_SEND = %i[include extend prepend].freeze
|
47
47
|
|
48
|
+
# @!method include_statement(node)
|
48
49
|
def_node_matcher :include_statement, <<~PATTERN
|
49
50
|
(send nil? ${:include :extend :prepend}
|
50
51
|
const)
|
51
52
|
PATTERN
|
52
53
|
|
54
|
+
# @!method in_top_level_scope?(node)
|
53
55
|
def_node_matcher :in_top_level_scope?, <<~PATTERN
|
54
56
|
{
|
55
57
|
root? # either at the top level
|
@@ -82,8 +82,13 @@ module RuboCop
|
|
82
82
|
FORBIDDEN_MSG =
|
83
83
|
'Do not use `module_function` or `extend self`.'
|
84
84
|
|
85
|
+
# @!method module_function_node?(node)
|
85
86
|
def_node_matcher :module_function_node?, '(send nil? :module_function)'
|
87
|
+
|
88
|
+
# @!method extend_self_node?(node)
|
86
89
|
def_node_matcher :extend_self_node?, '(send nil? :extend self)'
|
90
|
+
|
91
|
+
# @!method private_directive?(node)
|
87
92
|
def_node_matcher :private_directive?, '(send nil? :private ...)'
|
88
93
|
|
89
94
|
def on_module(node)
|
@@ -47,11 +47,12 @@ module RuboCop
|
|
47
47
|
'in a conditional, use `Array#include?` instead.'
|
48
48
|
|
49
49
|
def on_new_investigation
|
50
|
-
@
|
51
|
-
@allowed_method_comparison = false
|
50
|
+
@last_comparison = nil
|
52
51
|
end
|
53
52
|
|
54
53
|
def on_or(node)
|
54
|
+
reset_comparison if switch_comparison?(node)
|
55
|
+
|
55
56
|
root_of_or_node = root_of_or_node(node)
|
56
57
|
|
57
58
|
return unless node == root_of_or_node
|
@@ -64,14 +65,21 @@ module RuboCop
|
|
64
65
|
|
65
66
|
corrector.replace(node, prefer_method)
|
66
67
|
end
|
68
|
+
|
69
|
+
@last_comparison = node
|
67
70
|
end
|
68
71
|
|
69
72
|
private
|
70
73
|
|
74
|
+
# @!method simple_double_comparison?(node)
|
71
75
|
def_node_matcher :simple_double_comparison?, '(send $lvar :== $lvar)'
|
76
|
+
|
77
|
+
# @!method simple_comparison_lhs?(node)
|
72
78
|
def_node_matcher :simple_comparison_lhs?, <<~PATTERN
|
73
79
|
(send $lvar :== $_)
|
74
80
|
PATTERN
|
81
|
+
|
82
|
+
# @!method simple_comparison_rhs?(node)
|
75
83
|
def_node_matcher :simple_comparison_rhs?, <<~PATTERN
|
76
84
|
(send $_ :== $lvar)
|
77
85
|
PATTERN
|
@@ -131,6 +139,17 @@ module RuboCop
|
|
131
139
|
end
|
132
140
|
end
|
133
141
|
|
142
|
+
def switch_comparison?(node)
|
143
|
+
return true if @last_comparison.nil?
|
144
|
+
|
145
|
+
@last_comparison.descendants.none? { |descendant| descendant == node }
|
146
|
+
end
|
147
|
+
|
148
|
+
def reset_comparison
|
149
|
+
@compared_elements = []
|
150
|
+
@allowed_method_comparison = false
|
151
|
+
end
|
152
|
+
|
134
153
|
def allow_method_comparison?
|
135
154
|
cop_config.fetch('AllowMethodComparison', true)
|
136
155
|
end
|
@@ -154,12 +154,14 @@ module RuboCop
|
|
154
154
|
end
|
155
155
|
end
|
156
156
|
|
157
|
+
# @!method splat_value(node)
|
157
158
|
def_node_matcher :splat_value, <<~PATTERN
|
158
159
|
(array (splat $_))
|
159
160
|
PATTERN
|
160
161
|
|
161
162
|
# Some of these patterns may not actually return an immutable object,
|
162
163
|
# but we want to consider them immutable for this cop.
|
164
|
+
# @!method operation_produces_immutable_object?(node)
|
163
165
|
def_node_matcher :operation_produces_immutable_object?, <<~PATTERN
|
164
166
|
{
|
165
167
|
(const _ _)
|
@@ -176,6 +178,7 @@ module RuboCop
|
|
176
178
|
}
|
177
179
|
PATTERN
|
178
180
|
|
181
|
+
# @!method range_enclosed_in_parentheses?(node)
|
179
182
|
def_node_matcher :range_enclosed_in_parentheses?, <<~PATTERN
|
180
183
|
(begin ({irange erange} _ _))
|
181
184
|
PATTERN
|
@@ -29,12 +29,14 @@ module RuboCop
|
|
29
29
|
#
|
30
30
|
class NegatedIfElseCondition < Base
|
31
31
|
include RangeHelp
|
32
|
+
include CommentsHelp
|
32
33
|
extend AutoCorrector
|
33
34
|
|
34
35
|
MSG = 'Invert the negated condition and swap the %<type>s branches.'
|
35
36
|
|
36
37
|
NEGATED_EQUALITY_METHODS = %i[!= !~].freeze
|
37
38
|
|
39
|
+
# @!method double_negation?(node)
|
38
40
|
def_node_matcher :double_negation?, '(send (send _ :!) :!)'
|
39
41
|
|
40
42
|
def self.autocorrect_incompatible_with
|
@@ -96,10 +98,22 @@ module RuboCop
|
|
96
98
|
if node.if_branch.nil?
|
97
99
|
corrector.remove(range_by_whole_lines(node.loc.else, include_final_newline: true))
|
98
100
|
else
|
99
|
-
|
100
|
-
|
101
|
+
if_range = node_with_comments(node.if_branch)
|
102
|
+
else_range = node_with_comments(node.else_branch)
|
103
|
+
|
104
|
+
corrector.replace(if_range, else_range.source)
|
105
|
+
corrector.replace(else_range, if_range.source)
|
101
106
|
end
|
102
107
|
end
|
108
|
+
|
109
|
+
def node_with_comments(node)
|
110
|
+
first_statement = node.begin_type? ? node.children[0] : node
|
111
|
+
return node if processed_source.ast_with_comments[first_statement].empty?
|
112
|
+
|
113
|
+
begin_pos = source_range_with_comment(first_statement).begin_pos
|
114
|
+
end_pos = node.source_range.end_pos
|
115
|
+
Parser::Source::Range.new(buffer, begin_pos, end_pos)
|
116
|
+
end
|
103
117
|
end
|
104
118
|
end
|
105
119
|
end
|
@@ -37,7 +37,10 @@ module RuboCop
|
|
37
37
|
|
38
38
|
RESTRICT_ON_SEND = %i[== === nil?].freeze
|
39
39
|
|
40
|
+
# @!method nil_comparison?(node)
|
40
41
|
def_node_matcher :nil_comparison?, '(send _ {:== :===} nil)'
|
42
|
+
|
43
|
+
# @!method nil_check?(node)
|
41
44
|
def_node_matcher :nil_check?, '(send _ :nil?)'
|
42
45
|
|
43
46
|
def on_send(node)
|
@@ -49,9 +49,16 @@ module RuboCop
|
|
49
49
|
|
50
50
|
RESTRICT_ON_SEND = %i[!= nil? !].freeze
|
51
51
|
|
52
|
+
# @!method not_equal_to_nil?(node)
|
52
53
|
def_node_matcher :not_equal_to_nil?, '(send _ :!= nil)'
|
54
|
+
|
55
|
+
# @!method unless_check?(node)
|
53
56
|
def_node_matcher :unless_check?, '(if (send _ :nil?) ...)'
|
57
|
+
|
58
|
+
# @!method nil_check?(node)
|
54
59
|
def_node_matcher :nil_check?, '(send _ :nil?)'
|
60
|
+
|
61
|
+
# @!method not_and_nil_check?(node)
|
55
62
|
def_node_matcher :not_and_nil_check?, '(send (send _ :nil?) :!)'
|
56
63
|
|
57
64
|
def on_send(node)
|
@@ -115,14 +115,17 @@ module RuboCop
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
# @!method predicate(node)
|
118
119
|
def_node_matcher :predicate, <<~PATTERN
|
119
120
|
(send $(...) ${:zero? :positive? :negative?})
|
120
121
|
PATTERN
|
121
122
|
|
123
|
+
# @!method comparison(node)
|
122
124
|
def_node_matcher :comparison, <<~PATTERN
|
123
125
|
(send [$(...) !gvar_type?] ${:== :> :<} (int 0))
|
124
126
|
PATTERN
|
125
127
|
|
128
|
+
# @!method inverted_comparison(node)
|
126
129
|
def_node_matcher :inverted_comparison, <<~PATTERN
|
127
130
|
(send (int 0) ${:== :> :<} [$(...) !gvar_type?])
|
128
131
|
PATTERN
|
@@ -31,6 +31,7 @@ module RuboCop
|
|
31
31
|
|
32
32
|
MSG = 'Use the double pipe equals operator `||=` instead.'
|
33
33
|
|
34
|
+
# @!method ternary_assignment?(node)
|
34
35
|
def_node_matcher :ternary_assignment?, <<~PATTERN
|
35
36
|
({lvasgn ivasgn cvasgn gvasgn} _var
|
36
37
|
(if
|
@@ -39,6 +40,7 @@ module RuboCop
|
|
39
40
|
$_))
|
40
41
|
PATTERN
|
41
42
|
|
43
|
+
# @!method unless_assignment?(node)
|
42
44
|
def_node_matcher :unless_assignment?, <<~PATTERN
|
43
45
|
(if
|
44
46
|
({lvar ivar cvar gvar} _var) nil?
|
@@ -115,6 +115,7 @@ module RuboCop
|
|
115
115
|
end
|
116
116
|
end
|
117
117
|
|
118
|
+
# @!method implicit_self_getter?(node)
|
118
119
|
def_node_matcher :implicit_self_getter?, '(send nil? $_)'
|
119
120
|
|
120
121
|
# Helper class necessitated by silly design of TSort prior to Ruby 2.1
|
@@ -123,8 +124,13 @@ module RuboCop
|
|
123
124
|
include TSort
|
124
125
|
extend RuboCop::NodePattern::Macros
|
125
126
|
|
127
|
+
# @!method var_name(node)
|
126
128
|
def_node_matcher :var_name, '{(casgn _ $_) (_ $_)}'
|
129
|
+
|
130
|
+
# @!method uses_var?(node)
|
127
131
|
def_node_search :uses_var?, '{({lvar ivar cvar gvar} %) (const _ %)}'
|
132
|
+
|
133
|
+
# @!method matching_calls(node, receiver, method_name)
|
128
134
|
def_node_search :matching_calls, '(send %1 %2 $...)'
|
129
135
|
|
130
136
|
def initialize(assignments)
|
@@ -30,6 +30,7 @@ module RuboCop
|
|
30
30
|
'integers with offsets.'
|
31
31
|
RESTRICT_ON_SEND = %i[+ - succ pred next].freeze
|
32
32
|
|
33
|
+
# @!method integer_op_rand?(node)
|
33
34
|
def_node_matcher :integer_op_rand?, <<~PATTERN
|
34
35
|
(send
|
35
36
|
int {:+ :-}
|
@@ -39,6 +40,7 @@ module RuboCop
|
|
39
40
|
{int (irange int int) (erange int int)}))
|
40
41
|
PATTERN
|
41
42
|
|
43
|
+
# @!method rand_op_integer?(node)
|
42
44
|
def_node_matcher :rand_op_integer?, <<~PATTERN
|
43
45
|
(send
|
44
46
|
(send
|
@@ -49,6 +51,7 @@ module RuboCop
|
|
49
51
|
int)
|
50
52
|
PATTERN
|
51
53
|
|
54
|
+
# @!method rand_modified?(node)
|
52
55
|
def_node_matcher :rand_modified?, <<~PATTERN
|
53
56
|
(send
|
54
57
|
(send
|
@@ -71,6 +74,7 @@ module RuboCop
|
|
71
74
|
|
72
75
|
private
|
73
76
|
|
77
|
+
# @!method random_call(node)
|
74
78
|
def_node_matcher :random_call, <<~PATTERN
|
75
79
|
{(send (send $_ _ $_) ...)
|
76
80
|
(send _ _ (send $_ _ $_))}
|
@@ -144,6 +148,7 @@ module RuboCop
|
|
144
148
|
end
|
145
149
|
end
|
146
150
|
|
151
|
+
# @!method to_int(node)
|
147
152
|
def_node_matcher :to_int, <<~PATTERN
|
148
153
|
(int $_)
|
149
154
|
PATTERN
|
@@ -63,6 +63,7 @@ module RuboCop
|
|
63
63
|
# end
|
64
64
|
# end
|
65
65
|
class RedundantBegin < Base
|
66
|
+
include RangeHelp
|
66
67
|
extend AutoCorrector
|
67
68
|
|
68
69
|
MSG = 'Redundant `begin` block detected.'
|
@@ -85,7 +86,9 @@ module RuboCop
|
|
85
86
|
end
|
86
87
|
|
87
88
|
def on_kwbegin(node)
|
88
|
-
return if
|
89
|
+
return if empty_begin?(node) ||
|
90
|
+
contain_rescue_or_ensure?(node) ||
|
91
|
+
valid_context_using_only_begin?(node)
|
89
92
|
|
90
93
|
register_offense(node)
|
91
94
|
end
|
@@ -93,12 +96,30 @@ module RuboCop
|
|
93
96
|
private
|
94
97
|
|
95
98
|
def register_offense(node)
|
96
|
-
|
97
|
-
|
99
|
+
offense_range = node.loc.begin
|
100
|
+
|
101
|
+
add_offense(offense_range) do |corrector|
|
102
|
+
if any_ancestor_assignment_node?(node)
|
103
|
+
replace_begin_with_statement(corrector, offense_range, node)
|
104
|
+
else
|
105
|
+
corrector.remove(offense_range)
|
106
|
+
end
|
107
|
+
|
98
108
|
corrector.remove(node.loc.end)
|
99
109
|
end
|
100
110
|
end
|
101
111
|
|
112
|
+
def replace_begin_with_statement(corrector, offense_range, node)
|
113
|
+
first_child = node.children.first
|
114
|
+
|
115
|
+
corrector.replace(offense_range, first_child.source)
|
116
|
+
corrector.remove(range_between(offense_range.end_pos, first_child.source_range.end_pos))
|
117
|
+
end
|
118
|
+
|
119
|
+
def empty_begin?(node)
|
120
|
+
node.children.empty?
|
121
|
+
end
|
122
|
+
|
102
123
|
def contain_rescue_or_ensure?(node)
|
103
124
|
first_child = node.children.first
|
104
125
|
|
@@ -108,9 +129,17 @@ module RuboCop
|
|
108
129
|
def valid_context_using_only_begin?(node)
|
109
130
|
parent = node.parent
|
110
131
|
|
111
|
-
|
132
|
+
valid_begin_assignment?(node) || parent&.post_condition_loop? ||
|
112
133
|
parent&.send_type? || parent&.operator_keyword?
|
113
134
|
end
|
135
|
+
|
136
|
+
def valid_begin_assignment?(node)
|
137
|
+
any_ancestor_assignment_node?(node) && !node.children.one?
|
138
|
+
end
|
139
|
+
|
140
|
+
def any_ancestor_assignment_node?(node)
|
141
|
+
node.each_ancestor.any?(&:assignment?)
|
142
|
+
end
|
114
143
|
end
|
115
144
|
end
|
116
145
|
end
|