rubocop 1.22.3 → 1.26.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/LICENSE.txt +1 -1
- data/README.md +2 -3
- data/config/default.yml +110 -19
- data/lib/rubocop/cli/command/auto_genenerate_config.rb +1 -1
- data/lib/rubocop/cli/command/init_dotfile.rb +1 -1
- data/lib/rubocop/cli/command/show_docs_url.rb +48 -0
- data/lib/rubocop/cli/command/suggest_extensions.rb +1 -1
- data/lib/rubocop/cli.rb +2 -1
- data/lib/rubocop/config_loader_resolver.rb +1 -1
- data/lib/rubocop/cop/badge.rb +7 -1
- data/lib/rubocop/cop/bundler/duplicated_gem.rb +1 -1
- data/lib/rubocop/cop/correctors/each_to_for_corrector.rb +1 -1
- data/lib/rubocop/cop/correctors/if_then_corrector.rb +55 -0
- data/lib/rubocop/cop/documentation.rb +19 -2
- data/lib/rubocop/cop/gemspec/date_assignment.rb +2 -10
- data/lib/rubocop/cop/gemspec/duplicated_assignment.rb +1 -10
- data/lib/rubocop/cop/gemspec/require_mfa.rb +145 -0
- data/lib/rubocop/cop/gemspec/required_ruby_version.rb +10 -3
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +3 -10
- data/lib/rubocop/cop/generator.rb +7 -11
- data/lib/rubocop/cop/internal_affairs/redundant_context_config_parameter.rb +46 -0
- data/lib/rubocop/cop/internal_affairs/redundant_method_dispatch_node.rb +47 -0
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +3 -1
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/argument_alignment.rb +36 -9
- data/lib/rubocop/cop/layout/case_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/comment_indentation.rb +31 -2
- data/lib/rubocop/cop/layout/dot_position.rb +4 -0
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +22 -1
- data/lib/rubocop/cop/layout/empty_lines_around_exception_handling_keywords.rb +5 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +7 -2
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +7 -8
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/space_after_colon.rb +1 -1
- data/lib/rubocop/cop/layout/space_before_first_arg.rb +4 -0
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +11 -5
- data/lib/rubocop/cop/lint/ambiguous_range.rb +2 -2
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -1
- data/lib/rubocop/cop/lint/constant_definition_in_block.rb +1 -1
- data/lib/rubocop/cop/lint/deprecated_class_methods.rb +16 -4
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +6 -0
- data/lib/rubocop/cop/lint/each_with_object_argument.rb +1 -1
- data/lib/rubocop/cop/lint/incompatible_io_select_with_fiber_scheduler.rb +10 -5
- data/lib/rubocop/cop/lint/inherit_exception.rb +19 -28
- data/lib/rubocop/cop/lint/number_conversion.rb +5 -2
- data/lib/rubocop/cop/lint/parentheses_as_grouped_expression.rb +10 -6
- data/lib/rubocop/cop/lint/redundant_dir_glob_sort.rb +5 -0
- data/lib/rubocop/cop/lint/symbol_conversion.rb +3 -2
- data/lib/rubocop/cop/lint/useless_ruby2_keywords.rb +117 -0
- data/lib/rubocop/cop/lint/useless_times.rb +13 -9
- data/lib/rubocop/cop/metrics/block_length.rb +1 -0
- data/lib/rubocop/cop/metrics/cyclomatic_complexity.rb +0 -9
- data/lib/rubocop/cop/metrics/method_length.rb +1 -0
- data/lib/rubocop/cop/metrics/module_length.rb +1 -1
- data/lib/rubocop/cop/metrics/parameter_lists.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/code_length_calculator.rb +1 -1
- data/lib/rubocop/cop/metrics/utils/repeated_attribute_discount.rb +1 -1
- data/lib/rubocop/cop/mixin/enforce_superclass.rb +5 -0
- data/lib/rubocop/cop/mixin/gemspec_help.rb +30 -0
- data/lib/rubocop/cop/mixin/hash_alignment_styles.rb +4 -3
- data/lib/rubocop/cop/mixin/hash_shorthand_syntax.rb +82 -0
- data/lib/rubocop/cop/mixin/line_length_help.rb +17 -6
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +3 -1
- data/lib/rubocop/cop/mixin/string_literals_help.rb +1 -5
- data/lib/rubocop/cop/naming/block_forwarding.rb +121 -0
- data/lib/rubocop/cop/naming/file_name.rb +37 -4
- data/lib/rubocop/cop/naming/method_parameter_name.rb +1 -1
- data/lib/rubocop/cop/security/open.rb +11 -1
- data/lib/rubocop/cop/security/yaml_load.rb +9 -3
- data/lib/rubocop/cop/style/character_literal.rb +8 -1
- data/lib/rubocop/cop/style/collection_compact.rb +31 -13
- data/lib/rubocop/cop/style/combinable_loops.rb +2 -2
- data/lib/rubocop/cop/style/def_with_parentheses.rb +16 -11
- data/lib/rubocop/cop/style/documentation.rb +1 -1
- data/lib/rubocop/cop/style/empty_case_condition.rb +10 -0
- data/lib/rubocop/cop/style/empty_method.rb +1 -1
- data/lib/rubocop/cop/style/file_read.rb +112 -0
- data/lib/rubocop/cop/style/file_write.rb +124 -0
- data/lib/rubocop/cop/style/for.rb +4 -0
- data/lib/rubocop/cop/style/format_string_token.rb +2 -1
- data/lib/rubocop/cop/style/hash_conversion.rb +2 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +36 -0
- data/lib/rubocop/cop/style/hash_transform_keys.rb +6 -6
- data/lib/rubocop/cop/style/hash_transform_values.rb +6 -6
- data/lib/rubocop/cop/style/if_inside_else.rb +15 -0
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/lambda_call.rb +12 -20
- data/lib/rubocop/cop/style/map_to_hash.rb +68 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +20 -0
- data/lib/rubocop/cop/style/method_call_with_args_parentheses.rb +3 -1
- data/lib/rubocop/cop/style/method_def_parentheses.rb +17 -13
- data/lib/rubocop/cop/style/nested_file_dirname.rb +66 -0
- data/lib/rubocop/cop/style/numeric_literals.rb +10 -1
- data/lib/rubocop/cop/style/one_line_conditional.rb +18 -39
- data/lib/rubocop/cop/style/open_struct_use.rb +69 -0
- data/lib/rubocop/cop/style/optional_boolean_parameter.rb +3 -2
- data/lib/rubocop/cop/style/parentheses_around_condition.rb +12 -2
- data/lib/rubocop/cop/style/quoted_symbols.rb +11 -1
- data/lib/rubocop/cop/style/redundant_begin.rb +2 -6
- data/lib/rubocop/cop/style/redundant_interpolation.rb +17 -3
- data/lib/rubocop/cop/style/redundant_regexp_character_class.rb +5 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/safe_navigation.rb +1 -5
- data/lib/rubocop/cop/style/sample.rb +5 -3
- data/lib/rubocop/cop/style/select_by_regexp.rb +6 -1
- data/lib/rubocop/cop/style/single_line_block_params.rb +2 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +53 -13
- data/lib/rubocop/cop/style/string_concatenation.rb +7 -1
- data/lib/rubocop/cop/style/swap_values.rb +2 -0
- data/lib/rubocop/cop/style/ternary_parentheses.rb +16 -2
- data/lib/rubocop/cop/style/trailing_comma_in_array_literal.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_hash_literal.rb +1 -1
- data/lib/rubocop/cop/style/unless_else.rb +4 -0
- data/lib/rubocop/cop/team.rb +1 -1
- data/lib/rubocop/cop/util.rb +9 -1
- data/lib/rubocop/cop/variable_force.rb +1 -5
- data/lib/rubocop/cops_documentation_generator.rb +2 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +16 -2
- data/lib/rubocop/formatter/html_formatter.rb +5 -2
- data/lib/rubocop/formatter/json_formatter.rb +4 -1
- data/lib/rubocop/options.rb +14 -3
- data/lib/rubocop/remote_config.rb +2 -4
- data/lib/rubocop/result_cache.rb +1 -1
- data/lib/rubocop/rspec/shared_contexts.rb +4 -0
- data/lib/rubocop/runner.rb +1 -1
- data/lib/rubocop/target_ruby.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop/yaml_duplication_checker.rb +1 -1
- data/lib/rubocop.rb +12 -0
- metadata +22 -7
@@ -43,7 +43,7 @@ module RuboCop
|
|
43
43
|
end
|
44
44
|
|
45
45
|
def value_delta(pair)
|
46
|
-
return 0 if pair.value_on_new_line?
|
46
|
+
return 0 if pair.value_on_new_line? || pair.value_omission?
|
47
47
|
|
48
48
|
correct_value_column = pair.loc.operator.end.column + 1
|
49
49
|
actual_value_column = pair.value.loc.column
|
@@ -111,7 +111,8 @@ module RuboCop
|
|
111
111
|
correct_value_column = first_pair.key.loc.column +
|
112
112
|
current_pair.delimiter(true).length +
|
113
113
|
max_key_width
|
114
|
-
|
114
|
+
|
115
|
+
current_pair.value_omission? ? 0 : correct_value_column - current_pair.value.loc.column
|
115
116
|
end
|
116
117
|
end
|
117
118
|
|
@@ -134,7 +135,7 @@ module RuboCop
|
|
134
135
|
end
|
135
136
|
|
136
137
|
def value_delta(first_pair, current_pair)
|
137
|
-
first_pair.value_delta(current_pair)
|
138
|
+
current_pair.value_omission? ? 0 : first_pair.value_delta(current_pair)
|
138
139
|
end
|
139
140
|
end
|
140
141
|
|
@@ -0,0 +1,82 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# This module checks for Ruby 3.1's hash value omission syntax.
|
6
|
+
module HashShorthandSyntax
|
7
|
+
OMIT_HASH_VALUE_MSG = 'Omit the hash value.'
|
8
|
+
EXPLICIT_HASH_VALUE_MSG = 'Explicit the hash value.'
|
9
|
+
|
10
|
+
def on_pair(node)
|
11
|
+
return if ignore_hash_shorthand_syntax?(node)
|
12
|
+
|
13
|
+
hash_key_source = node.key.source
|
14
|
+
|
15
|
+
if enforced_shorthand_syntax == 'always'
|
16
|
+
return if node.value_omission? || require_hash_value?(hash_key_source, node)
|
17
|
+
|
18
|
+
message = OMIT_HASH_VALUE_MSG
|
19
|
+
replacement = "#{hash_key_source}:"
|
20
|
+
else
|
21
|
+
return unless node.value_omission?
|
22
|
+
|
23
|
+
message = EXPLICIT_HASH_VALUE_MSG
|
24
|
+
replacement = "#{hash_key_source}: #{hash_key_source}"
|
25
|
+
end
|
26
|
+
|
27
|
+
add_offense(node.value, message: message) do |corrector|
|
28
|
+
corrector.replace(node, replacement)
|
29
|
+
end
|
30
|
+
end
|
31
|
+
|
32
|
+
private
|
33
|
+
|
34
|
+
def ignore_hash_shorthand_syntax?(pair_node)
|
35
|
+
target_ruby_version <= 3.0 || enforced_shorthand_syntax == 'either' ||
|
36
|
+
!pair_node.parent.hash_type?
|
37
|
+
end
|
38
|
+
|
39
|
+
def enforced_shorthand_syntax
|
40
|
+
cop_config.fetch('EnforcedShorthandSyntax', 'always')
|
41
|
+
end
|
42
|
+
|
43
|
+
def require_hash_value?(hash_key_source, node)
|
44
|
+
return true if !node.key.sym_type? || require_hash_value_for_around_hash_literal?(node)
|
45
|
+
|
46
|
+
hash_value = node.value
|
47
|
+
return true unless hash_value.send_type? || hash_value.lvar_type?
|
48
|
+
|
49
|
+
hash_key_source != hash_value.source || hash_key_source.end_with?('!', '?')
|
50
|
+
end
|
51
|
+
|
52
|
+
def require_hash_value_for_around_hash_literal?(node)
|
53
|
+
return false unless (ancestor = node.parent.parent)
|
54
|
+
return false if ancestor.send_type? && ancestor.method?(:[])
|
55
|
+
|
56
|
+
!node.parent.braces? && !use_element_of_hash_literal_as_receiver?(ancestor, node.parent) &&
|
57
|
+
(use_modifier_form_without_parenthesized_method_call?(ancestor) ||
|
58
|
+
without_parentheses_call_expr_follows?(ancestor))
|
59
|
+
end
|
60
|
+
|
61
|
+
def use_element_of_hash_literal_as_receiver?(ancestor, parent)
|
62
|
+
# `{value:}.do_something` is a valid syntax.
|
63
|
+
ancestor.send_type? && ancestor.receiver == parent
|
64
|
+
end
|
65
|
+
|
66
|
+
def use_modifier_form_without_parenthesized_method_call?(ancestor)
|
67
|
+
return false if ancestor.respond_to?(:parenthesized?) && ancestor.parenthesized?
|
68
|
+
return false unless (parent = ancestor.parent)
|
69
|
+
|
70
|
+
parent.respond_to?(:modifier_form?) && parent.modifier_form?
|
71
|
+
end
|
72
|
+
|
73
|
+
def without_parentheses_call_expr_follows?(ancestor)
|
74
|
+
right_sibling = ancestor.right_sibling
|
75
|
+
right_sibling ||= ancestor.each_ancestor.find(&:assignment?)&.right_sibling
|
76
|
+
return false unless right_sibling
|
77
|
+
|
78
|
+
ancestor.respond_to?(:parenthesized?) && !ancestor.parenthesized? && !!right_sibling
|
79
|
+
end
|
80
|
+
end
|
81
|
+
end
|
82
|
+
end
|
@@ -39,12 +39,7 @@ module RuboCop
|
|
39
39
|
pos + indentation_difference(line)
|
40
40
|
end
|
41
41
|
|
42
|
-
|
43
|
-
# This allows for URIs that are wrapped in quotes or parens to be handled properly
|
44
|
-
# while not allowing additional words to be added after the URL.
|
45
|
-
if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
|
46
|
-
end_position += match.offset(0).last
|
47
|
-
end
|
42
|
+
end_position = extend_uri_end_position(line, end_position)
|
48
43
|
|
49
44
|
return nil if begin_position < max_line_length && end_position < max_line_length
|
50
45
|
|
@@ -65,6 +60,22 @@ module RuboCop
|
|
65
60
|
(line.index(/[^\t]/) || 0) * (tab_indentation_width - 1)
|
66
61
|
end
|
67
62
|
|
63
|
+
def extend_uri_end_position(line, end_position)
|
64
|
+
# Extend the end position YARD comments with linked URLs of the form {<uri> <title>}
|
65
|
+
if line&.match(/{(\s|\S)*}$/)
|
66
|
+
match = line[end_position..line_length(line)]&.match(/(\s|\S)*}/)
|
67
|
+
end_position += match.offset(0).last
|
68
|
+
end
|
69
|
+
|
70
|
+
# Extend the end position until the start of the next word, if any.
|
71
|
+
# This allows for URIs that are wrapped in quotes or parens to be handled properly
|
72
|
+
# while not allowing additional words to be added after the URL.
|
73
|
+
if (match = line[end_position..line_length(line)]&.match(/^\S+(?=\s|$)/))
|
74
|
+
end_position += match.offset(0).last
|
75
|
+
end
|
76
|
+
end_position
|
77
|
+
end
|
78
|
+
|
68
79
|
def tab_indentation_width
|
69
80
|
config.for_cop('Layout/IndentationStyle')['IndentationWidth'] ||
|
70
81
|
config.for_cop('Layout/IndentationWidth')['Width']
|
@@ -29,7 +29,9 @@ module RuboCop
|
|
29
29
|
# b c { block }. <-- b is indented relative to a
|
30
30
|
# d <-- d is indented relative to a
|
31
31
|
def left_hand_side(lhs)
|
32
|
-
|
32
|
+
while lhs.parent&.send_type? && lhs.parent.loc.dot && !lhs.parent.assignment_method?
|
33
|
+
lhs = lhs.parent
|
34
|
+
end
|
33
35
|
lhs
|
34
36
|
end
|
35
37
|
|
@@ -13,11 +13,7 @@ module RuboCop
|
|
13
13
|
if style == :single_quotes
|
14
14
|
!double_quotes_required?(src)
|
15
15
|
else
|
16
|
-
|
17
|
-
# 1. It contains a double quote
|
18
|
-
# 2. It contains text that would become an escape sequence with double quotes
|
19
|
-
# 3. It contains text that would become an interpolation with double quotes
|
20
|
-
!/" | (?<!\\)\\[aAbcdefkMnprsStuUxzZ0-7] | \#[@{$]/x.match?(src)
|
16
|
+
!/" | \\[^'\\] | \#[@{$]/x.match?(src)
|
21
17
|
end
|
22
18
|
end
|
23
19
|
end
|
@@ -0,0 +1,121 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Naming
|
6
|
+
# In Ruby 3.1, anonymous block forwarding has been added.
|
7
|
+
#
|
8
|
+
# This cop identifies places where `do_something(&block)` can be replaced
|
9
|
+
# by `do_something(&)`.
|
10
|
+
#
|
11
|
+
# It also supports the opposite style by alternative `explicit` option.
|
12
|
+
# You can specify the block variable name for auto-correction with `BlockForwardingName`.
|
13
|
+
# The default variable name is `block`. If the name is already in use, it will not be
|
14
|
+
# auto-corrected.
|
15
|
+
#
|
16
|
+
# @example EnforcedStyle: anonymous (default)
|
17
|
+
#
|
18
|
+
# # bad
|
19
|
+
# def foo(&block)
|
20
|
+
# bar(&block)
|
21
|
+
# end
|
22
|
+
#
|
23
|
+
# # good
|
24
|
+
# def foo(&)
|
25
|
+
# bar(&)
|
26
|
+
# end
|
27
|
+
#
|
28
|
+
# @example EnforcedStyle: explicit
|
29
|
+
#
|
30
|
+
# # bad
|
31
|
+
# def foo(&)
|
32
|
+
# bar(&)
|
33
|
+
# end
|
34
|
+
#
|
35
|
+
# # good
|
36
|
+
# def foo(&block)
|
37
|
+
# bar(&block)
|
38
|
+
# end
|
39
|
+
#
|
40
|
+
class BlockForwarding < Base
|
41
|
+
include ConfigurableEnforcedStyle
|
42
|
+
include RangeHelp
|
43
|
+
extend AutoCorrector
|
44
|
+
extend TargetRubyVersion
|
45
|
+
|
46
|
+
minimum_target_ruby_version 3.1
|
47
|
+
|
48
|
+
MSG = 'Use %<style>s block forwarding.'
|
49
|
+
|
50
|
+
def on_def(node)
|
51
|
+
return if node.arguments.empty?
|
52
|
+
|
53
|
+
last_argument = node.arguments.last
|
54
|
+
return if expected_block_forwarding_style?(node, last_argument)
|
55
|
+
|
56
|
+
register_offense(last_argument, node)
|
57
|
+
|
58
|
+
node.each_descendant(:block_pass) do |block_pass_node|
|
59
|
+
next if block_pass_node.children.first&.sym_type? ||
|
60
|
+
last_argument.source != block_pass_node.source
|
61
|
+
|
62
|
+
register_offense(block_pass_node, node)
|
63
|
+
end
|
64
|
+
end
|
65
|
+
alias on_defs on_def
|
66
|
+
|
67
|
+
private
|
68
|
+
|
69
|
+
def expected_block_forwarding_style?(node, last_argument)
|
70
|
+
if style == :anonymous
|
71
|
+
!explicit_block_argument?(last_argument) ||
|
72
|
+
use_kwarg_in_method_definition?(node) ||
|
73
|
+
use_block_argument_as_local_variable?(node, last_argument.source[1..-1])
|
74
|
+
else
|
75
|
+
!anonymous_block_argument?(last_argument)
|
76
|
+
end
|
77
|
+
end
|
78
|
+
|
79
|
+
def use_kwarg_in_method_definition?(node)
|
80
|
+
node.arguments.each_descendant(:kwarg, :kwoptarg).any?
|
81
|
+
end
|
82
|
+
|
83
|
+
def anonymous_block_argument?(node)
|
84
|
+
node.blockarg_type? && node.name.nil?
|
85
|
+
end
|
86
|
+
|
87
|
+
def explicit_block_argument?(node)
|
88
|
+
node.blockarg_type? && !node.name.nil?
|
89
|
+
end
|
90
|
+
|
91
|
+
def register_offense(block_argument, node)
|
92
|
+
add_offense(block_argument, message: format(MSG, style: style)) do |corrector|
|
93
|
+
if style == :anonymous
|
94
|
+
corrector.replace(block_argument, '&')
|
95
|
+
|
96
|
+
arguments = block_argument.parent
|
97
|
+
|
98
|
+
add_parentheses(arguments, corrector) unless arguments.parenthesized_call?
|
99
|
+
else
|
100
|
+
unless use_block_argument_as_local_variable?(node, block_forwarding_name)
|
101
|
+
corrector.replace(block_argument, "&#{block_forwarding_name}")
|
102
|
+
end
|
103
|
+
end
|
104
|
+
end
|
105
|
+
end
|
106
|
+
|
107
|
+
def use_block_argument_as_local_variable?(node, last_argument)
|
108
|
+
return if node.body.nil?
|
109
|
+
|
110
|
+
node.body.each_descendant(:lvar).any? do |lvar|
|
111
|
+
!lvar.parent.block_pass_type? && lvar.source == last_argument
|
112
|
+
end
|
113
|
+
end
|
114
|
+
|
115
|
+
def block_forwarding_name
|
116
|
+
cop_config.fetch('BlockForwardingName', 'block')
|
117
|
+
end
|
118
|
+
end
|
119
|
+
end
|
120
|
+
end
|
121
|
+
end
|
@@ -14,6 +14,18 @@ module RuboCop
|
|
14
14
|
# (i.e. `bundler-console` becomes `Bundler::Console`). As such, the
|
15
15
|
# gemspec is supposed to be named `bundler-console.gemspec`.
|
16
16
|
#
|
17
|
+
# When `ExpectMatchingDefinition` (default: `false`) is `true`, the cop requires
|
18
|
+
# each file to have a class, module or `Struct` defined in it that matches
|
19
|
+
# the filename. This can be further configured using
|
20
|
+
# `CheckDefinitionPathHierarchy` (default: `true`) to determine whether the
|
21
|
+
# path should match the namespace of the above definition.
|
22
|
+
#
|
23
|
+
# When `IgnoreExecutableScripts` (default: `true`) is `true`, files that start
|
24
|
+
# with a shebang line are not considered by the cop.
|
25
|
+
#
|
26
|
+
# When `Regex` is set, the cop will flag any filename that does not match
|
27
|
+
# the regular expression.
|
28
|
+
#
|
17
29
|
# @example
|
18
30
|
# # bad
|
19
31
|
# lib/layoutManager.rb
|
@@ -28,11 +40,19 @@ module RuboCop
|
|
28
40
|
include RangeHelp
|
29
41
|
|
30
42
|
MSG_SNAKE_CASE = 'The name of this source file (`%<basename>s`) should use snake_case.'
|
31
|
-
MSG_NO_DEFINITION = '
|
43
|
+
MSG_NO_DEFINITION = '`%<basename>s` should define a class or module called `%<namespace>s`.'
|
32
44
|
MSG_REGEX = '`%<basename>s` should match `%<regex>s`.'
|
33
45
|
|
34
46
|
SNAKE_CASE = /^[\d[[:lower:]]_.?!]+$/.freeze
|
35
47
|
|
48
|
+
# @!method struct_definition(node)
|
49
|
+
def_node_matcher :struct_definition, <<~PATTERN
|
50
|
+
{
|
51
|
+
(casgn $_ $_ (send (const {nil? cbase} :Struct) :new ...))
|
52
|
+
(casgn $_ $_ (block (send (const {nil? cbase} :Struct) :new ...) ...))
|
53
|
+
}
|
54
|
+
PATTERN
|
55
|
+
|
36
56
|
def on_new_investigation
|
37
57
|
file_path = processed_source.file_path
|
38
58
|
return if config.file_to_exclude?(file_path) || config.allowed_camel_case_file?(file_path)
|
@@ -103,6 +123,10 @@ module RuboCop
|
|
103
123
|
cop_config['CheckDefinitionPathHierarchy']
|
104
124
|
end
|
105
125
|
|
126
|
+
def definition_path_hierarchy_roots
|
127
|
+
cop_config['CheckDefinitionPathHierarchyRoots'] || []
|
128
|
+
end
|
129
|
+
|
106
130
|
def regex
|
107
131
|
cop_config['Regex']
|
108
132
|
end
|
@@ -126,7 +150,7 @@ module RuboCop
|
|
126
150
|
name = namespace.pop
|
127
151
|
|
128
152
|
on_node(%i[class module casgn], node) do |child|
|
129
|
-
next unless (const = child
|
153
|
+
next unless (const = find_definition(child))
|
130
154
|
|
131
155
|
const_namespace, const_name = *const
|
132
156
|
next if name != const_name && !match_acronym?(name, const_name)
|
@@ -138,6 +162,15 @@ module RuboCop
|
|
138
162
|
nil
|
139
163
|
end
|
140
164
|
|
165
|
+
def find_definition(node)
|
166
|
+
node.defined_module || defined_struct(node)
|
167
|
+
end
|
168
|
+
|
169
|
+
def defined_struct(node)
|
170
|
+
namespace, name = *struct_definition(node)
|
171
|
+
s(:const, namespace, name) if name
|
172
|
+
end
|
173
|
+
|
141
174
|
def match_namespace(node, namespace, expected)
|
142
175
|
match_partial = partial_matcher!(expected)
|
143
176
|
|
@@ -177,13 +210,13 @@ module RuboCop
|
|
177
210
|
allowed_acronyms.any? { |acronym| expected.gsub(acronym.capitalize, acronym) == name }
|
178
211
|
end
|
179
212
|
|
180
|
-
def to_namespace(path)
|
213
|
+
def to_namespace(path) # rubocop:disable Metrics/AbcSize
|
181
214
|
components = Pathname(path).each_filename.to_a
|
182
215
|
# To convert a pathname to a Ruby namespace, we need a starting point
|
183
216
|
# But RC can be run from any working directory, and can check any path
|
184
217
|
# We can't assume that the working directory, or any other, is the
|
185
218
|
# "starting point" to build a namespace.
|
186
|
-
start =
|
219
|
+
start = definition_path_hierarchy_roots
|
187
220
|
start_index = nil
|
188
221
|
|
189
222
|
# To find the closest namespace root take the path components, and
|
@@ -3,7 +3,8 @@
|
|
3
3
|
module RuboCop
|
4
4
|
module Cop
|
5
5
|
module Security
|
6
|
-
# This cop checks for the use of `Kernel#open` and `URI.open
|
6
|
+
# This cop checks for the use of `Kernel#open` and `URI.open` with dynamic
|
7
|
+
# data.
|
7
8
|
#
|
8
9
|
# `Kernel#open` and `URI.open` enable not only file access but also process
|
9
10
|
# invocation by prefixing a pipe symbol (e.g., `open("| ls")`).
|
@@ -11,6 +12,9 @@ module RuboCop
|
|
11
12
|
# the argument of `Kernel#open` and `URI.open`. It would be better to use
|
12
13
|
# `File.open`, `IO.popen` or `URI.parse#open` explicitly.
|
13
14
|
#
|
15
|
+
# NOTE: `open` and `URI.open` with literal strings are not flagged by this
|
16
|
+
# cop.
|
17
|
+
#
|
14
18
|
# @safety
|
15
19
|
# This cop could register false positives if `open` is redefined
|
16
20
|
# in a class and then used without a receiver in that class.
|
@@ -18,12 +22,18 @@ module RuboCop
|
|
18
22
|
# @example
|
19
23
|
# # bad
|
20
24
|
# open(something)
|
25
|
+
# open("| #{something}")
|
21
26
|
# URI.open(something)
|
22
27
|
#
|
23
28
|
# # good
|
24
29
|
# File.open(something)
|
25
30
|
# IO.popen(something)
|
26
31
|
# URI.parse(something).open
|
32
|
+
#
|
33
|
+
# # good (literal strings)
|
34
|
+
# open("foo.text")
|
35
|
+
# open("| foo")
|
36
|
+
# URI.open("http://example.com")
|
27
37
|
class Open < Base
|
28
38
|
MSG = 'The use of `%<receiver>sopen` is a serious security risk.'
|
29
39
|
RESTRICT_ON_SEND = %i[open].freeze
|
@@ -7,17 +7,21 @@ module RuboCop
|
|
7
7
|
# potential security issues leading to remote code execution when
|
8
8
|
# loading from an untrusted source.
|
9
9
|
#
|
10
|
+
# NOTE: Ruby 3.1+ (Psych 4) uses `Psych.load` as `Psych.safe_load` by default.
|
11
|
+
#
|
10
12
|
# @safety
|
11
13
|
# The behaviour of the code might change depending on what was
|
12
14
|
# in the YAML payload, since `YAML.safe_load` is more restrictive.
|
13
15
|
#
|
14
16
|
# @example
|
15
17
|
# # bad
|
16
|
-
# YAML.load("---
|
18
|
+
# YAML.load("--- !ruby/object:Foo {}") # Psych 3 is unsafe by default
|
17
19
|
#
|
18
20
|
# # good
|
19
|
-
# YAML.safe_load("---
|
20
|
-
# YAML.
|
21
|
+
# YAML.safe_load("--- !ruby/object:Foo {}", [Foo]) # Ruby 2.5 (Psych 3)
|
22
|
+
# YAML.safe_load("--- !ruby/object:Foo {}", permitted_classes: [Foo]) # Ruby 3.0- (Psych 3)
|
23
|
+
# YAML.load("--- !ruby/object:Foo {}", permitted_classes: [Foo]) # Ruby 3.1+ (Psych 4)
|
24
|
+
# YAML.dump(foo)
|
21
25
|
#
|
22
26
|
class YAMLLoad < Base
|
23
27
|
extend AutoCorrector
|
@@ -31,6 +35,8 @@ module RuboCop
|
|
31
35
|
PATTERN
|
32
36
|
|
33
37
|
def on_send(node)
|
38
|
+
return if target_ruby_version >= 3.1
|
39
|
+
|
34
40
|
yaml_load(node) do
|
35
41
|
add_offense(node.loc.selector) do |corrector|
|
36
42
|
corrector.replace(node.loc.selector, 'safe_load')
|
@@ -4,6 +4,12 @@ module RuboCop
|
|
4
4
|
module Cop
|
5
5
|
module Style
|
6
6
|
# Checks for uses of the character literal ?x.
|
7
|
+
# Starting with Ruby 1.9 character literals are
|
8
|
+
# essentially one-character strings, so this syntax
|
9
|
+
# is mostly redundant at this point.
|
10
|
+
#
|
11
|
+
# ? character literal can be used to express meta and control character.
|
12
|
+
# That's a good use case of ? literal so it doesn't count it as an offense.
|
7
13
|
#
|
8
14
|
# @example
|
9
15
|
# # bad
|
@@ -12,8 +18,9 @@ module RuboCop
|
|
12
18
|
# # good
|
13
19
|
# 'x'
|
14
20
|
#
|
15
|
-
# # good
|
21
|
+
# # good - control & meta escapes
|
16
22
|
# ?\C-\M-d
|
23
|
+
# "\C-\M-d" # same as above
|
17
24
|
class CharacterLiteral < Base
|
18
25
|
include StringHelp
|
19
26
|
extend AutoCorrector
|
@@ -16,6 +16,7 @@ module RuboCop
|
|
16
16
|
#
|
17
17
|
# @example
|
18
18
|
# # bad
|
19
|
+
# array.reject(&:nil?)
|
19
20
|
# array.reject { |e| e.nil? }
|
20
21
|
# array.select { |e| !e.nil? }
|
21
22
|
#
|
@@ -23,6 +24,7 @@ module RuboCop
|
|
23
24
|
# array.compact
|
24
25
|
#
|
25
26
|
# # bad
|
27
|
+
# hash.reject!(&:nil?)
|
26
28
|
# hash.reject! { |k, v| v.nil? }
|
27
29
|
# hash.select! { |k, v| !v.nil? }
|
28
30
|
#
|
@@ -37,11 +39,18 @@ module RuboCop
|
|
37
39
|
|
38
40
|
RESTRICT_ON_SEND = %i[reject reject! select select!].freeze
|
39
41
|
|
42
|
+
# @!method reject_method_with_block_pass?(node)
|
43
|
+
def_node_matcher :reject_method_with_block_pass?, <<~PATTERN
|
44
|
+
(send !nil? {:reject :reject!}
|
45
|
+
(block_pass
|
46
|
+
(sym :nil?)))
|
47
|
+
PATTERN
|
48
|
+
|
40
49
|
# @!method reject_method?(node)
|
41
50
|
def_node_matcher :reject_method?, <<~PATTERN
|
42
51
|
(block
|
43
52
|
(send
|
44
|
-
|
53
|
+
!nil? {:reject :reject!})
|
45
54
|
$(args ...)
|
46
55
|
(send
|
47
56
|
$(lvar _) :nil?))
|
@@ -51,7 +60,7 @@ module RuboCop
|
|
51
60
|
def_node_matcher :select_method?, <<~PATTERN
|
52
61
|
(block
|
53
62
|
(send
|
54
|
-
|
63
|
+
!nil? {:select :select!})
|
55
64
|
$(args ...)
|
56
65
|
(send
|
57
66
|
(send
|
@@ -59,16 +68,9 @@ module RuboCop
|
|
59
68
|
PATTERN
|
60
69
|
|
61
70
|
def on_send(node)
|
62
|
-
|
63
|
-
return unless block_node&.block_type?
|
64
|
-
|
65
|
-
return unless (method_name, args, receiver =
|
66
|
-
reject_method?(block_node) || select_method?(block_node))
|
71
|
+
return unless (range = offense_range(node))
|
67
72
|
|
68
|
-
|
69
|
-
|
70
|
-
range = offense_range(node, block_node)
|
71
|
-
good = good_method_name(method_name)
|
73
|
+
good = good_method_name(node.method_name)
|
72
74
|
message = format(MSG, good: good, bad: range.source)
|
73
75
|
|
74
76
|
add_offense(range, message: message) { |corrector| corrector.replace(range, good) }
|
@@ -76,6 +78,22 @@ module RuboCop
|
|
76
78
|
|
77
79
|
private
|
78
80
|
|
81
|
+
def offense_range(node)
|
82
|
+
if reject_method_with_block_pass?(node)
|
83
|
+
range(node, node)
|
84
|
+
else
|
85
|
+
block_node = node.parent
|
86
|
+
|
87
|
+
return unless block_node&.block_type?
|
88
|
+
unless (args, receiver = reject_method?(block_node) || select_method?(block_node))
|
89
|
+
return
|
90
|
+
end
|
91
|
+
return unless args.last.source == receiver.source
|
92
|
+
|
93
|
+
range(node, block_node)
|
94
|
+
end
|
95
|
+
end
|
96
|
+
|
79
97
|
def good_method_name(method_name)
|
80
98
|
if method_name.to_s.end_with?('!')
|
81
99
|
'compact!'
|
@@ -84,8 +102,8 @@ module RuboCop
|
|
84
102
|
end
|
85
103
|
end
|
86
104
|
|
87
|
-
def
|
88
|
-
range_between(
|
105
|
+
def range(begin_pos_node, end_pos_node)
|
106
|
+
range_between(begin_pos_node.loc.selector.begin_pos, end_pos_node.loc.end.end_pos)
|
89
107
|
end
|
90
108
|
end
|
91
109
|
end
|
@@ -77,14 +77,14 @@ module RuboCop
|
|
77
77
|
|
78
78
|
def collection_looping_method?(node)
|
79
79
|
# TODO: Remove `Symbol#to_s` after supporting only Ruby >= 2.7.
|
80
|
-
method_name = node.
|
80
|
+
method_name = node.method_name.to_s
|
81
81
|
method_name.start_with?('each') || method_name.end_with?('_each')
|
82
82
|
end
|
83
83
|
|
84
84
|
def same_collection_looping?(node, sibling)
|
85
85
|
sibling&.block_type? &&
|
86
86
|
sibling.send_node.method?(node.method_name) &&
|
87
|
-
sibling.
|
87
|
+
sibling.receiver == node.receiver &&
|
88
88
|
sibling.send_node.arguments == node.send_node.arguments
|
89
89
|
end
|
90
90
|
end
|
@@ -11,27 +11,33 @@ module RuboCop
|
|
11
11
|
#
|
12
12
|
# # bad
|
13
13
|
# def foo()
|
14
|
-
#
|
14
|
+
# do_something
|
15
15
|
# end
|
16
16
|
#
|
17
17
|
# # good
|
18
18
|
# def foo
|
19
|
-
#
|
19
|
+
# do_something
|
20
20
|
# end
|
21
21
|
#
|
22
|
-
# #
|
23
|
-
# def foo()
|
22
|
+
# # bad
|
23
|
+
# def foo() = do_something
|
24
|
+
#
|
25
|
+
# # good
|
26
|
+
# def foo = do_something
|
27
|
+
#
|
28
|
+
# # good (without parentheses it's a syntax error)
|
29
|
+
# def foo() do_something end
|
24
30
|
#
|
25
31
|
# @example
|
26
32
|
#
|
27
33
|
# # bad
|
28
34
|
# def Baz.foo()
|
29
|
-
#
|
35
|
+
# do_something
|
30
36
|
# end
|
31
37
|
#
|
32
38
|
# # good
|
33
39
|
# def Baz.foo
|
34
|
-
#
|
40
|
+
# do_something
|
35
41
|
# end
|
36
42
|
class DefWithParentheses < Base
|
37
43
|
extend AutoCorrector
|
@@ -39,12 +45,11 @@ module RuboCop
|
|
39
45
|
MSG = "Omit the parentheses in defs when the method doesn't accept any arguments."
|
40
46
|
|
41
47
|
def on_def(node)
|
42
|
-
return if node.single_line?
|
43
|
-
return unless !node.arguments? && (
|
48
|
+
return if node.single_line? && !node.endless?
|
49
|
+
return unless !node.arguments? && (node_arguments = node.arguments.source_range)
|
44
50
|
|
45
|
-
add_offense(
|
46
|
-
corrector.remove(
|
47
|
-
corrector.remove(node.arguments.loc.end)
|
51
|
+
add_offense(node_arguments) do |corrector|
|
52
|
+
corrector.remove(node_arguments)
|
48
53
|
end
|
49
54
|
end
|
50
55
|
alias on_defs on_def
|