rubocop 1.18.1 → 1.22.3
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/README.md +1 -1
- data/config/default.yml +116 -23
- data/lib/rubocop/cli.rb +18 -0
- data/lib/rubocop/config.rb +5 -0
- data/lib/rubocop/config_loader.rb +5 -3
- data/lib/rubocop/config_loader_resolver.rb +22 -7
- data/lib/rubocop/config_validator.rb +27 -6
- data/lib/rubocop/cop/base.rb +3 -3
- data/lib/rubocop/cop/bundler/gem_comment.rb +3 -3
- data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
- data/lib/rubocop/cop/bundler/insecure_protocol_source.rb +45 -21
- data/lib/rubocop/cop/bundler/ordered_gems.rb +3 -12
- data/lib/rubocop/cop/correctors/lambda_literal_to_method_corrector.rb +2 -2
- data/lib/rubocop/cop/correctors/line_break_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/ordered_gem_corrector.rb +11 -10
- data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
- data/lib/rubocop/cop/documentation.rb +1 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +3 -12
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +31 -24
- data/lib/rubocop/cop/generator.rb +14 -8
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
- data/lib/rubocop/cop/internal_affairs/location_line_equality_comparison.rb +60 -0
- data/lib/rubocop/cop/internal_affairs/node_matcher_directive.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/assignment_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +3 -3
- data/lib/rubocop/cop/layout/class_structure.rb +7 -2
- data/lib/rubocop/cop/layout/dot_position.rb +34 -5
- data/lib/rubocop/cop/layout/empty_comment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +7 -4
- data/lib/rubocop/cop/layout/end_alignment.rb +9 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_array_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_hash_element_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/first_parameter_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
- data/lib/rubocop/cop/layout/heredoc_argument_closing_parenthesis.rb +1 -1
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/indentation_width.rb +1 -1
- data/lib/rubocop/cop/layout/leading_comment_space.rb +2 -2
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +36 -17
- data/lib/rubocop/cop/layout/line_length.rb +9 -7
- data/lib/rubocop/cop/layout/multiline_assignment_layout.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_block_layout.rb +3 -3
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/redundant_line_break.rb +1 -0
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +23 -10
- data/lib/rubocop/cop/layout/single_line_block_chain.rb +15 -4
- data/lib/rubocop/cop/layout/space_after_not.rb +1 -0
- data/lib/rubocop/cop/layout/space_around_equals_in_parameter_default.rb +2 -1
- data/lib/rubocop/cop/layout/space_around_keyword.rb +2 -2
- data/lib/rubocop/cop/layout/space_around_operators.rb +12 -1
- data/lib/rubocop/cop/layout/space_before_brackets.rb +1 -0
- data/lib/rubocop/cop/layout/space_before_comment.rb +2 -2
- data/lib/rubocop/cop/layout/space_inside_parens.rb +78 -32
- data/lib/rubocop/cop/layout/space_inside_reference_brackets.rb +1 -1
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
- data/lib/rubocop/cop/lint/ambiguous_operator_precedence.rb +111 -0
- data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
- data/lib/rubocop/cop/lint/assignment_in_condition.rb +7 -5
- data/lib/rubocop/cop/lint/binary_operator_with_identical_operands.rb +18 -5
- data/lib/rubocop/cop/lint/boolean_symbol.rb +5 -0
- data/lib/rubocop/cop/lint/debugger.rb +2 -4
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +4 -4
- data/lib/rubocop/cop/lint/deprecated_constants.rb +3 -2
- data/lib/rubocop/cop/lint/disjunctive_assignment_in_constructor.rb +24 -1
- data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
- data/lib/rubocop/cop/lint/else_layout.rb +10 -6
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +1 -1
- data/lib/rubocop/cop/lint/erb_new_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/float_out_of_range.rb +1 -1
- data/lib/rubocop/cop/lint/hash_compare_by_identity.rb +12 -3
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +67 -0
- data/lib/rubocop/cop/lint/interpolation_check.rb +5 -0
- data/lib/rubocop/cop/lint/loop.rb +4 -3
- data/lib/rubocop/cop/lint/non_deterministic_require_order.rb +5 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +12 -1
- data/lib/rubocop/cop/lint/numbered_parameter_assignment.rb +1 -1
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +4 -2
- data/lib/rubocop/cop/lint/out_of_range_regexp_ref.rb +17 -0
- data/lib/rubocop/cop/lint/percent_string_array.rb +10 -0
- data/lib/rubocop/cop/lint/raise_exception.rb +4 -0
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +5 -4
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +50 -0
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/symbol_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/triple_quotes.rb +1 -1
- data/lib/rubocop/cop/lint/unexpected_block_arity.rb +8 -3
- data/lib/rubocop/cop/lint/unused_method_argument.rb +2 -3
- data/lib/rubocop/cop/lint/useless_method_definition.rb +3 -2
- data/lib/rubocop/cop/lint/useless_setter_call.rb +7 -4
- data/lib/rubocop/cop/lint/useless_times.rb +5 -4
- data/lib/rubocop/cop/metrics/abc_size.rb +6 -0
- data/lib/rubocop/cop/metrics/parameter_lists.rb +5 -2
- data/lib/rubocop/cop/metrics/perceived_complexity.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/abc_size_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/code_length.rb +1 -1
- data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
- data/lib/rubocop/cop/mixin/end_keyword_alignment.rb +1 -2
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +23 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +9 -4
- data/lib/rubocop/cop/mixin/heredoc.rb +5 -0
- data/lib/rubocop/cop/mixin/multiline_element_indentation.rb +1 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -2
- data/lib/rubocop/cop/mixin/multiline_literal_brace_layout.rb +1 -1
- data/lib/rubocop/cop/mixin/ordered_gem_node.rb +9 -1
- data/lib/rubocop/cop/mixin/percent_array.rb +18 -7
- data/lib/rubocop/cop/mixin/preceding_following_alignment.rb +9 -1
- data/lib/rubocop/cop/mixin/require_library.rb +59 -0
- data/lib/rubocop/cop/mixin/space_after_punctuation.rb +1 -1
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +2 -2
- data/lib/rubocop/cop/mixin/statement_modifier.rb +1 -1
- data/lib/rubocop/cop/mixin/string_literals_help.rb +5 -1
- data/lib/rubocop/cop/mixin/trailing_body.rb +1 -1
- data/lib/rubocop/cop/naming/ascii_identifiers.rb +0 -3
- data/lib/rubocop/cop/naming/block_parameter_name.rb +1 -1
- data/lib/rubocop/cop/naming/constant_name.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +27 -10
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +5 -4
- data/lib/rubocop/cop/naming/rescued_exceptions_variable_name.rb +7 -0
- data/lib/rubocop/cop/security/io_methods.rb +49 -0
- data/lib/rubocop/cop/security/json_load.rb +8 -7
- data/lib/rubocop/cop/security/open.rb +4 -0
- data/lib/rubocop/cop/security/yaml_load.rb +4 -0
- data/lib/rubocop/cop/style/accessor_grouping.rb +2 -2
- data/lib/rubocop/cop/style/and_or.rb +5 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +13 -2
- data/lib/rubocop/cop/style/array_coercion.rb +21 -3
- data/lib/rubocop/cop/style/ascii_comments.rb +0 -3
- data/lib/rubocop/cop/style/block_delimiters.rb +50 -2
- data/lib/rubocop/cop/style/case_equality.rb +6 -9
- data/lib/rubocop/cop/style/case_like_if.rb +5 -0
- data/lib/rubocop/cop/style/class_and_module_children.rb +9 -0
- data/lib/rubocop/cop/style/collection_compact.rb +7 -5
- data/lib/rubocop/cop/style/collection_methods.rb +8 -6
- data/lib/rubocop/cop/style/combinable_loops.rb +3 -2
- data/lib/rubocop/cop/style/comment_annotation.rb +55 -25
- data/lib/rubocop/cop/style/commented_keyword.rb +9 -3
- data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
- data/lib/rubocop/cop/style/date_time.rb +5 -0
- data/lib/rubocop/cop/style/document_dynamic_eval_definition.rb +1 -1
- data/lib/rubocop/cop/style/documentation.rb +23 -8
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
- data/lib/rubocop/cop/style/double_negation.rb +27 -6
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/encoding.rb +26 -15
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +46 -11
- data/lib/rubocop/cop/style/float_division.rb +10 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +14 -3
- data/lib/rubocop/cop/style/global_std_stream.rb +4 -0
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
- data/lib/rubocop/cop/style/hash_each_methods.rb +5 -0
- data/lib/rubocop/cop/style/hash_except.rb +4 -3
- data/lib/rubocop/cop/style/hash_syntax.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_keys.rb +4 -9
- data/lib/rubocop/cop/style/hash_transform_values.rb +4 -6
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +32 -5
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +18 -4
- data/lib/rubocop/cop/style/infinite_loop.rb +4 -3
- data/lib/rubocop/cop/style/inverse_methods.rb +9 -2
- data/lib/rubocop/cop/style/lambda_call.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +14 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +6 -6
- data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
- data/lib/rubocop/cop/style/missing_else.rb +7 -0
- data/lib/rubocop/cop/style/module_function.rb +8 -9
- data/lib/rubocop/cop/style/multiline_in_pattern_then.rb +1 -1
- data/lib/rubocop/cop/style/multiline_when_then.rb +1 -1
- data/lib/rubocop/cop/style/mutable_constant.rb +79 -14
- data/lib/rubocop/cop/style/negated_if.rb +1 -1
- data/lib/rubocop/cop/style/negated_unless.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +2 -2
- data/lib/rubocop/cop/style/not.rb +2 -2
- data/lib/rubocop/cop/style/numbered_parameters.rb +46 -0
- data/lib/rubocop/cop/style/numbered_parameters_limit.rb +50 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +7 -8
- data/lib/rubocop/cop/style/numeric_predicate.rb +5 -0
- data/lib/rubocop/cop/style/optional_arguments.rb +4 -0
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +14 -4
- data/lib/rubocop/cop/style/parallel_assignment.rb +1 -1
- data/lib/rubocop/cop/style/percent_q_literals.rb +2 -2
- data/lib/rubocop/cop/style/preferred_hash_methods.rb +9 -4
- data/lib/rubocop/cop/style/quoted_symbols.rb +10 -6
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_argument.rb +19 -9
- data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
- data/lib/rubocop/cop/style/redundant_condition.rb +2 -3
- data/lib/rubocop/cop/style/redundant_fetch_block.rb +4 -0
- data/lib/rubocop/cop/style/redundant_file_extension_in_require.rb +12 -3
- data/lib/rubocop/cop/style/redundant_freeze.rb +4 -4
- data/lib/rubocop/cop/style/redundant_interpolation.rb +1 -1
- data/lib/rubocop/cop/style/redundant_percent_q.rb +2 -3
- data/lib/rubocop/cop/style/redundant_self.rb +10 -0
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +4 -3
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +53 -20
- data/lib/rubocop/cop/style/regexp_literal.rb +3 -3
- data/lib/rubocop/cop/style/return_nil.rb +2 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +13 -2
- data/lib/rubocop/cop/style/select_by_regexp.rb +139 -0
- data/lib/rubocop/cop/style/semicolon.rb +32 -24
- data/lib/rubocop/cop/style/single_argument_dig.rb +5 -0
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +25 -15
- data/lib/rubocop/cop/style/slicing_with_range.rb +13 -0
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +25 -0
- data/lib/rubocop/cop/style/static_class.rb +5 -5
- data/lib/rubocop/cop/style/string_chars.rb +4 -2
- data/lib/rubocop/cop/style/string_concatenation.rb +5 -1
- data/lib/rubocop/cop/style/string_hash_keys.rb +4 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +4 -0
- data/lib/rubocop/cop/style/swap_values.rb +4 -2
- data/lib/rubocop/cop/style/symbol_array.rb +3 -3
- data/lib/rubocop/cop/style/symbol_proc.rb +26 -0
- data/lib/rubocop/cop/style/trailing_comma_in_block_args.rb +19 -0
- data/lib/rubocop/cop/style/trivial_accessors.rb +1 -1
- data/lib/rubocop/cop/style/word_array.rb +23 -5
- data/lib/rubocop/cop/style/yoda_condition.rb +24 -7
- data/lib/rubocop/cop/style/zero_length_predicate.rb +6 -0
- data/lib/rubocop/cop/util.rb +22 -6
- data/lib/rubocop/cops_documentation_generator.rb +17 -5
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +44 -15
- data/lib/rubocop/options.rb +127 -113
- data/lib/rubocop/rake_task.rb +1 -1
- data/lib/rubocop/result_cache.rb +3 -3
- data/lib/rubocop/rspec/cop_helper.rb +1 -1
- data/lib/rubocop/rspec/expect_offense.rb +6 -2
- data/lib/rubocop/rspec/parallel_formatter.rb +90 -0
- data/lib/rubocop/rspec/support.rb +1 -0
- data/lib/rubocop/runner.rb +2 -3
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +14 -2
- metadata +21 -5
@@ -9,6 +9,16 @@ module RuboCop
|
|
9
9
|
# example, mistranslating an array of literals to percent string notation)
|
10
10
|
# rather than meant to be part of the resulting strings.
|
11
11
|
#
|
12
|
+
# @safety
|
13
|
+
# The cop is unsafe because the correction changes the values in the array
|
14
|
+
# and that might have been done purposely.
|
15
|
+
#
|
16
|
+
# [source,ruby]
|
17
|
+
# ----
|
18
|
+
# %w('foo', "bar") #=> ["'foo',", '"bar"']
|
19
|
+
# %w(foo bar) #=> ['foo', 'bar']
|
20
|
+
# ----
|
21
|
+
#
|
12
22
|
# @example
|
13
23
|
#
|
14
24
|
# # bad
|
@@ -13,6 +13,10 @@ module RuboCop
|
|
13
13
|
# `Exception`. Alternatively, make `Exception` a fully qualified class
|
14
14
|
# name with an explicit namespace.
|
15
15
|
#
|
16
|
+
# @safety
|
17
|
+
# This cop is unsafe because it will change the exception class being
|
18
|
+
# raised, which is a change in behaviour.
|
19
|
+
#
|
16
20
|
# @example
|
17
21
|
# # bad
|
18
22
|
# raise Exception, 'Error message here'
|
@@ -7,13 +7,14 @@ module RuboCop
|
|
7
7
|
# `instance_of?`, `kind_of?`, `is_a?`, `eql?`, `respond_to?`, and `equal?` methods
|
8
8
|
# are checked by default. These are customizable with `AllowedMethods` option.
|
9
9
|
#
|
10
|
-
# This cop is marked as unsafe, because auto-correction can change the
|
11
|
-
# return type of the expression. An offending expression that previously
|
12
|
-
# could return `nil` will be auto-corrected to never return `nil`.
|
13
|
-
#
|
14
10
|
# In the example below, the safe navigation operator (`&.`) is unnecessary
|
15
11
|
# because `NilClass` has methods like `respond_to?` and `is_a?`.
|
16
12
|
#
|
13
|
+
# @safety
|
14
|
+
# This cop is unsafe, because auto-correction can change the return type of
|
15
|
+
# the expression. An offending expression that previously could return `nil`
|
16
|
+
# will be auto-corrected to never return `nil`.
|
17
|
+
#
|
17
18
|
# @example
|
18
19
|
# # bad
|
19
20
|
# do_something if attrs&.respond_to?(:[])
|
@@ -0,0 +1,50 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Lint
|
6
|
+
# Checks for uses a file requiring itself with `require_relative`.
|
7
|
+
#
|
8
|
+
# @example
|
9
|
+
#
|
10
|
+
# # bad
|
11
|
+
#
|
12
|
+
# # foo.rb
|
13
|
+
# require_relative 'foo'
|
14
|
+
# require_relative 'bar'
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
#
|
18
|
+
# # foo.rb
|
19
|
+
# require_relative 'bar'
|
20
|
+
#
|
21
|
+
class RequireRelativeSelfPath < Base
|
22
|
+
include RangeHelp
|
23
|
+
extend AutoCorrector
|
24
|
+
|
25
|
+
MSG = 'Remove the `require_relative` that requires itself.'
|
26
|
+
RESTRICT_ON_SEND = %i[require_relative].freeze
|
27
|
+
|
28
|
+
def on_send(node)
|
29
|
+
return unless (required_feature = node.first_argument)
|
30
|
+
return unless required_feature.respond_to?(:value)
|
31
|
+
return unless same_file?(processed_source.file_path, required_feature.value)
|
32
|
+
|
33
|
+
add_offense(node) do |corrector|
|
34
|
+
corrector.remove(range_by_whole_lines(node.source_range, include_final_newline: true))
|
35
|
+
end
|
36
|
+
end
|
37
|
+
|
38
|
+
private
|
39
|
+
|
40
|
+
def same_file?(file_path, required_feature)
|
41
|
+
file_path == required_feature || remove_ext(file_path) == required_feature
|
42
|
+
end
|
43
|
+
|
44
|
+
def remove_ext(file_path)
|
45
|
+
File.basename(file_path, File.extname(file_path))
|
46
|
+
end
|
47
|
+
end
|
48
|
+
end
|
49
|
+
end
|
50
|
+
end
|
@@ -10,7 +10,7 @@ module RuboCop
|
|
10
10
|
#
|
11
11
|
# NOTE: Shadowing of variables in block passed to `Ractor.new` is allowed
|
12
12
|
# because `Ractor` should not access outer variables.
|
13
|
-
# eg. following
|
13
|
+
# eg. following style is encouraged:
|
14
14
|
#
|
15
15
|
# worker_id, pipe = env
|
16
16
|
# Ractor.new(worker_id, pipe) do |worker_id, pipe|
|
@@ -143,7 +143,7 @@ module RuboCop
|
|
143
143
|
# Although some operators can be converted to symbols normally
|
144
144
|
# (ie. `:==`), these are not accepted as hash keys and will
|
145
145
|
# raise a syntax error (eg. `{ ==: ... }`). Therefore, if the
|
146
|
-
# symbol does not start with an
|
146
|
+
# symbol does not start with an alphanumeric or underscore, it
|
147
147
|
# will be ignored.
|
148
148
|
return unless node.value.to_s.match?(/\A[a-z0-9_]/i)
|
149
149
|
|
@@ -3,7 +3,7 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Lint
|
6
|
-
# This cop checks for "triple quotes" (strings
|
6
|
+
# This cop checks for "triple quotes" (strings delimited by any odd number
|
7
7
|
# of quotes greater than 1).
|
8
8
|
#
|
9
9
|
# Ruby allows multiple strings to be implicitly concatenated by just
|
@@ -13,14 +13,19 @@ module RuboCop
|
|
13
13
|
# Keyword arguments (including `**kwargs`) do not get counted towards
|
14
14
|
# this, as they are not used by the methods in question.
|
15
15
|
#
|
16
|
-
# NOTE: This cop matches for method names only and hence cannot tell apart
|
17
|
-
# methods with same name in different classes.
|
18
|
-
#
|
19
16
|
# Method names and their expected arity can be configured like this:
|
20
17
|
#
|
18
|
+
# [source,yaml]
|
19
|
+
# ----
|
21
20
|
# Methods:
|
22
21
|
# inject: 2
|
23
22
|
# reduce: 2
|
23
|
+
# ----
|
24
|
+
#
|
25
|
+
# @safety
|
26
|
+
# This cop matches for method names only and hence cannot tell apart
|
27
|
+
# methods with same name in different classes, which may lead to a
|
28
|
+
# false positive.
|
24
29
|
#
|
25
30
|
# @example
|
26
31
|
# # bad
|
@@ -87,9 +87,8 @@ module RuboCop
|
|
87
87
|
end
|
88
88
|
|
89
89
|
def ignored_method?(body)
|
90
|
-
cop_config['IgnoreEmptyMethods'] && body.nil? ||
|
91
|
-
cop_config['IgnoreNotImplementedMethods'] &&
|
92
|
-
not_implemented?(body)
|
90
|
+
(cop_config['IgnoreEmptyMethods'] && body.nil?) ||
|
91
|
+
(cop_config['IgnoreNotImplementedMethods'] && not_implemented?(body))
|
93
92
|
end
|
94
93
|
|
95
94
|
def message(variable)
|
@@ -6,8 +6,9 @@ module RuboCop
|
|
6
6
|
# This cop checks for useless method definitions, specifically: empty constructors
|
7
7
|
# and methods just delegating to `super`.
|
8
8
|
#
|
9
|
-
#
|
10
|
-
#
|
9
|
+
# @safety
|
10
|
+
# This cop is unsafe as it can register false positives for cases when an empty
|
11
|
+
# constructor just overrides the parent constructor, which is bad anyway.
|
11
12
|
#
|
12
13
|
# @example
|
13
14
|
# # bad
|
@@ -5,11 +5,14 @@ module RuboCop
|
|
5
5
|
module Lint
|
6
6
|
# This cop checks for setter call to local variable as the final
|
7
7
|
# expression of a function definition.
|
8
|
-
# Its auto-correction is marked as unsafe because return value will be changed.
|
9
8
|
#
|
10
|
-
#
|
11
|
-
#
|
12
|
-
#
|
9
|
+
# @safety
|
10
|
+
# There are edge cases in which the local variable references a
|
11
|
+
# value that is also accessible outside the local scope. This is not
|
12
|
+
# detected by the cop, and it can yield a false positive.
|
13
|
+
#
|
14
|
+
# As well, auto-correction is unsafe because the method's
|
15
|
+
# return value will be changed.
|
13
16
|
#
|
14
17
|
# @example
|
15
18
|
#
|
@@ -7,8 +7,9 @@ module RuboCop
|
|
7
7
|
# (when the integer <= 0) or that will only ever yield once
|
8
8
|
# (`1.times`).
|
9
9
|
#
|
10
|
-
#
|
11
|
-
# is
|
10
|
+
# @safety
|
11
|
+
# This cop is unsafe as `times` returns its receiver, which is
|
12
|
+
# *usually* OK, but might change behavior.
|
12
13
|
#
|
13
14
|
# @example
|
14
15
|
# # bad
|
@@ -65,7 +66,7 @@ module RuboCop
|
|
65
66
|
private
|
66
67
|
|
67
68
|
def never_process?(count, node)
|
68
|
-
count < 1 || node.block_type? && node.body.nil?
|
69
|
+
count < 1 || (node.block_type? && node.body.nil?)
|
69
70
|
end
|
70
71
|
|
71
72
|
def remove_node(corrector, node)
|
@@ -81,7 +82,7 @@ module RuboCop
|
|
81
82
|
return if block_reassigns_arg?(node, block_arg)
|
82
83
|
|
83
84
|
source = node.body.source
|
84
|
-
source.gsub!(/\b#{block_arg}\b/, '
|
85
|
+
source.gsub!(/\b#{block_arg}\b/, '0') if block_arg
|
85
86
|
|
86
87
|
corrector.replace(node, fix_indentation(source, node.loc.column...node.body.loc.column))
|
87
88
|
end
|
@@ -8,6 +8,12 @@ module RuboCop
|
|
8
8
|
# (method calls), and conditions. See http://c2.com/cgi/wiki?AbcMetric
|
9
9
|
# and https://en.wikipedia.org/wiki/ABC_Software_Metric.
|
10
10
|
#
|
11
|
+
# Interpreting ABC size:
|
12
|
+
#
|
13
|
+
# * <= 17 satisfactory
|
14
|
+
# * 18..30 unsatisfactory
|
15
|
+
# * > 30 dangerous
|
16
|
+
#
|
11
17
|
# You can have repeated "attributes" calls count as a single "branch".
|
12
18
|
# For this purpose, attributes are any method with no argument; no attempt
|
13
19
|
# is meant to distinguish actual `attr_reader` from other methods.
|
@@ -9,6 +9,9 @@ module RuboCop
|
|
9
9
|
# Keyword arguments can optionally be excluded from the total count,
|
10
10
|
# as they add less complexity than positional or optional parameters.
|
11
11
|
#
|
12
|
+
# NOTE: Explicit block argument `&block` is not counted to prevent
|
13
|
+
# erroneous change that is avoided by making block argument implicit.
|
14
|
+
#
|
12
15
|
# @example Max: 3
|
13
16
|
# # good
|
14
17
|
# def foo(a, b, c = 1)
|
@@ -94,9 +97,9 @@ module RuboCop
|
|
94
97
|
|
95
98
|
def args_count(node)
|
96
99
|
if count_keyword_args?
|
97
|
-
node.children.
|
100
|
+
node.children.count { |a| !a.blockarg_type? }
|
98
101
|
else
|
99
|
-
node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) }
|
102
|
+
node.children.count { |a| !NAMED_KEYWORD_TYPES.include?(a.type) && !a.blockarg_type? }
|
100
103
|
end
|
101
104
|
end
|
102
105
|
|
@@ -46,7 +46,7 @@ module RuboCop
|
|
46
46
|
visit_depth_last(@node) { |child| calculate_node(child) }
|
47
47
|
|
48
48
|
[
|
49
|
-
Math.sqrt(@assignment**2 + @branch**2 + @condition**2).round(2),
|
49
|
+
Math.sqrt((@assignment**2) + (@branch**2) + (@condition**2)).round(2),
|
50
50
|
"<#{@assignment}, #{@branch}, #{@condition}>"
|
51
51
|
]
|
52
52
|
end
|
@@ -147,7 +147,7 @@ module RuboCop
|
|
147
147
|
|
148
148
|
# Returns true for lines that shall not be included in the count.
|
149
149
|
def irrelevant_line?(source_line)
|
150
|
-
source_line.blank? || !count_comments? && comment_line?(source_line)
|
150
|
+
source_line.blank? || (!count_comments? && comment_line?(source_line))
|
151
151
|
end
|
152
152
|
|
153
153
|
def count_comments?
|
@@ -2,40 +2,63 @@
|
|
2
2
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
|
-
|
6
|
-
|
7
|
-
|
8
|
-
|
9
|
-
|
10
|
-
|
11
|
-
|
12
|
-
|
13
|
-
|
14
|
-
|
15
|
-
|
16
|
-
|
17
|
-
|
18
|
-
|
19
|
-
|
20
|
-
|
21
|
-
|
22
|
-
|
23
|
-
|
24
|
-
|
25
|
-
|
26
|
-
|
27
|
-
|
28
|
-
|
29
|
-
|
30
|
-
|
31
|
-
|
32
|
-
|
33
|
-
|
34
|
-
|
35
|
-
|
36
|
-
|
37
|
-
|
38
|
-
|
5
|
+
# Representation of an annotation comment in source code (eg. `# TODO: blah blah blah`).
|
6
|
+
class AnnotationComment
|
7
|
+
extend Forwardable
|
8
|
+
|
9
|
+
attr_reader :comment, :margin, :keyword, :colon, :space, :note
|
10
|
+
|
11
|
+
# @param [Parser::Source::Comment] comment
|
12
|
+
# @param [Array<String>] keywords
|
13
|
+
def initialize(comment, keywords)
|
14
|
+
@comment = comment
|
15
|
+
@keywords = keywords
|
16
|
+
@margin, @keyword, @colon, @space, @note = split_comment(comment)
|
17
|
+
end
|
18
|
+
|
19
|
+
def annotation?
|
20
|
+
keyword_appearance? && !just_keyword_of_sentence?
|
21
|
+
end
|
22
|
+
|
23
|
+
def correct?(colon:)
|
24
|
+
return false unless keyword && space && note
|
25
|
+
return false unless keyword == keyword.upcase
|
26
|
+
|
27
|
+
self.colon.nil? == !colon
|
28
|
+
end
|
29
|
+
|
30
|
+
# Returns the range bounds for just the annotation
|
31
|
+
def bounds
|
32
|
+
start = comment.loc.expression.begin_pos + margin.length
|
33
|
+
length = [keyword, colon, space].reduce(0) { |len, elem| len + elem.to_s.length }
|
34
|
+
[start, start + length]
|
35
|
+
end
|
36
|
+
|
37
|
+
private
|
38
|
+
|
39
|
+
attr_reader :keywords
|
40
|
+
|
41
|
+
def split_comment(comment)
|
42
|
+
# Sort keywords by reverse length so that if a keyword is in a phrase
|
43
|
+
# but also on its own, both will match properly.
|
44
|
+
keywords_regex = Regexp.new(
|
45
|
+
Regexp.union(keywords.sort_by { |w| -w.length }).source,
|
46
|
+
Regexp::IGNORECASE
|
47
|
+
)
|
48
|
+
regex = /^(# ?)(\b#{keywords_regex}\b)(\s*:)?(\s+)?(\S+)?/i
|
49
|
+
|
50
|
+
match = comment.text.match(regex)
|
51
|
+
return false unless match
|
52
|
+
|
53
|
+
match.captures
|
54
|
+
end
|
55
|
+
|
56
|
+
def keyword_appearance?
|
57
|
+
keyword && (colon || space)
|
58
|
+
end
|
59
|
+
|
60
|
+
def just_keyword_of_sentence?
|
61
|
+
keyword == keyword.capitalize && !colon && space && note
|
39
62
|
end
|
40
63
|
end
|
41
64
|
end
|
@@ -97,9 +97,9 @@ module RuboCop
|
|
97
97
|
# If a send node contains a heredoc argument, splitting cannot happen
|
98
98
|
# after the heredoc or else it will cause a syntax error.
|
99
99
|
def shift_elements_for_heredoc_arg(node, elements, index)
|
100
|
-
return index unless node.send_type?
|
100
|
+
return index unless node.send_type? || node.array_type?
|
101
101
|
|
102
|
-
heredoc_index = elements.index { |arg|
|
102
|
+
heredoc_index = elements.index { |arg| arg.respond_to?(:heredoc?) && arg.heredoc? }
|
103
103
|
return index unless heredoc_index
|
104
104
|
return nil if heredoc_index.zero?
|
105
105
|
|
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
|
44
44
|
# Returns true for lines that shall not be included in the count.
|
45
45
|
def irrelevant_line(source_line)
|
46
|
-
source_line.blank? || !count_comments? && comment_line?(source_line)
|
46
|
+
source_line.blank? || (!count_comments? && comment_line?(source_line))
|
47
47
|
end
|
48
48
|
|
49
49
|
def build_code_length_calculator(node)
|
@@ -5,7 +5,6 @@ module RuboCop
|
|
5
5
|
# Common functionality for checking documentation.
|
6
6
|
module DocumentationComment
|
7
7
|
extend NodePattern::Macros
|
8
|
-
include Style::AnnotationComment
|
9
8
|
|
10
9
|
private
|
11
10
|
|
@@ -15,7 +14,7 @@ module RuboCop
|
|
15
14
|
return false unless preceding_comment?(node, preceding_lines.last)
|
16
15
|
|
17
16
|
preceding_lines.any? do |comment|
|
18
|
-
!
|
17
|
+
!AnnotationComment.new(comment, annotation_keywords).annotation? &&
|
19
18
|
!interpreter_directive_comment?(comment) &&
|
20
19
|
!rubocop_directive_comment?(comment)
|
21
20
|
end
|
@@ -44,6 +43,10 @@ module RuboCop
|
|
44
43
|
def rubocop_directive_comment?(comment)
|
45
44
|
!!DirectiveComment.new(comment).match_captures
|
46
45
|
end
|
46
|
+
|
47
|
+
def annotation_keywords
|
48
|
+
config.for_cop('Style/CommentAnnotation')['Keywords']
|
49
|
+
end
|
47
50
|
end
|
48
51
|
end
|
49
52
|
end
|
@@ -34,8 +34,7 @@ module RuboCop
|
|
34
34
|
|
35
35
|
def matching_ranges(end_loc, align_ranges)
|
36
36
|
align_ranges.select do |_, range|
|
37
|
-
range
|
38
|
-
column_offset_between(range, end_loc).zero?
|
37
|
+
same_line?(range, end_loc) || column_offset_between(range, end_loc).zero?
|
39
38
|
end
|
40
39
|
end
|
41
40
|
|
@@ -8,7 +8,9 @@ module RuboCop
|
|
8
8
|
|
9
9
|
FROZEN_STRING_LITERAL = '# frozen_string_literal:'
|
10
10
|
FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
|
11
|
-
|
11
|
+
FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze
|
12
|
+
|
13
|
+
private_constant :FROZEN_STRING_LITERAL_TYPES_RUBY27
|
12
14
|
|
13
15
|
def frozen_string_literal_comment_exists?
|
14
16
|
leading_comment_lines.any? { |line| MagicComment.parse(line).valid_literal_value? }
|
@@ -16,6 +18,26 @@ module RuboCop
|
|
16
18
|
|
17
19
|
private
|
18
20
|
|
21
|
+
def frozen_string_literal?(node)
|
22
|
+
frozen_string = if target_ruby_version >= 3.0
|
23
|
+
uninterpolated_string?(node) || frozen_heredoc?(node)
|
24
|
+
else
|
25
|
+
FROZEN_STRING_LITERAL_TYPES_RUBY27.include?(node.type)
|
26
|
+
end
|
27
|
+
|
28
|
+
frozen_string && frozen_string_literals_enabled?
|
29
|
+
end
|
30
|
+
|
31
|
+
def uninterpolated_string?(node)
|
32
|
+
node.str_type? || (node.dstr_type? && node.each_descendant(:begin).none?)
|
33
|
+
end
|
34
|
+
|
35
|
+
def frozen_heredoc?(node)
|
36
|
+
return false unless node.dstr_type? && node.heredoc?
|
37
|
+
|
38
|
+
node.children.all?(&:str_type?)
|
39
|
+
end
|
40
|
+
|
19
41
|
def frozen_string_literals_enabled?
|
20
42
|
ruby_version = processed_source.ruby_version
|
21
43
|
return false unless ruby_version
|
@@ -139,9 +139,9 @@ module RuboCop
|
|
139
139
|
end
|
140
140
|
|
141
141
|
def self.from_map_to_h(node, match)
|
142
|
-
|
143
|
-
|
144
|
-
|
142
|
+
if node.parent&.block_type? && node.parent.send_node == node
|
143
|
+
strip_trailing_chars = 0
|
144
|
+
else
|
145
145
|
map_range = node.children.first.source_range
|
146
146
|
node_range = node.source_range
|
147
147
|
strip_trailing_chars = node_range.end_pos - map_range.end_pos
|
@@ -175,7 +175,12 @@ module RuboCop
|
|
175
175
|
end
|
176
176
|
|
177
177
|
def set_new_body_expression(transforming_body_expr, corrector)
|
178
|
-
|
178
|
+
body_source = transforming_body_expr.loc.expression.source
|
179
|
+
if transforming_body_expr.hash_type? && !transforming_body_expr.braces?
|
180
|
+
body_source = "{ #{body_source} }"
|
181
|
+
end
|
182
|
+
|
183
|
+
corrector.replace(block_node.body, body_source)
|
179
184
|
end
|
180
185
|
end
|
181
186
|
end
|
@@ -20,6 +20,11 @@ module RuboCop
|
|
20
20
|
|
21
21
|
private
|
22
22
|
|
23
|
+
def indent_level(str)
|
24
|
+
indentations = str.lines.map { |line| line[/^\s*/] }.reject { |line| line.end_with?("\n") }
|
25
|
+
indentations.empty? ? 0 : indentations.min_by(&:size).size
|
26
|
+
end
|
27
|
+
|
23
28
|
def delimiter_string(node)
|
24
29
|
node.source.match(OPENING_DELIMITER).captures[1]
|
25
30
|
end
|
@@ -15,7 +15,7 @@ module RuboCop
|
|
15
15
|
node.arguments.each do |arg|
|
16
16
|
on_node(type, arg, :send) do |type_node|
|
17
17
|
left_brace = type_node.loc.begin
|
18
|
-
if left_brace && left_brace
|
18
|
+
if left_brace && same_line?(left_brace, left_parenthesis)
|
19
19
|
yield type_node, left_parenthesis
|
20
20
|
ignore_node(type_node)
|
21
21
|
end
|
@@ -134,7 +134,7 @@ module RuboCop
|
|
134
134
|
|
135
135
|
next if a.setter_method?
|
136
136
|
next unless kind == :with_or_without_parentheses ||
|
137
|
-
kind == :with_parentheses && parentheses?(a)
|
137
|
+
(kind == :with_parentheses && parentheses?(a))
|
138
138
|
|
139
139
|
a.arguments.any? { |arg| within_node?(node, arg) }
|
140
140
|
end
|
@@ -156,7 +156,7 @@ module RuboCop
|
|
156
156
|
|
157
157
|
def disqualified_rhs?(candidate, ancestor)
|
158
158
|
UNALIGNED_RHS_TYPES.include?(ancestor.type) ||
|
159
|
-
ancestor.block_type? && part_of_block_body?(candidate, ancestor)
|
159
|
+
(ancestor.block_type? && part_of_block_body?(candidate, ancestor))
|
160
160
|
end
|
161
161
|
|
162
162
|
def valid_rhs?(candidate, ancestor)
|
@@ -90,7 +90,7 @@ module RuboCop
|
|
90
90
|
# This method depends on the fact that we have guarded
|
91
91
|
# against implicit and empty literals.
|
92
92
|
def opening_brace_on_same_line?(node)
|
93
|
-
node.loc.begin
|
93
|
+
same_line?(node.loc.begin, children(node).first)
|
94
94
|
end
|
95
95
|
|
96
96
|
# This method depends on the fact that we have guarded
|
@@ -35,7 +35,15 @@ module RuboCop
|
|
35
35
|
previous: gem_name(current),
|
36
36
|
current: gem_name(previous)
|
37
37
|
)
|
38
|
-
|
38
|
+
|
39
|
+
add_offense(current, message: message) do |corrector|
|
40
|
+
OrderedGemCorrector.correct(
|
41
|
+
processed_source,
|
42
|
+
current,
|
43
|
+
previous_declaration(current),
|
44
|
+
treat_comments_as_separators
|
45
|
+
).call(corrector)
|
46
|
+
end
|
39
47
|
end
|
40
48
|
|
41
49
|
def gem_name(declaration_node)
|
@@ -18,15 +18,16 @@ module RuboCop
|
|
18
18
|
!parent.parenthesized? && parent&.block_literal?
|
19
19
|
end
|
20
20
|
|
21
|
+
# Override to determine values that are invalid in a percent array
|
22
|
+
def invalid_percent_array_contents?(_node)
|
23
|
+
false
|
24
|
+
end
|
25
|
+
|
21
26
|
def allowed_bracket_array?(node)
|
22
27
|
comments_in_array?(node) || below_array_length?(node) ||
|
23
28
|
invalid_percent_array_context?(node)
|
24
29
|
end
|
25
30
|
|
26
|
-
def message(_node)
|
27
|
-
style == :percent ? self.class::PERCENT_MSG : self.class::ARRAY_MSG
|
28
|
-
end
|
29
|
-
|
30
31
|
def comments_in_array?(node)
|
31
32
|
line_span = node.source_range.first_line...node.source_range.last_line
|
32
33
|
processed_source.each_comment_in_lines(line_span).any?
|
@@ -35,9 +36,19 @@ module RuboCop
|
|
35
36
|
def check_percent_array(node)
|
36
37
|
array_style_detected(:percent, node.values.size)
|
37
38
|
|
38
|
-
|
39
|
+
brackets_required = invalid_percent_array_contents?(node)
|
40
|
+
return unless style == :brackets || brackets_required
|
39
41
|
|
40
|
-
|
42
|
+
# If in percent style but brackets are required due to
|
43
|
+
# string content, the file should be excluded in auto-gen-config
|
44
|
+
no_acceptable_style! if brackets_required
|
45
|
+
|
46
|
+
bracketed_array = build_bracketed_array(node)
|
47
|
+
message = format(self.class::ARRAY_MSG, prefer: bracketed_array)
|
48
|
+
|
49
|
+
add_offense(node, message: message) do |corrector|
|
50
|
+
corrector.replace(node, bracketed_array)
|
51
|
+
end
|
41
52
|
end
|
42
53
|
|
43
54
|
def check_bracketed_array(node, literal_prefix)
|
@@ -47,7 +58,7 @@ module RuboCop
|
|
47
58
|
|
48
59
|
return unless style == :percent
|
49
60
|
|
50
|
-
add_offense(node) do |corrector|
|
61
|
+
add_offense(node, message: self.class::PERCENT_MSG) do |corrector|
|
51
62
|
percent_literal_corrector = PercentLiteralCorrector.new(@config, @preferred_delimiters)
|
52
63
|
percent_literal_corrector.correct(corrector, node, literal_prefix)
|
53
64
|
end
|