rubocop 1.57.1 → 1.65.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +4 -5
- data/assets/output.css.erb +159 -0
- data/assets/output.html.erb +1 -160
- data/config/default.yml +136 -19
- data/lib/rubocop/cached_data.rb +11 -3
- data/lib/rubocop/cli/command/auto_generate_config.rb +22 -8
- data/lib/rubocop/cli/command/lsp.rb +2 -2
- data/lib/rubocop/cli/command/show_docs_url.rb +2 -2
- data/lib/rubocop/cli.rb +10 -1
- data/lib/rubocop/config.rb +36 -12
- data/lib/rubocop/config_finder.rb +12 -2
- data/lib/rubocop/config_loader.rb +1 -2
- data/lib/rubocop/config_loader_resolver.rb +9 -3
- data/lib/rubocop/config_obsoletion.rb +11 -8
- data/lib/rubocop/config_validator.rb +14 -7
- data/lib/rubocop/cop/autocorrect_logic.rb +6 -1
- data/lib/rubocop/cop/base.rb +63 -16
- data/lib/rubocop/cop/bundler/gem_comment.rb +2 -2
- data/lib/rubocop/cop/bundler/gem_version.rb +3 -5
- data/lib/rubocop/cop/cop.rb +20 -2
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +4 -8
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +5 -13
- data/lib/rubocop/cop/documentation.rb +16 -6
- data/lib/rubocop/cop/exclude_limit.rb +1 -1
- data/lib/rubocop/cop/force.rb +12 -0
- data/lib/rubocop/cop/gemspec/add_runtime_dependency.rb +38 -0
- data/lib/rubocop/cop/gemspec/dependency_version.rb +3 -5
- data/lib/rubocop/cop/gemspec/deprecated_attribute_assignment.rb +2 -2
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +2 -2
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +5 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -3
- data/lib/rubocop/cop/internal_affairs/example_description.rb +6 -5
- data/lib/rubocop/cop/internal_affairs/method_name_end_with.rb +8 -6
- data/lib/rubocop/cop/internal_affairs/method_name_equal.rb +19 -20
- data/lib/rubocop/cop/internal_affairs/node_first_or_last_argument.rb +53 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +123 -29
- data/lib/rubocop/cop/internal_affairs/redundant_expect_offense_arguments.rb +34 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/empty_comment.rb +3 -1
- data/lib/rubocop/cop/layout/empty_line_after_magic_comment.rb +14 -7
- data/lib/rubocop/cop/layout/empty_line_after_multiline_condition.rb +1 -1
- data/lib/rubocop/cop/layout/end_alignment.rb +15 -3
- data/lib/rubocop/cop/layout/extra_spacing.rb +4 -10
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +24 -7
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +2 -2
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/line_continuation_leading_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +20 -20
- data/lib/rubocop/cop/layout/redundant_line_break.rb +16 -3
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +4 -4
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +5 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +53 -20
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +19 -10
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +3 -4
- data/lib/rubocop/cop/legacy/corrector.rb +12 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +6 -6
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +2 -2
- data/lib/rubocop/cop/lint/constant_overwritten_in_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/debugger.rb +29 -3
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -1
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +2 -2
- data/lib/rubocop/cop/lint/empty_when.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +24 -17
- data/lib/rubocop/cop/lint/float_comparison.rb +10 -0
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +2 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +14 -7
- data/lib/rubocop/cop/lint/it_without_arguments_in_block.rb +56 -0
- data/lib/rubocop/cop/lint/literal_as_condition.rb +1 -1
- data/lib/rubocop/cop/lint/literal_assignment_in_condition.rb +85 -0
- data/lib/rubocop/cop/lint/mixed_case_range.rb +9 -4
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +6 -21
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +3 -5
- data/lib/rubocop/cop/lint/number_conversion.rb +9 -4
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +54 -6
- data/lib/rubocop/cop/lint/redundant_with_index.rb +6 -2
- data/lib/rubocop/cop/lint/redundant_with_object.rb +2 -2
- data/lib/rubocop/cop/lint/rescue_type.rb +1 -3
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +3 -4
- data/lib/rubocop/cop/lint/script_permission.rb +3 -3
- data/lib/rubocop/cop/lint/self_assignment.rb +38 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +7 -2
- data/lib/rubocop/cop/lint/syntax.rb +6 -3
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -3
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -1
- data/lib/rubocop/cop/lint/unmodified_reduce_accumulator.rb +3 -2
- data/lib/rubocop/cop/lint/unreachable_code.rb +4 -2
- data/lib/rubocop/cop/lint/unreachable_loop.rb +8 -2
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -2
- data/lib/rubocop/cop/lint/useless_times.rb +2 -2
- data/lib/rubocop/cop/lint/void.rb +53 -12
- data/lib/rubocop/cop/metrics/abc_size.rb +3 -3
- data/lib/rubocop/cop/metrics/block_nesting.rb +19 -7
- data/lib/rubocop/cop/metrics/class_length.rb +6 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +5 -5
- data/lib/rubocop/cop/mixin/alignment.rb +5 -1
- data/lib/rubocop/cop/mixin/allowed_methods.rb +7 -1
- data/lib/rubocop/cop/mixin/allowed_pattern.rb +15 -3
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +1 -1
- data/lib/rubocop/cop/mixin/code_length.rb +12 -1
- data/lib/rubocop/cop/mixin/comments_help.rb +16 -12
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -0
- data/lib/rubocop/cop/mixin/configurable_max.rb +5 -1
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +23 -13
- data/lib/rubocop/cop/mixin/method_complexity.rb +15 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/rescue_node.rb +4 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +1 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/block_forwarding.rb +34 -7
- data/lib/rubocop/cop/naming/constant_name.rb +1 -2
- data/lib/rubocop/cop/naming/file_name.rb +2 -2
- data/lib/rubocop/cop/naming/inclusive_language.rb +1 -2
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +2 -2
- data/lib/rubocop/cop/registry.rb +1 -1
- data/lib/rubocop/cop/security/compound_hash.rb +2 -2
- data/lib/rubocop/cop/security/open.rb +2 -2
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +52 -2
- data/lib/rubocop/cop/style/accessor_grouping.rb +1 -1
- data/lib/rubocop/cop/style/alias.rb +1 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +155 -21
- data/lib/rubocop/cop/style/array_first_last.rb +64 -0
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +21 -14
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +2 -2
- data/lib/rubocop/cop/style/case_like_if.rb +5 -5
- data/lib/rubocop/cop/style/class_check.rb +1 -0
- data/lib/rubocop/cop/style/class_vars.rb +3 -3
- data/lib/rubocop/cop/style/collection_compact.rb +21 -11
- data/lib/rubocop/cop/style/combinable_loops.rb +13 -7
- data/lib/rubocop/cop/style/commented_keyword.rb +5 -2
- data/lib/rubocop/cop/style/concat_array_literals.rb +1 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +7 -8
- data/lib/rubocop/cop/style/copyright.rb +31 -21
- data/lib/rubocop/cop/style/date_time.rb +5 -4
- data/lib/rubocop/cop/style/documentation.rb +24 -24
- data/lib/rubocop/cop/style/documentation_method.rb +20 -0
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +7 -7
- data/lib/rubocop/cop/style/each_with_object.rb +2 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -1
- data/lib/rubocop/cop/style/eval_with_location.rb +6 -15
- data/lib/rubocop/cop/style/exact_regexp_match.rb +4 -2
- data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
- data/lib/rubocop/cop/style/for.rb +2 -0
- data/lib/rubocop/cop/style/format_string.rb +9 -9
- data/lib/rubocop/cop/style/hash_each_methods.rb +105 -11
- data/lib/rubocop/cop/style/hash_except.rb +10 -6
- data/lib/rubocop/cop/style/hash_syntax.rb +24 -2
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +12 -1
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +5 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +14 -13
- data/lib/rubocop/cop/style/invertible_unless_condition.rb +44 -2
- data/lib/rubocop/cop/style/map_compact_with_conditional_block.rb +82 -50
- data/lib/rubocop/cop/style/map_into_array.rb +175 -0
- data/lib/rubocop/cop/style/map_to_hash.rb +18 -8
- data/lib/rubocop/cop/style/map_to_set.rb +1 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +19 -5
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +20 -0
- data/lib/rubocop/cop/style/method_def_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/missing_respond_to_missing.rb +2 -2
- data/lib/rubocop/cop/style/multiline_method_signature.rb +10 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +5 -3
- data/lib/rubocop/cop/style/next.rb +1 -1
- data/lib/rubocop/cop/style/nil_comparison.rb +2 -0
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +10 -2
- data/lib/rubocop/cop/style/object_then.rb +5 -3
- data/lib/rubocop/cop/style/one_line_conditional.rb +1 -1
- data/lib/rubocop/cop/style/operator_method_call.rb +2 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +3 -5
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +8 -0
- data/lib/rubocop/cop/style/quoted_symbols.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +4 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +27 -3
- data/lib/rubocop/cop/style/redundant_assignment.rb +10 -2
- data/lib/rubocop/cop/style/redundant_begin.rb +1 -1
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +5 -4
- data/lib/rubocop/cop/style/redundant_double_splat_hash_braces.rb +17 -10
- data/lib/rubocop/cop/style/redundant_each.rb +7 -4
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +3 -3
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +1 -1
- data/lib/rubocop/cop/style/redundant_filter_chain.rb +5 -4
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +19 -2
- data/lib/rubocop/cop/style/redundant_parentheses.rb +50 -19
- data/lib/rubocop/cop/style/redundant_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/redundant_return.rb +7 -1
- data/lib/rubocop/cop/style/redundant_self.rb +17 -2
- data/lib/rubocop/cop/style/redundant_sort.rb +9 -8
- data/lib/rubocop/cop/style/redundant_sort_by.rb +2 -2
- data/lib/rubocop/cop/style/redundant_string_escape.rb +1 -1
- data/lib/rubocop/cop/style/require_order.rb +1 -1
- data/lib/rubocop/cop/style/sample.rb +3 -4
- data/lib/rubocop/cop/style/select_by_regexp.rb +7 -6
- data/lib/rubocop/cop/style/self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +8 -0
- data/lib/rubocop/cop/style/send.rb +4 -4
- data/lib/rubocop/cop/style/send_with_literal_method_name.rb +104 -0
- data/lib/rubocop/cop/style/single_argument_dig.rb +7 -3
- data/lib/rubocop/cop/style/single_line_do_end_block.rb +3 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +76 -10
- data/lib/rubocop/cop/style/special_global_vars.rb +1 -2
- data/lib/rubocop/cop/style/string_chars.rb +1 -0
- data/lib/rubocop/cop/style/strip.rb +7 -4
- data/lib/rubocop/cop/style/super_arguments.rb +174 -0
- data/lib/rubocop/cop/style/super_with_args_parentheses.rb +35 -0
- data/lib/rubocop/cop/style/symbol_proc.rb +75 -5
- data/lib/rubocop/cop/style/top_level_method_definition.rb +1 -1
- data/lib/rubocop/cop/style/unpack_first.rb +11 -14
- data/lib/rubocop/cop/style/zero_length_predicate.rb +28 -24
- data/lib/rubocop/cop/team.rb +13 -0
- data/lib/rubocop/cop/util.rb +7 -1
- data/lib/rubocop/cop/utils/regexp_ranges.rb +1 -1
- data/lib/rubocop/cops_documentation_generator.rb +16 -4
- data/lib/rubocop/directive_comment.rb +10 -8
- data/lib/rubocop/ext/regexp_node.rb +9 -4
- data/lib/rubocop/ext/regexp_parser.rb +4 -21
- data/lib/rubocop/formatter/clang_style_formatter.rb +3 -7
- data/lib/rubocop/formatter/disabled_config_formatter.rb +23 -8
- data/lib/rubocop/formatter/formatter_set.rb +7 -1
- data/lib/rubocop/formatter/html_formatter.rb +37 -14
- data/lib/rubocop/formatter/json_formatter.rb +0 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +12 -2
- data/lib/rubocop/formatter/tap_formatter.rb +3 -7
- data/lib/rubocop/formatter.rb +1 -1
- data/lib/rubocop/lockfile.rb +56 -7
- data/lib/rubocop/lsp/logger.rb +1 -1
- data/lib/rubocop/lsp/routes.rb +12 -15
- data/lib/rubocop/lsp/runtime.rb +1 -1
- data/lib/rubocop/lsp/server.rb +7 -2
- data/lib/rubocop/lsp/severity.rb +1 -1
- data/lib/rubocop/lsp.rb +36 -0
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/options.rb +14 -11
- data/lib/rubocop/path_util.rb +6 -2
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/result_cache.rb +0 -1
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/expect_offense.rb +16 -8
- data/lib/rubocop/rspec/shared_contexts.rb +73 -16
- data/lib/rubocop/rspec/support.rb +3 -0
- data/lib/rubocop/runner.rb +14 -3
- data/lib/rubocop/server/cache.rb +11 -2
- data/lib/rubocop/server/client_command/exec.rb +2 -3
- data/lib/rubocop/server/client_command/start.rb +1 -1
- data/lib/rubocop/server/core.rb +4 -0
- data/lib/rubocop/server/server_command/exec.rb +0 -1
- data/lib/rubocop/target_finder.rb +84 -78
- data/lib/rubocop/target_ruby.rb +82 -80
- data/lib/rubocop/version.rb +19 -4
- data/lib/rubocop.rb +8 -0
- metadata +27 -29
- /data/lib/rubocop/formatter/{git_hub_actions_formatter.rb → github_actions_formatter.rb} +0 -0
|
@@ -115,8 +115,8 @@ module RuboCop
|
|
|
115
115
|
end
|
|
116
116
|
|
|
117
117
|
# Check for `if` and `case` statements where each branch is used for
|
|
118
|
-
#
|
|
119
|
-
# condition can be used instead.
|
|
118
|
+
# both the assignment and comparison of the same variable
|
|
119
|
+
# when using the return of the condition can be used instead.
|
|
120
120
|
#
|
|
121
121
|
# @example EnforcedStyle: assign_to_condition (default)
|
|
122
122
|
# # bad
|
|
@@ -214,7 +214,7 @@ module RuboCop
|
|
|
214
214
|
extend AutoCorrector
|
|
215
215
|
|
|
216
216
|
MSG = 'Use the return of the conditional for variable assignment and comparison.'
|
|
217
|
-
ASSIGN_TO_CONDITION_MSG = 'Assign variables inside of conditionals'
|
|
217
|
+
ASSIGN_TO_CONDITION_MSG = 'Assign variables inside of conditionals.'
|
|
218
218
|
VARIABLE_ASSIGNMENT_TYPES = %i[casgn cvasgn gvasgn ivasgn lvasgn].freeze
|
|
219
219
|
ASSIGNMENT_TYPES = VARIABLE_ASSIGNMENT_TYPES + %i[and_asgn or_asgn op_asgn masgn].freeze
|
|
220
220
|
LINE_LENGTH = 'Layout/LineLength'
|
|
@@ -233,7 +233,7 @@ module RuboCop
|
|
|
233
233
|
PATTERN
|
|
234
234
|
|
|
235
235
|
ASSIGNMENT_TYPES.each do |type|
|
|
236
|
-
define_method "on_#{type}" do |node|
|
|
236
|
+
define_method :"on_#{type}" do |node|
|
|
237
237
|
return if part_of_ignored_node?(node)
|
|
238
238
|
return if node.parent&.shorthand_asgn?
|
|
239
239
|
|
|
@@ -460,9 +460,8 @@ module RuboCop
|
|
|
460
460
|
|
|
461
461
|
def assignment(node)
|
|
462
462
|
*_, condition = *node
|
|
463
|
-
|
|
464
|
-
|
|
465
|
-
condition.source_range.begin_pos)
|
|
463
|
+
|
|
464
|
+
node.source_range.begin.join(condition.source_range.begin)
|
|
466
465
|
end
|
|
467
466
|
|
|
468
467
|
def correct_if_branches(corrector, cop, node)
|
|
@@ -534,7 +533,7 @@ module RuboCop
|
|
|
534
533
|
end
|
|
535
534
|
|
|
536
535
|
def element_assignment?(node)
|
|
537
|
-
node.send_type? && node.
|
|
536
|
+
node.send_type? && !node.method?(:[]=)
|
|
538
537
|
end
|
|
539
538
|
|
|
540
539
|
def extract_branches(node)
|
|
@@ -28,18 +28,27 @@ module RuboCop
|
|
|
28
28
|
def on_new_investigation
|
|
29
29
|
return if notice.empty? || notice_found?(processed_source)
|
|
30
30
|
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
|
|
34
|
-
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
|
|
31
|
+
verify_autocorrect_notice!
|
|
32
|
+
message = format(MSG, notice: notice)
|
|
33
|
+
if processed_source.blank?
|
|
34
|
+
add_global_offense(message)
|
|
35
|
+
else
|
|
36
|
+
offense_range = source_range(processed_source.buffer, 1, 0)
|
|
37
|
+
add_offense(offense_range, message: message) do |corrector|
|
|
38
|
+
autocorrect(corrector)
|
|
39
|
+
end
|
|
38
40
|
end
|
|
39
41
|
end
|
|
40
42
|
|
|
41
43
|
private
|
|
42
44
|
|
|
45
|
+
def autocorrect(corrector)
|
|
46
|
+
token = insert_notice_before(processed_source)
|
|
47
|
+
range = token.nil? ? range_between(0, 0) : token.pos
|
|
48
|
+
|
|
49
|
+
corrector.insert_before(range, "#{autocorrect_notice}\n")
|
|
50
|
+
end
|
|
51
|
+
|
|
43
52
|
def notice
|
|
44
53
|
cop_config['Notice']
|
|
45
54
|
end
|
|
@@ -48,17 +57,16 @@ module RuboCop
|
|
|
48
57
|
cop_config['AutocorrectNotice']
|
|
49
58
|
end
|
|
50
59
|
|
|
51
|
-
def offense_range
|
|
52
|
-
source_range(processed_source.buffer, 1, 0)
|
|
53
|
-
end
|
|
54
|
-
|
|
55
60
|
def verify_autocorrect_notice!
|
|
56
|
-
|
|
61
|
+
if autocorrect_notice.nil? || autocorrect_notice.empty?
|
|
62
|
+
raise Warning, "#{cop_name}: #{AUTOCORRECT_EMPTY_WARNING}"
|
|
63
|
+
end
|
|
57
64
|
|
|
58
65
|
regex = Regexp.new(notice)
|
|
59
|
-
return if autocorrect_notice
|
|
66
|
+
return if autocorrect_notice.gsub(/^# */, '').match?(regex)
|
|
60
67
|
|
|
61
|
-
|
|
68
|
+
message = "AutocorrectNotice '#{autocorrect_notice}' must match Notice /#{notice}/"
|
|
69
|
+
raise Warning, "#{cop_name}: #{message}"
|
|
62
70
|
end
|
|
63
71
|
|
|
64
72
|
def insert_notice_before(processed_source)
|
|
@@ -72,26 +80,28 @@ module RuboCop
|
|
|
72
80
|
return false if token_index >= processed_source.tokens.size
|
|
73
81
|
|
|
74
82
|
token = processed_source.tokens[token_index]
|
|
75
|
-
token.comment? &&
|
|
83
|
+
token.comment? && /\A#!.*\z/.match?(token.text)
|
|
76
84
|
end
|
|
77
85
|
|
|
78
86
|
def encoding_token?(processed_source, token_index)
|
|
79
87
|
return false if token_index >= processed_source.tokens.size
|
|
80
88
|
|
|
81
89
|
token = processed_source.tokens[token_index]
|
|
82
|
-
token.comment? &&
|
|
90
|
+
token.comment? && /\A#.*coding\s?[:=]\s?(?:UTF|utf)-8/.match?(token.text)
|
|
83
91
|
end
|
|
84
92
|
|
|
85
93
|
def notice_found?(processed_source)
|
|
86
|
-
|
|
87
|
-
|
|
94
|
+
notice_regexp = Regexp.new(notice.lines.map(&:strip).join)
|
|
95
|
+
multiline_notice = +''
|
|
88
96
|
processed_source.tokens.each do |token|
|
|
89
97
|
break unless token.comment?
|
|
90
98
|
|
|
91
|
-
|
|
92
|
-
|
|
99
|
+
multiline_notice << token.text.sub(/\A# */, '')
|
|
100
|
+
|
|
101
|
+
break if notice_regexp.match?(token.text)
|
|
93
102
|
end
|
|
94
|
-
|
|
103
|
+
|
|
104
|
+
multiline_notice.match?(notice_regexp)
|
|
95
105
|
end
|
|
96
106
|
end
|
|
97
107
|
end
|
|
@@ -49,12 +49,12 @@ module RuboCop
|
|
|
49
49
|
class DateTime < Base
|
|
50
50
|
extend AutoCorrector
|
|
51
51
|
|
|
52
|
-
CLASS_MSG = 'Prefer Time over DateTime
|
|
53
|
-
COERCION_MSG = 'Do not use
|
|
52
|
+
CLASS_MSG = 'Prefer `Time` over `DateTime`.'
|
|
53
|
+
COERCION_MSG = 'Do not use `#to_datetime`.'
|
|
54
54
|
|
|
55
55
|
# @!method date_time?(node)
|
|
56
56
|
def_node_matcher :date_time?, <<~PATTERN
|
|
57
|
-
(
|
|
57
|
+
(call (const {nil? (cbase)} :DateTime) ...)
|
|
58
58
|
PATTERN
|
|
59
59
|
|
|
60
60
|
# @!method historic_date?(node)
|
|
@@ -64,7 +64,7 @@ module RuboCop
|
|
|
64
64
|
|
|
65
65
|
# @!method to_datetime?(node)
|
|
66
66
|
def_node_matcher :to_datetime?, <<~PATTERN
|
|
67
|
-
(
|
|
67
|
+
(call _ :to_datetime)
|
|
68
68
|
PATTERN
|
|
69
69
|
|
|
70
70
|
def on_send(node)
|
|
@@ -75,6 +75,7 @@ module RuboCop
|
|
|
75
75
|
|
|
76
76
|
add_offense(node, message: message) { |corrector| autocorrect(corrector, node) }
|
|
77
77
|
end
|
|
78
|
+
alias on_csend on_send
|
|
78
79
|
|
|
79
80
|
private
|
|
80
81
|
|
|
@@ -29,36 +29,36 @@ module RuboCop
|
|
|
29
29
|
# end
|
|
30
30
|
#
|
|
31
31
|
# # allowed
|
|
32
|
-
#
|
|
32
|
+
# # Class without body
|
|
33
|
+
# class Person
|
|
34
|
+
# end
|
|
35
|
+
#
|
|
36
|
+
# # Namespace - A namespace can be a class or a module
|
|
37
|
+
# # Containing a class
|
|
38
|
+
# module Namespace
|
|
39
|
+
# # Description/Explanation of Person class
|
|
33
40
|
# class Person
|
|
41
|
+
# # ...
|
|
34
42
|
# end
|
|
43
|
+
# end
|
|
35
44
|
#
|
|
36
|
-
#
|
|
37
|
-
#
|
|
38
|
-
#
|
|
39
|
-
# # Description/Explanation of Person class
|
|
40
|
-
# class Person
|
|
41
|
-
# # ...
|
|
42
|
-
# end
|
|
45
|
+
# # Containing constant visibility declaration
|
|
46
|
+
# module Namespace
|
|
47
|
+
# class Private
|
|
43
48
|
# end
|
|
44
49
|
#
|
|
45
|
-
#
|
|
46
|
-
#
|
|
47
|
-
# class Private
|
|
48
|
-
# end
|
|
49
|
-
#
|
|
50
|
-
# private_constant :Private
|
|
51
|
-
# end
|
|
50
|
+
# private_constant :Private
|
|
51
|
+
# end
|
|
52
52
|
#
|
|
53
|
-
#
|
|
54
|
-
#
|
|
55
|
-
#
|
|
56
|
-
#
|
|
53
|
+
# # Containing constant definition
|
|
54
|
+
# module Namespace
|
|
55
|
+
# Public = Class.new
|
|
56
|
+
# end
|
|
57
57
|
#
|
|
58
|
-
#
|
|
59
|
-
#
|
|
60
|
-
#
|
|
61
|
-
#
|
|
58
|
+
# # Macro calls
|
|
59
|
+
# module Namespace
|
|
60
|
+
# extend Foo
|
|
61
|
+
# end
|
|
62
62
|
#
|
|
63
63
|
# @example AllowedConstants: ['ClassMethods']
|
|
64
64
|
#
|
|
@@ -67,7 +67,7 @@ module RuboCop
|
|
|
67
67
|
# module ClassMethods
|
|
68
68
|
# # ...
|
|
69
69
|
# end
|
|
70
|
-
#
|
|
70
|
+
# end
|
|
71
71
|
#
|
|
72
72
|
class Documentation < Base
|
|
73
73
|
include DocumentationComment
|
|
@@ -95,6 +95,17 @@ module RuboCop
|
|
|
95
95
|
# end
|
|
96
96
|
# end
|
|
97
97
|
#
|
|
98
|
+
# @example AllowedMethods: ['method_missing', 'respond_to_missing?']
|
|
99
|
+
#
|
|
100
|
+
# # good
|
|
101
|
+
# class Foo
|
|
102
|
+
# def method_missing(name, *args)
|
|
103
|
+
# end
|
|
104
|
+
#
|
|
105
|
+
# def respond_to_missing?(symbol, include_private)
|
|
106
|
+
# end
|
|
107
|
+
# end
|
|
108
|
+
#
|
|
98
109
|
class DocumentationMethod < Base
|
|
99
110
|
include DocumentationComment
|
|
100
111
|
include DefNode
|
|
@@ -119,6 +130,7 @@ module RuboCop
|
|
|
119
130
|
def check(node)
|
|
120
131
|
return if non_public?(node) && !require_for_non_public_methods?
|
|
121
132
|
return if documentation_comment?(node)
|
|
133
|
+
return if method_allowed?(node)
|
|
122
134
|
|
|
123
135
|
add_offense(node)
|
|
124
136
|
end
|
|
@@ -126,6 +138,14 @@ module RuboCop
|
|
|
126
138
|
def require_for_non_public_methods?
|
|
127
139
|
cop_config['RequireForNonPublicMethods']
|
|
128
140
|
end
|
|
141
|
+
|
|
142
|
+
def method_allowed?(node)
|
|
143
|
+
allowed_methods.include?(node.method_name)
|
|
144
|
+
end
|
|
145
|
+
|
|
146
|
+
def allowed_methods
|
|
147
|
+
@allowed_methods ||= cop_config.fetch('AllowedMethods', []).map(&:to_sym)
|
|
148
|
+
end
|
|
129
149
|
end
|
|
130
150
|
end
|
|
131
151
|
end
|
|
@@ -32,27 +32,27 @@ module RuboCop
|
|
|
32
32
|
|
|
33
33
|
send_node = node.send_node
|
|
34
34
|
|
|
35
|
-
|
|
36
|
-
|
|
37
|
-
add_offense(range) do |corrector|
|
|
35
|
+
add_offense(send_node) do |corrector|
|
|
38
36
|
range_type, min, max = each_range(node)
|
|
39
37
|
|
|
40
38
|
max += 1 if range_type == :irange
|
|
41
39
|
|
|
42
|
-
corrector.replace(
|
|
40
|
+
corrector.replace(send_node, "#{max - min}.times")
|
|
43
41
|
end
|
|
44
42
|
end
|
|
45
43
|
|
|
46
44
|
private
|
|
47
45
|
|
|
48
46
|
def offending?(node)
|
|
47
|
+
return false unless node.arguments.empty?
|
|
48
|
+
|
|
49
49
|
each_range_with_zero_origin?(node) || each_range_without_block_argument?(node)
|
|
50
50
|
end
|
|
51
51
|
|
|
52
52
|
# @!method each_range(node)
|
|
53
53
|
def_node_matcher :each_range, <<~PATTERN
|
|
54
54
|
(block
|
|
55
|
-
(
|
|
55
|
+
(call
|
|
56
56
|
(begin
|
|
57
57
|
(${irange erange}
|
|
58
58
|
(int $_) (int $_)))
|
|
@@ -64,7 +64,7 @@ module RuboCop
|
|
|
64
64
|
# @!method each_range_with_zero_origin?(node)
|
|
65
65
|
def_node_matcher :each_range_with_zero_origin?, <<~PATTERN
|
|
66
66
|
(block
|
|
67
|
-
(
|
|
67
|
+
(call
|
|
68
68
|
(begin
|
|
69
69
|
({irange erange}
|
|
70
70
|
(int 0) (int _)))
|
|
@@ -76,7 +76,7 @@ module RuboCop
|
|
|
76
76
|
# @!method each_range_without_block_argument?(node)
|
|
77
77
|
def_node_matcher :each_range_without_block_argument?, <<~PATTERN
|
|
78
78
|
(block
|
|
79
|
-
(
|
|
79
|
+
(call
|
|
80
80
|
(begin
|
|
81
81
|
({irange erange}
|
|
82
82
|
(int _) (int _)))
|
|
@@ -58,12 +58,12 @@ module RuboCop
|
|
|
58
58
|
|
|
59
59
|
# @!method each_with_object_block_candidate?(node)
|
|
60
60
|
def_node_matcher :each_with_object_block_candidate?, <<~PATTERN
|
|
61
|
-
(block $(
|
|
61
|
+
(block $(call _ {:inject :reduce} _) $_ $_)
|
|
62
62
|
PATTERN
|
|
63
63
|
|
|
64
64
|
# @!method each_with_object_numblock_candidate?(node)
|
|
65
65
|
def_node_matcher :each_with_object_numblock_candidate?, <<~PATTERN
|
|
66
|
-
(numblock $(
|
|
66
|
+
(numblock $(call _ {:inject :reduce} _) 2 $_)
|
|
67
67
|
PATTERN
|
|
68
68
|
|
|
69
69
|
def autocorrect_block(corrector, node, return_value)
|
|
@@ -83,7 +83,7 @@ module RuboCop
|
|
|
83
83
|
parent = node.parent
|
|
84
84
|
return false unless parent && %i[send super zsuper].include?(parent.type)
|
|
85
85
|
|
|
86
|
-
node.equal?(parent.
|
|
86
|
+
node.equal?(parent.first_argument) && !parentheses?(node.parent)
|
|
87
87
|
end
|
|
88
88
|
|
|
89
89
|
def replacement_range(node)
|
|
@@ -84,7 +84,7 @@ module RuboCop
|
|
|
84
84
|
# are considered.
|
|
85
85
|
return if node.method?(:eval) && !valid_eval_receiver?(node.receiver)
|
|
86
86
|
|
|
87
|
-
code = node.
|
|
87
|
+
code = node.first_argument
|
|
88
88
|
return unless code && (code.str_type? || code.dstr_type?)
|
|
89
89
|
|
|
90
90
|
check_location(node, code)
|
|
@@ -128,17 +128,6 @@ module RuboCop
|
|
|
128
128
|
node.method?(:eval) ? node.arguments.size >= 2 : true
|
|
129
129
|
end
|
|
130
130
|
|
|
131
|
-
# FIXME: It's a Style/ConditionalAssignment's false positive.
|
|
132
|
-
# rubocop:disable Style/ConditionalAssignment
|
|
133
|
-
def with_lineno?(node)
|
|
134
|
-
if node.method?(:eval)
|
|
135
|
-
node.arguments.size == 4
|
|
136
|
-
else
|
|
137
|
-
node.arguments.size == 3
|
|
138
|
-
end
|
|
139
|
-
end
|
|
140
|
-
# rubocop:enable Style/ConditionalAssignment
|
|
141
|
-
|
|
142
131
|
def add_offense_for_incorrect_line(method_name, line_node, sign, line_diff)
|
|
143
132
|
expected = expected_line(sign, line_diff)
|
|
144
133
|
message = format(MSG_INCORRECT_LINE,
|
|
@@ -152,7 +141,7 @@ module RuboCop
|
|
|
152
141
|
end
|
|
153
142
|
|
|
154
143
|
def check_file(node, file_node)
|
|
155
|
-
return
|
|
144
|
+
return if special_file_keyword?(file_node)
|
|
156
145
|
|
|
157
146
|
message = format(MSG_INCORRECT_FILE,
|
|
158
147
|
method_name: node.method_name,
|
|
@@ -165,7 +154,9 @@ module RuboCop
|
|
|
165
154
|
end
|
|
166
155
|
|
|
167
156
|
def check_line(node, code)
|
|
168
|
-
line_node = node.
|
|
157
|
+
line_node = node.last_argument
|
|
158
|
+
return if line_node.variable? || (line_node.send_type? && !line_node.method?(:+))
|
|
159
|
+
|
|
169
160
|
line_diff = line_difference(line_node, code)
|
|
170
161
|
if line_diff.zero?
|
|
171
162
|
add_offense_for_same_line(node, line_node)
|
|
@@ -227,7 +218,7 @@ module RuboCop
|
|
|
227
218
|
end
|
|
228
219
|
|
|
229
220
|
def missing_line(node, code)
|
|
230
|
-
line_diff = line_difference(node.
|
|
221
|
+
line_diff = line_difference(node.last_argument, code)
|
|
231
222
|
sign = line_diff.positive? ? :+ : :-
|
|
232
223
|
expected_line(sign, line_diff)
|
|
233
224
|
end
|
|
@@ -30,7 +30,7 @@ module RuboCop
|
|
|
30
30
|
|
|
31
31
|
# @!method exact_regexp_match(node)
|
|
32
32
|
def_node_matcher :exact_regexp_match, <<~PATTERN
|
|
33
|
-
(
|
|
33
|
+
(call
|
|
34
34
|
_ {:=~ :=== :!~ :match :match?}
|
|
35
35
|
(regexp
|
|
36
36
|
(str $_)
|
|
@@ -38,17 +38,19 @@ module RuboCop
|
|
|
38
38
|
PATTERN
|
|
39
39
|
|
|
40
40
|
def on_send(node)
|
|
41
|
+
return unless (receiver = node.receiver)
|
|
41
42
|
return unless (regexp = exact_regexp_match(node))
|
|
42
43
|
|
|
43
44
|
parsed_regexp = Regexp::Parser.parse(regexp)
|
|
44
45
|
return unless exact_match_pattern?(parsed_regexp)
|
|
45
46
|
|
|
46
|
-
prefer = "#{
|
|
47
|
+
prefer = "#{receiver.source} #{new_method(node)} '#{parsed_regexp[1].text}'"
|
|
47
48
|
|
|
48
49
|
add_offense(node, message: format(MSG, prefer: prefer)) do |corrector|
|
|
49
50
|
corrector.replace(node, prefer)
|
|
50
51
|
end
|
|
51
52
|
end
|
|
53
|
+
alias on_csend on_send
|
|
52
54
|
|
|
53
55
|
private
|
|
54
56
|
|
|
@@ -86,7 +86,7 @@ module RuboCop
|
|
|
86
86
|
|
|
87
87
|
def extract_block_name(def_node)
|
|
88
88
|
if def_node.block_argument?
|
|
89
|
-
def_node.
|
|
89
|
+
def_node.last_argument.name
|
|
90
90
|
else
|
|
91
91
|
'block'
|
|
92
92
|
end
|
|
@@ -127,7 +127,7 @@ module RuboCop
|
|
|
127
127
|
end
|
|
128
128
|
|
|
129
129
|
def insert_argument(node, corrector, block_name)
|
|
130
|
-
last_arg = node.
|
|
130
|
+
last_arg = node.last_argument
|
|
131
131
|
arg_range = range_with_surrounding_comma(last_arg.source_range, :right)
|
|
132
132
|
replacement = " &#{block_name}"
|
|
133
133
|
replacement = ",#{replacement}" unless arg_range.source.end_with?(',')
|
|
@@ -25,27 +25,27 @@ module RuboCop
|
|
|
25
25
|
#
|
|
26
26
|
# @example EnforcedStyle: format (default)
|
|
27
27
|
# # bad
|
|
28
|
-
# puts sprintf('%10s', '
|
|
29
|
-
# puts '%10s' % '
|
|
28
|
+
# puts sprintf('%10s', 'foo')
|
|
29
|
+
# puts '%10s' % 'foo'
|
|
30
30
|
#
|
|
31
31
|
# # good
|
|
32
|
-
# puts format('%10s', '
|
|
32
|
+
# puts format('%10s', 'foo')
|
|
33
33
|
#
|
|
34
34
|
# @example EnforcedStyle: sprintf
|
|
35
35
|
# # bad
|
|
36
|
-
# puts format('%10s', '
|
|
37
|
-
# puts '%10s' % '
|
|
36
|
+
# puts format('%10s', 'foo')
|
|
37
|
+
# puts '%10s' % 'foo'
|
|
38
38
|
#
|
|
39
39
|
# # good
|
|
40
|
-
# puts sprintf('%10s', '
|
|
40
|
+
# puts sprintf('%10s', 'foo')
|
|
41
41
|
#
|
|
42
42
|
# @example EnforcedStyle: percent
|
|
43
43
|
# # bad
|
|
44
|
-
# puts format('%10s', '
|
|
45
|
-
# puts sprintf('%10s', '
|
|
44
|
+
# puts format('%10s', 'foo')
|
|
45
|
+
# puts sprintf('%10s', 'foo')
|
|
46
46
|
#
|
|
47
47
|
# # good
|
|
48
|
-
# puts '%10s' % '
|
|
48
|
+
# puts '%10s' % 'foo'
|
|
49
49
|
#
|
|
50
50
|
class FormatString < Base
|
|
51
51
|
include ConfigurableEnforcedStyle
|