rubocop 0.35.1 → 0.36.0
Sign up to get free protection for your applications and to get access to all the features.
Potentially problematic release.
This version of rubocop might be problematic. Click here for more details.
- checksums.yaml +4 -4
- data/CHANGELOG.md +164 -0
- data/LICENSE.txt +1 -1
- data/README.md +72 -21
- data/bin/rubocop +1 -0
- data/config/default.yml +167 -18
- data/config/disabled.yml +19 -6
- data/config/enabled.yml +159 -14
- data/lib/rubocop.rb +67 -26
- data/lib/rubocop/ast_node.rb +488 -14
- data/lib/rubocop/ast_node/builder.rb +24 -0
- data/lib/rubocop/ast_node/sexp.rb +13 -0
- data/lib/rubocop/cached_data.rb +58 -0
- data/lib/rubocop/cli.rb +47 -10
- data/lib/rubocop/comment_config.rb +9 -2
- data/lib/rubocop/config.rb +99 -31
- data/lib/rubocop/config_loader.rb +23 -14
- data/lib/rubocop/config_store.rb +1 -0
- data/lib/rubocop/cop/autocorrect_logic.rb +2 -1
- data/lib/rubocop/cop/commissioner.rb +3 -5
- data/lib/rubocop/cop/cop.rb +23 -17
- data/lib/rubocop/cop/corrector.rb +25 -0
- data/lib/rubocop/cop/force.rb +1 -0
- data/lib/rubocop/cop/ignored_node.rb +3 -2
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +2 -1
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +2 -1
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +4 -3
- data/lib/rubocop/cop/lint/block_alignment.rb +29 -91
- data/lib/rubocop/cop/lint/circular_argument_reference.rb +2 -1
- data/lib/rubocop/cop/lint/condition_position.rb +2 -1
- data/lib/rubocop/cop/lint/debugger.rb +29 -12
- data/lib/rubocop/cop/lint/def_end_alignment.rb +16 -18
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +6 -6
- data/lib/rubocop/cop/lint/duplicate_methods.rb +98 -74
- data/lib/rubocop/cop/lint/duplicated_key.rb +3 -2
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +3 -2
- data/lib/rubocop/cop/lint/else_layout.rb +2 -1
- data/lib/rubocop/cop/lint/empty_ensure.rb +2 -1
- data/lib/rubocop/cop/lint/empty_interpolation.rb +2 -1
- data/lib/rubocop/cop/lint/end_alignment.rb +77 -39
- data/lib/rubocop/cop/lint/end_in_method.rb +2 -1
- data/lib/rubocop/cop/lint/ensure_return.rb +2 -1
- data/lib/rubocop/cop/lint/eval.rb +2 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +31 -0
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +14 -30
- data/lib/rubocop/cop/lint/handle_exceptions.rb +2 -1
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +85 -0
- data/lib/rubocop/cop/lint/ineffective_access_modifier.rb +120 -0
- data/lib/rubocop/cop/lint/invalid_character_literal.rb +3 -1
- data/lib/rubocop/cop/lint/literal_in_condition.rb +6 -9
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +6 -9
- data/lib/rubocop/cop/lint/loop.rb +2 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +19 -3
- data/lib/rubocop/cop/lint/next_without_accumulator.rb +38 -0
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +5 -8
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +8 -6
- data/lib/rubocop/cop/lint/rand_one.rb +36 -0
- data/lib/rubocop/cop/lint/require_parentheses.rb +6 -5
- data/lib/rubocop/cop/lint/rescue_exception.rb +3 -2
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +2 -1
- data/lib/rubocop/cop/lint/string_conversion_in_interpolation.rb +6 -4
- data/lib/rubocop/cop/lint/syntax.rb +9 -5
- data/lib/rubocop/cop/lint/underscore_prefixed_variable_name.rb +3 -2
- data/lib/rubocop/cop/lint/unneeded_disable.rb +121 -18
- data/lib/rubocop/cop/lint/unreachable_code.rb +5 -4
- data/lib/rubocop/cop/lint/unused_block_argument.rb +9 -7
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +56 -29
- data/lib/rubocop/cop/lint/useless_assignment.rb +4 -16
- data/lib/rubocop/cop/lint/useless_comparison.rb +3 -2
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +2 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +14 -20
- data/lib/rubocop/cop/lint/void.rb +10 -11
- data/lib/rubocop/cop/metrics/abc_size.rb +3 -1
- data/lib/rubocop/cop/metrics/block_nesting.rb +2 -1
- data/lib/rubocop/cop/metrics/class_length.rb +1 -0
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +4 -2
- data/lib/rubocop/cop/metrics/line_length.rb +35 -13
- data/lib/rubocop/cop/metrics/method_length.rb +2 -1
- data/lib/rubocop/cop/metrics/module_length.rb +1 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +2 -1
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +4 -2
- data/lib/rubocop/cop/mixin/access_modifier_node.rb +3 -10
- data/lib/rubocop/cop/mixin/annotation_comment.rb +1 -0
- data/lib/rubocop/cop/mixin/array_hash_indentation.rb +80 -0
- data/lib/rubocop/cop/mixin/array_syntax.rb +2 -1
- data/lib/rubocop/cop/mixin/autocorrect_alignment.rb +14 -20
- data/lib/rubocop/cop/mixin/autocorrect_unless_changing_ast.rb +5 -4
- data/lib/rubocop/cop/mixin/check_assignment.rb +20 -15
- data/lib/rubocop/cop/mixin/classish_length.rb +1 -0
- data/lib/rubocop/cop/mixin/code_length.rb +1 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +17 -15
- data/lib/rubocop/cop/mixin/configurable_max.rb +1 -0
- data/lib/rubocop/cop/mixin/configurable_naming.rb +4 -0
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +9 -4
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +24 -16
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +3 -2
- data/lib/rubocop/cop/mixin/hash_node.rb +15 -0
- data/lib/rubocop/cop/mixin/if_node.rb +1 -0
- data/lib/rubocop/cop/mixin/method_complexity.rb +1 -0
- data/lib/rubocop/cop/mixin/method_preference.rb +1 -0
- data/lib/rubocop/cop/mixin/min_body_length.rb +1 -0
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +177 -0
- data/lib/rubocop/cop/mixin/negative_conditional.rb +1 -0
- data/lib/rubocop/cop/mixin/on_method_def.rb +6 -5
- data/lib/rubocop/cop/mixin/on_normal_if_unless.rb +1 -0
- data/lib/rubocop/cop/mixin/parentheses.rb +22 -0
- data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -0
- data/lib/rubocop/cop/mixin/percent_literal.rb +1 -0
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +79 -0
- data/lib/rubocop/cop/mixin/safe_assignment.rb +1 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +2 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -1
- data/lib/rubocop/cop/mixin/space_inside.rb +2 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +6 -5
- data/lib/rubocop/cop/mixin/string_help.rb +2 -9
- data/lib/rubocop/cop/mixin/string_literals_help.rb +13 -7
- data/lib/rubocop/cop/mixin/surrounding_space.rb +3 -2
- data/lib/rubocop/cop/mixin/trailing_comma.rb +134 -0
- data/lib/rubocop/cop/mixin/unused_argument.rb +1 -0
- data/lib/rubocop/cop/offense.rb +19 -14
- data/lib/rubocop/cop/performance/case_when_splat.rb +8 -8
- data/lib/rubocop/cop/performance/casecmp.rb +54 -0
- data/lib/rubocop/cop/performance/count.rb +10 -9
- data/lib/rubocop/cop/performance/detect.rb +6 -5
- data/lib/rubocop/cop/performance/double_start_end_with.rb +65 -0
- data/lib/rubocop/cop/performance/end_with.rb +55 -0
- data/lib/rubocop/cop/performance/fixed_size.rb +1 -0
- data/lib/rubocop/cop/performance/flat_map.rb +9 -8
- data/lib/rubocop/cop/performance/hash_each.rb +86 -0
- data/lib/rubocop/cop/performance/lstrip_rstrip.rb +44 -0
- data/lib/rubocop/cop/performance/range_include.rb +40 -0
- data/lib/rubocop/cop/performance/redundant_block_call.rb +57 -0
- data/lib/rubocop/cop/performance/redundant_match.rb +51 -0
- data/lib/rubocop/cop/performance/redundant_merge.rb +85 -0
- data/lib/rubocop/cop/performance/redundant_sort_by.rb +45 -0
- data/lib/rubocop/cop/performance/reverse_each.rb +3 -2
- data/lib/rubocop/cop/performance/sample.rb +6 -5
- data/lib/rubocop/cop/performance/size.rb +2 -1
- data/lib/rubocop/cop/performance/start_with.rb +58 -0
- data/lib/rubocop/cop/performance/string_replacement.rb +18 -23
- data/lib/rubocop/cop/performance/times_map.rb +49 -0
- data/lib/rubocop/cop/rails/action_filter.rb +4 -3
- data/lib/rubocop/cop/rails/date.rb +5 -4
- data/lib/rubocop/cop/rails/delegate.rb +3 -2
- data/lib/rubocop/cop/rails/find_by.rb +20 -14
- data/lib/rubocop/cop/rails/find_each.rb +23 -2
- data/lib/rubocop/cop/rails/has_and_belongs_to_many.rb +3 -2
- data/lib/rubocop/cop/rails/output.rb +4 -2
- data/lib/rubocop/cop/rails/pluralization_grammar.rb +3 -2
- data/lib/rubocop/cop/rails/read_write_attribute.rb +5 -7
- data/lib/rubocop/cop/rails/scope_args.rb +3 -2
- data/lib/rubocop/cop/rails/time_zone.rb +14 -10
- data/lib/rubocop/cop/rails/validation.rb +4 -3
- data/lib/rubocop/cop/severity.rb +8 -7
- data/lib/rubocop/cop/style/access_modifier_indentation.rb +5 -4
- data/lib/rubocop/cop/style/accessor_method_name.rb +1 -0
- data/lib/rubocop/cop/style/alias.rb +84 -24
- data/lib/rubocop/cop/style/align_array.rb +2 -1
- data/lib/rubocop/cop/style/align_hash.rb +13 -14
- data/lib/rubocop/cop/style/align_parameters.rb +3 -2
- data/lib/rubocop/cop/style/and_or.rb +9 -7
- data/lib/rubocop/cop/style/array_join.rb +5 -5
- data/lib/rubocop/cop/style/ascii_comments.rb +2 -1
- data/lib/rubocop/cop/style/ascii_identifiers.rb +2 -1
- data/lib/rubocop/cop/style/attr.rb +30 -5
- data/lib/rubocop/cop/style/auto_resource_cleanup.rb +3 -3
- data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -1
- data/lib/rubocop/cop/style/begin_block.rb +2 -1
- data/lib/rubocop/cop/style/block_comments.rb +2 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +10 -9
- data/lib/rubocop/cop/style/block_end_newline.rb +3 -2
- data/lib/rubocop/cop/style/braces_around_hash_parameters.rb +9 -8
- data/lib/rubocop/cop/style/case_equality.rb +2 -1
- data/lib/rubocop/cop/style/case_indentation.rb +2 -1
- data/lib/rubocop/cop/style/character_literal.rb +11 -7
- data/lib/rubocop/cop/style/class_and_module_camel_case.rb +2 -1
- data/lib/rubocop/cop/style/class_and_module_children.rb +3 -2
- data/lib/rubocop/cop/style/class_check.rb +2 -1
- data/lib/rubocop/cop/style/class_methods.rb +2 -1
- data/lib/rubocop/cop/style/class_vars.rb +2 -1
- data/lib/rubocop/cop/style/closing_parenthesis_indentation.rb +3 -2
- data/lib/rubocop/cop/style/collection_methods.rb +2 -1
- data/lib/rubocop/cop/style/colon_method_call.rb +3 -2
- data/lib/rubocop/cop/style/command_literal.rb +8 -7
- data/lib/rubocop/cop/style/comment_annotation.rb +3 -2
- data/lib/rubocop/cop/style/comment_indentation.rb +4 -6
- data/lib/rubocop/cop/style/conditional_assignment.rb +362 -0
- data/lib/rubocop/cop/style/constant_name.rb +2 -1
- data/lib/rubocop/cop/style/copyright.rb +7 -6
- data/lib/rubocop/cop/style/def_with_parentheses.rb +2 -1
- data/lib/rubocop/cop/style/deprecated_hash_methods.rb +3 -2
- data/lib/rubocop/cop/style/documentation.rb +7 -11
- data/lib/rubocop/cop/style/dot_position.rb +3 -2
- data/lib/rubocop/cop/style/double_negation.rb +2 -1
- data/lib/rubocop/cop/style/each_with_object.rb +4 -3
- data/lib/rubocop/cop/style/else_alignment.rb +3 -2
- data/lib/rubocop/cop/style/empty_else.rb +4 -3
- data/lib/rubocop/cop/style/empty_line_between_defs.rb +2 -1
- data/lib/rubocop/cop/style/empty_lines.rb +10 -4
- data/lib/rubocop/cop/style/empty_lines_around_access_modifier.rb +13 -5
- data/lib/rubocop/cop/style/empty_lines_around_block_body.rb +7 -3
- data/lib/rubocop/cop/style/empty_lines_around_class_body.rb +6 -3
- data/lib/rubocop/cop/style/empty_lines_around_method_body.rb +4 -3
- data/lib/rubocop/cop/style/empty_lines_around_module_body.rb +4 -2
- data/lib/rubocop/cop/style/empty_literal.rb +20 -5
- data/lib/rubocop/cop/style/encoding.rb +8 -11
- data/lib/rubocop/cop/style/end_block.rb +3 -1
- data/lib/rubocop/cop/style/end_of_line.rb +2 -1
- data/lib/rubocop/cop/style/even_odd.rb +4 -3
- data/lib/rubocop/cop/style/extra_spacing.rb +110 -74
- data/lib/rubocop/cop/style/file_name.rb +103 -6
- data/lib/rubocop/cop/style/first_array_element_line_break.rb +3 -2
- data/lib/rubocop/cop/style/first_hash_element_line_break.rb +5 -6
- data/lib/rubocop/cop/style/first_method_argument_line_break.rb +14 -1
- data/lib/rubocop/cop/style/first_method_parameter_line_break.rb +2 -1
- data/lib/rubocop/cop/style/first_parameter_indentation.rb +6 -4
- data/lib/rubocop/cop/style/flip_flop.rb +2 -1
- data/lib/rubocop/cop/style/for.rb +2 -1
- data/lib/rubocop/cop/style/format_string.rb +1 -0
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +89 -0
- data/lib/rubocop/cop/style/global_vars.rb +2 -1
- data/lib/rubocop/cop/style/guard_clause.rb +63 -11
- data/lib/rubocop/cop/style/hash_syntax.rb +10 -10
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +93 -0
- data/lib/rubocop/cop/style/if_inside_else.rb +49 -0
- data/lib/rubocop/cop/style/if_unless_modifier.rb +6 -5
- data/lib/rubocop/cop/style/if_with_semicolon.rb +2 -1
- data/lib/rubocop/cop/style/indent_array.rb +89 -38
- data/lib/rubocop/cop/style/indent_assignment.rb +43 -0
- data/lib/rubocop/cop/style/indent_hash.rb +16 -77
- data/lib/rubocop/cop/style/indentation_consistency.rb +2 -1
- data/lib/rubocop/cop/style/indentation_width.rb +11 -11
- data/lib/rubocop/cop/style/infinite_loop.rb +5 -9
- data/lib/rubocop/cop/style/initial_indentation.rb +2 -1
- data/lib/rubocop/cop/style/inline_comment.rb +2 -1
- data/lib/rubocop/cop/style/lambda.rb +14 -11
- data/lib/rubocop/cop/style/lambda_call.rb +4 -4
- data/lib/rubocop/cop/style/leading_comment_space.rb +2 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +3 -1
- data/lib/rubocop/cop/style/method_call_parentheses.rb +9 -1
- data/lib/rubocop/cop/style/method_called_on_do_end_block.rb +3 -2
- data/lib/rubocop/cop/style/method_def_parentheses.rb +4 -4
- data/lib/rubocop/cop/style/method_name.rb +1 -0
- data/lib/rubocop/cop/style/missing_else.rb +5 -3
- data/lib/rubocop/cop/style/module_function.rb +2 -1
- data/lib/rubocop/cop/style/multiline_array_brace_layout.rb +95 -0
- data/lib/rubocop/cop/style/multiline_assignment_layout.rb +91 -0
- data/lib/rubocop/cop/style/multiline_block_chain.rb +3 -2
- data/lib/rubocop/cop/style/multiline_block_layout.rb +11 -9
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -0
- data/lib/rubocop/cop/style/multiline_method_call_indentation.rb +137 -0
- data/lib/rubocop/cop/style/multiline_operation_indentation.rb +25 -135
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +2 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +4 -5
- data/lib/rubocop/cop/style/negated_if.rb +3 -3
- data/lib/rubocop/cop/style/negated_while.rb +3 -3
- data/lib/rubocop/cop/style/nested_modifier.rb +6 -5
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +48 -0
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +2 -1
- data/lib/rubocop/cop/style/next.rb +79 -15
- data/lib/rubocop/cop/style/nil_comparison.rb +5 -5
- data/lib/rubocop/cop/style/non_nil_check.rb +5 -5
- data/lib/rubocop/cop/style/not.rb +5 -9
- data/lib/rubocop/cop/style/numeric_literals.rb +5 -4
- data/lib/rubocop/cop/style/one_line_conditional.rb +3 -2
- data/lib/rubocop/cop/style/op_method.rb +7 -4
- data/lib/rubocop/cop/style/option_hash.rb +13 -7
- data/lib/rubocop/cop/style/optional_arguments.rb +3 -2
- data/lib/rubocop/cop/style/parallel_assignment.rb +40 -16
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +3 -16
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +3 -2
- data/lib/rubocop/cop/style/percent_q_literals.rb +3 -6
- data/lib/rubocop/cop/style/perl_backrefs.rb +4 -3
- data/lib/rubocop/cop/style/predicate_name.rb +1 -0
- data/lib/rubocop/cop/style/proc.rb +3 -2
- data/lib/rubocop/cop/style/raise_args.rb +2 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +5 -5
- data/lib/rubocop/cop/style/redundant_freeze.rb +5 -4
- data/lib/rubocop/cop/style/redundant_parentheses.rb +80 -0
- data/lib/rubocop/cop/style/redundant_return.rb +5 -4
- data/lib/rubocop/cop/style/redundant_self.rb +7 -8
- data/lib/rubocop/cop/style/regexp_literal.rb +9 -8
- data/lib/rubocop/cop/style/rescue_ensure_alignment.rb +3 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +11 -9
- data/lib/rubocop/cop/style/self_assignment.rb +4 -5
- data/lib/rubocop/cop/style/semicolon.rb +3 -2
- data/lib/rubocop/cop/style/send.rb +3 -1
- data/lib/rubocop/cop/style/signal_exception.rb +5 -3
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +7 -7
- data/lib/rubocop/cop/style/space_after_colon.rb +2 -1
- data/lib/rubocop/cop/style/space_after_comma.rb +1 -0
- data/lib/rubocop/cop/style/space_after_control_keyword.rb +5 -5
- data/lib/rubocop/cop/style/space_after_method_name.rb +3 -2
- data/lib/rubocop/cop/style/space_after_not.rb +4 -3
- data/lib/rubocop/cop/style/space_after_semicolon.rb +1 -0
- data/lib/rubocop/cop/style/space_around_block_parameters.rb +8 -7
- data/lib/rubocop/cop/style/space_around_equals_in_parameter_default.rb +1 -0
- data/lib/rubocop/cop/style/space_around_operators.rb +72 -32
- data/lib/rubocop/cop/style/space_before_block_braces.rb +2 -1
- data/lib/rubocop/cop/style/space_before_comma.rb +1 -0
- data/lib/rubocop/cop/style/space_before_comment.rb +2 -1
- data/lib/rubocop/cop/style/{single_space_before_first_arg.rb → space_before_first_arg.rb} +13 -4
- data/lib/rubocop/cop/style/space_before_modifier_keyword.rb +4 -3
- data/lib/rubocop/cop/style/space_before_semicolon.rb +1 -0
- data/lib/rubocop/cop/style/space_inside_block_braces.rb +3 -2
- data/lib/rubocop/cop/style/space_inside_brackets.rb +1 -0
- data/lib/rubocop/cop/style/space_inside_hash_literal_braces.rb +4 -1
- data/lib/rubocop/cop/style/space_inside_parens.rb +1 -0
- data/lib/rubocop/cop/style/space_inside_range_literal.rb +5 -4
- data/lib/rubocop/cop/style/space_inside_string_interpolation.rb +8 -17
- data/lib/rubocop/cop/style/special_global_vars.rb +97 -52
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +16 -9
- data/lib/rubocop/cop/style/string_literals.rb +41 -1
- data/lib/rubocop/cop/style/string_literals_in_interpolation.rb +2 -1
- data/lib/rubocop/cop/style/string_methods.rb +2 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +3 -2
- data/lib/rubocop/cop/style/symbol_array.rb +74 -7
- data/lib/rubocop/cop/style/symbol_literal.rb +4 -7
- data/lib/rubocop/cop/style/symbol_proc.rb +11 -7
- data/lib/rubocop/cop/style/tab.rb +25 -2
- data/lib/rubocop/cop/style/trailing_blank_lines.rb +1 -2
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +45 -0
- data/lib/rubocop/cop/style/trailing_comma_in_literal.rb +56 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +8 -7
- data/lib/rubocop/cop/style/trailing_whitespace.rb +2 -1
- data/lib/rubocop/cop/style/trivial_accessors.rb +18 -9
- data/lib/rubocop/cop/style/unless_else.rb +2 -1
- data/lib/rubocop/cop/style/unneeded_capital_w.rb +4 -3
- data/lib/rubocop/cop/style/unneeded_interpolation.rb +87 -0
- data/lib/rubocop/cop/style/unneeded_percent_q.rb +23 -7
- data/lib/rubocop/cop/style/variable_interpolation.rb +8 -6
- data/lib/rubocop/cop/style/variable_name.rb +1 -0
- data/lib/rubocop/cop/style/when_then.rb +2 -1
- data/lib/rubocop/cop/style/while_until_do.rb +3 -2
- data/lib/rubocop/cop/style/while_until_modifier.rb +3 -4
- data/lib/rubocop/cop/style/word_array.rb +74 -51
- data/lib/rubocop/cop/team.rb +21 -15
- data/lib/rubocop/cop/util.rb +102 -69
- data/lib/rubocop/cop/variable_force.rb +2 -1
- data/lib/rubocop/cop/variable_force/assignment.rb +2 -1
- data/lib/rubocop/cop/variable_force/locatable.rb +1 -0
- data/lib/rubocop/cop/variable_force/reference.rb +1 -0
- data/lib/rubocop/cop/variable_force/scope.rb +2 -1
- data/lib/rubocop/cop/variable_force/variable.rb +2 -1
- data/lib/rubocop/cop/variable_force/variable_table.rb +2 -1
- data/lib/rubocop/error.rb +12 -0
- data/lib/rubocop/formatter/base_formatter.rb +10 -1
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -0
- data/lib/rubocop/formatter/colorizable.rb +6 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +29 -15
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +3 -1
- data/lib/rubocop/formatter/emacs_style_formatter.rb +7 -3
- data/lib/rubocop/formatter/file_list_formatter.rb +1 -0
- data/lib/rubocop/formatter/formatter_set.rb +10 -19
- data/lib/rubocop/formatter/fuubar_style_formatter.rb +2 -1
- data/lib/rubocop/formatter/html_formatter.rb +15 -14
- data/lib/rubocop/formatter/json_formatter.rb +2 -1
- data/lib/rubocop/formatter/offense_count_formatter.rb +1 -0
- data/lib/rubocop/formatter/progress_formatter.rb +3 -3
- data/lib/rubocop/formatter/simple_text_formatter.rb +50 -17
- data/lib/rubocop/formatter/text_util.rb +8 -10
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +61 -0
- data/lib/rubocop/name_similarity.rb +22 -0
- data/lib/rubocop/node_pattern.rb +126 -35
- data/lib/rubocop/options.rb +28 -19
- data/lib/rubocop/path_util.rb +1 -0
- data/lib/rubocop/processed_source.rb +41 -16
- data/lib/rubocop/rake_task.rb +6 -9
- data/lib/rubocop/remote_config.rb +1 -0
- data/lib/rubocop/result_cache.rb +60 -43
- data/lib/rubocop/runner.rb +48 -45
- data/lib/rubocop/string_util.rb +1 -0
- data/lib/rubocop/target_finder.rb +2 -1
- data/lib/rubocop/token.rb +1 -0
- data/lib/rubocop/version.rb +3 -2
- data/lib/rubocop/warning.rb +1 -0
- data/relnotes/v0.36.0.md +306 -0
- data/rubocop.gemspec +3 -9
- metadata +48 -92
- data/lib/rubocop/cop/lint/space_before_first_arg.rb +0 -44
- data/lib/rubocop/cop/rails/default_scope.rb +0 -33
- data/lib/rubocop/cop/style/trailing_comma.rb +0 -182
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -71,10 +72,10 @@ module RuboCop
|
|
71
72
|
special = %w(protected private) # Extra indentation step after these.
|
72
73
|
previous_modifier = nil
|
73
74
|
members.first.children.each do |m|
|
74
|
-
if modifier_node?(m) && special.include?(m.
|
75
|
+
if modifier_node?(m) && special.include?(m.source)
|
75
76
|
previous_modifier = m
|
76
77
|
elsif previous_modifier
|
77
|
-
check_indentation(previous_modifier.
|
78
|
+
check_indentation(previous_modifier.source_range, m, style)
|
78
79
|
previous_modifier = nil
|
79
80
|
end
|
80
81
|
end
|
@@ -82,9 +83,8 @@ module RuboCop
|
|
82
83
|
|
83
84
|
def on_send(node)
|
84
85
|
super
|
85
|
-
|
86
|
-
|
87
|
-
args)
|
86
|
+
return unless modifier_and_def_on_same_line?(node)
|
87
|
+
_, _, *args = *node
|
88
88
|
|
89
89
|
*_, body = *args.first
|
90
90
|
|
@@ -96,7 +96,7 @@ module RuboCop
|
|
96
96
|
end
|
97
97
|
base = style == 'def' ? args.first : node
|
98
98
|
|
99
|
-
check_indentation(base.
|
99
|
+
check_indentation(base.source_range, body)
|
100
100
|
ignore_node(args.first)
|
101
101
|
end
|
102
102
|
|
@@ -114,12 +114,12 @@ module RuboCop
|
|
114
114
|
|
115
115
|
_condition, body = *node
|
116
116
|
return unless node.loc.keyword.begin_pos ==
|
117
|
-
node.
|
117
|
+
node.source_range.begin_pos
|
118
118
|
|
119
119
|
check_indentation(base.loc, body)
|
120
120
|
end
|
121
121
|
|
122
|
-
|
122
|
+
alias on_until on_while
|
123
123
|
|
124
124
|
def on_case(node)
|
125
125
|
_condition, *branches = *node
|
@@ -234,7 +234,7 @@ module RuboCop
|
|
234
234
|
# Returns true if the given node is within another node that has
|
235
235
|
# already been marked for auto-correction by this cop.
|
236
236
|
def other_offense_in_same_range?(node)
|
237
|
-
expr = node.
|
237
|
+
expr = node.source_range
|
238
238
|
@offense_ranges ||= []
|
239
239
|
|
240
240
|
return true if @offense_ranges.any? { |r| within?(expr, r) }
|
@@ -253,7 +253,7 @@ module RuboCop
|
|
253
253
|
|
254
254
|
# Don't check indentation if the line doesn't start with the body.
|
255
255
|
# For example, lines like "else do_something".
|
256
|
-
first_char_pos_on_line = body_node.
|
256
|
+
first_char_pos_on_line = body_node.source_range.source_line =~ /\S/
|
257
257
|
return false unless body_node.loc.column == first_char_pos_on_line
|
258
258
|
|
259
259
|
if [:rescue, :ensure].include?(body_node.type)
|
@@ -265,7 +265,7 @@ module RuboCop
|
|
265
265
|
end
|
266
266
|
|
267
267
|
def offending_range(body_node, indentation)
|
268
|
-
expr = body_node.
|
268
|
+
expr = body_node.source_range
|
269
269
|
begin_pos = expr.begin_pos
|
270
270
|
ind = expr.begin_pos - indentation
|
271
271
|
pos = indentation >= 0 ? ind..begin_pos : begin_pos..ind
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -16,17 +17,12 @@ module RuboCop
|
|
16
17
|
# work
|
17
18
|
# end
|
18
19
|
class InfiniteLoop < Cop
|
19
|
-
MSG = 'Use `Kernel#loop` for infinite loops.'
|
20
|
-
|
21
|
-
TRUTHY_LITERALS = [:str, :dstr, :int, :float, :array,
|
22
|
-
:hash, :regexp, :true]
|
23
|
-
|
24
|
-
FALSEY_LITERALS = [:nil, :false]
|
20
|
+
MSG = 'Use `Kernel#loop` for infinite loops.'.freeze
|
25
21
|
|
26
22
|
def on_while(node)
|
27
23
|
condition, = *node
|
28
24
|
|
29
|
-
return unless
|
25
|
+
return unless condition.truthy_literal?
|
30
26
|
|
31
27
|
add_offense(node, :keyword)
|
32
28
|
end
|
@@ -34,7 +30,7 @@ module RuboCop
|
|
34
30
|
def on_until(node)
|
35
31
|
condition, = *node
|
36
32
|
|
37
|
-
return unless
|
33
|
+
return unless condition.falsey_literal?
|
38
34
|
|
39
35
|
add_offense(node, :keyword)
|
40
36
|
end
|
@@ -45,7 +41,7 @@ module RuboCop
|
|
45
41
|
end_range = if node.loc.begin
|
46
42
|
node.loc.begin.end
|
47
43
|
else
|
48
|
-
condition_node.
|
44
|
+
condition_node.source_range.end
|
49
45
|
end
|
50
46
|
lambda do |corrector|
|
51
47
|
corrector.replace(start_range.join(end_range), 'loop do')
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -6,7 +7,7 @@ module RuboCop
|
|
6
7
|
# This cops checks for indentation of the first non-blank non-comment
|
7
8
|
# line in a file.
|
8
9
|
class InitialIndentation < Cop
|
9
|
-
MSG = 'Indentation of first line in file detected.'
|
10
|
+
MSG = 'Indentation of first line in file detected.'.freeze
|
10
11
|
|
11
12
|
def investigate(processed_source)
|
12
13
|
first_token = processed_source.tokens.find do |t|
|
@@ -1,11 +1,12 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
5
6
|
module Style
|
6
7
|
# This cop checks for inline comments.
|
7
8
|
class InlineComment < Cop
|
8
|
-
MSG = 'Avoid inline comments.'
|
9
|
+
MSG = 'Avoid inline comments.'.freeze
|
9
10
|
|
10
11
|
def investigate(processed_source)
|
11
12
|
processed_source.comments.each do |comment|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -9,9 +10,11 @@ module RuboCop
|
|
9
10
|
class Lambda < Cop
|
10
11
|
include AutocorrectUnlessChangingAST
|
11
12
|
|
12
|
-
SINGLE_MSG = 'Use the new lambda literal syntax
|
13
|
-
|
14
|
-
|
13
|
+
SINGLE_MSG = 'Use the new lambda literal syntax ' \
|
14
|
+
'`->(params) {...}`.'.freeze
|
15
|
+
SINGLE_NO_ARG_MSG = 'Use the new lambda literal syntax ' \
|
16
|
+
'`-> {...}`.'.freeze
|
17
|
+
MULTI_MSG = 'Use the `lambda` method for multi-line lambdas.'.freeze
|
15
18
|
|
16
19
|
TARGET = s(:send, nil, :lambda)
|
17
20
|
|
@@ -27,9 +30,9 @@ module RuboCop
|
|
27
30
|
length = lambda_length(node)
|
28
31
|
|
29
32
|
if selector != '->' && length == 1
|
30
|
-
add_offense_for_single_line(node, block_method.
|
33
|
+
add_offense_for_single_line(node, block_method.source_range, args)
|
31
34
|
elsif selector == '->' && length > 1
|
32
|
-
add_offense(node, block_method.
|
35
|
+
add_offense(node, block_method.source_range, MULTI_MSG)
|
33
36
|
end
|
34
37
|
end
|
35
38
|
|
@@ -54,7 +57,7 @@ module RuboCop
|
|
54
57
|
lambda do |corrector|
|
55
58
|
block_method, _args = *node
|
56
59
|
|
57
|
-
if block_method.
|
60
|
+
if block_method.source == 'lambda'
|
58
61
|
autocorrect_old_to_new(corrector, node)
|
59
62
|
else
|
60
63
|
autocorrect_new_to_old(corrector, node)
|
@@ -69,8 +72,8 @@ module RuboCop
|
|
69
72
|
if needs_whitespace?(block_method, args, node)
|
70
73
|
corrector.insert_before(node.loc.begin, ' ')
|
71
74
|
end
|
72
|
-
corrector.replace(block_method.
|
73
|
-
corrector.remove(args.
|
75
|
+
corrector.replace(block_method.source_range, 'lambda')
|
76
|
+
corrector.remove(args.source_range) if args.source_range
|
74
77
|
return if args.children.empty?
|
75
78
|
arg_str = " |#{lambda_arg_string(args)}|"
|
76
79
|
corrector.insert_after(node.loc.begin, arg_str)
|
@@ -78,12 +81,12 @@ module RuboCop
|
|
78
81
|
|
79
82
|
def autocorrect_old_to_new(corrector, node)
|
80
83
|
block_method, args = *node
|
81
|
-
corrector.replace(block_method.
|
84
|
+
corrector.replace(block_method.source_range, '->')
|
82
85
|
return if args.children.empty?
|
83
86
|
|
84
87
|
arg_str = "(#{lambda_arg_string(args)})"
|
85
88
|
whitespace_and_old_args = node.loc.begin.end.join(args.loc.end)
|
86
|
-
corrector.insert_after(block_method.
|
89
|
+
corrector.insert_after(block_method.source_range, arg_str)
|
87
90
|
corrector.remove(whitespace_and_old_args)
|
88
91
|
end
|
89
92
|
|
@@ -97,7 +100,7 @@ module RuboCop
|
|
97
100
|
end
|
98
101
|
|
99
102
|
def lambda_arg_string(args)
|
100
|
-
args.children.map
|
103
|
+
args.children.map(&:source).join(', ')
|
101
104
|
end
|
102
105
|
end
|
103
106
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -40,10 +41,9 @@ module RuboCop
|
|
40
41
|
lambda do |corrector|
|
41
42
|
if style == :call
|
42
43
|
receiver_node, = *node
|
43
|
-
|
44
|
-
|
45
|
-
|
46
|
-
corrector.replace(expr, replacement)
|
44
|
+
receiver = receiver_node.source
|
45
|
+
replacement = node.source.sub("#{receiver}.", "#{receiver}.call")
|
46
|
+
corrector.replace(node.source_range, replacement)
|
47
47
|
else
|
48
48
|
corrector.remove(node.loc.selector)
|
49
49
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -9,7 +10,7 @@ module RuboCop
|
|
9
10
|
# like #++, #--, #:nodoc, etc. Neither is it required for
|
10
11
|
# =begin/=end comments.
|
11
12
|
class LeadingCommentSpace < Cop
|
12
|
-
MSG = 'Missing space after #.'
|
13
|
+
MSG = 'Missing space after #.'.freeze
|
13
14
|
|
14
15
|
def investigate(processed_source)
|
15
16
|
processed_source.comments.each do |comment|
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -20,7 +21,8 @@ module RuboCop
|
|
20
21
|
# 'bala'
|
21
22
|
#
|
22
23
|
class LineEndConcatenation < Cop
|
23
|
-
MSG = 'Use `\\` instead of `+` or `<<` to concatenate
|
24
|
+
MSG = 'Use `\\` instead of `+` or `<<` to concatenate ' \
|
25
|
+
'those strings.'.freeze
|
24
26
|
CONCAT_TOKEN_TYPES = [:tPLUS, :tLSHFT].freeze
|
25
27
|
SIMPLE_STRING_TOKEN_TYPE = :tSTRING
|
26
28
|
COMPLEX_STRING_EDGE_TOKEN_TYPES = [:tSTRING_BEG, :tSTRING_END].freeze
|
@@ -1,11 +1,13 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
5
6
|
module Style
|
6
7
|
# This cop checks for unwanted parentheses in parameterless method calls.
|
7
8
|
class MethodCallParentheses < Cop
|
8
|
-
MSG = 'Do not use parentheses for method calls with
|
9
|
+
MSG = 'Do not use parentheses for method calls with ' \
|
10
|
+
'no arguments.'.freeze
|
9
11
|
|
10
12
|
ASGN_NODES = [:lvasgn, :masgn] + Util::SHORTHAND_ASGN_NODES
|
11
13
|
|
@@ -16,6 +18,7 @@ module RuboCop
|
|
16
18
|
return if method_name =~ /\A[A-Z]/
|
17
19
|
return unless args.empty? && node.loc.begin
|
18
20
|
return if same_name_assignment?(node)
|
21
|
+
return if lambda_call_syntax?(node)
|
19
22
|
|
20
23
|
add_offense(node, :begin)
|
21
24
|
end
|
@@ -49,6 +52,11 @@ module RuboCop
|
|
49
52
|
asgn_node.loc.name.source == method_name.to_s
|
50
53
|
end
|
51
54
|
end
|
55
|
+
|
56
|
+
# don't check `lambda.()` syntax; the Style/LambdaCall cop does that
|
57
|
+
def lambda_call_syntax?(node)
|
58
|
+
node.method_name == :call && node.loc.selector.nil?
|
59
|
+
end
|
52
60
|
end
|
53
61
|
end
|
54
62
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -13,7 +14,7 @@ module RuboCop
|
|
13
14
|
# b
|
14
15
|
# end.c
|
15
16
|
class MethodCalledOnDoEndBlock < Cop
|
16
|
-
MSG = 'Avoid chaining a method call on a do...end block.'
|
17
|
+
MSG = 'Avoid chaining a method call on a do...end block.'.freeze
|
17
18
|
|
18
19
|
def on_block(node)
|
19
20
|
method, _args, _body = *node
|
@@ -33,7 +34,7 @@ module RuboCop
|
|
33
34
|
|
34
35
|
range = Parser::Source::Range.new(receiver.loc.end.source_buffer,
|
35
36
|
receiver.loc.end.begin_pos,
|
36
|
-
node.
|
37
|
+
node.source_range.end_pos)
|
37
38
|
add_offense(nil, range)
|
38
39
|
end
|
39
40
|
end
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -32,7 +33,7 @@ module RuboCop
|
|
32
33
|
corrector.replace(node.loc.begin, ' ')
|
33
34
|
corrector.remove(node.loc.end)
|
34
35
|
else
|
35
|
-
args_expr = args_node(node).
|
36
|
+
args_expr = args_node(node).source_range
|
36
37
|
args_with_space = range_with_surrounding_space(args_expr, :left)
|
37
38
|
just_space = Parser::Source::Range.new(args_expr.source_buffer,
|
38
39
|
args_with_space.begin_pos,
|
@@ -46,7 +47,7 @@ module RuboCop
|
|
46
47
|
private
|
47
48
|
|
48
49
|
def missing_parentheses(node, args)
|
49
|
-
add_offense(node, args.
|
50
|
+
add_offense(node, args.source_range,
|
50
51
|
'Use def with parentheses when there are parameters.') do
|
51
52
|
unexpected_style_detected(:require_no_parentheses)
|
52
53
|
end
|
@@ -61,11 +62,10 @@ module RuboCop
|
|
61
62
|
def args_node(def_node)
|
62
63
|
if def_node.type == :def
|
63
64
|
_method_name, args, _body = *def_node
|
64
|
-
args
|
65
65
|
else
|
66
66
|
_scope, _method_name, args, _body = *def_node
|
67
|
-
args
|
68
67
|
end
|
68
|
+
args
|
69
69
|
end
|
70
70
|
|
71
71
|
def arguments?(args)
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -32,9 +33,10 @@ module RuboCop
|
|
32
33
|
include OnNormalIfUnless
|
33
34
|
include ConfigurableEnforcedStyle
|
34
35
|
|
35
|
-
MSG = '`%s` condition requires an `else`-clause.'
|
36
|
-
MSG_NIL = '`%s` condition requires an `else`-clause with
|
37
|
-
|
36
|
+
MSG = '`%s` condition requires an `else`-clause.'.freeze
|
37
|
+
MSG_NIL = '`%s` condition requires an `else`-clause with ' \
|
38
|
+
'`nil` in it.'.freeze
|
39
|
+
MSG_EMPTY = '`%s` condition requires an empty `else`-clause.'.freeze
|
38
40
|
|
39
41
|
def on_normal_if_unless(node)
|
40
42
|
unless_else_cop = config.for_cop('Style/UnlessElse')
|
@@ -1,4 +1,5 @@
|
|
1
1
|
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
2
3
|
|
3
4
|
module RuboCop
|
4
5
|
module Cop
|
@@ -13,7 +14,7 @@ module RuboCop
|
|
13
14
|
# ...
|
14
15
|
# end
|
15
16
|
class ModuleFunction < Cop
|
16
|
-
MSG = 'Use `module_function` instead of `extend self`.'
|
17
|
+
MSG = 'Use `module_function` instead of `extend self`.'.freeze
|
17
18
|
|
18
19
|
TARGET_NODE = s(:send, nil, :extend, s(:self))
|
19
20
|
|
@@ -0,0 +1,95 @@
|
|
1
|
+
# encoding: utf-8
|
2
|
+
# frozen_string_literal: true
|
3
|
+
|
4
|
+
module RuboCop
|
5
|
+
module Cop
|
6
|
+
module Style
|
7
|
+
# This cop checks that the closing brace in an array literal is
|
8
|
+
# symmetrical with respect to the opening brace and the array
|
9
|
+
# elements.
|
10
|
+
#
|
11
|
+
# If an array's opening brace is on the same line as the first element
|
12
|
+
# of the array, then the closing brace should be on the same line as
|
13
|
+
# the last element of the array.
|
14
|
+
#
|
15
|
+
# If an array's opening brace is on a separate line from the first
|
16
|
+
# element of the array, then the closing brace should be on the line
|
17
|
+
# after the last element of the array.
|
18
|
+
#
|
19
|
+
# @example
|
20
|
+
#
|
21
|
+
# # bad
|
22
|
+
# [ :a,
|
23
|
+
# :b
|
24
|
+
# ]
|
25
|
+
#
|
26
|
+
# # bad
|
27
|
+
# [
|
28
|
+
# :a,
|
29
|
+
# :b ]
|
30
|
+
#
|
31
|
+
# # good
|
32
|
+
# [ :a,
|
33
|
+
# :b ]
|
34
|
+
#
|
35
|
+
# #good
|
36
|
+
# [
|
37
|
+
# :a,
|
38
|
+
# :b
|
39
|
+
# ]
|
40
|
+
class MultilineArrayBraceLayout < Cop
|
41
|
+
SAME_LINE_MESSAGE = 'Closing array brace must be on the same line as ' \
|
42
|
+
'the last array element when opening brace is on the same line as ' \
|
43
|
+
'the first array element.'.freeze
|
44
|
+
|
45
|
+
NEW_LINE_MESSAGE = 'Closing array brace must be on the line after ' \
|
46
|
+
'the last array element when opening brace is on a separate line ' \
|
47
|
+
'from the first array element.'.freeze
|
48
|
+
|
49
|
+
def on_array(node)
|
50
|
+
return unless node.loc.begin # Ignore implicit arrays.
|
51
|
+
return if node.children.empty? # Ignore empty arrays.
|
52
|
+
|
53
|
+
if opening_brace_on_same_line?(node)
|
54
|
+
return if closing_brace_on_same_line?(node)
|
55
|
+
|
56
|
+
add_offense(node, :expression, SAME_LINE_MESSAGE)
|
57
|
+
else
|
58
|
+
return unless closing_brace_on_same_line?(node)
|
59
|
+
|
60
|
+
add_offense(node, :expression, NEW_LINE_MESSAGE)
|
61
|
+
end
|
62
|
+
end
|
63
|
+
|
64
|
+
def autocorrect(node)
|
65
|
+
if closing_brace_on_same_line?(node)
|
66
|
+
lambda do |corrector|
|
67
|
+
corrector.insert_before(node.loc.end, "\n".freeze)
|
68
|
+
end
|
69
|
+
else
|
70
|
+
range = Parser::Source::Range.new(
|
71
|
+
node.source_range.source_buffer,
|
72
|
+
node.children.last.source_range.end_pos,
|
73
|
+
node.loc.end.begin_pos)
|
74
|
+
|
75
|
+
->(corrector) { corrector.remove(range) }
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
private
|
80
|
+
|
81
|
+
# This method depends on the fact that we have guarded
|
82
|
+
# against implicit and empty arrays.
|
83
|
+
def opening_brace_on_same_line?(node)
|
84
|
+
node.loc.begin.line == node.children.first.loc.first_line
|
85
|
+
end
|
86
|
+
|
87
|
+
# This method depends on the fact that we have guarded
|
88
|
+
# against implicit and empty arrays.
|
89
|
+
def closing_brace_on_same_line?(node)
|
90
|
+
node.loc.end.line == node.children.last.loc.last_line
|
91
|
+
end
|
92
|
+
end
|
93
|
+
end
|
94
|
+
end
|
95
|
+
end
|