rubocop 1.18.1 → 1.22.3
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.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +116 -23
- data/lib/rubocop/cli.rb +18 -0
- data/lib/rubocop/config.rb +5 -0
- data/lib/rubocop/config_loader.rb +5 -3
- data/lib/rubocop/config_loader_resolver.rb +22 -7
- data/lib/rubocop/config_validator.rb +27 -6
- data/lib/rubocop/cop/base.rb +3 -3
- data/lib/rubocop/cop/bundler/gem_comment.rb +3 -3
- data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +45 -21
- data/lib/rubocop/cop/bundler/ordered_gems.rb +3 -12
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +11 -10
- data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
- data/lib/rubocop/cop/documentation.rb +1 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +3 -12
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +31 -24
- data/lib/rubocop/cop/generator.rb +14 -8
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +60 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/assignment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +3 -3
- data/lib/rubocop/cop/layout/class_structure.rb +7 -2
- data/lib/rubocop/cop/layout/dot_position.rb +34 -5
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +7 -4
- data/lib/rubocop/cop/layout/end_alignment.rb +9 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +36 -17
- data/lib/rubocop/cop/layout/line_length.rb +9 -7
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +3 -3
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -0
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +23 -10
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +15 -4
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -0
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_parens.rb +78 -32
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +111 -0
- data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +7 -5
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +18 -5
- data/lib/rubocop/cop/lint/boolean_symbol.rb +5 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -4
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
- data/lib/rubocop/cop/lint/deprecated_constants.rb +3 -2
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +24 -1
- data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
- data/lib/rubocop/cop/lint/else_layout.rb +10 -6
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +12 -3
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +67 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +5 -0
- data/lib/rubocop/cop/lint/loop.rb +4 -3
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +5 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +12 -1
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +4 -2
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +17 -0
- data/lib/rubocop/cop/lint/percent_string_array.rb +10 -0
- data/lib/rubocop/cop/lint/raise_exception.rb +4 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +5 -4
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +50 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/triple_quotes.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +8 -3
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -3
- data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -2
- data/lib/rubocop/cop/lint/useless_setter_call.rb +7 -4
- data/lib/rubocop/cop/lint/useless_times.rb +5 -4
- data/lib/rubocop/cop/metrics/abc_size.rb +6 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/code_length.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -2
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +23 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -4
- data/lib/rubocop/cop/mixin/heredoc.rb +5 -0
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -1
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +9 -1
- data/lib/rubocop/cop/mixin/percent_array.rb +18 -7
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +9 -1
- data/lib/rubocop/cop/mixin/require_library.rb +59 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -2
- data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
- data/lib/rubocop/cop/mixin/string_literals_help.rb +5 -1
- data/lib/rubocop/cop/mixin/trailing_body.rb +1 -1
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +0 -3
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +27 -10
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +5 -4
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +7 -0
- data/lib/rubocop/cop/security/io_methods.rb +49 -0
- data/lib/rubocop/cop/security/json_load.rb +8 -7
- data/lib/rubocop/cop/security/open.rb +4 -0
- data/lib/rubocop/cop/security/yaml_load.rb +4 -0
- data/lib/rubocop/cop/style/accessor_grouping.rb +2 -2
- data/lib/rubocop/cop/style/and_or.rb +5 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +13 -2
- data/lib/rubocop/cop/style/array_coercion.rb +21 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +0 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +50 -2
- data/lib/rubocop/cop/style/case_equality.rb +6 -9
- data/lib/rubocop/cop/style/case_like_if.rb +5 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +9 -0
- data/lib/rubocop/cop/style/collection_compact.rb +7 -5
- data/lib/rubocop/cop/style/collection_methods.rb +8 -6
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
- data/lib/rubocop/cop/style/comment_annotation.rb +55 -25
- data/lib/rubocop/cop/style/commented_keyword.rb +9 -3
- data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
- data/lib/rubocop/cop/style/date_time.rb +5 -0
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +23 -8
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
- data/lib/rubocop/cop/style/double_negation.rb +27 -6
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/encoding.rb +26 -15
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +46 -11
- data/lib/rubocop/cop/style/float_division.rb +10 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +14 -3
- data/lib/rubocop/cop/style/global_std_stream.rb +4 -0
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +5 -0
- data/lib/rubocop/cop/style/hash_except.rb +4 -3
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -9
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -6
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +32 -5
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +18 -4
- data/lib/rubocop/cop/style/infinite_loop.rb +4 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +9 -2
- data/lib/rubocop/cop/style/lambda_call.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -6
- data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
- data/lib/rubocop/cop/style/missing_else.rb +7 -0
- data/lib/rubocop/cop/style/module_function.rb +8 -9
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +79 -14
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_unless.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
- data/lib/rubocop/cop/style/not.rb +2 -2
- data/lib/rubocop/cop/style/numbered_parameters.rb +46 -0
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +50 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
- data/lib/rubocop/cop/style/numeric_predicate.rb +5 -0
- data/lib/rubocop/cop/style/optional_arguments.rb +4 -0
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +14 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -4
- data/lib/rubocop/cop/style/quoted_symbols.rb +10 -6
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +19 -9
- data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +4 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +12 -3
- data/lib/rubocop/cop/style/redundant_freeze.rb +4 -4
- data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self.rb +10 -0
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +4 -3
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +53 -20
- data/lib/rubocop/cop/style/regexp_literal.rb +3 -3
- data/lib/rubocop/cop/style/return_nil.rb +2 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +13 -2
- data/lib/rubocop/cop/style/select_by_regexp.rb +139 -0
- data/lib/rubocop/cop/style/semicolon.rb +32 -24
- data/lib/rubocop/cop/style/single_argument_dig.rb +5 -0
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +25 -15
- data/lib/rubocop/cop/style/slicing_with_range.rb +13 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +25 -0
- data/lib/rubocop/cop/style/static_class.rb +5 -5
- data/lib/rubocop/cop/style/string_chars.rb +4 -2
- data/lib/rubocop/cop/style/string_concatenation.rb +5 -1
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -0
- data/lib/rubocop/cop/style/swap_values.rb +4 -2
- data/lib/rubocop/cop/style/symbol_array.rb +3 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +26 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +19 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +23 -5
- data/lib/rubocop/cop/style/yoda_condition.rb +24 -7
- data/lib/rubocop/cop/style/zero_length_predicate.rb +6 -0
- data/lib/rubocop/cop/util.rb +22 -6
- data/lib/rubocop/cops_documentation_generator.rb +17 -5
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +44 -15
- data/lib/rubocop/options.rb +127 -113
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/result_cache.rb +3 -3
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +6 -2
- data/lib/rubocop/rspec/parallel_formatter.rb +90 -0
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/runner.rb +2 -3
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +14 -2
- metadata +21 -5
@@ -14,8 +14,25 @@ module RuboCop
|
|
14
14
|
# positives. Luckily, there is no harm in freezing an already
|
15
15
|
# frozen object.
|
16
16
|
#
|
17
|
+
# From Ruby 3.0, this cop honours the magic comment
|
18
|
+
# 'shareable_constant_value'. When this magic comment is set to any
|
19
|
+
# acceptable value other than none, it will suppress the offenses
|
20
|
+
# raised by this cop. It enforces frozen state.
|
21
|
+
#
|
17
22
|
# NOTE: Regexp and Range literals are frozen objects since Ruby 3.0.
|
18
23
|
#
|
24
|
+
# NOTE: From Ruby 3.0, interpolated strings are not frozen when
|
25
|
+
# `# frozen-string-literal: true` is used, so this cop enforces explicit
|
26
|
+
# freezing for such strings.
|
27
|
+
#
|
28
|
+
# NOTE: From Ruby 3.0, this cop allows explicit freezing of constants when
|
29
|
+
# the `shareable_constant_value` directive is used.
|
30
|
+
#
|
31
|
+
# @safety
|
32
|
+
# This cop's autocorrection is unsafe since any mutations on objects that
|
33
|
+
# are made frozen will change from being accepted to raising `FrozenError`,
|
34
|
+
# and will need to be manually refactored.
|
35
|
+
#
|
19
36
|
# @example EnforcedStyle: literals (default)
|
20
37
|
# # bad
|
21
38
|
# CONST = [1, 2, 3]
|
@@ -52,7 +69,55 @@ module RuboCop
|
|
52
69
|
# puts 1
|
53
70
|
# end
|
54
71
|
# end.freeze
|
72
|
+
#
|
73
|
+
# @example
|
74
|
+
# # Magic comment - shareable_constant_value: literal
|
75
|
+
#
|
76
|
+
# # bad
|
77
|
+
# CONST = [1, 2, 3]
|
78
|
+
#
|
79
|
+
# # good
|
80
|
+
# # shareable_constant_value: literal
|
81
|
+
# CONST = [1, 2, 3]
|
82
|
+
#
|
55
83
|
class MutableConstant < Base
|
84
|
+
# Handles magic comment shareable_constant_value with O(n ^ 2) complexity
|
85
|
+
# n - number of lines in the source
|
86
|
+
# Iterates over all lines before a CONSTANT
|
87
|
+
# until it reaches shareable_constant_value
|
88
|
+
module ShareableConstantValue
|
89
|
+
module_function
|
90
|
+
|
91
|
+
def recent_shareable_value?(node)
|
92
|
+
shareable_constant_comment = magic_comment_in_scope node
|
93
|
+
return false if shareable_constant_comment.nil?
|
94
|
+
|
95
|
+
shareable_constant_value = MagicComment.parse(shareable_constant_comment)
|
96
|
+
.shareable_constant_value
|
97
|
+
shareable_constant_value_enabled? shareable_constant_value
|
98
|
+
end
|
99
|
+
|
100
|
+
# Identifies the most recent magic comment with valid shareable constant values
|
101
|
+
# that's in scope for this node
|
102
|
+
def magic_comment_in_scope(node)
|
103
|
+
processed_source_till_node(node).reverse_each.find do |line|
|
104
|
+
MagicComment.parse(line).valid_shareable_constant_value?
|
105
|
+
end
|
106
|
+
end
|
107
|
+
|
108
|
+
private
|
109
|
+
|
110
|
+
def processed_source_till_node(node)
|
111
|
+
processed_source.lines[0..(node.last_line - 1)]
|
112
|
+
end
|
113
|
+
|
114
|
+
def shareable_constant_value_enabled?(value)
|
115
|
+
%w[literal experimental_everything experimental_copy].include? value
|
116
|
+
end
|
117
|
+
end
|
118
|
+
private_constant :ShareableConstantValue
|
119
|
+
|
120
|
+
include ShareableConstantValue
|
56
121
|
include FrozenStringLiteral
|
57
122
|
include ConfigurableEnforcedStyle
|
58
123
|
extend AutoCorrector
|
@@ -61,13 +126,12 @@ module RuboCop
|
|
61
126
|
|
62
127
|
def on_casgn(node)
|
63
128
|
_scope, _const_name, value = *node
|
64
|
-
|
65
|
-
|
129
|
+
if value.nil? # This is only the case for `CONST += ...` or similarg66
|
130
|
+
parent = node.parent
|
131
|
+
return unless parent.or_asgn_type? # We only care about `CONST ||= ...`
|
66
132
|
|
67
|
-
|
68
|
-
|
69
|
-
|
70
|
-
return unless lhs&.casgn_type?
|
133
|
+
value = parent.children.last
|
134
|
+
end
|
71
135
|
|
72
136
|
on_assignment(value)
|
73
137
|
end
|
@@ -86,18 +150,18 @@ module RuboCop
|
|
86
150
|
return if immutable_literal?(value)
|
87
151
|
return if operation_produces_immutable_object?(value)
|
88
152
|
return if frozen_string_literal?(value)
|
153
|
+
return if shareable_constant_value?(value)
|
89
154
|
|
90
155
|
add_offense(value) { |corrector| autocorrect(corrector, value) }
|
91
156
|
end
|
92
157
|
|
93
158
|
def check(value)
|
94
159
|
range_enclosed_in_parentheses = range_enclosed_in_parentheses?(value)
|
95
|
-
|
96
160
|
return unless mutable_literal?(value) ||
|
97
|
-
target_ruby_version <= 2.7 && range_enclosed_in_parentheses
|
161
|
+
(target_ruby_version <= 2.7 && range_enclosed_in_parentheses)
|
98
162
|
|
99
|
-
return if
|
100
|
-
|
163
|
+
return if frozen_string_literal?(value)
|
164
|
+
return if shareable_constant_value?(value)
|
101
165
|
|
102
166
|
add_offense(value) { |corrector| autocorrect(corrector, value) }
|
103
167
|
end
|
@@ -118,18 +182,19 @@ module RuboCop
|
|
118
182
|
end
|
119
183
|
|
120
184
|
def mutable_literal?(value)
|
121
|
-
return false if value.nil?
|
122
185
|
return false if frozen_regexp_or_range_literals?(value)
|
123
186
|
|
124
187
|
value.mutable_literal?
|
125
188
|
end
|
126
189
|
|
127
190
|
def immutable_literal?(node)
|
128
|
-
|
191
|
+
frozen_regexp_or_range_literals?(node) || node.immutable_literal?
|
129
192
|
end
|
130
193
|
|
131
|
-
def
|
132
|
-
|
194
|
+
def shareable_constant_value?(node)
|
195
|
+
return false if target_ruby_version < 3.0
|
196
|
+
|
197
|
+
recent_shareable_value? node
|
133
198
|
end
|
134
199
|
|
135
200
|
def frozen_regexp_or_range_literals?(node)
|
@@ -63,7 +63,7 @@ module RuboCop
|
|
63
63
|
|
64
64
|
def on_send(node)
|
65
65
|
return if ignored_node?(node) ||
|
66
|
-
!include_semantic_changes? && nil_comparison_style == 'comparison'
|
66
|
+
(!include_semantic_changes? && nil_comparison_style == 'comparison')
|
67
67
|
return unless register_offense?(node)
|
68
68
|
|
69
69
|
message = message(node)
|
@@ -87,7 +87,7 @@ module RuboCop
|
|
87
87
|
|
88
88
|
def register_offense?(node)
|
89
89
|
not_equal_to_nil?(node) ||
|
90
|
-
include_semantic_changes? && (not_and_nil_check?(node) || unless_and_nil_check?(node))
|
90
|
+
(include_semantic_changes? && (not_and_nil_check?(node) || unless_and_nil_check?(node)))
|
91
91
|
end
|
92
92
|
|
93
93
|
def autocorrect(corrector, node)
|
@@ -53,8 +53,8 @@ module RuboCop
|
|
53
53
|
|
54
54
|
def requires_parens?(child)
|
55
55
|
child.and_type? || child.or_type? ||
|
56
|
-
child.send_type? && child.binary_operation? ||
|
57
|
-
child.if_type? && child.ternary?
|
56
|
+
(child.send_type? && child.binary_operation?) ||
|
57
|
+
(child.if_type? && child.ternary?)
|
58
58
|
end
|
59
59
|
|
60
60
|
def correct_opposite_method(corrector, range, child)
|
@@ -0,0 +1,46 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for numbered parameters.
|
7
|
+
#
|
8
|
+
# It can either restrict the use of numbered parameters to
|
9
|
+
# single-lined blocks, or disallow completely numbered parameters.
|
10
|
+
#
|
11
|
+
# @example EnforcedStyle: allow_single_line (default)
|
12
|
+
# # bad
|
13
|
+
# collection.each do
|
14
|
+
# puts _1
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# collection.each { puts _1 }
|
19
|
+
#
|
20
|
+
# @example EnforcedStyle: disallow
|
21
|
+
# # bad
|
22
|
+
# collection.each { puts _1 }
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# collection.each { |item| puts item }
|
26
|
+
#
|
27
|
+
class NumberedParameters < Base
|
28
|
+
include ConfigurableEnforcedStyle
|
29
|
+
extend TargetRubyVersion
|
30
|
+
|
31
|
+
MSG_DISALLOW = 'Avoid using numbered parameters.'
|
32
|
+
MSG_MULTI_LINE = 'Avoid using numbered parameters for multi-line blocks.'
|
33
|
+
|
34
|
+
minimum_target_ruby_version 2.7
|
35
|
+
|
36
|
+
def on_numblock(node)
|
37
|
+
if style == :disallow
|
38
|
+
add_offense(node, message: MSG_DISALLOW)
|
39
|
+
elsif node.multiline?
|
40
|
+
add_offense(node, message: MSG_MULTI_LINE)
|
41
|
+
end
|
42
|
+
end
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop detects use of an excessive amount of numbered parameters in a
|
7
|
+
# single block. Having too many numbered parameters can make code too
|
8
|
+
# cryptic and hard to read.
|
9
|
+
#
|
10
|
+
# The cop defaults to registering an offense if there is more than 1 numbered
|
11
|
+
# parameter but this maximum can be configured by setting `Max`.
|
12
|
+
#
|
13
|
+
# @example Max: 1 (default)
|
14
|
+
# # bad
|
15
|
+
# foo { _1.call(_2, _3, _4) }
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# foo { do_something(_1) }
|
19
|
+
class NumberedParametersLimit < Base
|
20
|
+
extend TargetRubyVersion
|
21
|
+
extend ExcludeLimit
|
22
|
+
|
23
|
+
DEFAULT_MAX_VALUE = 1
|
24
|
+
|
25
|
+
minimum_target_ruby_version 2.7
|
26
|
+
exclude_limit 'Max'
|
27
|
+
|
28
|
+
MSG = 'Avoid using more than %<max>i numbered %<parameter>s; %<count>i detected.'
|
29
|
+
|
30
|
+
def on_numblock(node)
|
31
|
+
_send_node, param_count, * = *node
|
32
|
+
return if param_count <= max_count
|
33
|
+
|
34
|
+
parameter = max_count > 1 ? 'parameters' : 'parameter'
|
35
|
+
message = format(MSG, max: max_count, parameter: parameter, count: param_count)
|
36
|
+
add_offense(node, message: message) { self.max = param_count }
|
37
|
+
end
|
38
|
+
|
39
|
+
private
|
40
|
+
|
41
|
+
def max_count
|
42
|
+
max = cop_config.fetch('Max', DEFAULT_MAX_VALUE)
|
43
|
+
|
44
|
+
# Ruby does not allow more than 9 numbered parameters
|
45
|
+
[max, 9].min
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -58,18 +58,17 @@ module RuboCop
|
|
58
58
|
|
59
59
|
case int
|
60
60
|
when /^\d+$/
|
61
|
-
|
62
|
-
|
63
|
-
register_offense(node)
|
61
|
+
register_offense(node) { self.min_digits = int.size + 1 }
|
64
62
|
when /\d{4}/, short_group_regex
|
65
|
-
|
66
|
-
|
67
|
-
register_offense(node)
|
63
|
+
register_offense(node) { self.config_to_allow_offenses = { 'Enabled' => false } }
|
68
64
|
end
|
69
65
|
end
|
70
66
|
|
71
|
-
def register_offense(node)
|
72
|
-
add_offense(node)
|
67
|
+
def register_offense(node, &_block)
|
68
|
+
add_offense(node) do |corrector|
|
69
|
+
yield
|
70
|
+
corrector.replace(node, format_number(node))
|
71
|
+
end
|
73
72
|
end
|
74
73
|
|
75
74
|
def short_group_regex
|
@@ -16,6 +16,11 @@ module RuboCop
|
|
16
16
|
# populated with objects which can be compared with integers, but are
|
17
17
|
# not themselves `Integer` polymorphic.
|
18
18
|
#
|
19
|
+
# @safety
|
20
|
+
# This cop is unsafe because it cannot be guaranteed that the receiver
|
21
|
+
# defines the predicates or can be compared to a number, which may lead
|
22
|
+
# to a false positive for non-standard classes.
|
23
|
+
#
|
19
24
|
# @example EnforcedStyle: predicate (default)
|
20
25
|
# # bad
|
21
26
|
#
|
@@ -6,6 +6,10 @@ module RuboCop
|
|
6
6
|
# This cop checks for optional arguments to methods
|
7
7
|
# that do not come at the end of the argument list.
|
8
8
|
#
|
9
|
+
# @safety
|
10
|
+
# This cop is unsafe because changing a method signature will
|
11
|
+
# implicitly change behaviour.
|
12
|
+
#
|
9
13
|
# @example
|
10
14
|
# # bad
|
11
15
|
# def foo(a = 1, b, c)
|
@@ -7,6 +7,10 @@ module RuboCop
|
|
7
7
|
# boolean arguments when defining methods. `respond_to_missing?` method is allowed by default.
|
8
8
|
# These are customizable with `AllowedMethods` option.
|
9
9
|
#
|
10
|
+
# @safety
|
11
|
+
# This cop is unsafe because changing a method signature will
|
12
|
+
# implicitly change behaviour.
|
13
|
+
#
|
10
14
|
# @example
|
11
15
|
# # bad
|
12
16
|
# def some_method(bar = false)
|
@@ -33,8 +37,8 @@ module RuboCop
|
|
33
37
|
class OptionalBooleanParameter < Base
|
34
38
|
include AllowedMethods
|
35
39
|
|
36
|
-
MSG = '
|
37
|
-
|
40
|
+
MSG = 'Prefer keyword arguments for arguments with a boolean default value; ' \
|
41
|
+
'use `%<replacement>s` instead of `%<original>s`.'
|
38
42
|
|
39
43
|
def on_def(node)
|
40
44
|
return if allowed_method?(node.method_name)
|
@@ -42,11 +46,17 @@ module RuboCop
|
|
42
46
|
node.arguments.each do |arg|
|
43
47
|
next unless arg.optarg_type?
|
44
48
|
|
45
|
-
|
46
|
-
add_offense(arg) if BOOLEAN_TYPES.include?(value.type)
|
49
|
+
add_offense(arg, message: format_message(arg)) if arg.default_value.boolean_type?
|
47
50
|
end
|
48
51
|
end
|
49
52
|
alias on_defs on_def
|
53
|
+
|
54
|
+
private
|
55
|
+
|
56
|
+
def format_message(argument)
|
57
|
+
source = argument.source
|
58
|
+
format(MSG, original: source, replacement: source.sub(/\s+=/, ':'))
|
59
|
+
end
|
50
60
|
end
|
51
61
|
end
|
52
62
|
end
|
@@ -153,7 +153,7 @@ module RuboCop
|
|
153
153
|
|
154
154
|
def dependency?(lhs, rhs)
|
155
155
|
uses_var?(rhs, var_name(lhs)) ||
|
156
|
-
lhs.send_type? && lhs.assignment_method? && accesses?(rhs, lhs)
|
156
|
+
(lhs.send_type? && lhs.assignment_method? && accesses?(rhs, lhs))
|
157
157
|
end
|
158
158
|
|
159
159
|
# `lhs` is an assignment method call like `obj.attr=` or `ary[idx]=`.
|
@@ -53,8 +53,8 @@ module RuboCop
|
|
53
53
|
end
|
54
54
|
|
55
55
|
def correct_literal_style?(node)
|
56
|
-
style == :lower_case_q && type(node) == '%q' ||
|
57
|
-
style == :upper_case_q && type(node) == '%Q'
|
56
|
+
(style == :lower_case_q && type(node) == '%q') ||
|
57
|
+
(style == :upper_case_q && type(node) == '%Q')
|
58
58
|
end
|
59
59
|
|
60
60
|
def message(_range)
|
@@ -3,10 +3,15 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
|
-
# This cop
|
7
|
-
# Hash#has_value
|
8
|
-
#
|
9
|
-
# names
|
6
|
+
# This cop checks for uses of methods `Hash#has_key?` and
|
7
|
+
# `Hash#has_value?`, and suggests using `Hash#key?` and `Hash#value?` instead.
|
8
|
+
#
|
9
|
+
# It is configurable to enforce the verbose method names, by using the
|
10
|
+
# `EnforcedStyle: verbose` configuration.
|
11
|
+
#
|
12
|
+
# @safety
|
13
|
+
# This cop is unsafe because it cannot be guaranteed that the receiver
|
14
|
+
# is a `Hash` or responds to the replacement methods.
|
10
15
|
#
|
11
16
|
# @example EnforcedStyle: short (default)
|
12
17
|
# # bad
|
@@ -4,7 +4,8 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# Checks if the quotes used for quoted symbols match the configured defaults.
|
7
|
-
# By default uses the same configuration as `Style/StringLiterals
|
7
|
+
# By default uses the same configuration as `Style/StringLiterals`; if that
|
8
|
+
# cop is not enabled, the default `EnforcedStyle` is `single_quotes`.
|
8
9
|
#
|
9
10
|
# String interpolation is always kept in double quotes.
|
10
11
|
#
|
@@ -75,11 +76,14 @@ module RuboCop
|
|
75
76
|
end
|
76
77
|
|
77
78
|
def correct_quotes(str)
|
78
|
-
if style == :single_quotes
|
79
|
-
|
80
|
-
|
81
|
-
|
82
|
-
|
79
|
+
correction = if style == :single_quotes
|
80
|
+
to_string_literal(str)
|
81
|
+
else
|
82
|
+
str.gsub("\\'", "'").inspect
|
83
|
+
end
|
84
|
+
|
85
|
+
# The conversion process doubles escaped slashes, so they have to be reverted
|
86
|
+
correction.gsub('\\\\', '\\')
|
83
87
|
end
|
84
88
|
|
85
89
|
def style
|
@@ -5,22 +5,29 @@ module RuboCop
|
|
5
5
|
module Style
|
6
6
|
# This cop checks for a redundant argument passed to certain methods.
|
7
7
|
#
|
8
|
-
#
|
9
|
-
#
|
10
|
-
# 1. This cop matches for method names only and hence cannot tell apart
|
11
|
-
# methods with same name in different classes.
|
12
|
-
# 2. This cop is limited to methods with single parameter.
|
13
|
-
# 3. This cop is unsafe if certain special global variables (e.g. `$;`, `$/`) are set.
|
14
|
-
# That depends on the nature of the target methods, of course.
|
8
|
+
# NOTE: This cop is limited to methods with single parameter.
|
15
9
|
#
|
16
10
|
# Method names and their redundant arguments can be configured like this:
|
17
11
|
#
|
12
|
+
# [source,yaml]
|
13
|
+
# ----
|
18
14
|
# Methods:
|
19
15
|
# join: ''
|
20
16
|
# split: ' '
|
21
17
|
# chomp: "\n"
|
22
18
|
# chomp!: "\n"
|
23
19
|
# foo: 2
|
20
|
+
# ----
|
21
|
+
#
|
22
|
+
# @safety
|
23
|
+
# This cop is unsafe because of the following limitations:
|
24
|
+
#
|
25
|
+
# 1. This cop matches by method names only and hence cannot tell apart
|
26
|
+
# methods with same name in different classes.
|
27
|
+
# 2. This cop may be unsafe if certain special global variables (e.g. `$;`, `$/`) are set.
|
28
|
+
# That depends on the nature of the target methods, of course. For example, the default
|
29
|
+
# argument to join is `$OUTPUT_FIELD_SEPARATOR` (or `$,`) rather than `''`, and if that
|
30
|
+
# global is changed, `''` is no longer a redundant argument.
|
24
31
|
#
|
25
32
|
# @example
|
26
33
|
# # bad
|
@@ -51,8 +58,11 @@ module RuboCop
|
|
51
58
|
return if node.arguments.count != 1
|
52
59
|
return unless redundant_argument?(node)
|
53
60
|
|
54
|
-
|
55
|
-
|
61
|
+
offense_range = argument_range(node)
|
62
|
+
message = format(MSG, arg: node.arguments.first.source)
|
63
|
+
|
64
|
+
add_offense(offense_range, message: message) do |corrector|
|
65
|
+
corrector.remove(offense_range)
|
56
66
|
end
|
57
67
|
end
|
58
68
|
|
@@ -84,6 +84,7 @@ module RuboCop
|
|
84
84
|
|
85
85
|
def on_kwbegin(node)
|
86
86
|
return if empty_begin?(node) ||
|
87
|
+
begin_block_has_multiline_statements?(node) ||
|
87
88
|
contain_rescue_or_ensure?(node) ||
|
88
89
|
valid_context_using_only_begin?(node)
|
89
90
|
|
@@ -102,6 +103,9 @@ module RuboCop
|
|
102
103
|
corrector.remove(offense_range)
|
103
104
|
end
|
104
105
|
|
106
|
+
if use_modifier_form_after_multiline_begin_block?(node)
|
107
|
+
correct_modifier_form_after_multiline_begin_block(corrector, node)
|
108
|
+
end
|
105
109
|
corrector.remove(node.loc.end)
|
106
110
|
end
|
107
111
|
end
|
@@ -127,10 +131,31 @@ module RuboCop
|
|
127
131
|
corrector.insert_before(node.parent, comments) unless comments.blank?
|
128
132
|
end
|
129
133
|
|
134
|
+
def use_modifier_form_after_multiline_begin_block?(node)
|
135
|
+
return unless (parent = node.parent)
|
136
|
+
|
137
|
+
node.multiline? && parent.if_type? && parent.modifier_form?
|
138
|
+
end
|
139
|
+
|
140
|
+
def correct_modifier_form_after_multiline_begin_block(corrector, node)
|
141
|
+
condition_range = condition_range(node.parent)
|
142
|
+
|
143
|
+
corrector.insert_after(node.children.first, " #{condition_range.source}")
|
144
|
+
corrector.remove(range_by_whole_lines(condition_range, include_final_newline: true))
|
145
|
+
end
|
146
|
+
|
147
|
+
def condition_range(node)
|
148
|
+
range_between(node.loc.keyword.begin_pos, node.condition.source_range.end_pos)
|
149
|
+
end
|
150
|
+
|
130
151
|
def empty_begin?(node)
|
131
152
|
node.children.empty?
|
132
153
|
end
|
133
154
|
|
155
|
+
def begin_block_has_multiline_statements?(node)
|
156
|
+
node.children.count >= 2
|
157
|
+
end
|
158
|
+
|
134
159
|
def contain_rescue_or_ensure?(node)
|
135
160
|
first_child = node.children.first
|
136
161
|
|
@@ -120,11 +120,10 @@ module RuboCop
|
|
120
120
|
end
|
121
121
|
|
122
122
|
def require_parentheses?(node)
|
123
|
-
node.basic_conditional? &&
|
124
|
-
node.modifier_form? ||
|
123
|
+
(node.basic_conditional? && node.modifier_form?) ||
|
125
124
|
node.range_type? ||
|
126
125
|
node.rescue_type? ||
|
127
|
-
node.respond_to?(:semantic_operator?) && node.semantic_operator?
|
126
|
+
(node.respond_to?(:semantic_operator?) && node.semantic_operator?)
|
128
127
|
end
|
129
128
|
|
130
129
|
def without_argument_parentheses_method?(node)
|
@@ -9,6 +9,10 @@ module RuboCop
|
|
9
9
|
# In such cases `fetch(key, value)` method is faster
|
10
10
|
# than `fetch(key) { value }`.
|
11
11
|
#
|
12
|
+
# @safety
|
13
|
+
# This cop is unsafe because it cannot be guaranteed that the receiver
|
14
|
+
# does not have a different implementation of `fetch`.
|
15
|
+
#
|
12
16
|
# @example SafeForConstants: false (default)
|
13
17
|
# # bad
|
14
18
|
# hash.fetch(:key) { 5 }
|
@@ -25,6 +25,7 @@ module RuboCop
|
|
25
25
|
# require_relative '../foo.so'
|
26
26
|
#
|
27
27
|
class RedundantFileExtensionInRequire < Base
|
28
|
+
include RangeHelp
|
28
29
|
extend AutoCorrector
|
29
30
|
|
30
31
|
MSG = 'Redundant `.rb` file extension detected.'
|
@@ -39,13 +40,21 @@ module RuboCop
|
|
39
40
|
require_call?(node) do |name_node|
|
40
41
|
return unless name_node.value.end_with?('.rb')
|
41
42
|
|
42
|
-
|
43
|
-
correction = name_node.value.delete_suffix('.rb')
|
43
|
+
extension_range = extension_range(name_node)
|
44
44
|
|
45
|
-
|
45
|
+
add_offense(extension_range) do |corrector|
|
46
|
+
corrector.remove(extension_range)
|
46
47
|
end
|
47
48
|
end
|
48
49
|
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def extension_range(name_node)
|
54
|
+
end_of_path_string = name_node.source_range.end_pos
|
55
|
+
|
56
|
+
range_between(end_of_path_string - 4, end_of_path_string - 1)
|
57
|
+
end
|
49
58
|
end
|
50
59
|
end
|
51
60
|
end
|
@@ -7,6 +7,9 @@ module RuboCop
|
|
7
7
|
#
|
8
8
|
# NOTE: Regexp and Range literals are frozen objects since Ruby 3.0.
|
9
9
|
#
|
10
|
+
# NOTE: From Ruby 3.0, this cop allows explicit freezing of interpolated
|
11
|
+
# string literals when `# frozen-string-literal: true` is used.
|
12
|
+
#
|
10
13
|
# @example
|
11
14
|
# # bad
|
12
15
|
# CONST = 1.freeze
|
@@ -37,9 +40,7 @@ module RuboCop
|
|
37
40
|
node = strip_parenthesis(node)
|
38
41
|
|
39
42
|
return true if node.immutable_literal?
|
40
|
-
|
41
|
-
return true if FROZEN_STRING_LITERAL_TYPES.include?(node.type) &&
|
42
|
-
frozen_string_literals_enabled?
|
43
|
+
return true if frozen_string_literal?(node)
|
43
44
|
|
44
45
|
target_ruby_version >= 3.0 && (node.regexp_type? || node.range_type?)
|
45
46
|
end
|
@@ -58,7 +59,6 @@ module RuboCop
|
|
58
59
|
(begin (send {float int} {:+ :- :* :** :/ :% :<<} _))
|
59
60
|
(begin (send !{(str _) array} {:+ :- :* :** :/ :%} {float int}))
|
60
61
|
(begin (send _ {:== :=== :!= :<= :>= :< :>} _))
|
61
|
-
(send (const {nil? cbase} :ENV) :[] _)
|
62
62
|
(send _ {:count :length :size} ...)
|
63
63
|
(block (send _ {:count :length :size} ...) ...)
|
64
64
|
}
|