rubocop 0.80.0 → 0.84.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +4 -4
- data/config/default.yml +171 -34
- data/lib/rubocop.rb +15 -62
- data/lib/rubocop/ast_aliases.rb +8 -0
- data/lib/rubocop/cli.rb +11 -5
- data/lib/rubocop/cli/command/show_cops.rb +2 -6
- data/lib/rubocop/config.rb +41 -13
- data/lib/rubocop/config_loader.rb +40 -36
- data/lib/rubocop/config_loader_resolver.rb +27 -4
- data/lib/rubocop/config_obsoletion.rb +2 -0
- data/lib/rubocop/config_validator.rb +18 -1
- data/lib/rubocop/cop/autocorrect_logic.rb +1 -2
- data/lib/rubocop/cop/badge.rb +5 -5
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +1 -1
- data/lib/rubocop/cop/corrector.rb +49 -27
- data/lib/rubocop/cop/correctors/alignment_corrector.rb +4 -8
- data/lib/rubocop/cop/correctors/condition_corrector.rb +1 -2
- data/lib/rubocop/cop/correctors/empty_line_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +3 -3
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/parentheses_corrector.rb +1 -3
- data/lib/rubocop/cop/correctors/percent_literal_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/space_corrector.rb +1 -3
- data/lib/rubocop/cop/correctors/string_literal_corrector.rb +2 -2
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -3
- data/lib/rubocop/cop/generator.rb +4 -3
- data/lib/rubocop/cop/ignored_node.rb +1 -3
- data/lib/rubocop/cop/internal_affairs/offense_location_keyword.rb +1 -1
- data/lib/rubocop/cop/layout/array_alignment.rb +53 -10
- data/lib/rubocop/cop/layout/block_end_newline.rb +5 -3
- data/lib/rubocop/cop/layout/condition_position.rb +12 -2
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/else_alignment.rb +8 -0
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +2 -6
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +126 -0
- data/lib/rubocop/cop/layout/end_of_line.rb +2 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +0 -2
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +14 -10
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_method_argument_line_break.rb +1 -3
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +4 -8
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +2 -2
- data/lib/rubocop/cop/layout/{tab.rb → indentation_style.rb} +48 -6
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -3
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +7 -4
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +2 -4
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +1 -3
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +13 -4
- data/lib/rubocop/cop/layout/space_around_block_parameters.rb +3 -3
- data/lib/rubocop/cop/layout/space_around_method_call_operator.rb +129 -0
- data/lib/rubocop/cop/layout/space_around_operators.rb +19 -2
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -3
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_array_literal_brackets.rb +1 -3
- data/lib/rubocop/cop/layout/space_inside_block_braces.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_range_literal.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +2 -6
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +2 -2
- data/lib/rubocop/cop/lint/ambiguous_operator.rb +40 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +14 -0
- data/lib/rubocop/cop/lint/boolean_symbol.rb +12 -0
- data/lib/rubocop/cop/lint/debugger.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +133 -0
- data/lib/rubocop/cop/lint/duplicate_methods.rb +1 -5
- data/lib/rubocop/cop/lint/empty_when.rb +29 -6
- data/lib/rubocop/cop/lint/ensure_return.rb +18 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +3 -7
- data/lib/rubocop/cop/lint/implicit_string_concatenation.rb +1 -1
- data/lib/rubocop/cop/lint/inherit_exception.rb +1 -1
- data/lib/rubocop/cop/lint/interpolation_check.rb +1 -1
- data/lib/rubocop/cop/lint/literal_as_condition.rb +10 -13
- data/lib/rubocop/cop/lint/literal_in_interpolation.rb +1 -1
- data/lib/rubocop/cop/lint/loop.rb +6 -4
- data/lib/rubocop/cop/lint/multiple_comparison.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +2 -2
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +2 -2
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +31 -11
- data/lib/rubocop/cop/lint/percent_string_array.rb +2 -4
- data/lib/rubocop/cop/lint/raise_exception.rb +75 -0
- data/lib/rubocop/cop/lint/redundant_cop_disable_directive.rb +1 -6
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +1 -1
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +1 -1
- data/lib/rubocop/cop/lint/struct_new_override.rb +58 -0
- data/lib/rubocop/cop/lint/suppressed_exception.rb +20 -25
- data/lib/rubocop/cop/lint/syntax.rb +1 -3
- data/lib/rubocop/cop/lint/unified_integer.rb +0 -2
- data/lib/rubocop/cop/lint/unused_method_argument.rb +32 -6
- data/lib/rubocop/cop/lint/uri_regexp.rb +4 -4
- data/lib/rubocop/cop/lint/useless_access_modifier.rb +13 -3
- data/lib/rubocop/cop/lint/useless_assignment.rb +3 -2
- data/lib/rubocop/cop/lint/useless_else_without_rescue.rb +5 -0
- data/lib/rubocop/cop/migration/department_name.rb +21 -12
- data/lib/rubocop/cop/mixin/alignment.rb +1 -3
- data/lib/rubocop/cop/mixin/array_min_size.rb +2 -6
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +4 -12
- data/lib/rubocop/cop/mixin/configurable_enforced_style.rb +1 -3
- data/lib/rubocop/cop/mixin/configurable_formatting.rb +1 -3
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +6 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +10 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -3
- data/lib/rubocop/cop/mixin/line_length_help.rb +2 -1
- data/lib/rubocop/cop/mixin/method_complexity.rb +5 -0
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -3
- data/lib/rubocop/cop/mixin/parser_diagnostic.rb +1 -1
- data/lib/rubocop/cop/mixin/statement_modifier.rb +7 -22
- data/lib/rubocop/cop/mixin/surrounding_space.rb +1 -3
- data/lib/rubocop/cop/mixin/target_ruby_version.rb +5 -1
- data/lib/rubocop/cop/mixin/trailing_comma.rb +2 -4
- data/lib/rubocop/cop/naming/class_and_module_camel_case.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +2 -1
- data/lib/rubocop/cop/naming/file_name.rb +1 -3
- data/lib/rubocop/cop/naming/heredoc_delimiter_naming.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +26 -0
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +1 -1
- data/lib/rubocop/cop/registry.rb +13 -10
- data/lib/rubocop/cop/severity.rb +1 -3
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +26 -6
- data/lib/rubocop/cop/style/alias.rb +4 -4
- data/lib/rubocop/cop/style/and_or.rb +7 -8
- data/lib/rubocop/cop/style/array_join.rb +1 -1
- data/lib/rubocop/cop/style/attr.rb +1 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +2 -8
- data/lib/rubocop/cop/style/case_equality.rb +24 -1
- data/lib/rubocop/cop/style/character_literal.rb +2 -2
- data/lib/rubocop/cop/style/collection_methods.rb +2 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +9 -11
- data/lib/rubocop/cop/style/copyright.rb +1 -1
- data/lib/rubocop/cop/style/dir.rb +1 -1
- data/lib/rubocop/cop/style/disable_cops_within_source_code_directive.rb +49 -0
- data/lib/rubocop/cop/style/documentation.rb +43 -5
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -1
- data/lib/rubocop/cop/style/double_negation.rb +41 -4
- data/lib/rubocop/cop/style/each_for_simple_loop.rb +1 -1
- data/lib/rubocop/cop/style/each_with_object.rb +3 -3
- data/lib/rubocop/cop/style/empty_literal.rb +1 -3
- data/lib/rubocop/cop/style/empty_method.rb +1 -5
- data/lib/rubocop/cop/style/end_block.rb +6 -0
- data/lib/rubocop/cop/style/even_odd.rb +1 -1
- data/lib/rubocop/cop/style/expand_path_arguments.rb +3 -3
- data/lib/rubocop/cop/style/exponential_notation.rb +119 -0
- data/lib/rubocop/cop/style/format_string.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +2 -4
- data/lib/rubocop/cop/style/guard_clause.rb +25 -2
- data/lib/rubocop/cop/style/hash_each_methods.rb +5 -3
- data/lib/rubocop/cop/style/hash_syntax.rb +15 -10
- data/lib/rubocop/cop/style/hash_transform_keys.rb +6 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +6 -5
- data/lib/rubocop/cop/style/if_unless_modifier.rb +23 -3
- data/lib/rubocop/cop/style/if_with_semicolon.rb +16 -0
- data/lib/rubocop/cop/style/inverse_methods.rb +9 -5
- data/lib/rubocop/cop/style/lambda.rb +3 -2
- data/lib/rubocop/cop/style/lambda_call.rb +1 -21
- 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/mixin_grouping.rb +1 -1
- data/lib/rubocop/cop/style/module_function.rb +58 -12
- data/lib/rubocop/cop/style/multiline_if_modifier.rb +1 -1
- data/lib/rubocop/cop/style/multiline_memoization.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +16 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +2 -4
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +4 -4
- data/lib/rubocop/cop/style/next.rb +2 -2
- data/lib/rubocop/cop/style/nil_comparison.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +4 -4
- data/lib/rubocop/cop/style/not.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literal_prefix.rb +1 -1
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/numeric_predicate.rb +1 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +6 -9
- data/lib/rubocop/cop/style/optional_arguments.rb +1 -1
- data/lib/rubocop/cop/style/or_assignment.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +1 -1
- data/lib/rubocop/cop/style/perl_backrefs.rb +2 -2
- data/lib/rubocop/cop/style/proc.rb +1 -1
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/random_with_offset.rb +3 -3
- data/lib/rubocop/cop/style/redundant_condition.rb +18 -6
- data/lib/rubocop/cop/style/redundant_conditional.rb +1 -1
- data/lib/rubocop/cop/style/redundant_exception.rb +3 -3
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -2
- data/lib/rubocop/cop/style/redundant_parentheses.rb +2 -6
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -2
- data/lib/rubocop/cop/style/redundant_return.rb +5 -7
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/rescue_modifier.rb +1 -1
- data/lib/rubocop/cop/style/return_nil.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +3 -7
- data/lib/rubocop/cop/style/self_assignment.rb +1 -1
- data/lib/rubocop/cop/style/slicing_with_range.rb +39 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +3 -7
- data/lib/rubocop/cop/style/stabby_lambda_parentheses.rb +1 -4
- data/lib/rubocop/cop/style/string_hash_keys.rb +1 -1
- data/lib/rubocop/cop/style/symbol_array.rb +1 -1
- data/lib/rubocop/cop/style/symbol_literal.rb +2 -2
- data/lib/rubocop/cop/style/ternary_parentheses.rb +2 -5
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +35 -0
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +41 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +88 -0
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +44 -0
- data/lib/rubocop/cop/style/trailing_underscore_variable.rb +2 -6
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/unpack_first.rb +0 -4
- data/lib/rubocop/cop/style/variable_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/while_until_modifier.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +1 -1
- data/lib/rubocop/cop/style/zero_length_predicate.rb +1 -1
- data/lib/rubocop/cop/util.rb +24 -0
- data/lib/rubocop/cop/variable_force.rb +3 -9
- data/lib/rubocop/cop/variable_force/assignment.rb +1 -0
- data/lib/rubocop/cop/variable_force/branch.rb +2 -6
- data/lib/rubocop/cop/variable_force/scope.rb +1 -0
- data/lib/rubocop/cop/variable_force/variable.rb +3 -6
- data/lib/rubocop/ext/processed_source.rb +18 -0
- data/lib/rubocop/formatter/base_formatter.rb +0 -4
- data/lib/rubocop/formatter/clang_style_formatter.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +4 -12
- data/lib/rubocop/formatter/formatter_set.rb +1 -4
- data/lib/rubocop/formatter/junit_formatter.rb +17 -6
- data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
- data/lib/rubocop/formatter/tap_formatter.rb +1 -1
- data/lib/rubocop/name_similarity.rb +12 -9
- data/lib/rubocop/options.rb +20 -13
- data/lib/rubocop/remote_config.rb +1 -3
- data/lib/rubocop/result_cache.rb +1 -3
- data/lib/rubocop/rspec/cop_helper.rb +2 -4
- data/lib/rubocop/rspec/expect_offense.rb +4 -10
- data/lib/rubocop/rspec/shared_contexts.rb +54 -20
- data/lib/rubocop/runner.rb +15 -12
- data/lib/rubocop/target_finder.rb +5 -7
- data/lib/rubocop/target_ruby.rb +2 -2
- data/lib/rubocop/version.rb +5 -3
- metadata +32 -77
- data/lib/rubocop/ast/builder.rb +0 -83
- data/lib/rubocop/ast/node.rb +0 -632
- 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 -57
- data/lib/rubocop/ast/node/block_node.rb +0 -117
- data/lib/rubocop/ast/node/break_node.rb +0 -17
- 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 -261
- 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 -13
- 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 -200
- data/lib/rubocop/cop/lint/end_in_method.rb +0 -40
- data/lib/rubocop/formatter/disabled_lines_formatter.rb +0 -57
- data/lib/rubocop/node_pattern.rb +0 -887
- data/lib/rubocop/processed_source.rb +0 -216
- data/lib/rubocop/string_util.rb +0 -14
- data/lib/rubocop/token.rb +0 -114
@@ -48,15 +48,15 @@ module RuboCop
|
|
48
48
|
|
49
49
|
first_arg, second_arg = *node.arguments
|
50
50
|
|
51
|
-
corrector.replace(first_arg
|
52
|
-
corrector.replace(second_arg
|
51
|
+
corrector.replace(first_arg, second_arg.source)
|
52
|
+
corrector.replace(second_arg, first_arg.source)
|
53
53
|
|
54
54
|
return_value = return_value(node.body)
|
55
55
|
|
56
56
|
if return_value_occupies_whole_line?(return_value)
|
57
57
|
corrector.remove(whole_line_expression(return_value))
|
58
58
|
else
|
59
|
-
corrector.remove(return_value
|
59
|
+
corrector.remove(return_value)
|
60
60
|
end
|
61
61
|
end
|
62
62
|
end
|
@@ -68,9 +68,7 @@ module RuboCop
|
|
68
68
|
|
69
69
|
def first_argument_unparenthesized?(node)
|
70
70
|
parent = node.parent
|
71
|
-
unless parent && %i[send super zsuper].include?(parent.type)
|
72
|
-
return false
|
73
|
-
end
|
71
|
+
return false unless parent && %i[send super zsuper].include?(parent.type)
|
74
72
|
|
75
73
|
node.object_id == parent.arguments.first.object_id &&
|
76
74
|
!parentheses?(node.parent)
|
@@ -57,7 +57,7 @@ module RuboCop
|
|
57
57
|
|
58
58
|
def autocorrect(node)
|
59
59
|
lambda do |corrector|
|
60
|
-
corrector.replace(node
|
60
|
+
corrector.replace(node, corrected(node))
|
61
61
|
end
|
62
62
|
end
|
63
63
|
|
@@ -90,10 +90,6 @@ module RuboCop
|
|
90
90
|
compact_style? ? '; ' : "\n#{indent}"
|
91
91
|
end
|
92
92
|
|
93
|
-
def comment_lines?(node)
|
94
|
-
processed_source[line_range(node)].any? { |line| comment_line?(line) }
|
95
|
-
end
|
96
|
-
|
97
93
|
def compact?(node)
|
98
94
|
node.single_line?
|
99
95
|
end
|
@@ -38,7 +38,7 @@ module RuboCop
|
|
38
38
|
replacement_method = replacement_method(arg, method)
|
39
39
|
|
40
40
|
correction = "#{base_number.source}.#{replacement_method}?"
|
41
|
-
->(corrector) { corrector.replace(node
|
41
|
+
->(corrector) { corrector.replace(node, correction) }
|
42
42
|
end
|
43
43
|
end
|
44
44
|
|
@@ -97,7 +97,7 @@ module RuboCop
|
|
97
97
|
autocorrect_expand_path(corrector, current_path, default_dir)
|
98
98
|
elsif (default_dir = pathname_parent_expand_path(node)) ||
|
99
99
|
(default_dir = pathname_new_parent_expand_path(node))
|
100
|
-
corrector.replace(default_dir
|
100
|
+
corrector.replace(default_dir, '__dir__')
|
101
101
|
remove_parent_method(corrector, default_dir)
|
102
102
|
end
|
103
103
|
end
|
@@ -145,8 +145,8 @@ module RuboCop
|
|
145
145
|
else
|
146
146
|
new_path = "'#{parent_path(stripped_current_path)}'"
|
147
147
|
|
148
|
-
corrector.replace(current_path
|
149
|
-
corrector.replace(default_dir
|
148
|
+
corrector.replace(current_path, new_path)
|
149
|
+
corrector.replace(default_dir, '__dir__')
|
150
150
|
end
|
151
151
|
end
|
152
152
|
|
@@ -0,0 +1,119 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Style
|
6
|
+
# This cop enforces consistency when using exponential notation
|
7
|
+
# for numbers in the code (eg 1.2e4). Different styles are supported:
|
8
|
+
# - `scientific` which enforces a mantissa between 1 (inclusive)
|
9
|
+
# and 10 (exclusive).
|
10
|
+
# - `engineering` which enforces the exponent to be a multiple of 3
|
11
|
+
# and the mantissa to be between 0.1 (inclusive)
|
12
|
+
# and 10 (exclusive).
|
13
|
+
# - `integral` which enforces the mantissa to always be a whole number
|
14
|
+
# without trailing zeroes.
|
15
|
+
#
|
16
|
+
# @example EnforcedStyle: scientific (default)
|
17
|
+
# # Enforces a mantissa between 1 (inclusive) and 10 (exclusive).
|
18
|
+
#
|
19
|
+
# # bad
|
20
|
+
# 10e6
|
21
|
+
# 0.3e4
|
22
|
+
# 11.7e5
|
23
|
+
# 3.14e0
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# 1e7
|
27
|
+
# 3e3
|
28
|
+
# 1.17e6
|
29
|
+
# 3.14
|
30
|
+
#
|
31
|
+
# @example EnforcedStyle: engineering
|
32
|
+
# # Enforces using multiple of 3 exponents,
|
33
|
+
# # mantissa should be between 0.1 (inclusive) and 1000 (exclusive)
|
34
|
+
#
|
35
|
+
# # bad
|
36
|
+
# 3.2e7
|
37
|
+
# 0.1e5
|
38
|
+
# 12e5
|
39
|
+
# 1232e6
|
40
|
+
#
|
41
|
+
# # good
|
42
|
+
# 32e6
|
43
|
+
# 10e3
|
44
|
+
# 1.2e6
|
45
|
+
# 1.232e9
|
46
|
+
#
|
47
|
+
# @example EnforcedStyle: integral
|
48
|
+
# # Enforces the mantissa to have no decimal part and no
|
49
|
+
# # trailing zeroes.
|
50
|
+
#
|
51
|
+
# # bad
|
52
|
+
# 3.2e7
|
53
|
+
# 0.1e5
|
54
|
+
# 120e4
|
55
|
+
#
|
56
|
+
# # good
|
57
|
+
# 32e6
|
58
|
+
# 1e4
|
59
|
+
# 12e5
|
60
|
+
#
|
61
|
+
class ExponentialNotation < Cop
|
62
|
+
include ConfigurableEnforcedStyle
|
63
|
+
|
64
|
+
def on_float(node)
|
65
|
+
add_offense(node) if offense?(node)
|
66
|
+
end
|
67
|
+
|
68
|
+
private
|
69
|
+
|
70
|
+
def scientific?(node)
|
71
|
+
mantissa, = node.source.split('e')
|
72
|
+
mantissa =~ /^-?[1-9](\.\d*[0-9])?$/
|
73
|
+
end
|
74
|
+
|
75
|
+
def engineering?(node)
|
76
|
+
mantissa, exponent = node.source.split('e')
|
77
|
+
return false unless /^-?\d+$/.match?(exponent)
|
78
|
+
return false unless (exponent.to_i % 3).zero?
|
79
|
+
return false if /^-?\d{4}/.match?(mantissa)
|
80
|
+
return false if /^-?0\d/.match?(mantissa)
|
81
|
+
return false if /^-?0.0/.match?(mantissa)
|
82
|
+
|
83
|
+
true
|
84
|
+
end
|
85
|
+
|
86
|
+
def integral(node)
|
87
|
+
mantissa, = node.source.split('e')
|
88
|
+
mantissa =~ /^-?[1-9](\d*[1-9])?$/
|
89
|
+
end
|
90
|
+
|
91
|
+
def offense?(node)
|
92
|
+
return false unless node.source['e']
|
93
|
+
|
94
|
+
case style
|
95
|
+
when :scientific
|
96
|
+
!scientific?(node)
|
97
|
+
when :engineering
|
98
|
+
!engineering?(node)
|
99
|
+
when :integral
|
100
|
+
!integral(node)
|
101
|
+
else
|
102
|
+
false
|
103
|
+
end
|
104
|
+
end
|
105
|
+
|
106
|
+
def message(_node)
|
107
|
+
case style
|
108
|
+
when :scientific
|
109
|
+
'Use a mantissa in [1, 10[.'
|
110
|
+
when :engineering
|
111
|
+
'Use an exponent divisible by 3 and a mantissa in [0.1, 1000[.'
|
112
|
+
when :integral
|
113
|
+
'Use an integer as mantissa, without trailing zero.'
|
114
|
+
end
|
115
|
+
end
|
116
|
+
end
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
@@ -104,7 +104,7 @@ module RuboCop
|
|
104
104
|
|
105
105
|
corrected = "#{style}(#{node.receiver.source}, #{args})"
|
106
106
|
|
107
|
-
corrector.replace(node
|
107
|
+
corrector.replace(node, corrected)
|
108
108
|
end
|
109
109
|
|
110
110
|
def autocorrect_to_percent(corrector, node)
|
@@ -119,7 +119,7 @@ module RuboCop
|
|
119
119
|
"[#{param_args.map(&:source).join(', ')}]"
|
120
120
|
end
|
121
121
|
|
122
|
-
corrector.replace(node
|
122
|
+
corrector.replace(node, "#{format} % #{args}")
|
123
123
|
end
|
124
124
|
end
|
125
125
|
end
|
@@ -149,9 +149,7 @@ module RuboCop
|
|
149
149
|
end
|
150
150
|
|
151
151
|
next_token = processed_source.tokens[token_number]
|
152
|
-
if next_token && next_token.text =~ Encoding::ENCODING_PATTERN
|
153
|
-
token = next_token
|
154
|
-
end
|
152
|
+
token = next_token if next_token && next_token.text =~ Encoding::ENCODING_PATTERN
|
155
153
|
|
156
154
|
token
|
157
155
|
end
|
@@ -216,7 +214,7 @@ module RuboCop
|
|
216
214
|
if comment
|
217
215
|
corrector.insert_after(line_range(comment.line), following_comment)
|
218
216
|
else
|
219
|
-
corrector.insert_before(
|
217
|
+
corrector.insert_before(processed_source.buffer.source_range, preceding_comment)
|
220
218
|
end
|
221
219
|
end
|
222
220
|
|
@@ -35,6 +35,17 @@ module RuboCop
|
|
35
35
|
# # good
|
36
36
|
# raise 'exception' if something
|
37
37
|
# ok
|
38
|
+
#
|
39
|
+
# # bad
|
40
|
+
# if something
|
41
|
+
# foo || raise('exception')
|
42
|
+
# else
|
43
|
+
# ok
|
44
|
+
# end
|
45
|
+
#
|
46
|
+
# # good
|
47
|
+
# foo || raise('exception') if something
|
48
|
+
# ok
|
38
49
|
class GuardClause < Cop
|
39
50
|
include MinBodyLength
|
40
51
|
include StatementModifier
|
@@ -69,7 +80,8 @@ module RuboCop
|
|
69
80
|
else
|
70
81
|
opposite_keyword(node)
|
71
82
|
end
|
72
|
-
|
83
|
+
|
84
|
+
register_offense(node, guard_clause_source(guard_clause), kw)
|
73
85
|
end
|
74
86
|
|
75
87
|
private
|
@@ -98,13 +110,24 @@ module RuboCop
|
|
98
110
|
message: format(MSG, example: example))
|
99
111
|
end
|
100
112
|
|
113
|
+
def guard_clause_source(guard_clause)
|
114
|
+
parent = guard_clause.parent
|
115
|
+
|
116
|
+
if parent.and_type? || parent.or_type?
|
117
|
+
guard_clause.parent.source
|
118
|
+
else
|
119
|
+
guard_clause.source
|
120
|
+
end
|
121
|
+
end
|
122
|
+
|
101
123
|
def too_long_for_single_line?(node, example)
|
102
124
|
max = max_line_length
|
103
125
|
max && node.source_range.column + example.length > max
|
104
126
|
end
|
105
127
|
|
106
128
|
def accepted_form?(node, ending = false)
|
107
|
-
accepted_if?(node, ending) || node.condition.multiline?
|
129
|
+
accepted_if?(node, ending) || node.condition.multiline? ||
|
130
|
+
node.parent&.assignment?
|
108
131
|
end
|
109
132
|
|
110
133
|
def accepted_if?(node, ending)
|
@@ -40,6 +40,8 @@ module RuboCop
|
|
40
40
|
|
41
41
|
def register_kv_offense(node)
|
42
42
|
kv_each(node) do |target, method|
|
43
|
+
return unless target.receiver.receiver
|
44
|
+
|
43
45
|
msg = format(message, prefer: "each_#{method[0..-2]}",
|
44
46
|
current: "#{method}.each")
|
45
47
|
|
@@ -58,7 +60,7 @@ module RuboCop
|
|
58
60
|
end
|
59
61
|
|
60
62
|
def correct_implicit(node, corrector, method_name)
|
61
|
-
corrector.replace(node
|
63
|
+
corrector.replace(node, method_name)
|
62
64
|
correct_args(node, corrector)
|
63
65
|
end
|
64
66
|
|
@@ -68,14 +70,14 @@ module RuboCop
|
|
68
70
|
return correct_implicit(node, corrector, name) unless receiver
|
69
71
|
|
70
72
|
new_source = receiver.source + ".#{name}"
|
71
|
-
corrector.replace(node
|
73
|
+
corrector.replace(node, new_source)
|
72
74
|
end
|
73
75
|
|
74
76
|
def correct_args(node, corrector)
|
75
77
|
args = node.parent.arguments
|
76
78
|
name, = *args.children.find { |arg| used?(arg) }
|
77
79
|
|
78
|
-
corrector.replace(args
|
80
|
+
corrector.replace(args, "|#{name}|")
|
79
81
|
end
|
80
82
|
|
81
83
|
def kv_range(outer_node)
|
@@ -144,11 +144,11 @@ module RuboCop
|
|
144
144
|
# Prefer { :production? => false } over { production?: false } and
|
145
145
|
# similarly for other non-alnum final characters (except quotes,
|
146
146
|
# to prefer { "x y": 1 } over { :"x y" => 1 }).
|
147
|
-
return false unless
|
147
|
+
return false unless /[\p{Alnum}"']\z/.match?(sym_name)
|
148
148
|
end
|
149
149
|
|
150
150
|
# Most hash keys can be matched against a simple regex.
|
151
|
-
return true if
|
151
|
+
return true if /\A[_a-z]\w*[?!]?\z/i.match?(sym_name)
|
152
152
|
|
153
153
|
# For more complicated hash keys, let the parser validate the syntax.
|
154
154
|
parse("{ #{sym_name}: :foo }").valid_syntax?
|
@@ -168,11 +168,7 @@ module RuboCop
|
|
168
168
|
end
|
169
169
|
|
170
170
|
def autocorrect_ruby19(corrector, pair_node)
|
171
|
-
|
172
|
-
op = pair_node.loc.operator
|
173
|
-
|
174
|
-
range = key.join(op)
|
175
|
-
range = range_with_surrounding_space(range: range, side: :right)
|
171
|
+
range = range_for_autocorrect_ruby19(pair_node)
|
176
172
|
|
177
173
|
space = argument_without_space?(pair_node.parent) ? ' ' : ''
|
178
174
|
|
@@ -180,6 +176,17 @@ module RuboCop
|
|
180
176
|
range,
|
181
177
|
range.source.sub(/^:(.*\S)\s*=>\s*$/, space.to_s + '\1: ')
|
182
178
|
)
|
179
|
+
|
180
|
+
hash_node = pair_node.parent
|
181
|
+
corrector.wrap(hash_node, '{', '}') if hash_node.parent&.return_type? && !hash_node.braces?
|
182
|
+
end
|
183
|
+
|
184
|
+
def range_for_autocorrect_ruby19(pair_node)
|
185
|
+
key = pair_node.key.source_range
|
186
|
+
operator = pair_node.loc.operator
|
187
|
+
|
188
|
+
range = key.join(operator)
|
189
|
+
range_with_surrounding_space(range: range, side: :right)
|
183
190
|
end
|
184
191
|
|
185
192
|
def argument_without_space?(node)
|
@@ -188,11 +195,9 @@ module RuboCop
|
|
188
195
|
end
|
189
196
|
|
190
197
|
def autocorrect_hash_rockets(corrector, pair_node)
|
191
|
-
key = pair_node.key.source_range
|
192
198
|
op = pair_node.loc.operator
|
193
199
|
|
194
|
-
corrector.
|
195
|
-
corrector.insert_before(key, ':')
|
200
|
+
corrector.wrap(pair_node.key, ':', pair_node.inverse_delimiter(true))
|
196
201
|
corrector.remove(range_with_surrounding_space(range: op))
|
197
202
|
end
|
198
203
|
|
@@ -31,7 +31,9 @@ module RuboCop
|
|
31
31
|
|
32
32
|
def_node_matcher :on_bad_each_with_object, <<~PATTERN
|
33
33
|
(block
|
34
|
-
({send csend}
|
34
|
+
({send csend}
|
35
|
+
!{(send _ :each_with_index) (array ...)}
|
36
|
+
:each_with_object (hash))
|
35
37
|
(args
|
36
38
|
(mlhs
|
37
39
|
(arg $_)
|
@@ -55,7 +57,9 @@ module RuboCop
|
|
55
57
|
def_node_matcher :on_bad_map_to_h, <<~PATTERN
|
56
58
|
({send csend}
|
57
59
|
(block
|
58
|
-
({send csend}
|
60
|
+
({send csend}
|
61
|
+
!{(send _ :each_with_index) (array ...)}
|
62
|
+
{:map :collect})
|
59
63
|
(args
|
60
64
|
(arg $_)
|
61
65
|
(arg _val))
|
@@ -24,14 +24,13 @@ module RuboCop
|
|
24
24
|
# {a: 1, b: 2}.transform_values { |v| foo(v) }
|
25
25
|
# {a: 1, b: 2}.transform_values { |v| v * v }
|
26
26
|
class HashTransformValues < Cop
|
27
|
-
extend TargetRubyVersion
|
28
27
|
include HashTransformMethod
|
29
28
|
|
30
|
-
minimum_target_ruby_version 2.4
|
31
|
-
|
32
29
|
def_node_matcher :on_bad_each_with_object, <<~PATTERN
|
33
30
|
(block
|
34
|
-
({send csend}
|
31
|
+
({send csend}
|
32
|
+
!{(send _ :each_with_index) (array ...)}
|
33
|
+
:each_with_object (hash))
|
35
34
|
(args
|
36
35
|
(mlhs
|
37
36
|
(arg _key)
|
@@ -55,7 +54,9 @@ module RuboCop
|
|
55
54
|
def_node_matcher :on_bad_map_to_h, <<~PATTERN
|
56
55
|
({send csend}
|
57
56
|
(block
|
58
|
-
({send csend}
|
57
|
+
({send csend}
|
58
|
+
!{(send _ :each_with_index) (array ...)}
|
59
|
+
{:map :collect})
|
59
60
|
(args
|
60
61
|
(arg _key)
|
61
62
|
(arg $_))
|