rubocop 1.66.1 → 1.69.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +96 -12
- data/config/internal_affairs.yml +11 -0
- data/lib/rubocop/cached_data.rb +12 -4
- data/lib/rubocop/cli/command/auto_generate_config.rb +6 -7
- data/lib/rubocop/cli/command/execute_runner.rb +1 -1
- data/lib/rubocop/cli/command/lsp.rb +2 -2
- data/lib/rubocop/cli/command/version.rb +2 -2
- data/lib/rubocop/config_loader_resolver.rb +3 -3
- data/lib/rubocop/config_validator.rb +2 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +22 -2
- data/lib/rubocop/cop/base.rb +7 -3
- data/lib/rubocop/cop/bundler/gem_filename.rb +0 -1
- data/lib/rubocop/cop/bundler/gem_version.rb +1 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +0 -1
- data/lib/rubocop/cop/cop.rb +8 -0
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +1 -12
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +10 -0
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +1 -2
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +0 -2
- data/lib/rubocop/cop/generator.rb +6 -0
- data/lib/rubocop/cop/internal_affairs/cop_description.rb +0 -4
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +3 -4
- data/lib/rubocop/cop/internal_affairs/numblock_handler.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/operator_keyword.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +6 -21
- data/lib/rubocop/cop/internal_affairs/redundant_source_range.rb +8 -1
- data/lib/rubocop/cop/internal_affairs/style_detected_api_use.rb +0 -2
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +0 -5
- data/lib/rubocop/cop/internal_affairs.rb +17 -0
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +5 -1
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/array_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/begin_end_alignment.rb +0 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +2 -2
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -3
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -4
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +8 -0
- data/lib/rubocop/cop/layout/indentation_width.rb +11 -12
- data/lib/rubocop/cop/layout/leading_comment_space.rb +71 -1
- data/lib/rubocop/cop/layout/line_length.rb +118 -4
- data/lib/rubocop/cop/layout/multiline_method_call_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +2 -3
- data/lib/rubocop/cop/layout/parameter_alignment.rb +3 -4
- data/lib/rubocop/cop/layout/redundant_line_break.rb +3 -35
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +3 -2
- data/lib/rubocop/cop/layout/space_around_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_operators.rb +16 -17
- data/lib/rubocop/cop/layout/space_before_brackets.rb +5 -5
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +6 -0
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +4 -0
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +4 -0
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +0 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +4 -1
- data/lib/rubocop/cop/lint/big_decimal_new.rb +4 -7
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +10 -12
- data/lib/rubocop/cop/lint/boolean_symbol.rb +1 -1
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +6 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_branch.rb +39 -4
- data/lib/rubocop/cop/lint/duplicate_set_element.rb +74 -0
- data/lib/rubocop/cop/lint/empty_ensure.rb +1 -1
- data/lib/rubocop/cop/lint/empty_file.rb +0 -2
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -4
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/float_comparison.rb +15 -7
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -3
- data/lib/rubocop/cop/lint/hash_new_with_keyword_arguments_as_default.rb +55 -0
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +10 -4
- data/lib/rubocop/cop/lint/interpolation_check.rb +9 -0
- data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +8 -14
- data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -0
- data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +25 -2
- data/lib/rubocop/cop/lint/mixed_case_range.rb +2 -5
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/no_return_in_begin_end_blocks.rb +2 -2
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +8 -1
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -3
- data/lib/rubocop/cop/lint/number_conversion.rb +0 -1
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -2
- data/lib/rubocop/cop/lint/numeric_operation_with_constant_result.rb +106 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -2
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +5 -6
- data/lib/rubocop/cop/lint/redundant_cop_enable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +13 -8
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +8 -7
- data/lib/rubocop/cop/lint/refinement_import_methods.rb +1 -1
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +0 -1
- data/lib/rubocop/cop/lint/rescue_type.rb +3 -7
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +9 -0
- data/lib/rubocop/cop/lint/safe_navigation_consistency.rb +109 -41
- data/lib/rubocop/cop/lint/self_assignment.rb +8 -10
- data/lib/rubocop/cop/lint/shadowed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/unescaped_bracket_in_regexp.rb +91 -0
- data/lib/rubocop/cop/lint/unreachable_code.rb +51 -2
- data/lib/rubocop/cop/lint/unused_method_argument.rb +18 -2
- data/lib/rubocop/cop/lint/uri_regexp.rb +25 -7
- data/lib/rubocop/cop/lint/useless_defined.rb +55 -0
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +4 -0
- data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +14 -25
- data/lib/rubocop/cop/lint/void.rb +3 -2
- data/lib/rubocop/cop/metrics/class_length.rb +7 -7
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +4 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +2 -3
- data/lib/rubocop/cop/mixin/check_assignment.rb +4 -12
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +10 -0
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +49 -0
- data/lib/rubocop/cop/mixin/dig_help.rb +27 -0
- data/lib/rubocop/cop/mixin/endless_method_rewriter.rb +24 -0
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +3 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +5 -9
- data/lib/rubocop/cop/mixin/percent_array.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +0 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +3 -2
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +17 -1
- data/lib/rubocop/cop/naming/accessor_method_name.rb +6 -6
- data/lib/rubocop/cop/naming/block_forwarding.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +6 -7
- data/lib/rubocop/cop/naming/file_name.rb +0 -2
- data/lib/rubocop/cop/naming/inclusive_language.rb +12 -3
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +11 -12
- data/lib/rubocop/cop/naming/predicate_name.rb +1 -1
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +3 -11
- data/lib/rubocop/cop/naming/variable_name.rb +3 -4
- data/lib/rubocop/cop/naming/variable_number.rb +2 -3
- data/lib/rubocop/cop/offense.rb +4 -5
- data/lib/rubocop/cop/security/compound_hash.rb +1 -0
- data/lib/rubocop/cop/security/yaml_load.rb +3 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +64 -25
- data/lib/rubocop/cop/style/accessor_grouping.rb +10 -2
- data/lib/rubocop/cop/style/ambiguous_endless_method_definition.rb +79 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +46 -6
- data/lib/rubocop/cop/style/array_intersect.rb +5 -4
- data/lib/rubocop/cop/style/bitwise_predicate.rb +100 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +41 -5
- data/lib/rubocop/cop/style/case_like_if.rb +8 -11
- data/lib/rubocop/cop/style/collection_compact.rb +10 -10
- data/lib/rubocop/cop/style/combinable_defined.rb +115 -0
- data/lib/rubocop/cop/style/combinable_loops.rb +7 -0
- data/lib/rubocop/cop/style/commented_keyword.rb +17 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +20 -22
- data/lib/rubocop/cop/style/constant_visibility.rb +3 -12
- data/lib/rubocop/cop/style/data_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/dig_chain.rb +89 -0
- data/lib/rubocop/cop/style/endless_method.rb +1 -14
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/fetch_env_var.rb +1 -0
- data/lib/rubocop/cop/style/file_null.rb +73 -0
- data/lib/rubocop/cop/style/file_touch.rb +75 -0
- data/lib/rubocop/cop/style/for.rb +0 -1
- data/lib/rubocop/cop/style/global_vars.rb +1 -3
- data/lib/rubocop/cop/style/guard_clause.rb +16 -3
- data/lib/rubocop/cop/style/hash_conversion.rb +1 -2
- data/lib/rubocop/cop/style/hash_each_methods.rb +6 -0
- data/lib/rubocop/cop/style/hash_except.rb +19 -7
- data/lib/rubocop/cop/style/hash_syntax.rb +2 -2
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -2
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -2
- data/lib/rubocop/cop/style/if_with_semicolon.rb +20 -7
- data/lib/rubocop/cop/style/inverse_methods.rb +0 -1
- data/lib/rubocop/cop/style/keyword_arguments_merging.rb +67 -0
- data/lib/rubocop/cop/style/lambda.rb +1 -1
- data/lib/rubocop/cop/style/lambda_call.rb +3 -2
- data/lib/rubocop/cop/style/map_into_array.rb +59 -8
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +13 -8
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +7 -11
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +33 -3
- data/lib/rubocop/cop/style/multiline_memoization.rb +2 -2
- data/lib/rubocop/cop/style/multiple_comparison.rb +28 -39
- data/lib/rubocop/cop/style/mutable_constant.rb +4 -5
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +6 -4
- data/lib/rubocop/cop/style/nested_modifier.rb +1 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +1 -1
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +5 -4
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/object_then.rb +1 -0
- data/lib/rubocop/cop/style/one_line_conditional.rb +29 -4
- data/lib/rubocop/cop/style/operator_method_call.rb +25 -7
- data/lib/rubocop/cop/style/or_assignment.rb +3 -6
- data/lib/rubocop/cop/style/parallel_assignment.rb +8 -13
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +3 -1
- data/lib/rubocop/cop/style/redundant_assignment.rb +1 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +4 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +37 -22
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +24 -5
- data/lib/rubocop/cop/style/redundant_parentheses.rb +11 -13
- data/lib/rubocop/cop/style/redundant_regexp_argument.rb +1 -0
- data/lib/rubocop/cop/style/redundant_return.rb +2 -2
- data/lib/rubocop/cop/style/redundant_self.rb +8 -15
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +7 -5
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +4 -4
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/require_order.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +15 -4
- data/lib/rubocop/cop/style/return_nil_in_predicate_method_definition.rb +54 -12
- data/lib/rubocop/cop/style/safe_navigation.rb +104 -50
- data/lib/rubocop/cop/style/safe_navigation_chain_length.rb +52 -0
- data/lib/rubocop/cop/style/select_by_regexp.rb +10 -7
- data/lib/rubocop/cop/style/self_assignment.rb +11 -17
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +2 -3
- data/lib/rubocop/cop/style/single_argument_dig.rb +9 -5
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +13 -3
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +2 -3
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -1
- data/lib/rubocop/cop/style/string_concatenation.rb +13 -12
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/swap_values.rb +4 -15
- data/lib/rubocop/cop/style/ternary_parentheses.rb +26 -5
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +4 -4
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -2
- data/lib/rubocop/cop/team.rb +8 -1
- data/lib/rubocop/cop/util.rb +1 -1
- data/lib/rubocop/cop/variable_force/assignment.rb +18 -3
- data/lib/rubocop/cop/variable_force/branch.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +5 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -2
- data/lib/rubocop/cop/variable_force.rb +4 -10
- data/lib/rubocop/cops_documentation_generator.rb +90 -41
- data/lib/rubocop/file_finder.rb +9 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/lsp/runtime.rb +2 -0
- data/lib/rubocop/lsp/server.rb +0 -1
- data/lib/rubocop/rspec/expect_offense.rb +1 -0
- data/lib/rubocop/runner.rb +17 -6
- data/lib/rubocop/server/cache.rb +6 -1
- data/lib/rubocop/server/core.rb +1 -0
- data/lib/rubocop/target_ruby.rb +13 -13
- data/lib/rubocop/version.rb +28 -7
- data/lib/rubocop/yaml_duplication_checker.rb +20 -27
- data/lib/rubocop.rb +18 -0
- metadata +32 -14
@@ -71,11 +71,17 @@ module RuboCop
|
|
71
71
|
(send nil? :bad_method ...)
|
72
72
|
PATTERN
|
73
73
|
|
74
|
+
# Called on every `send` node (method call) while walking the AST.
|
75
|
+
# TODO: remove this method if inspecting `send` nodes is unneeded for your cop.
|
76
|
+
# By default, this is aliased to `on_csend` as well to handle method calls
|
77
|
+
# with safe navigation, remove the alias if this is unnecessary.
|
78
|
+
# If kept, ensure your tests cover safe navigation as well!
|
74
79
|
def on_send(node)
|
75
80
|
return unless bad_method?(node)
|
76
81
|
|
77
82
|
add_offense(node)
|
78
83
|
end
|
84
|
+
alias on_csend on_send
|
79
85
|
end
|
80
86
|
end
|
81
87
|
end
|
@@ -19,6 +19,7 @@ module RuboCop
|
|
19
19
|
extend AutoCorrector
|
20
20
|
|
21
21
|
MSG = 'Use `%<preferred>s`.'
|
22
|
+
RESTRICT_ON_SEND = [:==].freeze
|
22
23
|
|
23
24
|
# @!method line_send(node)
|
24
25
|
def_node_matcher :line_send, <<~PATTERN
|
@@ -36,10 +37,8 @@ module RuboCop
|
|
36
37
|
def on_send(node)
|
37
38
|
return unless location_line_equality_comparison?(node)
|
38
39
|
|
39
|
-
|
40
|
-
|
41
|
-
lhs_receiver = extract_receiver(lhs)
|
42
|
-
rhs_receiver = extract_receiver(rhs)
|
40
|
+
lhs_receiver = extract_receiver(node.receiver)
|
41
|
+
rhs_receiver = extract_receiver(node.first_argument)
|
43
42
|
preferred = "same_line?(#{lhs_receiver}, #{rhs_receiver})"
|
44
43
|
|
45
44
|
add_offense(node, message: format(MSG, preferred: preferred)) do |corrector|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module InternalAffairs
|
6
|
-
# Checks for missing `numblock handlers. The blocks with numbered
|
6
|
+
# Checks for missing `numblock` handlers. The blocks with numbered
|
7
7
|
# arguments introduced in Ruby 2.7 are parsed with a node type of
|
8
8
|
# `numblock` instead of block. Cops that define `block` handlers
|
9
9
|
# need to define `numblock` handlers or disable this cope for them.
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module InternalAffairs
|
6
|
+
# Enforces the use of `node.operator_keyword?` instead of `node.and_type? || node.or_type?`.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# node.and_type? || node.or_type?
|
11
|
+
# node.or_type? || node.and_type?
|
12
|
+
#
|
13
|
+
# # good
|
14
|
+
# node.operator_keyword?
|
15
|
+
#
|
16
|
+
class OperatorKeyword < Base
|
17
|
+
extend AutoCorrector
|
18
|
+
|
19
|
+
MSG = 'Use `%<prefer>s`.'
|
20
|
+
|
21
|
+
# @!method and_or_type(node)
|
22
|
+
def_node_matcher :and_or_type, <<~PATTERN
|
23
|
+
{
|
24
|
+
(or $(send _node :and_type?) $(send _node :or_type?))
|
25
|
+
(or $(send _node :or_type?) $(send _node :and_type?))
|
26
|
+
(or
|
27
|
+
(or _ $(send _node :and_type?)) $(send _node :or_type?))
|
28
|
+
(or
|
29
|
+
(or _ $(send _node :or_type?)) $(send _node :and_type?))
|
30
|
+
}
|
31
|
+
PATTERN
|
32
|
+
|
33
|
+
def on_or(node)
|
34
|
+
return unless (lhs, rhs = and_or_type(node))
|
35
|
+
|
36
|
+
offense = lhs.receiver.source_range.join(rhs.source_range.end)
|
37
|
+
prefer = "#{lhs.receiver.source}.operator_keyword?"
|
38
|
+
|
39
|
+
add_offense(offense, message: format(MSG, prefer: prefer)) do |corrector|
|
40
|
+
corrector.replace(offense, prefer)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -11,13 +11,9 @@ module RuboCop
|
|
11
11
|
#
|
12
12
|
# # bad
|
13
13
|
# add_offense(node, message: MSG)
|
14
|
-
# add_offense(node, message: message)
|
15
|
-
# add_offense(node, message: message(node))
|
16
14
|
#
|
17
15
|
# # good
|
18
16
|
# add_offense(node)
|
19
|
-
# add_offense(node, message: CUSTOM_MSG)
|
20
|
-
# add_offense(node, message: message(other_node))
|
21
17
|
#
|
22
18
|
class RedundantMessageArgument < Base
|
23
19
|
include RangeHelp
|
@@ -28,22 +24,21 @@ module RuboCop
|
|
28
24
|
|
29
25
|
# @!method node_type_check(node)
|
30
26
|
def_node_matcher :node_type_check, <<~PATTERN
|
31
|
-
(send nil? :add_offense
|
27
|
+
(send nil? :add_offense _node $hash)
|
32
28
|
PATTERN
|
33
29
|
|
34
30
|
# @!method redundant_message_argument(node)
|
35
31
|
def_node_matcher :redundant_message_argument, <<~PATTERN
|
36
32
|
(pair
|
37
33
|
(sym :message)
|
38
|
-
$
|
34
|
+
$(const nil? :MSG))
|
39
35
|
PATTERN
|
40
36
|
|
41
|
-
# @!method message_method_call(node)
|
42
|
-
def_node_matcher :message_method_call, '(send nil? :message $_node)'
|
43
|
-
|
44
37
|
def on_send(node)
|
45
|
-
node_type_check(node)
|
46
|
-
|
38
|
+
return unless (kwargs = node_type_check(node))
|
39
|
+
|
40
|
+
kwargs.pairs.each do |pair|
|
41
|
+
redundant_message_argument(pair) do
|
47
42
|
add_offense(pair) do |corrector|
|
48
43
|
range = offending_range(pair)
|
49
44
|
|
@@ -60,16 +55,6 @@ module RuboCop
|
|
60
55
|
|
61
56
|
range_with_surrounding_comma(with_space, :left)
|
62
57
|
end
|
63
|
-
|
64
|
-
def find_offending_argument(searched_node, kwargs)
|
65
|
-
kwargs.pairs.each do |pair|
|
66
|
-
redundant_message_argument(pair) do |message_argument|
|
67
|
-
node = message_method_call(message_argument)
|
68
|
-
|
69
|
-
yield pair if !node || node == searched_node
|
70
|
-
end
|
71
|
-
end
|
72
|
-
end
|
73
58
|
end
|
74
59
|
end
|
75
60
|
end
|
@@ -14,6 +14,12 @@ module RuboCop
|
|
14
14
|
# node.source
|
15
15
|
#
|
16
16
|
# # bad
|
17
|
+
# add_offense(node.source_range)
|
18
|
+
#
|
19
|
+
# # good
|
20
|
+
# add_offense(node)
|
21
|
+
#
|
22
|
+
# # bad
|
17
23
|
# add_offense(node) { |corrector| corrector.replace(node.source_range, prefer) }
|
18
24
|
# add_offense(node) { |corrector| corrector.insert_before(node.source_range, prefer) }
|
19
25
|
# add_offense(node) { |corrector| corrector.insert_before_multi(node.source_range, prefer) }
|
@@ -34,7 +40,7 @@ module RuboCop
|
|
34
40
|
|
35
41
|
MSG = 'Remove the redundant `source_range`.'
|
36
42
|
RESTRICT_ON_SEND = %i[
|
37
|
-
source
|
43
|
+
source add_offense
|
38
44
|
replace remove insert_before insert_before_multi insert_after insert_after_multi swap
|
39
45
|
].freeze
|
40
46
|
|
@@ -42,6 +48,7 @@ module RuboCop
|
|
42
48
|
def_node_matcher :redundant_source_range, <<~PATTERN
|
43
49
|
{
|
44
50
|
(send $(send _ :source_range) :source)
|
51
|
+
(send nil? :add_offense $(send _ :source_range) ...)
|
45
52
|
(send _ {
|
46
53
|
:replace :insert_before :insert_before_multi :insert_after :insert_after_multi
|
47
54
|
} $(send _ :source_range) _)
|
@@ -17,6 +17,7 @@ require_relative 'internal_affairs/node_matcher_directive'
|
|
17
17
|
require_relative 'internal_affairs/node_type_predicate'
|
18
18
|
require_relative 'internal_affairs/numblock_handler'
|
19
19
|
require_relative 'internal_affairs/offense_location_keyword'
|
20
|
+
require_relative 'internal_affairs/operator_keyword'
|
20
21
|
require_relative 'internal_affairs/processed_source_buffer_name'
|
21
22
|
require_relative 'internal_affairs/redundant_context_config_parameter'
|
22
23
|
require_relative 'internal_affairs/redundant_described_class_as_subject'
|
@@ -31,3 +32,19 @@ require_relative 'internal_affairs/style_detected_api_use'
|
|
31
32
|
require_relative 'internal_affairs/undefined_config'
|
32
33
|
require_relative 'internal_affairs/useless_message_assertion'
|
33
34
|
require_relative 'internal_affairs/useless_restrict_on_send'
|
35
|
+
|
36
|
+
module RuboCop
|
37
|
+
# Patch in the InternalAffairs specific config values
|
38
|
+
module InternalAffairs
|
39
|
+
def self.inject!
|
40
|
+
path = File.join(ConfigLoader::RUBOCOP_HOME, 'config', 'internal_affairs.yml')
|
41
|
+
hash = ConfigLoader.load_yaml_configuration(path)
|
42
|
+
config = Config.new(hash, path)
|
43
|
+
puts "configuration from #{path}" if ConfigLoader.debug?
|
44
|
+
config = ConfigLoader.merge_with_default(config, path)
|
45
|
+
ConfigLoader.instance_variable_set(:@default_configuration, config)
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
|
50
|
+
RuboCop::InternalAffairs.inject!
|
@@ -59,7 +59,11 @@ module RuboCop
|
|
59
59
|
modifiers = body.each_child_node(:send).select(&:bare_access_modifier?)
|
60
60
|
end_range = node.loc.end
|
61
61
|
|
62
|
-
modifiers.each
|
62
|
+
modifiers.each do |modifier|
|
63
|
+
next if same_line?(node, modifier)
|
64
|
+
|
65
|
+
check_modifier(modifier, end_range)
|
66
|
+
end
|
63
67
|
end
|
64
68
|
|
65
69
|
def check_modifier(send_node, end_range)
|
@@ -3,8 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Layout
|
6
|
-
#
|
7
|
-
# definition are aligned.
|
6
|
+
# Check that the arguments on a multi-line method definition are aligned.
|
8
7
|
#
|
9
8
|
# @example EnforcedStyle: with_first_argument (default)
|
10
9
|
# # good
|
@@ -127,7 +127,6 @@ module RuboCop
|
|
127
127
|
start_loc,
|
128
128
|
end_loc,
|
129
129
|
do_source_line_column)
|
130
|
-
|
131
130
|
error_source_line_column = if style == :start_of_block
|
132
131
|
do_source_line_column
|
133
132
|
else
|
@@ -189,7 +188,7 @@ module RuboCop
|
|
189
188
|
# In offense message, we want to show the assignment LHS rather than
|
190
189
|
# the entire assignment.
|
191
190
|
def find_lhs_node(node)
|
192
|
-
node
|
191
|
+
node = node.lhs while node.op_asgn_type? || node.masgn_type?
|
193
192
|
node
|
194
193
|
end
|
195
194
|
|
@@ -61,7 +61,7 @@ module RuboCop
|
|
61
61
|
private
|
62
62
|
|
63
63
|
def autocorrect(corrector, node)
|
64
|
-
if style == :start_of_line && node.parent
|
64
|
+
if style == :start_of_line && node.parent&.send_type?
|
65
65
|
AlignmentCorrector.align_end(corrector, processed_source, node, node.parent)
|
66
66
|
else
|
67
67
|
AlignmentCorrector.align_end(corrector, processed_source, node, node)
|
@@ -135,7 +135,7 @@ module RuboCop
|
|
135
135
|
|
136
136
|
parent = next_sibling.parent
|
137
137
|
|
138
|
-
parent&.if_type? && parent
|
138
|
+
parent&.if_type? && parent.else?
|
139
139
|
end
|
140
140
|
|
141
141
|
def next_sibling_empty_or_guard_clause?(node)
|
@@ -200,7 +200,7 @@ module RuboCop
|
|
200
200
|
parent = node.parent
|
201
201
|
return false unless parent
|
202
202
|
|
203
|
-
parent.begin_type? &&
|
203
|
+
parent.begin_type? && same_line?(node, node.right_sibling)
|
204
204
|
end
|
205
205
|
|
206
206
|
# SimpleCov excludes code from the coverage report by wrapping it in `# :nocov:`:
|
@@ -84,9 +84,8 @@ module RuboCop
|
|
84
84
|
|
85
85
|
alias on_numblock on_block
|
86
86
|
|
87
|
-
def on_send(node)
|
88
|
-
return unless node.bare_access_modifier? &&
|
89
|
-
!(node.parent&.block_type? || node.parent&.numblock_type?)
|
87
|
+
def on_send(node)
|
88
|
+
return unless node.bare_access_modifier? && !node.block_literal?
|
90
89
|
return if expected_empty_lines?(node)
|
91
90
|
|
92
91
|
message = message(node)
|
@@ -72,8 +72,7 @@ module RuboCop
|
|
72
72
|
alias on_numblock on_def
|
73
73
|
|
74
74
|
def on_kwbegin(node)
|
75
|
-
|
76
|
-
check_body(body, node.loc.line)
|
75
|
+
check_body(node.children.first, node.loc.line)
|
77
76
|
end
|
78
77
|
|
79
78
|
private
|
@@ -128,10 +127,10 @@ module RuboCop
|
|
128
127
|
end
|
129
128
|
|
130
129
|
def keyword_locations_in_ensure(node)
|
131
|
-
|
130
|
+
rescue_body_without_ensure = node.children.first
|
132
131
|
[
|
133
132
|
node.loc.keyword,
|
134
|
-
*keyword_locations(
|
133
|
+
*keyword_locations(rescue_body_without_ensure)
|
135
134
|
]
|
136
135
|
end
|
137
136
|
end
|
@@ -63,13 +63,21 @@ module RuboCop
|
|
63
63
|
# }
|
64
64
|
# )
|
65
65
|
#
|
66
|
+
# @example AllowedMethods: ['some_method']
|
67
|
+
#
|
68
|
+
# # good
|
69
|
+
# some_method(foo, bar,
|
70
|
+
# baz)
|
66
71
|
class FirstMethodArgumentLineBreak < Base
|
67
72
|
include FirstElementLineBreak
|
73
|
+
include AllowedMethods
|
68
74
|
extend AutoCorrector
|
69
75
|
|
70
76
|
MSG = 'Add a line break before the first argument of a multi-line method argument list.'
|
71
77
|
|
72
78
|
def on_send(node)
|
79
|
+
return if allowed_method?(node.method_name)
|
80
|
+
|
73
81
|
args = node.arguments.dup
|
74
82
|
|
75
83
|
# If there is a trailing hash arg without explicit braces, like this:
|
@@ -3,13 +3,12 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Layout
|
6
|
-
# Checks for indentation that doesn't use the specified number
|
7
|
-
#
|
6
|
+
# Checks for indentation that doesn't use the specified number of spaces.
|
7
|
+
# The indentation width can be configured using the `Width` setting. The default width is 2.
|
8
8
|
#
|
9
|
-
# See also the IndentationConsistency cop which is the companion to this
|
10
|
-
# one.
|
9
|
+
# See also the `Layout/IndentationConsistency` cop which is the companion to this one.
|
11
10
|
#
|
12
|
-
# @example
|
11
|
+
# @example Width: 2 (default)
|
13
12
|
# # bad
|
14
13
|
# class A
|
15
14
|
# def test
|
@@ -59,16 +58,17 @@ module RuboCop
|
|
59
58
|
PATTERN
|
60
59
|
|
61
60
|
def on_rescue(node)
|
62
|
-
|
63
|
-
check_indentation(node.loc.else, else_node)
|
61
|
+
check_indentation(node.loc.else, node.else_branch)
|
64
62
|
end
|
65
63
|
|
66
|
-
def
|
64
|
+
def on_resbody(node)
|
67
65
|
check_indentation(node.loc.keyword, node.body)
|
68
66
|
end
|
67
|
+
alias on_for on_resbody
|
69
68
|
|
70
|
-
|
71
|
-
|
69
|
+
def on_ensure(node)
|
70
|
+
check_indentation(node.loc.keyword, node.branch)
|
71
|
+
end
|
72
72
|
|
73
73
|
def on_kwbegin(node)
|
74
74
|
# Check indentation against end keyword but only if it's first on its
|
@@ -326,8 +326,7 @@ module RuboCop
|
|
326
326
|
if body_node.rescue_type?
|
327
327
|
check_rescue?(body_node)
|
328
328
|
elsif body_node.ensure_type?
|
329
|
-
block_body, = *body_node
|
330
|
-
|
329
|
+
block_body, = *body_node # rubocop:disable InternalAffairs/NodeDestructuring
|
331
330
|
if block_body&.rescue_type?
|
332
331
|
check_rescue?(block_body)
|
333
332
|
else
|
@@ -49,18 +49,58 @@ module RuboCop
|
|
49
49
|
# #ruby=2.7.0
|
50
50
|
# #ruby-gemset=myproject
|
51
51
|
#
|
52
|
+
# @example AllowRBSInlineAnnotation: false (default)
|
53
|
+
#
|
54
|
+
# # bad
|
55
|
+
#
|
56
|
+
# include Enumerable #[Integer]
|
57
|
+
#
|
58
|
+
# attr_reader :name #: String
|
59
|
+
# attr_reader :age #: Integer?
|
60
|
+
#
|
61
|
+
# @example AllowRBSInlineAnnotation: true
|
62
|
+
#
|
63
|
+
# # good
|
64
|
+
#
|
65
|
+
# include Enumerable #[Integer]
|
66
|
+
#
|
67
|
+
# attr_reader :name #: String
|
68
|
+
# attr_reader :age #: Integer?
|
69
|
+
#
|
70
|
+
# @example AllowSteepAnnotation: false (default)
|
71
|
+
#
|
72
|
+
# # bad
|
73
|
+
# [1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
|
74
|
+
# list << n
|
75
|
+
# end
|
76
|
+
#
|
77
|
+
# name = 'John' #: String
|
78
|
+
#
|
79
|
+
# @example AllowSteepAnnotation: true
|
80
|
+
#
|
81
|
+
# # good
|
82
|
+
#
|
83
|
+
# [1, 2, 3].each_with_object([]) do |n, list| #$ Array[Integer]
|
84
|
+
# list << n
|
85
|
+
# end
|
86
|
+
#
|
87
|
+
# name = 'John' #: String
|
88
|
+
#
|
52
89
|
class LeadingCommentSpace < Base
|
53
90
|
include RangeHelp
|
54
91
|
extend AutoCorrector
|
55
92
|
|
56
93
|
MSG = 'Missing space after `#`.'
|
57
94
|
|
58
|
-
def on_new_investigation
|
95
|
+
def on_new_investigation # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
59
96
|
processed_source.comments.each do |comment|
|
60
97
|
next unless /\A(?!#\+\+|#--)(#+[^#\s=])/.match?(comment.text)
|
61
98
|
next if comment.loc.line == 1 && allowed_on_first_line?(comment)
|
99
|
+
next if shebang_continuation?(comment)
|
62
100
|
next if doxygen_comment_style?(comment)
|
63
101
|
next if gemfile_ruby_comment?(comment)
|
102
|
+
next if rbs_inline_annotation?(comment)
|
103
|
+
next if steep_annotation?(comment)
|
64
104
|
|
65
105
|
add_offense(comment) do |corrector|
|
66
106
|
expr = comment.source_range
|
@@ -84,6 +124,20 @@ module RuboCop
|
|
84
124
|
comment.text.start_with?('#!')
|
85
125
|
end
|
86
126
|
|
127
|
+
def shebang_continuation?(comment)
|
128
|
+
return false unless shebang?(comment)
|
129
|
+
return true if comment.loc.line == 1
|
130
|
+
|
131
|
+
previous_line_comment = processed_source.comment_at_line(comment.loc.line - 1)
|
132
|
+
return false unless previous_line_comment
|
133
|
+
|
134
|
+
# If the comment is a shebang but not on the first line, check if the previous
|
135
|
+
# line has a shebang comment that wasn't marked as an offense; if so, this comment
|
136
|
+
# continues the shebang and is acceptable.
|
137
|
+
shebang?(previous_line_comment) &&
|
138
|
+
!current_offense_locations.include?(previous_line_comment.source_range)
|
139
|
+
end
|
140
|
+
|
87
141
|
def rackup_options?(comment)
|
88
142
|
comment.text.start_with?('#\\')
|
89
143
|
end
|
@@ -115,6 +169,22 @@ module RuboCop
|
|
115
169
|
def gemfile_ruby_comment?(comment)
|
116
170
|
allow_gemfile_ruby_comment? && ruby_comment_in_gemfile?(comment)
|
117
171
|
end
|
172
|
+
|
173
|
+
def allow_rbs_inline_annotation?
|
174
|
+
cop_config['AllowRBSInlineAnnotation']
|
175
|
+
end
|
176
|
+
|
177
|
+
def rbs_inline_annotation?(comment)
|
178
|
+
allow_rbs_inline_annotation? && comment.text.start_with?(/#:|#\[.+\]/)
|
179
|
+
end
|
180
|
+
|
181
|
+
def allow_steep_annotation?
|
182
|
+
cop_config['AllowSteepAnnotation']
|
183
|
+
end
|
184
|
+
|
185
|
+
def steep_annotation?(comment)
|
186
|
+
allow_steep_annotation? && comment.text.start_with?(/#[$:]/)
|
187
|
+
end
|
118
188
|
end
|
119
189
|
end
|
120
190
|
end
|