rubocop 0.49.1 → 0.50.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 +5 -3
- data/bin/rubocop +1 -1
- data/config/default.yml +160 -99
- data/config/disabled.yml +4 -5
- data/config/enabled.yml +149 -47
- data/lib/rubocop.rb +41 -14
- data/lib/rubocop/ast/builder.rb +4 -1
- data/lib/rubocop/ast/node.rb +36 -42
- data/lib/rubocop/ast/node/args_node.rb +1 -13
- data/lib/rubocop/ast/node/array_node.rb +9 -0
- data/lib/rubocop/ast/node/block_node.rb +9 -0
- data/lib/rubocop/ast/node/def_node.rb +71 -0
- data/lib/rubocop/ast/node/for_node.rb +8 -0
- data/lib/rubocop/ast/node/if_node.rb +10 -2
- data/lib/rubocop/ast/node/mixin/collection_node.rb +15 -0
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +174 -0
- data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +89 -0
- data/lib/rubocop/ast/node/mixin/parameterized_node.rb +18 -31
- data/lib/rubocop/ast/node/regexp_node.rb +35 -0
- data/lib/rubocop/ast/node/send_node.rb +1 -154
- data/lib/rubocop/ast/node/super_node.rb +3 -24
- data/lib/rubocop/ast/node/yield_node.rb +21 -0
- data/lib/rubocop/ast/traversal.rb +6 -6
- data/lib/rubocop/cli.rb +7 -3
- data/lib/rubocop/config.rb +45 -8
- data/lib/rubocop/config_loader.rb +7 -5
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +3 -3
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +64 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +12 -12
- data/lib/rubocop/cop/commissioner.rb +8 -2
- data/lib/rubocop/cop/cop.rb +3 -1
- data/lib/rubocop/cop/generator.rb +94 -21
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +14 -3
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +43 -0
- data/lib/rubocop/cop/internal_affairs/redundant_location_argument.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/redundant_message_argument.rb +49 -0
- data/lib/rubocop/cop/internal_affairs/useless_message_assertion.rb +1 -1
- data/lib/rubocop/cop/layout/access_modifier_indentation.rb +2 -3
- data/lib/rubocop/cop/layout/align_array.rb +2 -2
- data/lib/rubocop/cop/layout/align_hash.rb +2 -2
- data/lib/rubocop/cop/layout/align_parameters.rb +5 -11
- data/lib/rubocop/cop/layout/closing_parenthesis_indentation.rb +5 -5
- data/lib/rubocop/cop/layout/comment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +9 -0
- data/lib/rubocop/cop/layout/else_alignment.rb +30 -13
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +4 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +20 -4
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +3 -3
- data/lib/rubocop/cop/layout/empty_lines_around_method_body.rb +3 -3
- data/lib/rubocop/cop/layout/first_method_parameter_line_break.rb +3 -3
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +5 -2
- data/lib/rubocop/cop/layout/indent_heredoc.rb +19 -24
- data/lib/rubocop/cop/layout/indentation_consistency.rb +1 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +12 -8
- data/lib/rubocop/cop/layout/leading_comment_space.rb +33 -18
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +20 -17
- data/lib/rubocop/cop/layout/multiline_method_definition_brace_layout.rb +3 -3
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +6 -0
- data/lib/rubocop/cop/layout/space_after_colon.rb +7 -0
- data/lib/rubocop/cop/layout/space_after_comma.rb +10 -0
- data/lib/rubocop/cop/layout/space_after_method_name.rb +5 -3
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +13 -4
- data/lib/rubocop/cop/layout/space_around_keyword.rb +9 -5
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +54 -5
- data/lib/rubocop/cop/layout/space_before_comment.rb +7 -0
- data/lib/rubocop/cop/layout/space_before_semicolon.rb +7 -0
- data/lib/rubocop/cop/layout/space_inside_array_percent_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_string_interpolation.rb +8 -4
- data/lib/rubocop/cop/layout/tab.rb +1 -1
- data/lib/rubocop/cop/lint/ambiguous_block_association.rb +4 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +15 -1
- data/lib/rubocop/cop/lint/block_alignment.rb +15 -6
- data/lib/rubocop/cop/lint/boolean_symbol.rb +38 -0
- data/lib/rubocop/cop/lint/condition_position.rb +5 -1
- data/lib/rubocop/cop/lint/debugger.rb +16 -9
- data/lib/rubocop/cop/lint/def_end_alignment.rb +4 -4
- data/lib/rubocop/cop/lint/duplicate_case_condition.rb +3 -3
- data/lib/rubocop/cop/lint/duplicate_methods.rb +73 -5
- data/lib/rubocop/cop/lint/duplicated_key.rb +1 -1
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/else_layout.rb +1 -1
- data/lib/rubocop/cop/lint/empty_expression.rb +1 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/empty_when.rb +1 -1
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +5 -5
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +35 -40
- data/lib/rubocop/cop/lint/handle_exceptions.rb +1 -1
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +18 -13
- data/lib/rubocop/cop/lint/inherit_exception.rb +8 -7
- data/lib/rubocop/cop/lint/interpolation_check.rb +36 -0
- data/lib/rubocop/cop/lint/literal_in_condition.rb +3 -3
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/multiple_compare.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +5 -7
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +1 -1
- data/lib/rubocop/cop/lint/percent_string_array.rb +3 -12
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/rand_one.rb +7 -1
- data/lib/rubocop/cop/lint/redundant_with_index.rb +77 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/rescue_type.rb +13 -6
- data/lib/rubocop/cop/lint/rescue_without_error_class.rb +38 -0
- data/lib/rubocop/cop/lint/return_in_void_context.rb +63 -0
- data/lib/rubocop/cop/lint/script_permission.rb +6 -0
- data/lib/rubocop/cop/lint/syntax.rb +17 -20
- data/lib/rubocop/cop/lint/unified_integer.rb +3 -2
- data/lib/rubocop/cop/lint/unneeded_disable.rb +1 -1
- data/lib/rubocop/cop/lint/unneeded_splat_expansion.rb +1 -1
- data/lib/rubocop/cop/lint/unreachable_code.rb +53 -8
- data/lib/rubocop/cop/lint/uri_escape_unescape.rb +74 -0
- data/lib/rubocop/cop/lint/uri_regexp.rb +73 -0
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +4 -8
- data/lib/rubocop/cop/lint/useless_setter_call.rb +10 -11
- data/lib/rubocop/cop/lint/void.rb +29 -23
- data/lib/rubocop/cop/metrics/line_length.rb +2 -2
- data/lib/rubocop/cop/metrics/method_length.rb +8 -3
- data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +2 -2
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +12 -3
- data/lib/rubocop/cop/mixin/heredoc.rb +28 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +9 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +68 -31
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +18 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -1
- data/lib/rubocop/cop/mixin/percent_array.rb +52 -0
- data/lib/rubocop/cop/mixin/string_help.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/accessor_method_name.rb +11 -12
- data/lib/rubocop/cop/{style → naming}/ascii_identifiers.rb +1 -1
- data/lib/rubocop/cop/{style/op_method.rb → naming/binary_operator_parameter_name.rb} +2 -2
- data/lib/rubocop/cop/{style → naming}/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/constant_name.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/file_name.rb +8 -4
- data/lib/rubocop/cop/naming/heredoc_delimiter_case.rb +68 -0
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +58 -0
- data/lib/rubocop/cop/{style → naming}/method_name.rb +1 -1
- data/lib/rubocop/cop/{style → naming}/predicate_name.rb +6 -7
- data/lib/rubocop/cop/{style → naming}/variable_name.rb +11 -15
- data/lib/rubocop/cop/{style → naming}/variable_number.rb +1 -1
- data/lib/rubocop/cop/performance/caller.rb +39 -11
- data/lib/rubocop/cop/performance/casecmp.rb +4 -4
- data/lib/rubocop/cop/performance/compare_with_block.rb +4 -4
- data/lib/rubocop/cop/performance/double_start_end_with.rb +4 -4
- data/lib/rubocop/cop/performance/end_with.rb +3 -3
- data/lib/rubocop/cop/performance/fixed_size.rb +1 -1
- data/lib/rubocop/cop/performance/hash_each_methods.rb +66 -25
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +2 -2
- data/lib/rubocop/cop/performance/range_include.rb +2 -2
- data/lib/rubocop/cop/performance/redundant_block_call.rb +6 -6
- data/lib/rubocop/cop/performance/redundant_match.rb +5 -5
- data/lib/rubocop/cop/performance/redundant_merge.rb +39 -23
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +2 -2
- data/lib/rubocop/cop/performance/regexp_match.rb +13 -5
- data/lib/rubocop/cop/performance/size.rb +1 -1
- data/lib/rubocop/cop/performance/start_with.rb +3 -3
- data/lib/rubocop/cop/performance/times_map.rb +23 -12
- data/lib/rubocop/cop/performance/unfreeze_string.rb +50 -0
- data/lib/rubocop/cop/performance/uri_default_parser.rb +47 -0
- data/lib/rubocop/cop/rails/active_support_aliases.rb +1 -1
- data/lib/rubocop/cop/rails/delegate.rb +36 -7
- data/lib/rubocop/cop/rails/delegate_allow_blank.rb +1 -1
- data/lib/rubocop/cop/rails/enum_uniqueness.rb +2 -2
- data/lib/rubocop/cop/rails/file_path.rb +3 -4
- data/lib/rubocop/cop/rails/find_each.rb +1 -1
- data/lib/rubocop/cop/rails/has_many_or_has_one_dependent.rb +48 -0
- data/lib/rubocop/cop/rails/http_positional_arguments.rb +5 -5
- data/lib/rubocop/cop/rails/not_null_column.rb +1 -1
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +2 -2
- data/lib/rubocop/cop/rails/relative_date_constant.rb +1 -1
- data/lib/rubocop/cop/rails/request_referer.rb +2 -2
- data/lib/rubocop/cop/rails/reversible_migration.rb +12 -12
- data/lib/rubocop/cop/rails/save_bang.rb +8 -6
- data/lib/rubocop/cop/rails/scope_args.rb +1 -1
- data/lib/rubocop/cop/security/eval.rb +2 -2
- data/lib/rubocop/cop/security/json_load.rb +2 -2
- data/lib/rubocop/cop/security/marshal_load.rb +2 -2
- data/lib/rubocop/cop/security/yaml_load.rb +2 -2
- data/lib/rubocop/cop/style/alias.rb +44 -20
- data/lib/rubocop/cop/style/and_or.rb +48 -34
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +1 -1
- data/lib/rubocop/cop/style/block_comments.rb +3 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -1
- data/lib/rubocop/cop/style/command_literal.rb +20 -9
- data/lib/rubocop/cop/style/conditional_assignment.rb +30 -28
- data/lib/rubocop/cop/style/copyright.rb +10 -10
- data/lib/rubocop/cop/style/def_with_parentheses.rb +6 -5
- data/lib/rubocop/cop/style/dir.rb +52 -0
- data/lib/rubocop/cop/style/documentation_method.rb +2 -6
- data/lib/rubocop/cop/style/empty_case_condition.rb +1 -1
- data/lib/rubocop/cop/style/empty_else.rb +3 -2
- data/lib/rubocop/cop/style/empty_literal.rb +1 -2
- data/lib/rubocop/cop/style/empty_method.rb +27 -17
- data/lib/rubocop/cop/style/flip_flop.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +4 -2
- data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +5 -1
- data/lib/rubocop/cop/style/if_with_semicolon.rb +1 -1
- data/lib/rubocop/cop/style/implicit_runtime_error.rb +4 -3
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +20 -8
- data/lib/rubocop/cop/style/lambda.rb +19 -9
- data/lib/rubocop/cop/style/lambda_call.rb +22 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +4 -20
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +6 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +18 -26
- data/lib/rubocop/cop/style/method_missing.rb +5 -18
- data/lib/rubocop/cop/style/min_max.rb +67 -0
- data/lib/rubocop/cop/style/missing_else.rb +16 -3
- data/lib/rubocop/cop/style/mixin_grouping.rb +2 -2
- data/lib/rubocop/cop/style/module_function.rb +8 -4
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +5 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +25 -3
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +2 -6
- data/lib/rubocop/cop/style/negated_if.rb +8 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +8 -8
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +14 -14
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +6 -2
- data/lib/rubocop/cop/style/numeric_literals.rb +2 -2
- data/lib/rubocop/cop/style/numeric_predicate.rb +8 -4
- data/lib/rubocop/cop/style/one_line_conditional.rb +8 -3
- data/lib/rubocop/cop/style/option_hash.rb +1 -1
- data/lib/rubocop/cop/style/optional_arguments.rb +1 -2
- data/lib/rubocop/cop/style/or_assignment.rb +88 -0
- data/lib/rubocop/cop/style/parallel_assignment.rb +2 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -11
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
- data/lib/rubocop/cop/style/perl_backrefs.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +16 -17
- data/lib/rubocop/cop/style/redundant_begin.rb +6 -5
- data/lib/rubocop/cop/style/redundant_conditional.rb +95 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +13 -11
- data/lib/rubocop/cop/style/redundant_return.rb +23 -11
- data/lib/rubocop/cop/style/redundant_self.rb +18 -9
- data/lib/rubocop/cop/style/regexp_literal.rb +12 -4
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/return_nil.rb +98 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +80 -43
- data/lib/rubocop/cop/style/single_line_block_params.rb +14 -13
- data/lib/rubocop/cop/style/single_line_methods.rb +9 -13
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -3
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +17 -39
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +22 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +5 -25
- data/lib/rubocop/cop/style/symbol_literal.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +3 -18
- data/lib/rubocop/cop/style/ternary_parentheses.rb +14 -10
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +28 -9
- data/lib/rubocop/cop/style/trivial_accessors.rb +39 -56
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +1 -1
- data/lib/rubocop/cop/style/variable_interpolation.rb +8 -3
- data/lib/rubocop/cop/style/word_array.rb +7 -24
- data/lib/rubocop/cop/style/yoda_condition.rb +49 -14
- data/lib/rubocop/cop/style/zero_length_predicate.rb +25 -18
- data/lib/rubocop/cop/team.rb +16 -8
- data/lib/rubocop/cop/util.rb +11 -0
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/formatter_set.rb +2 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +2 -0
- data/lib/rubocop/formatter/simple_text_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +71 -0
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +2 -0
- data/lib/rubocop/node_pattern.rb +44 -26
- data/lib/rubocop/options.rb +1 -0
- data/lib/rubocop/processed_source.rb +3 -1
- data/lib/rubocop/remote_config.rb +5 -1
- data/lib/rubocop/result_cache.rb +1 -0
- data/lib/rubocop/rspec/cop_helper.rb +10 -10
- data/lib/rubocop/rspec/expect_offense.rb +6 -8
- data/lib/rubocop/rspec/shared_examples.rb +8 -8
- data/lib/rubocop/string_util.rb +2 -0
- data/lib/rubocop/version.rb +1 -1
- metadata +51 -18
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +0 -41
- data/lib/rubocop/cop/mixin/on_method_def.rb +0 -44
@@ -0,0 +1,77 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for redundant `with_index`.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
# # bad
|
10
|
+
# ary.each_with_index do |v|
|
11
|
+
# v
|
12
|
+
# end
|
13
|
+
#
|
14
|
+
# # good
|
15
|
+
# ary.each do |v|
|
16
|
+
# v
|
17
|
+
# end
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# ary.each.with_index do |v|
|
21
|
+
# v
|
22
|
+
# end
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# ary.each do |v|
|
26
|
+
# v
|
27
|
+
# end
|
28
|
+
#
|
29
|
+
class RedundantWithIndex < Cop
|
30
|
+
MSG_EACH_WITH_INDEX = 'Use `each` instead of `each_with_index`.'.freeze
|
31
|
+
MSG_WITH_INDEX = 'Remove redundant `with_index`.'.freeze
|
32
|
+
|
33
|
+
def_node_matcher :redundant_with_index?, <<-PATTERN
|
34
|
+
(block
|
35
|
+
$(send
|
36
|
+
_ {:each_with_index :with_index})
|
37
|
+
(args
|
38
|
+
(arg _))
|
39
|
+
...)
|
40
|
+
PATTERN
|
41
|
+
|
42
|
+
def on_block(node)
|
43
|
+
redundant_with_index?(node) do |send|
|
44
|
+
add_offense(node, with_index_range(send))
|
45
|
+
end
|
46
|
+
end
|
47
|
+
|
48
|
+
def autocorrect(node)
|
49
|
+
lambda do |corrector|
|
50
|
+
redundant_with_index?(node) do |send|
|
51
|
+
if send.method_name == :each_with_index
|
52
|
+
corrector.replace(send.loc.selector, 'each')
|
53
|
+
else
|
54
|
+
corrector.remove(send.loc.selector)
|
55
|
+
corrector.remove(send.loc.dot)
|
56
|
+
end
|
57
|
+
end
|
58
|
+
end
|
59
|
+
end
|
60
|
+
|
61
|
+
private
|
62
|
+
|
63
|
+
def message(node)
|
64
|
+
if node.method_name == :each_with_index
|
65
|
+
MSG_EACH_WITH_INDEX
|
66
|
+
else
|
67
|
+
MSG_WITH_INDEX
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def with_index_range(send)
|
72
|
+
range_between(send.loc.selector.begin_pos, send.loc.selector.end_pos)
|
73
|
+
end
|
74
|
+
end
|
75
|
+
end
|
76
|
+
end
|
77
|
+
end
|
@@ -53,22 +53,29 @@ module RuboCop
|
|
53
53
|
format(MSG, invalid_exceptions.map(&:source).join(', ')))
|
54
54
|
end
|
55
55
|
|
56
|
+
private
|
57
|
+
|
56
58
|
def autocorrect(node)
|
57
59
|
rescued, _, _body = *node
|
58
|
-
exceptions = *rescued
|
59
|
-
valid_exceptions = exceptions - invalid_exceptions(exceptions)
|
60
|
-
correction = valid_exceptions.map(&:source).join(', ')
|
61
|
-
correction = " #{correction}" unless correction.empty?
|
62
60
|
range = Parser::Source::Range.new(node.loc.expression,
|
63
61
|
node.loc.keyword.end_pos,
|
64
62
|
rescued.loc.expression.end_pos)
|
65
63
|
|
66
64
|
lambda do |corrector|
|
67
|
-
corrector.replace(range, correction)
|
65
|
+
corrector.replace(range, correction(*rescued))
|
68
66
|
end
|
69
67
|
end
|
70
68
|
|
71
|
-
|
69
|
+
def correction(*exceptions)
|
70
|
+
correction = valid_exceptions(exceptions).map(&:source).join(', ')
|
71
|
+
correction = " #{correction}" unless correction.empty?
|
72
|
+
|
73
|
+
correction
|
74
|
+
end
|
75
|
+
|
76
|
+
def valid_exceptions(exceptions)
|
77
|
+
exceptions - invalid_exceptions(exceptions)
|
78
|
+
end
|
72
79
|
|
73
80
|
def invalid_exceptions(exceptions)
|
74
81
|
exceptions.select do |exception|
|
@@ -0,0 +1,38 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for uses of `rescue` with no error class specified.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # good
|
11
|
+
# begin
|
12
|
+
# foo
|
13
|
+
# rescue BarError
|
14
|
+
# bar
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # bad
|
18
|
+
# begin
|
19
|
+
# foo
|
20
|
+
# rescue
|
21
|
+
# bar
|
22
|
+
# end
|
23
|
+
class RescueWithoutErrorClass < Cop
|
24
|
+
MSG = 'Avoid rescuing without specifying an error class.'.freeze
|
25
|
+
|
26
|
+
def_node_matcher :rescue_without_error_class?, <<-PATTERN
|
27
|
+
(resbody nil ...)
|
28
|
+
PATTERN
|
29
|
+
|
30
|
+
def on_resbody(node)
|
31
|
+
return unless rescue_without_error_class?(node)
|
32
|
+
|
33
|
+
add_offense(node, :keyword)
|
34
|
+
end
|
35
|
+
end
|
36
|
+
end
|
37
|
+
end
|
38
|
+
end
|
@@ -0,0 +1,63 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# This cop checks for the use of a return with a value in a context
|
7
|
+
# where the value will be ignored. (initialize and setter methods)
|
8
|
+
#
|
9
|
+
# @example
|
10
|
+
#
|
11
|
+
# # bad
|
12
|
+
# def initialize
|
13
|
+
# foo
|
14
|
+
# return :qux if bar?
|
15
|
+
# baz
|
16
|
+
# end
|
17
|
+
#
|
18
|
+
# def foo=(bar)
|
19
|
+
# return 42
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @example
|
23
|
+
#
|
24
|
+
# # good
|
25
|
+
# def initialize
|
26
|
+
# foo
|
27
|
+
# return if bar?
|
28
|
+
# baz
|
29
|
+
# end
|
30
|
+
#
|
31
|
+
# def foo=(bar)
|
32
|
+
# return
|
33
|
+
# end
|
34
|
+
class ReturnInVoidContext < Cop
|
35
|
+
MSG = 'Do not return a value in `%s`.'.freeze
|
36
|
+
def on_return(return_node)
|
37
|
+
method_name = method_name(return_node)
|
38
|
+
return unless method_name && return_node.descendants.any? &&
|
39
|
+
useless_return_method?(method_name)
|
40
|
+
|
41
|
+
add_offense(return_node, :keyword, format(message, method_name))
|
42
|
+
end
|
43
|
+
|
44
|
+
private
|
45
|
+
|
46
|
+
def method_name(return_node)
|
47
|
+
method_node = return_node.each_ancestor(:block, :def, :defs).first
|
48
|
+
return nil unless method_node.def_type?
|
49
|
+
method_node.children.first
|
50
|
+
end
|
51
|
+
|
52
|
+
def method_setter?(method_name)
|
53
|
+
method_name.to_s.end_with?('=') &&
|
54
|
+
!AST::Node::COMPARISON_OPERATORS.include?(method_name)
|
55
|
+
end
|
56
|
+
|
57
|
+
def useless_return_method?(method_name)
|
58
|
+
method_name == :initialize || method_setter?(method_name)
|
59
|
+
end
|
60
|
+
end
|
61
|
+
end
|
62
|
+
end
|
63
|
+
end
|
@@ -18,6 +18,12 @@ module RuboCop
|
|
18
18
|
add_offense(comment, :expression, message)
|
19
19
|
end
|
20
20
|
|
21
|
+
def autocorrect(node)
|
22
|
+
lambda do |_corrector|
|
23
|
+
FileUtils.chmod('+x', node.loc.expression.source_buffer.name)
|
24
|
+
end
|
25
|
+
end
|
26
|
+
|
21
27
|
private
|
22
28
|
|
23
29
|
def start_with_shebang?(processed_source)
|
@@ -5,50 +5,47 @@ module RuboCop
|
|
5
5
|
module Lint
|
6
6
|
# This is actually not a cop and inspects nothing. It just provides
|
7
7
|
# methods to repack Parser's diagnostics/errors into RuboCop's offenses.
|
8
|
-
|
8
|
+
class Syntax < Cop
|
9
9
|
PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
|
10
10
|
:end_pos)
|
11
11
|
|
12
|
-
COP_NAME = 'Syntax'.freeze
|
13
12
|
ERROR_SOURCE_RANGE = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
|
14
13
|
|
15
|
-
def self.offenses_from_processed_source(processed_source
|
16
|
-
|
14
|
+
def self.offenses_from_processed_source(processed_source,
|
15
|
+
config, options)
|
16
|
+
cop = new(config, options)
|
17
17
|
|
18
18
|
if processed_source.parser_error
|
19
|
-
|
19
|
+
cop.add_offense_from_error(processed_source.parser_error)
|
20
20
|
end
|
21
21
|
|
22
22
|
processed_source.diagnostics.each do |diagnostic|
|
23
|
-
|
24
|
-
|
23
|
+
cop.add_offense_from_diagnostic(diagnostic,
|
24
|
+
processed_source.ruby_version)
|
25
25
|
end
|
26
26
|
|
27
|
-
offenses
|
27
|
+
cop.offenses
|
28
28
|
end
|
29
29
|
|
30
|
-
def
|
31
|
-
|
32
|
-
diagnostic.level,
|
33
|
-
diagnostic.location,
|
30
|
+
def add_offense_from_diagnostic(diagnostic, ruby_version)
|
31
|
+
message =
|
34
32
|
"#{diagnostic.message}\n(Using Ruby #{ruby_version} parser; " \
|
35
|
-
'configure using `TargetRubyVersion` parameter, under `AllCops`)'
|
36
|
-
|
37
|
-
)
|
33
|
+
'configure using `TargetRubyVersion` parameter, under `AllCops`)'
|
34
|
+
add_offense(nil, diagnostic.location, message, diagnostic.level)
|
38
35
|
end
|
39
36
|
|
40
|
-
def
|
37
|
+
def add_offense_from_error(error)
|
41
38
|
message = beautify_message(error.message)
|
42
|
-
|
39
|
+
add_offense(nil, ERROR_SOURCE_RANGE, message, :fatal)
|
43
40
|
end
|
44
41
|
|
45
|
-
|
42
|
+
private
|
43
|
+
|
44
|
+
def beautify_message(message)
|
46
45
|
message = message.capitalize
|
47
46
|
message << '.' unless message.end_with?('.')
|
48
47
|
message
|
49
48
|
end
|
50
|
-
|
51
|
-
private_class_method :beautify_message
|
52
49
|
end
|
53
50
|
end
|
54
51
|
end
|
@@ -20,12 +20,13 @@ module RuboCop
|
|
20
20
|
class UnifiedInteger < Cop
|
21
21
|
MSG = 'Use `Integer` instead of `%s`.'.freeze
|
22
22
|
|
23
|
-
def_node_matcher :fixnum_or_bignum_const
|
23
|
+
def_node_matcher :fixnum_or_bignum_const, <<-PATTERN
|
24
24
|
(:const {nil (:cbase)} ${:Fixnum :Bignum})
|
25
25
|
PATTERN
|
26
26
|
|
27
27
|
def on_const(node)
|
28
|
-
klass = fixnum_or_bignum_const
|
28
|
+
klass = fixnum_or_bignum_const(node)
|
29
|
+
|
29
30
|
return unless klass
|
30
31
|
|
31
32
|
add_offense(node, :expression, format(MSG, klass))
|
@@ -16,6 +16,17 @@ module RuboCop
|
|
16
16
|
# do_something
|
17
17
|
# end
|
18
18
|
#
|
19
|
+
# # bad
|
20
|
+
#
|
21
|
+
# def some_method
|
22
|
+
# if cond
|
23
|
+
# return
|
24
|
+
# else
|
25
|
+
# return
|
26
|
+
# end
|
27
|
+
# do_something
|
28
|
+
# end
|
29
|
+
#
|
19
30
|
# @example
|
20
31
|
#
|
21
32
|
# # good
|
@@ -26,25 +37,59 @@ module RuboCop
|
|
26
37
|
class UnreachableCode < Cop
|
27
38
|
MSG = 'Unreachable code detected.'.freeze
|
28
39
|
|
29
|
-
NODE_TYPES = %i[return next break retry redo].freeze
|
30
|
-
FLOW_COMMANDS = %i[throw raise fail].freeze
|
31
|
-
|
32
40
|
def on_begin(node)
|
33
41
|
expressions = *node
|
34
42
|
|
35
43
|
expressions.each_cons(2) do |e1, e2|
|
36
|
-
next unless
|
44
|
+
next unless flow_expression?(e1)
|
37
45
|
|
38
|
-
add_offense(e2
|
46
|
+
add_offense(e2)
|
39
47
|
end
|
40
48
|
end
|
41
49
|
|
50
|
+
alias on_kwbegin on_begin
|
51
|
+
|
42
52
|
private
|
43
53
|
|
44
|
-
|
45
|
-
|
54
|
+
def_node_matcher :flow_command?, <<-PATTERN
|
55
|
+
{
|
56
|
+
return next break retry redo
|
57
|
+
(send
|
58
|
+
{nil (const {nil cbase} :Kernel)}
|
59
|
+
{:raise :fail :throw}
|
60
|
+
...)
|
61
|
+
}
|
62
|
+
PATTERN
|
63
|
+
|
64
|
+
def flow_expression?(node)
|
65
|
+
return true if flow_command?(node)
|
66
|
+
case node.type
|
67
|
+
when :begin, :kwbegin
|
68
|
+
expressions = *node
|
69
|
+
expressions.any? { |expr| flow_expression?(expr) }
|
70
|
+
when :if
|
71
|
+
check_if(node)
|
72
|
+
when :case
|
73
|
+
check_case(node)
|
74
|
+
else
|
75
|
+
false
|
76
|
+
end
|
77
|
+
end
|
46
78
|
|
47
|
-
|
79
|
+
def check_if(node)
|
80
|
+
if_branch = node.if_branch
|
81
|
+
else_branch = node.else_branch
|
82
|
+
if_branch && else_branch &&
|
83
|
+
flow_expression?(if_branch) && flow_expression?(else_branch)
|
84
|
+
end
|
85
|
+
|
86
|
+
def check_case(node)
|
87
|
+
else_branch = node.else_branch
|
88
|
+
return false unless else_branch
|
89
|
+
return false unless flow_expression?(else_branch)
|
90
|
+
node.when_branches.all? do |branch|
|
91
|
+
branch.body && flow_expression?(branch.body)
|
92
|
+
end
|
48
93
|
end
|
49
94
|
end
|
50
95
|
end
|