rubocop 0.83.0 → 0.87.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 +26 -18
- data/config/default.yml +128 -23
- data/lib/rubocop.rb +21 -59
- data/lib/rubocop/ast_aliases.rb +8 -0
- data/lib/rubocop/cli.rb +2 -4
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +42 -7
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +2 -6
- data/lib/rubocop/comment_config.rb +1 -1
- data/lib/rubocop/config.rb +1 -1
- data/lib/rubocop/config_loader.rb +24 -70
- data/lib/rubocop/config_loader_resolver.rb +21 -9
- data/lib/rubocop/config_obsoletion.rb +0 -1
- data/lib/rubocop/config_store.rb +12 -2
- data/lib/rubocop/cop/autocorrect_logic.rb +14 -25
- data/lib/rubocop/cop/base.rb +399 -0
- data/lib/rubocop/cop/bundler/gem_comment.rb +70 -1
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +10 -20
- data/lib/rubocop/cop/commissioner.rb +48 -71
- data/lib/rubocop/cop/cop.rb +91 -227
- data/lib/rubocop/cop/corrector.rb +38 -115
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +2 -6
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +1 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/generator.rb +2 -2
- data/lib/rubocop/cop/generator/configuration_injector.rb +1 -1
- data/lib/rubocop/cop/ignored_node.rb +1 -3
- data/lib/rubocop/cop/internal_affairs/node_type_predicate.rb +11 -14
- data/lib/rubocop/cop/layout/case_indentation.rb +21 -22
- data/lib/rubocop/cop/layout/class_structure.rb +19 -16
- data/lib/rubocop/cop/layout/comment_indentation.rb +3 -3
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +57 -4
- data/lib/rubocop/cop/layout/end_of_line.rb +3 -3
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +6 -4
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -3
- data/lib/rubocop/cop/layout/first_array_element_line_break.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +8 -9
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +3 -7
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +20 -103
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
- data/lib/rubocop/cop/layout/line_length.rb +17 -17
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +1 -2
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +19 -25
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +1 -3
- data/lib/rubocop/cop/layout/space_around_operators.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_block_braces.rb +14 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +2 -4
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_hash_literal_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -3
- data/lib/rubocop/cop/legacy/corrections_proxy.rb +49 -0
- data/lib/rubocop/cop/legacy/corrector.rb +29 -0
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +5 -2
- data/lib/rubocop/cop/lint/constant_resolution.rb +89 -0
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +137 -0
- data/lib/rubocop/cop/lint/ensure_return.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -3
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/format_parameter_mismatch.rb +38 -2
- data/lib/rubocop/cop/lint/interpolation_check.rb +13 -0
- data/lib/rubocop/cop/lint/loop.rb +1 -1
- data/lib/rubocop/cop/lint/mixed_regexp_capture_types.rb +69 -0
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/nested_percent_literal.rb +1 -1
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +7 -7
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +20 -5
- data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
- data/lib/rubocop/cop/lint/percent_symbol_array.rb +1 -1
- data/lib/rubocop/cop/lint/raise_exception.rb +12 -4
- data/lib/rubocop/cop/lint/rand_one.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +31 -25
- data/lib/rubocop/cop/lint/redundant_require_statement.rb +3 -3
- data/lib/rubocop/cop/lint/redundant_splat_expansion.rb +2 -2
- data/lib/rubocop/cop/lint/regexp_as_condition.rb +6 -0
- data/lib/rubocop/cop/lint/rescue_exception.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_with_empty.rb +9 -1
- data/lib/rubocop/cop/lint/suppressed_exception.rb +15 -2
- data/lib/rubocop/cop/lint/syntax.rb +11 -28
- data/lib/rubocop/cop/lint/unreachable_code.rb +1 -1
- data/lib/rubocop/cop/lint/unused_method_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +2 -4
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +1 -1
- data/lib/rubocop/cop/lint/useless_setter_call.rb +1 -1
- data/lib/rubocop/cop/metrics/block_length.rb +22 -0
- data/lib/rubocop/cop/metrics/class_length.rb +25 -2
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +35 -3
- data/lib/rubocop/cop/metrics/method_length.rb +23 -0
- data/lib/rubocop/cop/metrics/module_length.rb +25 -2
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +129 -0
- data/lib/rubocop/cop/metrics/utils/iterating_block.rb +61 -0
- data/lib/rubocop/cop/migration/department_name.rb +7 -7
- data/lib/rubocop/cop/mixin/alignment.rb +1 -3
- data/lib/rubocop/cop/mixin/allowed_methods.rb +19 -0
- data/lib/rubocop/cop/mixin/array_min_size.rb +1 -3
- data/lib/rubocop/cop/mixin/auto_corrector.rb +12 -0
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +3 -9
- data/lib/rubocop/cop/mixin/code_length.rb +4 -0
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +3 -5
- data/lib/rubocop/cop/mixin/configurable_naming.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +2 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +3 -1
- data/lib/rubocop/cop/mixin/first_element_line_break.rb +1 -1
- data/lib/rubocop/cop/mixin/ignored_pattern.rb +1 -1
- data/lib/rubocop/cop/mixin/line_length_help.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/nil_methods.rb +3 -5
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +6 -1
- data/lib/rubocop/cop/mixin/parentheses.rb +1 -2
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +1 -1
- data/lib/rubocop/cop/mixin/range_help.rb +1 -1
- data/lib/rubocop/cop/mixin/regexp_literal_help.rb +43 -0
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -1
- data/lib/rubocop/cop/mixin/surrounding_space.rb +10 -5
- data/lib/rubocop/cop/mixin/too_many_lines.rb +3 -13
- data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
- data/lib/rubocop/cop/mixin/uncommunicative_name.rb +7 -5
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +27 -4
- data/lib/rubocop/cop/naming/binary_operator_parameter_name.rb +3 -3
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +11 -1
- data/lib/rubocop/cop/naming/file_name.rb +28 -17
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/predicate_name.rb +4 -6
- data/lib/rubocop/cop/naming/variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/variable_number.rb +1 -1
- data/lib/rubocop/cop/offense.rb +16 -2
- data/lib/rubocop/cop/registry.rb +63 -10
- data/lib/rubocop/cop/severity.rb +1 -3
- data/lib/rubocop/cop/style/accessor_grouping.rb +136 -0
- data/lib/rubocop/cop/style/and_or.rb +2 -2
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -3
- data/lib/rubocop/cop/style/bare_percent_literals.rb +2 -2
- data/lib/rubocop/cop/style/bisected_attr_accessor.rb +121 -0
- data/lib/rubocop/cop/style/block_delimiters.rb +4 -12
- data/lib/rubocop/cop/style/class_and_module_children.rb +1 -1
- data/lib/rubocop/cop/style/class_vars.rb +21 -0
- data/lib/rubocop/cop/style/command_literal.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +2 -4
- data/lib/rubocop/cop/style/copyright.rb +5 -5
- data/lib/rubocop/cop/style/date_time.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +2 -2
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +2 -2
- data/lib/rubocop/cop/style/double_negation.rb +41 -4
- data/lib/rubocop/cop/style/empty_case_condition.rb +8 -6
- data/lib/rubocop/cop/style/empty_literal.rb +6 -8
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/encoding.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +2 -2
- data/lib/rubocop/cop/style/exponential_notation.rb +5 -5
- data/lib/rubocop/cop/style/format_string_token.rb +2 -3
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -6
- data/lib/rubocop/cop/style/hash_each_methods.rb +1 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +16 -7
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +1 -1
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -1
- data/lib/rubocop/cop/style/if_unless_modifier_of_if_unless.rb +12 -0
- data/lib/rubocop/cop/style/inline_comment.rb +1 -1
- data/lib/rubocop/cop/style/inverse_methods.rb +1 -1
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +1 -3
- data/lib/rubocop/cop/style/method_call_without_args_parentheses.rb +1 -3
- data/lib/rubocop/cop/style/multiline_block_chain.rb +10 -1
- data/lib/rubocop/cop/style/multiline_if_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +17 -6
- data/lib/rubocop/cop/style/mutable_constant.rb +4 -4
- data/lib/rubocop/cop/style/negated_if.rb +3 -3
- data/lib/rubocop/cop/style/negated_unless.rb +3 -3
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +2 -5
- data/lib/rubocop/cop/style/nested_ternary_operator.rb +27 -0
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/non_nil_check.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +2 -2
- data/lib/rubocop/cop/style/one_line_conditional.rb +2 -6
- data/lib/rubocop/cop/style/percent_literal_delimiters.rb +1 -1
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +4 -10
- data/lib/rubocop/cop/style/redundant_assignment.rb +117 -0
- data/lib/rubocop/cop/style/redundant_conditional.rb +4 -3
- data/lib/rubocop/cop/style/redundant_exception.rb +14 -10
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +122 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +1 -1
- data/lib/rubocop/cop/style/redundant_parentheses.rb +10 -8
- data/lib/rubocop/cop/style/redundant_percent_q.rb +3 -3
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +90 -0
- data/lib/rubocop/cop/style/redundant_regexp_escape.rb +121 -0
- data/lib/rubocop/cop/style/redundant_self.rb +6 -9
- data/lib/rubocop/cop/style/rescue_standard_error.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +2 -6
- data/lib/rubocop/cop/style/sample.rb +1 -1
- data/lib/rubocop/cop/style/semicolon.rb +1 -1
- data/lib/rubocop/cop/style/signal_exception.rb +1 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +1 -1
- data/lib/rubocop/cop/style/special_global_vars.rb +2 -6
- data/lib/rubocop/cop/style/stderr_puts.rb +1 -1
- data/lib/rubocop/cop/style/struct_inheritance.rb +23 -2
- data/lib/rubocop/cop/style/symbol_array.rb +5 -5
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -4
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +3 -3
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +3 -3
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +13 -13
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +3 -3
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +1 -3
- data/lib/rubocop/cop/style/trivial_accessors.rb +8 -7
- data/lib/rubocop/cop/style/unless_else.rb +1 -1
- data/lib/rubocop/cop/style/when_then.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/yoda_condition.rb +18 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +2 -2
- data/lib/rubocop/cop/team.rb +141 -81
- data/lib/rubocop/cop/util.rb +3 -3
- data/lib/rubocop/cop/utils/format_string.rb +19 -2
- data/lib/rubocop/cop/variable_force.rb +3 -9
- data/lib/rubocop/cop/variable_force/branch.rb +1 -3
- data/lib/rubocop/cop/variable_force/variable.rb +2 -6
- data/lib/rubocop/ext/processed_source.rb +18 -0
- data/lib/rubocop/formatter/base_formatter.rb +0 -4
- data/lib/rubocop/formatter/disabled_config_formatter.rb +5 -13
- data/lib/rubocop/formatter/formatter_set.rb +2 -4
- data/lib/rubocop/formatter/junit_formatter.rb +14 -4
- data/lib/rubocop/magic_comment.rb +1 -1
- data/lib/rubocop/name_similarity.rb +7 -3
- data/lib/rubocop/options.rb +30 -15
- data/lib/rubocop/path_util.rb +2 -2
- data/lib/rubocop/platform.rb +1 -1
- data/lib/rubocop/rake_task.rb +6 -9
- data/lib/rubocop/remote_config.rb +1 -3
- data/lib/rubocop/result_cache.rb +5 -7
- data/lib/rubocop/rspec/cop_helper.rb +5 -28
- data/lib/rubocop/rspec/expect_offense.rb +60 -23
- data/lib/rubocop/rspec/shared_contexts.rb +55 -17
- data/lib/rubocop/runner.rb +42 -38
- data/lib/rubocop/target_finder.rb +2 -4
- data/lib/rubocop/target_ruby.rb +5 -2
- data/lib/rubocop/version.rb +5 -3
- metadata +60 -62
- data/lib/rubocop/ast/builder.rb +0 -85
- data/lib/rubocop/ast/node.rb +0 -637
- data/lib/rubocop/ast/node/alias_node.rb +0 -24
- data/lib/rubocop/ast/node/and_node.rb +0 -29
- data/lib/rubocop/ast/node/args_node.rb +0 -29
- data/lib/rubocop/ast/node/array_node.rb +0 -70
- data/lib/rubocop/ast/node/block_node.rb +0 -121
- data/lib/rubocop/ast/node/break_node.rb +0 -17
- data/lib/rubocop/ast/node/case_match_node.rb +0 -56
- data/lib/rubocop/ast/node/case_node.rb +0 -56
- data/lib/rubocop/ast/node/class_node.rb +0 -31
- data/lib/rubocop/ast/node/def_node.rb +0 -82
- data/lib/rubocop/ast/node/defined_node.rb +0 -17
- data/lib/rubocop/ast/node/ensure_node.rb +0 -17
- data/lib/rubocop/ast/node/float_node.rb +0 -12
- data/lib/rubocop/ast/node/for_node.rb +0 -53
- data/lib/rubocop/ast/node/forward_args_node.rb +0 -18
- data/lib/rubocop/ast/node/hash_node.rb +0 -109
- data/lib/rubocop/ast/node/if_node.rb +0 -175
- data/lib/rubocop/ast/node/int_node.rb +0 -12
- data/lib/rubocop/ast/node/keyword_splat_node.rb +0 -45
- data/lib/rubocop/ast/node/mixin/basic_literal_node.rb +0 -16
- data/lib/rubocop/ast/node/mixin/binary_operator_node.rb +0 -43
- data/lib/rubocop/ast/node/mixin/collection_node.rb +0 -15
- data/lib/rubocop/ast/node/mixin/conditional_node.rb +0 -45
- data/lib/rubocop/ast/node/mixin/hash_element_node.rb +0 -125
- data/lib/rubocop/ast/node/mixin/method_dispatch_node.rb +0 -269
- data/lib/rubocop/ast/node/mixin/method_identifier_predicates.rb +0 -114
- data/lib/rubocop/ast/node/mixin/modifier_node.rb +0 -17
- data/lib/rubocop/ast/node/mixin/numeric_node.rb +0 -21
- data/lib/rubocop/ast/node/mixin/parameterized_node.rb +0 -61
- data/lib/rubocop/ast/node/mixin/predicate_operator_node.rb +0 -35
- data/lib/rubocop/ast/node/module_node.rb +0 -24
- data/lib/rubocop/ast/node/or_node.rb +0 -29
- data/lib/rubocop/ast/node/pair_node.rb +0 -63
- data/lib/rubocop/ast/node/range_node.rb +0 -18
- data/lib/rubocop/ast/node/regexp_node.rb +0 -33
- data/lib/rubocop/ast/node/resbody_node.rb +0 -24
- data/lib/rubocop/ast/node/retry_node.rb +0 -17
- data/lib/rubocop/ast/node/return_node.rb +0 -24
- data/lib/rubocop/ast/node/self_class_node.rb +0 -24
- data/lib/rubocop/ast/node/send_node.rb +0 -17
- data/lib/rubocop/ast/node/str_node.rb +0 -16
- data/lib/rubocop/ast/node/super_node.rb +0 -21
- data/lib/rubocop/ast/node/symbol_node.rb +0 -12
- data/lib/rubocop/ast/node/until_node.rb +0 -35
- data/lib/rubocop/ast/node/when_node.rb +0 -53
- data/lib/rubocop/ast/node/while_node.rb +0 -35
- data/lib/rubocop/ast/node/yield_node.rb +0 -21
- data/lib/rubocop/ast/sexp.rb +0 -16
- data/lib/rubocop/ast/traversal.rb +0 -202
- data/lib/rubocop/cop/mixin/classish_length.rb +0 -37
- data/lib/rubocop/node_pattern.rb +0 -887
- data/lib/rubocop/processed_source.rb +0 -213
- data/lib/rubocop/token.rb +0 -114
@@ -6,6 +6,12 @@ module RuboCop
|
|
6
6
|
# This cop checks for class and module names with
|
7
7
|
# an underscore in them.
|
8
8
|
#
|
9
|
+
# `AllowedNames` config takes an array of permitted names.
|
10
|
+
# Its default value is `['module_parent']`.
|
11
|
+
# These names can be full class/module names or part of the name.
|
12
|
+
# eg. Adding `my_class` to the `AllowedNames` config will allow names like
|
13
|
+
# `my_class`, `my_class::User`, `App::my_class`, `App::my_class::User`, etc.
|
14
|
+
#
|
9
15
|
# @example
|
10
16
|
# # bad
|
11
17
|
# class My_Class
|
@@ -18,11 +24,15 @@ module RuboCop
|
|
18
24
|
# end
|
19
25
|
# module MyModule
|
20
26
|
# end
|
27
|
+
# class module_parent::MyModule
|
28
|
+
# end
|
21
29
|
class ClassAndModuleCamelCase < Cop
|
22
30
|
MSG = 'Use CamelCase for classes and modules.'
|
23
31
|
|
24
32
|
def on_class(node)
|
25
|
-
|
33
|
+
allowed = /#{cop_config['AllowedNames'].join('|')}/
|
34
|
+
name = node.loc.name.source.gsub(allowed, '')
|
35
|
+
return unless /_/.match?(name)
|
26
36
|
|
27
37
|
add_offense(node, location: :name)
|
28
38
|
end
|
@@ -49,25 +49,36 @@ module RuboCop
|
|
49
49
|
|
50
50
|
def for_bad_filename(file_path)
|
51
51
|
basename = File.basename(file_path)
|
52
|
-
msg = if filename_good?(basename)
|
53
|
-
return if matching_definition?(file_path)
|
54
52
|
|
55
|
-
|
56
|
-
|
57
|
-
|
53
|
+
if filename_good?(basename)
|
54
|
+
msg = perform_class_and_module_naming_checks(file_path, basename)
|
55
|
+
else
|
56
|
+
msg = other_message(basename) unless bad_filename_allowed?
|
57
|
+
end
|
58
58
|
|
59
|
-
|
60
|
-
|
59
|
+
yield source_range(processed_source.buffer, 1, 0), msg if msg
|
60
|
+
end
|
61
61
|
|
62
|
-
|
62
|
+
def perform_class_and_module_naming_checks(file_path, basename)
|
63
|
+
return unless expect_matching_definition?
|
64
|
+
|
65
|
+
if check_definition_path_hierarchy? &&
|
66
|
+
!matching_definition?(file_path)
|
67
|
+
msg = no_definition_message(basename, file_path)
|
68
|
+
elsif !matching_class?(basename)
|
69
|
+
msg = no_definition_message(basename, basename)
|
70
|
+
end
|
71
|
+
msg
|
63
72
|
end
|
64
73
|
|
65
74
|
def matching_definition?(file_path)
|
66
|
-
return true unless expect_matching_definition?
|
67
|
-
|
68
75
|
find_class_or_module(processed_source.ast, to_namespace(file_path))
|
69
76
|
end
|
70
77
|
|
78
|
+
def matching_class?(file_name)
|
79
|
+
find_class_or_module(processed_source.ast, to_namespace(file_name))
|
80
|
+
end
|
81
|
+
|
71
82
|
def bad_filename_allowed?
|
72
83
|
ignore_executable_scripts? && processed_source.start_with?('#!')
|
73
84
|
end
|
@@ -94,6 +105,10 @@ module RuboCop
|
|
94
105
|
cop_config['ExpectMatchingDefinition']
|
95
106
|
end
|
96
107
|
|
108
|
+
def check_definition_path_hierarchy?
|
109
|
+
cop_config['CheckDefinitionPathHierarchy']
|
110
|
+
end
|
111
|
+
|
97
112
|
def regex
|
98
113
|
cop_config['Regex']
|
99
114
|
end
|
@@ -104,14 +119,13 @@ module RuboCop
|
|
104
119
|
|
105
120
|
def filename_good?(basename)
|
106
121
|
basename = basename.sub(/^\./, '')
|
107
|
-
basename = basename.sub(/\.[
|
122
|
+
basename = basename.sub(/\.[^.]+$/, '')
|
108
123
|
# special handling for Action Pack Variants file names like
|
109
124
|
# some_file.xlsx+mobile.axlsx
|
110
125
|
basename = basename.sub('+', '_')
|
111
|
-
basename
|
126
|
+
basename.match?(regex || SNAKE_CASE)
|
112
127
|
end
|
113
128
|
|
114
|
-
# rubocop:disable Metrics/CyclomaticComplexity
|
115
129
|
def find_class_or_module(node, namespace)
|
116
130
|
return nil unless node
|
117
131
|
|
@@ -130,7 +144,6 @@ module RuboCop
|
|
130
144
|
|
131
145
|
nil
|
132
146
|
end
|
133
|
-
# rubocop:enable Metrics/CyclomaticComplexity
|
134
147
|
|
135
148
|
def match_namespace(node, namespace, expected)
|
136
149
|
match_partial = partial_matcher!(expected)
|
@@ -153,9 +166,7 @@ module RuboCop
|
|
153
166
|
|
154
167
|
namespace, name = *namespace
|
155
168
|
|
156
|
-
if name == expected.last || match_acronym?(expected.last, name)
|
157
|
-
expected.pop
|
158
|
-
end
|
169
|
+
expected.pop if name == expected.last || match_acronym?(expected.last, name)
|
159
170
|
end
|
160
171
|
|
161
172
|
false
|
@@ -28,6 +28,8 @@ module RuboCop
|
|
28
28
|
# def value?
|
29
29
|
# end
|
30
30
|
class PredicateName < Cop
|
31
|
+
include AllowedMethods
|
32
|
+
|
31
33
|
def_node_matcher :dynamic_method_define, <<~PATTERN
|
32
34
|
(send nil? #method_definition_macros
|
33
35
|
(sym $_)
|
@@ -67,10 +69,10 @@ module RuboCop
|
|
67
69
|
private
|
68
70
|
|
69
71
|
def allowed_method_name?(method_name, prefix)
|
70
|
-
!method_name.match(/^#{prefix}[^0-9]/) ||
|
72
|
+
!method_name.match?(/^#{prefix}[^0-9]/) ||
|
71
73
|
method_name == expected_name(method_name, prefix) ||
|
72
74
|
method_name.end_with?('=') ||
|
73
|
-
|
75
|
+
allowed_method?(method_name)
|
74
76
|
end
|
75
77
|
|
76
78
|
def expected_name(method_name, prefix)
|
@@ -95,10 +97,6 @@ module RuboCop
|
|
95
97
|
cop_config['NamePrefix']
|
96
98
|
end
|
97
99
|
|
98
|
-
def allowed_methods
|
99
|
-
cop_config['AllowedMethods']
|
100
|
-
end
|
101
|
-
|
102
100
|
def method_definition_macros(macro_name)
|
103
101
|
cop_config['MethodDefinitionMacros'].include?(macro_name.to_s)
|
104
102
|
end
|
data/lib/rubocop/cop/offense.rb
CHANGED
@@ -54,14 +54,28 @@ module RuboCop
|
|
54
54
|
# @api private
|
55
55
|
attr_reader :status
|
56
56
|
|
57
|
+
# @api public
|
58
|
+
#
|
59
|
+
# @!attribute [r] corrector
|
60
|
+
#
|
61
|
+
# @return [Corrector | nil]
|
62
|
+
# the autocorrection for this offense, or `nil` when not available
|
63
|
+
attr_reader :corrector
|
64
|
+
|
65
|
+
PseudoSourceRange = Struct.new(:line, :column, :source_line, :begin_pos,
|
66
|
+
:end_pos)
|
67
|
+
|
68
|
+
NO_LOCATION = PseudoSourceRange.new(1, 0, '', 0, 1).freeze
|
69
|
+
|
57
70
|
# @api private
|
58
|
-
def initialize(severity, location, message, cop_name,
|
59
|
-
status = :uncorrected)
|
71
|
+
def initialize(severity, location, message, cop_name, # rubocop:disable Metrics/ParameterLists
|
72
|
+
status = :uncorrected, corrector = nil)
|
60
73
|
@severity = RuboCop::Cop::Severity.new(severity)
|
61
74
|
@location = location
|
62
75
|
@message = message.freeze
|
63
76
|
@cop_name = cop_name.freeze
|
64
77
|
@status = status
|
78
|
+
@corrector = corrector
|
65
79
|
freeze
|
66
80
|
end
|
67
81
|
|
data/lib/rubocop/cop/registry.rb
CHANGED
@@ -22,34 +22,42 @@ module RuboCop
|
|
22
22
|
|
23
23
|
# Registry that tracks all cops by their badge and department.
|
24
24
|
class Registry
|
25
|
+
include Enumerable
|
26
|
+
|
27
|
+
attr_reader :options
|
28
|
+
|
25
29
|
def initialize(cops = [], options = {})
|
26
30
|
@registry = {}
|
27
31
|
@departments = {}
|
28
32
|
@cops_by_cop_name = Hash.new { |hash, key| hash[key] = [] }
|
29
33
|
|
30
|
-
|
34
|
+
@enrollment_queue = cops
|
31
35
|
@options = options
|
32
36
|
end
|
33
37
|
|
34
38
|
def enlist(cop)
|
35
|
-
@
|
36
|
-
|
37
|
-
|
38
|
-
|
39
|
+
@enrollment_queue << cop
|
40
|
+
end
|
41
|
+
|
42
|
+
def dismiss(cop)
|
43
|
+
raise "Cop #{cop} could not be dismissed" unless @enrollment_queue.delete(cop)
|
39
44
|
end
|
40
45
|
|
41
46
|
# @return [Array<Symbol>] list of departments for current cops.
|
42
47
|
def departments
|
48
|
+
clear_enrollment_queue
|
43
49
|
@departments.keys
|
44
50
|
end
|
45
51
|
|
46
52
|
# @return [Registry] Cops for that specific department.
|
47
53
|
def with_department(department)
|
54
|
+
clear_enrollment_queue
|
48
55
|
with(@departments.fetch(department, []))
|
49
56
|
end
|
50
57
|
|
51
58
|
# @return [Registry] Cops not for a specific department.
|
52
59
|
def without_department(department)
|
60
|
+
clear_enrollment_queue
|
53
61
|
without_department = @departments.dup
|
54
62
|
without_department.delete(department)
|
55
63
|
|
@@ -94,9 +102,7 @@ module RuboCop
|
|
94
102
|
# @return [String] Qualified cop name
|
95
103
|
def qualified_cop_name(name, path, shall_warn = true)
|
96
104
|
badge = Badge.parse(name)
|
97
|
-
if shall_warn && department_missing?(badge, name)
|
98
|
-
print_warning(name, path)
|
99
|
-
end
|
105
|
+
print_warning(name, path) if shall_warn && department_missing?(badge, name)
|
100
106
|
return name if registered?(badge)
|
101
107
|
|
102
108
|
potential_badges = qualify_badge(badge)
|
@@ -121,6 +127,7 @@ module RuboCop
|
|
121
127
|
end
|
122
128
|
|
123
129
|
def unqualified_cop_names
|
130
|
+
clear_enrollment_queue
|
124
131
|
@unqualified_cop_names ||=
|
125
132
|
Set.new(@cops_by_cop_name.keys.map { |qn| File.basename(qn) }) <<
|
126
133
|
'RedundantCopDisableDirective'
|
@@ -128,18 +135,21 @@ module RuboCop
|
|
128
135
|
|
129
136
|
# @return [Hash{String => Array<Class>}]
|
130
137
|
def to_h
|
138
|
+
clear_enrollment_queue
|
131
139
|
@cops_by_cop_name
|
132
140
|
end
|
133
141
|
|
134
142
|
def cops
|
143
|
+
clear_enrollment_queue
|
135
144
|
@registry.values
|
136
145
|
end
|
137
146
|
|
138
147
|
def length
|
148
|
+
clear_enrollment_queue
|
139
149
|
@registry.size
|
140
150
|
end
|
141
151
|
|
142
|
-
def enabled(config, only, only_safe = false)
|
152
|
+
def enabled(config, only = [], only_safe = false)
|
143
153
|
select do |cop|
|
144
154
|
only.include?(cop.cop_name) || enabled?(cop, config, only_safe)
|
145
155
|
end
|
@@ -174,6 +184,7 @@ module RuboCop
|
|
174
184
|
end
|
175
185
|
|
176
186
|
def sort!
|
187
|
+
clear_enrollment_queue
|
177
188
|
@registry = Hash[@registry.sort_by { |badge, _| badge.cop_name }]
|
178
189
|
|
179
190
|
self
|
@@ -190,16 +201,57 @@ module RuboCop
|
|
190
201
|
# @param [String] cop_name
|
191
202
|
# @return [Class, nil]
|
192
203
|
def find_by_cop_name(cop_name)
|
193
|
-
|
204
|
+
to_h[cop_name].first
|
205
|
+
end
|
206
|
+
|
207
|
+
@global = new
|
208
|
+
|
209
|
+
class << self
|
210
|
+
attr_reader :global
|
211
|
+
end
|
212
|
+
|
213
|
+
def self.all
|
214
|
+
global.without_department(:Test).cops
|
215
|
+
end
|
216
|
+
|
217
|
+
def self.qualified_cop_name(name, origin)
|
218
|
+
global.qualified_cop_name(name, origin)
|
219
|
+
end
|
220
|
+
|
221
|
+
# Changes momentarily the global registry
|
222
|
+
# Intended for testing purposes
|
223
|
+
def self.with_temporary_global(temp_global = global.dup)
|
224
|
+
previous = @global
|
225
|
+
@global = temp_global
|
226
|
+
yield
|
227
|
+
ensure
|
228
|
+
@global = previous
|
194
229
|
end
|
195
230
|
|
196
231
|
private
|
197
232
|
|
233
|
+
def initialize_copy(reg)
|
234
|
+
initialize(reg.cops, reg.options)
|
235
|
+
end
|
236
|
+
|
237
|
+
def clear_enrollment_queue
|
238
|
+
return if @enrollment_queue.empty?
|
239
|
+
|
240
|
+
@enrollment_queue.each do |cop|
|
241
|
+
@registry[cop.badge] = cop
|
242
|
+
@departments[cop.department] ||= []
|
243
|
+
@departments[cop.department] << cop
|
244
|
+
@cops_by_cop_name[cop.cop_name] << cop
|
245
|
+
end
|
246
|
+
@enrollment_queue = []
|
247
|
+
end
|
248
|
+
|
198
249
|
def with(cops)
|
199
250
|
self.class.new(cops)
|
200
251
|
end
|
201
252
|
|
202
253
|
def qualify_badge(badge)
|
254
|
+
clear_enrollment_queue
|
203
255
|
@departments
|
204
256
|
.map { |department, _| badge.with_department(department) }
|
205
257
|
.select { |potential_badge| registered?(potential_badge) }
|
@@ -216,6 +268,7 @@ module RuboCop
|
|
216
268
|
end
|
217
269
|
|
218
270
|
def registered?(badge)
|
271
|
+
clear_enrollment_queue
|
219
272
|
@registry.key?(badge)
|
220
273
|
end
|
221
274
|
end
|
data/lib/rubocop/cop/severity.rb
CHANGED
@@ -31,9 +31,7 @@ module RuboCop
|
|
31
31
|
# @api private
|
32
32
|
def initialize(name_or_code)
|
33
33
|
name = Severity.name_from_code(name_or_code)
|
34
|
-
unless NAMES.include?(name)
|
35
|
-
raise ArgumentError, "Unknown severity: #{name}"
|
36
|
-
end
|
34
|
+
raise ArgumentError, "Unknown severity: #{name}" unless NAMES.include?(name)
|
37
35
|
|
38
36
|
@name = name.freeze
|
39
37
|
freeze
|
@@ -0,0 +1,136 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop checks for grouping of accessors in `class` and `module` bodies.
|
7
|
+
# By default it enforces accessors to be placed in grouped declarations,
|
8
|
+
# but it can be configured to enforce separating them in multiple declarations.
|
9
|
+
#
|
10
|
+
# @example EnforcedStyle: grouped (default)
|
11
|
+
# # bad
|
12
|
+
# class Foo
|
13
|
+
# attr_reader :bar
|
14
|
+
# attr_reader :baz
|
15
|
+
# end
|
16
|
+
#
|
17
|
+
# # good
|
18
|
+
# class Foo
|
19
|
+
# attr_reader :bar, :baz
|
20
|
+
# end
|
21
|
+
#
|
22
|
+
# @example EnforcedStyle: separated
|
23
|
+
# # bad
|
24
|
+
# class Foo
|
25
|
+
# attr_reader :bar, :baz
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# # good
|
29
|
+
# class Foo
|
30
|
+
# attr_reader :bar
|
31
|
+
# attr_reader :baz
|
32
|
+
# end
|
33
|
+
#
|
34
|
+
class AccessorGrouping < Cop
|
35
|
+
include ConfigurableEnforcedStyle
|
36
|
+
|
37
|
+
GROUPED_MSG = 'Group together all `%<accessor>s` attributes.'
|
38
|
+
SEPARATED_MSG = 'Use one attribute per `%<accessor>s`.'
|
39
|
+
|
40
|
+
ACCESSOR_METHODS = %i[attr_reader attr_writer attr_accessor attr].freeze
|
41
|
+
|
42
|
+
def on_class(node)
|
43
|
+
class_send_elements(node).each do |macro|
|
44
|
+
next unless accessor?(macro)
|
45
|
+
|
46
|
+
check(macro)
|
47
|
+
end
|
48
|
+
end
|
49
|
+
alias on_module on_class
|
50
|
+
|
51
|
+
def autocorrect(node)
|
52
|
+
lambda do |corrector|
|
53
|
+
corrector.replace(node, correction(node))
|
54
|
+
end
|
55
|
+
end
|
56
|
+
|
57
|
+
private
|
58
|
+
|
59
|
+
def class_send_elements(class_node)
|
60
|
+
class_def = class_node.body
|
61
|
+
|
62
|
+
if !class_def || class_def.def_type?
|
63
|
+
[]
|
64
|
+
elsif class_def.send_type?
|
65
|
+
[class_def]
|
66
|
+
else
|
67
|
+
class_def.each_child_node(:send).to_a
|
68
|
+
end
|
69
|
+
end
|
70
|
+
|
71
|
+
def accessor?(send_node)
|
72
|
+
send_node.macro? && ACCESSOR_METHODS.include?(send_node.method_name)
|
73
|
+
end
|
74
|
+
|
75
|
+
def check(send_node)
|
76
|
+
if grouped_style? && sibling_accessors(send_node).size > 1
|
77
|
+
add_offense(send_node)
|
78
|
+
elsif separated_style? && send_node.arguments.size > 1
|
79
|
+
add_offense(send_node)
|
80
|
+
end
|
81
|
+
end
|
82
|
+
|
83
|
+
def grouped_style?
|
84
|
+
style == :grouped
|
85
|
+
end
|
86
|
+
|
87
|
+
def separated_style?
|
88
|
+
style == :separated
|
89
|
+
end
|
90
|
+
|
91
|
+
def sibling_accessors(send_node)
|
92
|
+
send_node.parent.each_child_node(:send).select do |sibling|
|
93
|
+
sibling.macro? && sibling.method?(send_node.method_name)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
97
|
+
def message(send_node)
|
98
|
+
msg = grouped_style? ? GROUPED_MSG : SEPARATED_MSG
|
99
|
+
format(msg, accessor: send_node.method_name)
|
100
|
+
end
|
101
|
+
|
102
|
+
def correction(node)
|
103
|
+
if grouped_style?
|
104
|
+
accessors = sibling_accessors(node)
|
105
|
+
if node == accessors.first
|
106
|
+
group_accessors(node, accessors)
|
107
|
+
else
|
108
|
+
''
|
109
|
+
end
|
110
|
+
else
|
111
|
+
separate_accessors(node)
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def group_accessors(node, accessors)
|
116
|
+
accessor_names = accessors.flat_map do |accessor|
|
117
|
+
accessor.arguments.map(&:source)
|
118
|
+
end
|
119
|
+
|
120
|
+
"#{node.method_name} #{accessor_names.join(', ')}"
|
121
|
+
end
|
122
|
+
|
123
|
+
def separate_accessors(node)
|
124
|
+
node.arguments.map do |arg|
|
125
|
+
if arg == node.arguments.first
|
126
|
+
"#{node.method_name} #{arg.source}"
|
127
|
+
else
|
128
|
+
indent = ' ' * node.loc.column
|
129
|
+
"#{indent}#{node.method_name} #{arg.source}"
|
130
|
+
end
|
131
|
+
end.join("\n")
|
132
|
+
end
|
133
|
+
end
|
134
|
+
end
|
135
|
+
end
|
136
|
+
end
|