rubocop 1.8.0 → 1.11.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 +14 -13
- data/assets/output.html.erb +1 -1
- data/config/default.yml +89 -22
- data/config/obsoletion.yml +4 -0
- data/lib/rubocop.rb +9 -0
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +5 -4
- data/lib/rubocop/cli/command/execute_runner.rb +1 -1
- data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
- data/lib/rubocop/config.rb +5 -2
- data/lib/rubocop/config_loader.rb +7 -14
- data/lib/rubocop/config_store.rb +12 -1
- data/lib/rubocop/cop/base.rb +2 -1
- 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/exclude_limit.rb +26 -0
- 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 +3 -5
- data/lib/rubocop/cop/internal_affairs.rb +6 -1
- data/lib/rubocop/cop/internal_affairs/empty_line_between_expect_offense_and_correction.rb +68 -0
- data/lib/rubocop/cop/internal_affairs/example_description.rb +90 -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 +62 -0
- data/lib/rubocop/cop/internal_affairs/redundant_let_rubocop_config_new.rb +65 -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/block_alignment.rb +1 -0
- data/lib/rubocop/cop/layout/class_structure.rb +8 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +37 -17
- data/lib/rubocop/cop/layout/extra_spacing.rb +2 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +22 -3
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -0
- data/lib/rubocop/cop/layout/line_length.rb +2 -1
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +26 -0
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +9 -4
- 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_constants.rb +5 -0
- 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 +43 -6
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +47 -0
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +39 -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_dir_glob_sort.rb +5 -3
- 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/symbol_conversion.rb +103 -0
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +3 -0
- data/lib/rubocop/cop/lint/triple_quotes.rb +71 -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/message_annotator.rb +4 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -2
- data/lib/rubocop/cop/metrics/module_length.rb +1 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +6 -2
- 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/check_line_breakable.rb +5 -0
- data/lib/rubocop/cop/mixin/code_length.rb +3 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +0 -1
- data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
- data/lib/rubocop/cop/mixin/def_node.rb +1 -0
- 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/method_complexity.rb +4 -1
- 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 +5 -1
- 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 +2 -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 +38 -5
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/registry.rb +1 -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/severity.rb +3 -3
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +1 -0
- 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/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -0
- data/lib/rubocop/cop/style/case_equality.rb +2 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -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 +49 -9
- data/lib/rubocop/cop/style/documentation.rb +5 -0
- 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 +140 -49
- 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 +12 -1
- data/lib/rubocop/cop/style/exponential_notation.rb +6 -7
- data/lib/rubocop/cop/style/float_division.rb +7 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -0
- data/lib/rubocop/cop/style/format_string_token.rb +19 -2
- 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_transform_keys.rb +4 -0
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -0
- data/lib/rubocop/cop/style/if_inside_else.rb +14 -7
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +122 -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/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 +1 -0
- data/lib/rubocop/cop/style/nil_comparison.rb +6 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +1 -0
- data/lib/rubocop/cop/style/non_nil_check.rb +30 -13
- data/lib/rubocop/cop/style/numeric_literals.rb +6 -9
- data/lib/rubocop/cop/style/numeric_predicate.rb +4 -1
- 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 +7 -1
- 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_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_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/single_line_methods.rb +5 -2
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +28 -4
- 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_concatenation.rb +2 -1
- 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 +1 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +25 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/trailing_body_on_method_definition.rb +1 -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 +99 -0
- data/lib/rubocop/cop/style/unpack_first.rb +1 -0
- data/lib/rubocop/cop/style/while_until_modifier.rb +2 -4
- data/lib/rubocop/cop/style/yoda_condition.rb +1 -0
- data/lib/rubocop/cop/style/zero_length_predicate.rb +5 -0
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -0
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -1
- data/lib/rubocop/formatter/simple_text_formatter.rb +2 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +30 -1
- data/lib/rubocop/name_similarity.rb +1 -1
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +5 -2
- data/lib/rubocop/runner.rb +1 -0
- data/lib/rubocop/target_ruby.rb +21 -13
- data/lib/rubocop/version.rb +2 -2
- metadata +21 -7
@@ -24,6 +24,7 @@ module RuboCop
|
|
24
24
|
|
25
25
|
MSG = 'Use `Hash#compare_by_identity` instead of using `object_id` for keys.'
|
26
26
|
|
27
|
+
# @!method id_as_hash_key?(node)
|
27
28
|
def_node_matcher :id_as_hash_key?, <<~PATTERN
|
28
29
|
(send _ {:key? :has_key? :fetch :[] :[]=} (send _ :object_id) ...)
|
29
30
|
PATTERN
|
@@ -11,14 +11,10 @@ module RuboCop
|
|
11
11
|
# @example
|
12
12
|
#
|
13
13
|
# # bad
|
14
|
-
#
|
15
14
|
# x < y < z
|
16
15
|
# 10 <= x <= 20
|
17
16
|
#
|
18
|
-
# @example
|
19
|
-
#
|
20
17
|
# # good
|
21
|
-
#
|
22
18
|
# x < y && y < z
|
23
19
|
# 10 <= x && x <= 20
|
24
20
|
class MultipleComparison < Base
|
@@ -26,14 +22,19 @@ module RuboCop
|
|
26
22
|
|
27
23
|
MSG = 'Use the `&&` operator to compare multiple values.'
|
28
24
|
COMPARISON_METHODS = %i[< > <= >=].freeze
|
25
|
+
SET_OPERATION_OPERATORS = %i[& | ^].freeze
|
29
26
|
RESTRICT_ON_SEND = COMPARISON_METHODS
|
30
27
|
|
28
|
+
# @!method multiple_compare?(node)
|
31
29
|
def_node_matcher :multiple_compare?, <<~PATTERN
|
32
30
|
(send (send _ {:< :> :<= :>=} $_) {:#{COMPARISON_METHODS.join(' :')}} _)
|
33
31
|
PATTERN
|
34
32
|
|
35
33
|
def on_send(node)
|
36
34
|
return unless (center = multiple_compare?(node))
|
35
|
+
# It allows multiple comparison using `&`, `|`, and `^` set operation operators.
|
36
|
+
# e.g. `x >= y & y < z`
|
37
|
+
return if center.send_type? && SET_OPERATION_OPERATORS.include?(center.method_name)
|
37
38
|
|
38
39
|
add_offense(node) do |corrector|
|
39
40
|
new_center = "#{center.source} && #{center.source}"
|
@@ -81,14 +81,17 @@ module RuboCop
|
|
81
81
|
class_or_module_or_struct_new_call?(child)
|
82
82
|
end
|
83
83
|
|
84
|
+
# @!method eval_call?(node)
|
84
85
|
def_node_matcher :eval_call?, <<~PATTERN
|
85
86
|
(block (send _ {:instance_eval :class_eval :module_eval} ...) ...)
|
86
87
|
PATTERN
|
87
88
|
|
89
|
+
# @!method exec_call?(node)
|
88
90
|
def_node_matcher :exec_call?, <<~PATTERN
|
89
91
|
(block (send _ {:instance_exec :class_exec :module_exec} ...) ...)
|
90
92
|
PATTERN
|
91
93
|
|
94
|
+
# @!method class_or_module_or_struct_new_call?(node)
|
92
95
|
def_node_matcher :class_or_module_or_struct_new_call?, <<~PATTERN
|
93
96
|
(block (send (const {nil? cbase} {:Class :Module :Struct}) :new ...) ...)
|
94
97
|
PATTERN
|
@@ -25,6 +25,7 @@ module RuboCop
|
|
25
25
|
class NextWithoutAccumulator < Base
|
26
26
|
MSG = 'Use `next` with an accumulator argument in a `reduce`.'
|
27
27
|
|
28
|
+
# @!method on_body_of_reduce(node)
|
28
29
|
def_node_matcher :on_body_of_reduce, <<~PATTERN
|
29
30
|
(block (send _recv {:reduce :inject} !sym) _blockargs $(begin ...))
|
30
31
|
PATTERN
|
@@ -129,32 +129,39 @@ module RuboCop
|
|
129
129
|
unsorted_dir_glob_pass?(node) || unsorted_dir_each_pass?(node)
|
130
130
|
end
|
131
131
|
|
132
|
+
# @!method unsorted_dir_block?(node)
|
132
133
|
def_node_matcher :unsorted_dir_block?, <<~PATTERN
|
133
134
|
(send (const {nil? cbase} :Dir) :glob ...)
|
134
135
|
PATTERN
|
135
136
|
|
137
|
+
# @!method unsorted_dir_each?(node)
|
136
138
|
def_node_matcher :unsorted_dir_each?, <<~PATTERN
|
137
139
|
(send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each)
|
138
140
|
PATTERN
|
139
141
|
|
142
|
+
# @!method method_require?(node)
|
140
143
|
def_node_matcher :method_require?, <<~PATTERN
|
141
144
|
(block-pass (send nil? :method (sym :require)))
|
142
145
|
PATTERN
|
143
146
|
|
147
|
+
# @!method unsorted_dir_glob_pass?(node)
|
144
148
|
def_node_matcher :unsorted_dir_glob_pass?, <<~PATTERN
|
145
149
|
(send (const {nil? cbase} :Dir) :glob ...
|
146
150
|
(block-pass (send nil? :method (sym :require))))
|
147
151
|
PATTERN
|
148
152
|
|
153
|
+
# @!method unsorted_dir_each_pass?(node)
|
149
154
|
def_node_matcher :unsorted_dir_each_pass?, <<~PATTERN
|
150
155
|
(send (send (const {nil? cbase} :Dir) {:[] :glob} ...) :each
|
151
156
|
(block-pass (send nil? :method (sym :require))))
|
152
157
|
PATTERN
|
153
158
|
|
159
|
+
# @!method loop_variable(node)
|
154
160
|
def_node_matcher :loop_variable, <<~PATTERN
|
155
161
|
(args (arg $_))
|
156
162
|
PATTERN
|
157
163
|
|
164
|
+
# @!method var_is_required?(node, name)
|
158
165
|
def_node_search :var_is_required?, <<~PATTERN
|
159
166
|
(send nil? :require (lvar %1))
|
160
167
|
PATTERN
|
@@ -73,7 +73,10 @@ module RuboCop
|
|
73
73
|
!return_node.children.empty?
|
74
74
|
end
|
75
75
|
|
76
|
+
# @!method chained_send?(node)
|
76
77
|
def_node_matcher :chained_send?, '(send !nil? ...)'
|
78
|
+
|
79
|
+
# @!method define_method?(node)
|
77
80
|
def_node_matcher :define_method?, <<~PATTERN
|
78
81
|
(send _ {:define_method :define_singleton_method} _)
|
79
82
|
PATTERN
|
@@ -25,12 +25,18 @@ module RuboCop
|
|
25
25
|
# '10'.to_i
|
26
26
|
# '10.2'.to_f
|
27
27
|
# '10'.to_c
|
28
|
+
# ['1', '2', '3'].map(&:to_i)
|
29
|
+
# foo.try(:to_f)
|
30
|
+
# bar.send(:to_c)
|
28
31
|
#
|
29
32
|
# # good
|
30
33
|
#
|
31
34
|
# Integer('10', 10)
|
32
35
|
# Float('10.2')
|
33
36
|
# Complex('10')
|
37
|
+
# ['1', '2', '3'].map { |i| Integer(i, 10) }
|
38
|
+
# foo.try { |i| Float(i) }
|
39
|
+
# bar.send { |i| Complex(i) }
|
34
40
|
#
|
35
41
|
# @example IgnoredMethods: [minutes]
|
36
42
|
#
|
@@ -52,22 +58,35 @@ module RuboCop
|
|
52
58
|
}.freeze
|
53
59
|
MSG = 'Replace unsafe number conversion with number '\
|
54
60
|
'class parsing, instead of using '\
|
55
|
-
'%<
|
61
|
+
'%<current>s, use stricter '\
|
56
62
|
'%<corrected_method>s.'
|
57
|
-
|
63
|
+
METHODS = CONVERSION_METHOD_CLASS_MAPPING.keys.map(&:inspect).join(' ')
|
58
64
|
|
65
|
+
# @!method to_method(node)
|
59
66
|
def_node_matcher :to_method, <<~PATTERN
|
60
|
-
(send $_ ${
|
67
|
+
(send $_ ${#{METHODS}})
|
68
|
+
PATTERN
|
69
|
+
|
70
|
+
# @!method to_method_symbol(node)
|
71
|
+
def_node_matcher :to_method_symbol, <<~PATTERN
|
72
|
+
{(send _ $_ ${(sym ${#{METHODS}})} ...)
|
73
|
+
(send _ $_ ${(block_pass (sym ${#{METHODS}}))} ...)}
|
61
74
|
PATTERN
|
62
75
|
|
63
76
|
def on_send(node)
|
77
|
+
handle_conversion_method(node)
|
78
|
+
handle_as_symbol(node)
|
79
|
+
end
|
80
|
+
|
81
|
+
private
|
82
|
+
|
83
|
+
def handle_conversion_method(node)
|
64
84
|
to_method(node) do |receiver, to_method|
|
65
85
|
next if receiver.nil? || ignore_receiver?(receiver)
|
66
86
|
|
67
87
|
message = format(
|
68
88
|
MSG,
|
69
|
-
|
70
|
-
to_method: to_method,
|
89
|
+
current: "#{receiver.source}.#{to_method}",
|
71
90
|
corrected_method: correct_method(node, receiver)
|
72
91
|
)
|
73
92
|
add_offense(node, message: message) do |corrector|
|
@@ -76,13 +95,31 @@ module RuboCop
|
|
76
95
|
end
|
77
96
|
end
|
78
97
|
|
79
|
-
|
98
|
+
def handle_as_symbol(node)
|
99
|
+
to_method_symbol(node) do |receiver, sym_node, to_method|
|
100
|
+
next if receiver.nil?
|
101
|
+
|
102
|
+
message = format(
|
103
|
+
MSG,
|
104
|
+
current: sym_node.source,
|
105
|
+
corrected_method: correct_sym_method(to_method)
|
106
|
+
)
|
107
|
+
add_offense(node, message: message) do |corrector|
|
108
|
+
corrector.replace(sym_node, correct_sym_method(to_method))
|
109
|
+
end
|
110
|
+
end
|
111
|
+
end
|
80
112
|
|
81
113
|
def correct_method(node, receiver)
|
82
114
|
format(CONVERSION_METHOD_CLASS_MAPPING[node.method_name],
|
83
115
|
number_object: receiver.source)
|
84
116
|
end
|
85
117
|
|
118
|
+
def correct_sym_method(to_method)
|
119
|
+
body = format(CONVERSION_METHOD_CLASS_MAPPING[to_method], number_object: 'i')
|
120
|
+
"{ |i| #{body} }"
|
121
|
+
end
|
122
|
+
|
86
123
|
def ignore_receiver?(receiver)
|
87
124
|
if receiver.send_type? && ignored_method?(receiver.method_name)
|
88
125
|
true
|
@@ -0,0 +1,47 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for uses of numbered parameter assignment.
|
7
|
+
# It emulates the following warning in Ruby 2.7:
|
8
|
+
#
|
9
|
+
# % ruby -ve '_1 = :value'
|
10
|
+
# ruby 2.7.2p137 (2020-10-01 revision 5445e04352) [x86_64-darwin19]
|
11
|
+
# -e:1: warning: `_1' is reserved for numbered parameter; consider another name
|
12
|
+
#
|
13
|
+
# Assiging to numbered parameter (from `_1` to `_9`) cause an error in Ruby 3.0.
|
14
|
+
#
|
15
|
+
# % ruby -ve '_1 = :value'
|
16
|
+
# ruby 3.0.0p0 (2020-12-25 revision 95aff21468) [x86_64-darwin19]
|
17
|
+
# -e:1: _1 is reserved for numbered parameter
|
18
|
+
#
|
19
|
+
# NOTE: The parametered parameters are from `_1` to `_9`. This cop checks `_0`, and over `_10`
|
20
|
+
# as well to prevent confusion.
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
#
|
24
|
+
# # bad
|
25
|
+
# _1 = :value
|
26
|
+
#
|
27
|
+
# # good
|
28
|
+
# non_numbered_parameter_name = :value
|
29
|
+
#
|
30
|
+
class NumberedParameterAssignment < Base
|
31
|
+
NUM_PARAM_MSG = '`_%<number>s` is reserved for numbered parameter; consider another name.'
|
32
|
+
LVAR_MSG = '`_%<number>s` is similar to numbered parameter; consider another name.'
|
33
|
+
NUMBERED_PARAMETER_RANGE = (1..9).freeze
|
34
|
+
|
35
|
+
def on_lvasgn(node)
|
36
|
+
lhs, _rhs = *node
|
37
|
+
return unless /\A_(\d+)\z/ =~ lhs
|
38
|
+
|
39
|
+
number = Regexp.last_match(1).to_i
|
40
|
+
template = NUMBERED_PARAMETER_RANGE.include?(number) ? NUM_PARAM_MSG : LVAR_MSG
|
41
|
+
|
42
|
+
add_offense(node, message: format(template, number: number))
|
43
|
+
end
|
44
|
+
end
|
45
|
+
end
|
46
|
+
end
|
47
|
+
end
|
@@ -0,0 +1,39 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for unintended or-assignment to a constant.
|
7
|
+
#
|
8
|
+
# Constants should always be assigned in the same location. And its value
|
9
|
+
# should always be the same. If constants are assigned in multiple
|
10
|
+
# locations, the result may vary depending on the order of `require`.
|
11
|
+
#
|
12
|
+
# Also, if you already have such an implementation, auto-correction may
|
13
|
+
# change the result.
|
14
|
+
#
|
15
|
+
# @example
|
16
|
+
#
|
17
|
+
# # bad
|
18
|
+
# CONST ||= 1
|
19
|
+
#
|
20
|
+
# # good
|
21
|
+
# CONST = 1
|
22
|
+
#
|
23
|
+
class OrAssignmentToConstant < Base
|
24
|
+
extend AutoCorrector
|
25
|
+
|
26
|
+
MSG = 'Avoid using or-assignment with constants.'
|
27
|
+
|
28
|
+
def on_or_asgn(node)
|
29
|
+
lhs, _rhs = *node
|
30
|
+
return unless lhs&.casgn_type?
|
31
|
+
|
32
|
+
add_offense(node.loc.operator) do |corrector|
|
33
|
+
corrector.replace(node.loc.operator, '=')
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
39
|
+
end
|
@@ -33,10 +33,12 @@ module RuboCop
|
|
33
33
|
MSG = 'Use `StandardError` over `Exception`.'
|
34
34
|
RESTRICT_ON_SEND = %i[raise fail].freeze
|
35
35
|
|
36
|
+
# @!method exception?(node)
|
36
37
|
def_node_matcher :exception?, <<~PATTERN
|
37
38
|
(send nil? {:raise :fail} $(const ${cbase nil?} :Exception) ... )
|
38
39
|
PATTERN
|
39
40
|
|
41
|
+
# @!method exception_new_with_message?(node)
|
40
42
|
def_node_matcher :exception_new_with_message?, <<~PATTERN
|
41
43
|
(send nil? {:raise :fail}
|
42
44
|
(send $(const ${cbase nil?} :Exception) :new ... ))
|
@@ -25,6 +25,7 @@ module RuboCop
|
|
25
25
|
'Perhaps you meant `rand(2)` or `rand`?'
|
26
26
|
RESTRICT_ON_SEND = %i[rand].freeze
|
27
27
|
|
28
|
+
# @!method rand_one?(node)
|
28
29
|
def_node_matcher :rand_one?, <<~PATTERN
|
29
30
|
(send {(const {nil? cbase} :Kernel) nil?} :rand {(int {-1 1}) (float {-1.0 1.0})})
|
30
31
|
PATTERN
|
@@ -34,11 +34,13 @@ module RuboCop
|
|
34
34
|
|
35
35
|
def on_send(node)
|
36
36
|
return unless (receiver = node.receiver)
|
37
|
-
return unless receiver.receiver
|
37
|
+
return unless receiver.receiver&.const_type? && receiver.receiver.short_name == :Dir
|
38
38
|
return unless GLOB_METHODS.include?(receiver.method_name)
|
39
39
|
|
40
|
-
|
41
|
-
|
40
|
+
selector = node.loc.selector
|
41
|
+
|
42
|
+
add_offense(selector) do |corrector|
|
43
|
+
corrector.remove(selector)
|
42
44
|
corrector.remove(node.loc.dot)
|
43
45
|
end
|
44
46
|
end
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
MSG = 'Remove unnecessary `require` statement.'
|
29
29
|
RESTRICT_ON_SEND = %i[require].freeze
|
30
30
|
|
31
|
+
# @!method unnecessary_require_statement?(node)
|
31
32
|
def_node_matcher :unnecessary_require_statement?, <<~PATTERN
|
32
33
|
(send nil? :require
|
33
34
|
(str {"enumerator" "rational" "complex" "thread"}))
|
@@ -43,6 +43,7 @@ module RuboCop
|
|
43
43
|
|
44
44
|
NIL_SPECIFIC_METHODS = (nil.methods - Object.new.methods).to_set.freeze
|
45
45
|
|
46
|
+
# @!method respond_to_nil_specific_method?(node)
|
46
47
|
def_node_matcher :respond_to_nil_specific_method?, <<~PATTERN
|
47
48
|
(csend _ :respond_to? (sym %NIL_SPECIFIC_METHODS))
|
48
49
|
PATTERN
|
@@ -79,6 +79,7 @@ module RuboCop
|
|
79
79
|
PERCENT_CAPITAL_I = '%I'
|
80
80
|
ASSIGNMENT_TYPES = %i[lvasgn ivasgn cvasgn gvasgn].freeze
|
81
81
|
|
82
|
+
# @!method array_new?(node)
|
82
83
|
def_node_matcher :array_new?, <<~PATTERN
|
83
84
|
{
|
84
85
|
$(send (const {nil? cbase} :Array) :new ...)
|
@@ -86,6 +87,7 @@ module RuboCop
|
|
86
87
|
}
|
87
88
|
PATTERN
|
88
89
|
|
90
|
+
# @!method literal_expansion(node)
|
89
91
|
def_node_matcher :literal_expansion, <<~PATTERN
|
90
92
|
(splat {$({str dstr int float array} ...) (block $#array_new? ...) $#array_new?} ...)
|
91
93
|
PATTERN
|
@@ -140,13 +142,15 @@ module RuboCop
|
|
140
142
|
def replacement_range_and_content(node)
|
141
143
|
variable, = *node
|
142
144
|
loc = node.loc
|
145
|
+
expression = loc.expression
|
143
146
|
|
144
147
|
if array_new?(variable)
|
145
|
-
|
148
|
+
expression = node.parent.loc.expression if node.parent.array_type?
|
149
|
+
[expression, variable.source]
|
146
150
|
elsif !variable.array_type?
|
147
|
-
[
|
151
|
+
[expression, "[#{variable.source}]"]
|
148
152
|
elsif redundant_brackets?(node)
|
149
|
-
[
|
153
|
+
[expression, remove_brackets(variable)]
|
150
154
|
else
|
151
155
|
[loc.operator, '']
|
152
156
|
end
|