rubocop 1.92.3 → 1.93.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 +1 -1
- data/config/default.yml +5 -1
- data/lib/rubocop/config.rb +6 -0
- data/lib/rubocop/config_store.rb +1 -0
- data/lib/rubocop/config_validator.rb +45 -1
- data/lib/rubocop/cop/base.rb +5 -1
- data/lib/rubocop/cop/correctors/for_to_each_corrector.rb +25 -1
- data/lib/rubocop/cop/gemspec/ruby_version_globals_usage.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/invalid_node_pattern.rb +91 -0
- data/lib/rubocop/cop/internal_affairs/node_search_as_condition.rb +79 -0
- data/lib/rubocop/cop/internal_affairs/redundant_autocorrect_incompatible_with.rb +83 -0
- data/lib/rubocop/cop/internal_affairs.rb +3 -0
- data/lib/rubocop/cop/layout/empty_lines_after_module_inclusion.rb +16 -4
- data/lib/rubocop/cop/layout/empty_lines_around_access_modifier.rb +25 -7
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/line_length.rb +14 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +47 -9
- data/lib/rubocop/cop/lint/duplicate_require.rb +1 -1
- data/lib/rubocop/cop/lint/lambda_without_literal_block.rb +17 -4
- data/lib/rubocop/cop/lint/non_atomic_file_operation.rb +11 -3
- data/lib/rubocop/cop/lint/number_conversion.rb +10 -0
- data/lib/rubocop/cop/lint/redundant_string_coercion.rb +16 -2
- data/lib/rubocop/cop/lint/require_relative_self_path.rb +36 -1
- data/lib/rubocop/cop/lint/shadowing_outer_local_variable.rb +1 -1
- data/lib/rubocop/cop/lint/useless_assignment.rb +45 -1
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +3 -18
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_numeric_operation.rb +18 -6
- data/lib/rubocop/cop/lint/useless_times.rb +40 -12
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +16 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +3 -3
- data/lib/rubocop/cop/naming/block_forwarding.rb +2 -2
- data/lib/rubocop/cop/style/arguments_forwarding.rb +10 -9
- data/lib/rubocop/cop/style/array_coercion.rb +1 -1
- data/lib/rubocop/cop/style/array_intersect.rb +15 -7
- data/lib/rubocop/cop/style/block_delimiters.rb +0 -1
- data/lib/rubocop/cop/style/data_inheritance.rb +78 -9
- data/lib/rubocop/cop/style/dig_chain.rb +13 -2
- data/lib/rubocop/cop/style/each_with_object.rb +21 -5
- data/lib/rubocop/cop/style/empty_class_definition.rb +19 -1
- data/lib/rubocop/cop/style/empty_method.rb +11 -0
- data/lib/rubocop/cop/style/explicit_block_argument.rb +1 -1
- data/lib/rubocop/cop/style/fetch_env_var.rb +5 -5
- data/lib/rubocop/cop/style/file_read.rb +5 -1
- data/lib/rubocop/cop/style/file_write.rb +89 -20
- data/lib/rubocop/cop/style/hash_lookup_method.rb +2 -4
- data/lib/rubocop/cop/style/hash_transform_keys.rb +1 -1
- data/lib/rubocop/cop/style/hash_transform_values.rb +1 -1
- data/lib/rubocop/cop/style/line_end_concatenation.rb +1 -1
- data/lib/rubocop/cop/style/map_to_hash.rb +0 -4
- data/lib/rubocop/cop/style/method_def_parentheses.rb +0 -4
- data/lib/rubocop/cop/style/multiline_method_signature.rb +15 -5
- data/lib/rubocop/cop/style/multiline_ternary_operator.rb +31 -1
- data/lib/rubocop/cop/style/negated_if_else_condition.rb +9 -1
- data/lib/rubocop/cop/style/nested_parenthesized_calls.rb +0 -4
- data/lib/rubocop/cop/style/nil_comparison.rb +10 -0
- data/lib/rubocop/cop/style/nil_lambda.rb +4 -11
- data/lib/rubocop/cop/style/non_nil_check.rb +13 -0
- data/lib/rubocop/cop/style/operator_method_call.rb +14 -0
- data/lib/rubocop/cop/style/raise_args.rb +15 -0
- data/lib/rubocop/cop/style/redundant_array_constructor.rb +14 -0
- data/lib/rubocop/cop/style/redundant_begin.rb +26 -6
- data/lib/rubocop/cop/style/redundant_each.rb +18 -0
- data/lib/rubocop/cop/style/redundant_exception.rb +16 -6
- data/lib/rubocop/cop/style/redundant_initialize.rb +115 -2
- data/lib/rubocop/cop/style/redundant_interpolation.rb +2 -1
- data/lib/rubocop/cop/style/redundant_interpolation_unfreeze.rb +2 -2
- data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -0
- data/lib/rubocop/cop/style/redundant_regexp_constructor.rb +11 -1
- data/lib/rubocop/cop/style/redundant_self.rb +1 -1
- data/lib/rubocop/cop/style/redundant_self_assignment.rb +20 -12
- data/lib/rubocop/cop/style/redundant_sort.rb +1 -1
- data/lib/rubocop/cop/style/rescue_modifier.rb +0 -4
- data/lib/rubocop/cop/style/select_by_kind.rb +1 -6
- data/lib/rubocop/cop/style/select_by_range.rb +1 -6
- data/lib/rubocop/cop/style/select_by_regexp.rb +1 -6
- data/lib/rubocop/cop/style/self_assignment.rb +0 -4
- data/lib/rubocop/cop/style/single_argument_dig.rb +22 -2
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +12 -6
- data/lib/rubocop/cop/style/struct_inheritance.rb +68 -16
- data/lib/rubocop/cop/style/swap_values.rb +10 -13
- data/lib/rubocop/cop/style/symbol_proc.rb +1 -1
- data/lib/rubocop/cop/style/trailing_comma_in_arguments.rb +0 -4
- data/lib/rubocop/rspec/cop_helper.rb +8 -2
- data/lib/rubocop/rspec/shared_contexts.rb +11 -11
- data/lib/rubocop/runner.rb +16 -7
- data/lib/rubocop/target_ruby.rb +6 -8
- data/lib/rubocop/version.rb +18 -8
- metadata +9 -6
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: f85c9581854e9f222ff94a59b3f3547ab227d98951a7f141cf7a2d1b0339c5e5
|
|
4
|
+
data.tar.gz: 4707a5a1502dc167f12e70a19c3d29d7860701aacc8fd350d2ed2d3489b4a4f3
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 9dd135a4463eb93e7f656a2d50bda93a2e8bae7592f2573a69e8c9d951c902a737b790c0a2d2c7febfc9237a00b79318697cc2e1100f42419aaa3e89a00bcfb1
|
|
7
|
+
data.tar.gz: 45987d73a3f0b09ebf35b4bd24cb9cc481eead4a5ba31163b491f1ec35052455f9a90b9682c560bfffdabd399fb9d79da6ca104df486705d5506654f99898e60
|
data/README.md
CHANGED
|
@@ -51,7 +51,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
|
|
|
51
51
|
in your `Gemfile`:
|
|
52
52
|
|
|
53
53
|
```rb
|
|
54
|
-
gem 'rubocop', '~> 1.
|
|
54
|
+
gem 'rubocop', '~> 1.93', require: false
|
|
55
55
|
```
|
|
56
56
|
|
|
57
57
|
See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
|
data/config/default.yml
CHANGED
|
@@ -166,8 +166,12 @@ AllCops:
|
|
|
166
166
|
# - `default`
|
|
167
167
|
# - `parser_whitequark` ... https://github.com/whitequark/parser
|
|
168
168
|
# - `parser_prism` ... https://github.com/ruby/prism (`Prism::Translation::Parser`)
|
|
169
|
+
# - `prism` ... https://github.com/ruby/prism (without the translation layer)
|
|
169
170
|
# Not every parser engine can handle every `TargetRubyVersion`. `parser_whitequark`
|
|
170
|
-
# only works with versions 3.4 and below, `parser_prism` with versions 3.3
|
|
171
|
+
# only works with versions 3.4 and below, `parser_prism` and `prism` with versions 3.3
|
|
172
|
+
# and above. `prism` needs Prism 2.0 or later. `default` is `parser_whitequark` for
|
|
173
|
+
# versions below 3.3; for the others it is `prism` when Prism 2.0 or later is installed,
|
|
174
|
+
# and `parser_prism` otherwise.
|
|
171
175
|
ParserEngine: default
|
|
172
176
|
# Determines if a notification for extension libraries should be shown when
|
|
173
177
|
# rubocop is run. Keys are the name of the extension, and values are an array
|
data/lib/rubocop/config.rb
CHANGED
|
@@ -89,6 +89,12 @@ module RuboCop
|
|
|
89
89
|
self
|
|
90
90
|
end
|
|
91
91
|
|
|
92
|
+
# @api private
|
|
93
|
+
def validate_parser_engine
|
|
94
|
+
@validator.validate_parser_engine
|
|
95
|
+
self
|
|
96
|
+
end
|
|
97
|
+
|
|
92
98
|
def_delegators :@hash, :[], :[]=, :delete, :dig, :each, :key?, :keys, :each_key,
|
|
93
99
|
:fetch, :map, :merge, :replace, :to_h, :to_hash, :transform_values
|
|
94
100
|
def_delegators :@validator, :validate, :target_ruby_version
|
data/lib/rubocop/config_store.rb
CHANGED
|
@@ -23,7 +23,10 @@ module RuboCop
|
|
|
23
23
|
CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect References].to_set.freeze
|
|
24
24
|
CONFIG_CHECK_DEPARTMENTS = %w[pending preview override_department].freeze
|
|
25
25
|
CONFIG_CHECK_AUTOCORRECTS = %w[always contextual disabled].freeze
|
|
26
|
-
|
|
26
|
+
PRISM_PARSER_ENGINES = %i[parser_prism prism].freeze
|
|
27
|
+
PARSER_GEM_ENGINES = %i[parser_whitequark parser_prism].freeze
|
|
28
|
+
private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS, :PRISM_PARSER_ENGINES,
|
|
29
|
+
:PARSER_GEM_ENGINES
|
|
27
30
|
|
|
28
31
|
def_delegators :@config, :smart_loaded_path, :for_all_cops
|
|
29
32
|
|
|
@@ -64,6 +67,16 @@ module RuboCop
|
|
|
64
67
|
check_target_ruby
|
|
65
68
|
end
|
|
66
69
|
|
|
70
|
+
# Checks that the parser engine can parse the target Ruby version. This is
|
|
71
|
+
# for the runner to call before it parses a file, because a configuration
|
|
72
|
+
# that no file is inspected with is free to name any engine.
|
|
73
|
+
def validate_parser_engine
|
|
74
|
+
return if @parser_engine_validated
|
|
75
|
+
|
|
76
|
+
check_parser_engine
|
|
77
|
+
@parser_engine_validated = true
|
|
78
|
+
end
|
|
79
|
+
|
|
67
80
|
def target_ruby_version
|
|
68
81
|
target_ruby.version
|
|
69
82
|
end
|
|
@@ -99,6 +112,37 @@ module RuboCop
|
|
|
99
112
|
raise ValidationError, msg
|
|
100
113
|
end
|
|
101
114
|
|
|
115
|
+
def check_parser_engine
|
|
116
|
+
parser_engine = @config.parser_engine
|
|
117
|
+
prism = PRISM_PARSER_ENGINES.include?(parser_engine)
|
|
118
|
+
old_ruby = target_ruby_version < Version::MINIMUM_PARSABLE_PRISM_VERSION
|
|
119
|
+
|
|
120
|
+
if prism && old_ruby
|
|
121
|
+
raise ValidationError,
|
|
122
|
+
"The `#{parser_engine}` parser engine supports target Ruby versions " \
|
|
123
|
+
"#{Version::MINIMUM_PARSABLE_PRISM_VERSION} and above, but RuboCop found Ruby " \
|
|
124
|
+
"version #{target_ruby_version} in #{target_ruby.source}."
|
|
125
|
+
end
|
|
126
|
+
return if ProcessedSource.parser_gem_available?
|
|
127
|
+
|
|
128
|
+
check_parser_gem(parser_engine, old_ruby)
|
|
129
|
+
end
|
|
130
|
+
|
|
131
|
+
def check_parser_gem(parser_engine, old_ruby)
|
|
132
|
+
if old_ruby
|
|
133
|
+
raise ValidationError,
|
|
134
|
+
"RuboCop found Ruby version #{target_ruby_version} in #{target_ruby.source}, and " \
|
|
135
|
+
'analyzing code for Ruby versions below ' \
|
|
136
|
+
"#{Version::MINIMUM_PARSABLE_PRISM_VERSION} needs the `parser` gem. " \
|
|
137
|
+
'Add it to your Gemfile.'
|
|
138
|
+
end
|
|
139
|
+
return unless PARSER_GEM_ENGINES.include?(parser_engine)
|
|
140
|
+
|
|
141
|
+
raise ValidationError, "The `#{parser_engine}` parser engine needs the `parser` gem. " \
|
|
142
|
+
'Add it to your Gemfile, or remove `ParserEngine` from your ' \
|
|
143
|
+
'configuration.'
|
|
144
|
+
end
|
|
145
|
+
|
|
102
146
|
def alert_about_unrecognized_cops(invalid_cop_names)
|
|
103
147
|
unknown_cops = list_unknown_cops(invalid_cop_names)
|
|
104
148
|
|
data/lib/rubocop/cop/base.rb
CHANGED
|
@@ -319,8 +319,12 @@ module RuboCop
|
|
|
319
319
|
end
|
|
320
320
|
|
|
321
321
|
# There should be very limited reasons for a Cop to do it's own parsing
|
|
322
|
+
# The source is parsed the way the file under investigation was.
|
|
322
323
|
def parse(source, path = nil)
|
|
323
|
-
|
|
324
|
+
ruby_version = processed_source&.ruby_version || target_ruby_version
|
|
325
|
+
engine = processed_source&.parser_engine || parser_engine
|
|
326
|
+
|
|
327
|
+
ProcessedSource.new(source, ruby_version, path, parser_engine: engine)
|
|
324
328
|
end
|
|
325
329
|
|
|
326
330
|
# @api private
|
|
@@ -28,7 +28,7 @@ module RuboCop
|
|
|
28
28
|
closing_parenthesis = requires_parentheses? ? ')' : ''
|
|
29
29
|
dot = collection_node.csend_type? ? '&.' : '.'
|
|
30
30
|
|
|
31
|
-
"#{closing_parenthesis}#{dot}each do |#{
|
|
31
|
+
"#{closing_parenthesis}#{dot}each do |#{block_parameters}|"
|
|
32
32
|
end
|
|
33
33
|
|
|
34
34
|
def requires_parentheses?
|
|
@@ -56,6 +56,30 @@ module RuboCop
|
|
|
56
56
|
collection_node.source_range
|
|
57
57
|
end
|
|
58
58
|
end
|
|
59
|
+
|
|
60
|
+
def block_parameters
|
|
61
|
+
parameters = variable_node.source_range
|
|
62
|
+
source = parameters.source
|
|
63
|
+
|
|
64
|
+
trailing_commas_in_parentheses.reverse_each do |comma|
|
|
65
|
+
source[comma.begin_pos - parameters.begin_pos, comma.size] = ''
|
|
66
|
+
end
|
|
67
|
+
source << ',' if trailing_comma_required?
|
|
68
|
+
source
|
|
69
|
+
end
|
|
70
|
+
|
|
71
|
+
def trailing_commas_in_parentheses
|
|
72
|
+
variable_node.each_node(:mlhs).filter_map do |mlhs|
|
|
73
|
+
next unless mlhs.loc_is?(:end, ')')
|
|
74
|
+
|
|
75
|
+
gap = mlhs.children.last.source_range.end.join(mlhs.loc.end.begin)
|
|
76
|
+
gap if gap.source.include?(',')
|
|
77
|
+
end.sort_by(&:begin_pos)
|
|
78
|
+
end
|
|
79
|
+
|
|
80
|
+
def trailing_comma_required?
|
|
81
|
+
variable_node.mlhs_type? && variable_node.children.one? && !variable_node.loc.begin
|
|
82
|
+
end
|
|
59
83
|
end
|
|
60
84
|
end
|
|
61
85
|
end
|
|
@@ -0,0 +1,91 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module InternalAffairs
|
|
6
|
+
# Checks node patterns for parts that can never match: node types that
|
|
7
|
+
# do not exist, sequences with a number of children that a node of that
|
|
8
|
+
# type never has, a node where a node of that type has a value like a
|
|
9
|
+
# symbol (and the other way around), and predicates that nothing responds
|
|
10
|
+
# to. Alternatives that are in a union more than once are reported too.
|
|
11
|
+
#
|
|
12
|
+
# Patterns that are built with interpolation are not checked.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# # bad
|
|
16
|
+
# def_node_matcher :my_matcher, <<~PATTERN
|
|
17
|
+
# (def _name _args)
|
|
18
|
+
# PATTERN
|
|
19
|
+
#
|
|
20
|
+
# # good
|
|
21
|
+
# def_node_matcher :my_matcher, <<~PATTERN
|
|
22
|
+
# (def _name _args _body)
|
|
23
|
+
# PATTERN
|
|
24
|
+
#
|
|
25
|
+
# # bad
|
|
26
|
+
# def_node_matcher :my_matcher, <<~PATTERN
|
|
27
|
+
# (send nil? "puts" ...)
|
|
28
|
+
# PATTERN
|
|
29
|
+
#
|
|
30
|
+
# # good
|
|
31
|
+
# def_node_matcher :my_matcher, <<~PATTERN
|
|
32
|
+
# (send nil? :puts ...)
|
|
33
|
+
# PATTERN
|
|
34
|
+
#
|
|
35
|
+
# # bad
|
|
36
|
+
# def_node_matcher :my_matcher, <<~PATTERN
|
|
37
|
+
# (send _ {:first :last :first})
|
|
38
|
+
# PATTERN
|
|
39
|
+
#
|
|
40
|
+
# # good
|
|
41
|
+
# def_node_matcher :my_matcher, <<~PATTERN
|
|
42
|
+
# (send _ {:first :last})
|
|
43
|
+
# PATTERN
|
|
44
|
+
#
|
|
45
|
+
class InvalidNodePattern < Base
|
|
46
|
+
RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].freeze
|
|
47
|
+
|
|
48
|
+
def on_send(node)
|
|
49
|
+
pattern_node = node.arguments[1]
|
|
50
|
+
return unless pattern_node&.any_str_type? && (start = pattern_start(pattern_node))
|
|
51
|
+
|
|
52
|
+
problems(pattern_node).each do |problem|
|
|
53
|
+
add_offense(offense_range(start, problem.range), message: problem.message)
|
|
54
|
+
end
|
|
55
|
+
end
|
|
56
|
+
alias on_csend on_send
|
|
57
|
+
|
|
58
|
+
private
|
|
59
|
+
|
|
60
|
+
# @return [Parser::Source::Range, nil] where the pattern begins, if
|
|
61
|
+
# the positions in its value are the positions in the source
|
|
62
|
+
def pattern_start(pattern_node)
|
|
63
|
+
if pattern_node.heredoc?
|
|
64
|
+
pattern_node.loc.heredoc_body unless pattern_node.each_child_node(:begin).any?
|
|
65
|
+
elsif pattern_node.str_type? && pattern_node.loc?(:begin) && verbatim?(pattern_node)
|
|
66
|
+
pattern_node.loc.begin.end
|
|
67
|
+
end
|
|
68
|
+
end
|
|
69
|
+
|
|
70
|
+
def offense_range(start, range)
|
|
71
|
+
begin_pos = start.begin_pos + range.begin_pos
|
|
72
|
+
|
|
73
|
+
start.with(begin_pos: begin_pos, end_pos: begin_pos + range.size)
|
|
74
|
+
end
|
|
75
|
+
|
|
76
|
+
def verbatim?(pattern_node)
|
|
77
|
+
pattern_node.source == "#{pattern_node.loc.begin.source}#{pattern_node.value}" \
|
|
78
|
+
"#{pattern_node.loc.end.source}"
|
|
79
|
+
end
|
|
80
|
+
|
|
81
|
+
def problems(pattern_node)
|
|
82
|
+
value = pattern_node.heredoc? ? pattern_node.loc.heredoc_body.source : pattern_node.value
|
|
83
|
+
|
|
84
|
+
RuboCop::AST::NodePattern::Checker.new.check(value)
|
|
85
|
+
rescue RuboCop::AST::NodePattern::Invalid
|
|
86
|
+
[]
|
|
87
|
+
end
|
|
88
|
+
end
|
|
89
|
+
end
|
|
90
|
+
end
|
|
91
|
+
end
|
|
@@ -0,0 +1,79 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module InternalAffairs
|
|
6
|
+
# Checks for node searches that are used as a condition. A search that
|
|
7
|
+
# is defined with `def_node_search` and a name that does not end in `?`
|
|
8
|
+
# returns an enumerator when it is called without a block, and an
|
|
9
|
+
# enumerator is truthy even if nothing matches.
|
|
10
|
+
#
|
|
11
|
+
# Only the searches that are defined in the same file are known.
|
|
12
|
+
#
|
|
13
|
+
# @example
|
|
14
|
+
# def_node_search :assignments, '(lvasgn ...)'
|
|
15
|
+
#
|
|
16
|
+
# # bad
|
|
17
|
+
# return unless assignments(node)
|
|
18
|
+
#
|
|
19
|
+
# # bad
|
|
20
|
+
# assignments(node) && something
|
|
21
|
+
#
|
|
22
|
+
# # good
|
|
23
|
+
# return unless assignments(node).any?
|
|
24
|
+
#
|
|
25
|
+
# # good
|
|
26
|
+
# assignments(node) { |assignment| do_something(assignment) }
|
|
27
|
+
#
|
|
28
|
+
# # good
|
|
29
|
+
# def_node_search :assignments?, '(lvasgn ...)'
|
|
30
|
+
#
|
|
31
|
+
# return unless assignments?(node)
|
|
32
|
+
#
|
|
33
|
+
class NodeSearchAsCondition < Base
|
|
34
|
+
MSG = '`%<name>s` returns an enumerator, which is always truthy. ' \
|
|
35
|
+
'Use `%<name>s(...).any?`, or define the search with a name that ends in `?`.'
|
|
36
|
+
|
|
37
|
+
# @!method node_search_name(node)
|
|
38
|
+
def_node_matcher :node_search_name, <<~PATTERN
|
|
39
|
+
(send nil? :def_node_search (sym $_) ...)
|
|
40
|
+
PATTERN
|
|
41
|
+
|
|
42
|
+
def on_new_investigation
|
|
43
|
+
@node_searches = nil
|
|
44
|
+
end
|
|
45
|
+
|
|
46
|
+
def on_send(node) # rubocop:disable InternalAffairs/OnSendWithoutOnCSend -- a search is called without a receiver
|
|
47
|
+
return unless node_searches.include?(node.method_name)
|
|
48
|
+
return if node.receiver || node.block_literal? || node.block_argument?
|
|
49
|
+
return unless condition?(node)
|
|
50
|
+
|
|
51
|
+
add_offense(node, message: format(MSG, name: node.method_name))
|
|
52
|
+
end
|
|
53
|
+
|
|
54
|
+
private
|
|
55
|
+
|
|
56
|
+
def node_searches
|
|
57
|
+
@node_searches ||= processed_source.ast.each_node(:send).filter_map do |node|
|
|
58
|
+
name = node_search_name(node)
|
|
59
|
+
|
|
60
|
+
name unless name.nil? || name.end_with?('?')
|
|
61
|
+
end.to_set
|
|
62
|
+
end
|
|
63
|
+
|
|
64
|
+
def condition?(node)
|
|
65
|
+
parent = node.parent
|
|
66
|
+
return false unless parent
|
|
67
|
+
|
|
68
|
+
if parent.type?(:and, :or)
|
|
69
|
+
parent.lhs.equal?(node)
|
|
70
|
+
elsif parent.type?(:if, :while, :until)
|
|
71
|
+
parent.condition.equal?(node)
|
|
72
|
+
else
|
|
73
|
+
parent.send_type? && parent.method?(:!)
|
|
74
|
+
end
|
|
75
|
+
end
|
|
76
|
+
end
|
|
77
|
+
end
|
|
78
|
+
end
|
|
79
|
+
end
|
|
@@ -0,0 +1,83 @@
|
|
|
1
|
+
# frozen_string_literal: true
|
|
2
|
+
|
|
3
|
+
module RuboCop
|
|
4
|
+
module Cop
|
|
5
|
+
module InternalAffairs
|
|
6
|
+
# Checks for cops that are declared as incompatible for autocorrection by
|
|
7
|
+
# both cops of the pair. One declaration is enough: when either cop has
|
|
8
|
+
# been applied, the corrections of the other are held back until the next
|
|
9
|
+
# pass.
|
|
10
|
+
#
|
|
11
|
+
# The declaration that is reported is the one in the cop whose name comes
|
|
12
|
+
# second in alphabetical order.
|
|
13
|
+
#
|
|
14
|
+
# @example
|
|
15
|
+
# # bad
|
|
16
|
+
# class Bar < Base
|
|
17
|
+
# def self.autocorrect_incompatible_with
|
|
18
|
+
# [Style::Foo]
|
|
19
|
+
# end
|
|
20
|
+
# end
|
|
21
|
+
#
|
|
22
|
+
# class Foo < Base
|
|
23
|
+
# def self.autocorrect_incompatible_with
|
|
24
|
+
# [Style::Bar]
|
|
25
|
+
# end
|
|
26
|
+
# end
|
|
27
|
+
#
|
|
28
|
+
# # good
|
|
29
|
+
# class Bar < Base
|
|
30
|
+
# def self.autocorrect_incompatible_with
|
|
31
|
+
# [Style::Foo]
|
|
32
|
+
# end
|
|
33
|
+
# end
|
|
34
|
+
#
|
|
35
|
+
# class Foo < Base
|
|
36
|
+
# end
|
|
37
|
+
#
|
|
38
|
+
class RedundantAutocorrectIncompatibleWith < Base
|
|
39
|
+
MSG = '`%<other>s` already declares that it is incompatible with this cop.'
|
|
40
|
+
|
|
41
|
+
def on_defs(node)
|
|
42
|
+
return unless declaration?(node) && (own = defining_cop_name(node))
|
|
43
|
+
|
|
44
|
+
node.body.each_child_node(:const) do |const|
|
|
45
|
+
other = qualified_name(const.const_name, own)
|
|
46
|
+
next unless own > other && declared_by?(other, own)
|
|
47
|
+
|
|
48
|
+
add_offense(const, message: format(MSG, other: other))
|
|
49
|
+
end
|
|
50
|
+
end
|
|
51
|
+
|
|
52
|
+
private
|
|
53
|
+
|
|
54
|
+
def declaration?(node)
|
|
55
|
+
node.method?(:autocorrect_incompatible_with) && node.receiver.self_type? &&
|
|
56
|
+
node.body&.array_type?
|
|
57
|
+
end
|
|
58
|
+
|
|
59
|
+
# @return [String, nil] the name of the cop that the method is defined in
|
|
60
|
+
def defining_cop_name(node)
|
|
61
|
+
department, name = node.parent_module_name&.split('::')&.last(2)
|
|
62
|
+
|
|
63
|
+
"#{department}/#{name}" if name
|
|
64
|
+
end
|
|
65
|
+
|
|
66
|
+
# A cop of the same department can be given without the department.
|
|
67
|
+
def qualified_name(const_name, cop_name)
|
|
68
|
+
department, name = const_name.split('::').last(2)
|
|
69
|
+
return "#{department}/#{name}" if name
|
|
70
|
+
|
|
71
|
+
"#{cop_name.split('/').first}/#{department}"
|
|
72
|
+
end
|
|
73
|
+
|
|
74
|
+
def declared_by?(other, cop_name)
|
|
75
|
+
cop_class = Registry.global.find_by_cop_name(other)
|
|
76
|
+
return false unless cop_class
|
|
77
|
+
|
|
78
|
+
cop_class.autocorrect_incompatible_with.any? { |cop| cop.cop_name == cop_name }
|
|
79
|
+
end
|
|
80
|
+
end
|
|
81
|
+
end
|
|
82
|
+
end
|
|
83
|
+
end
|
|
@@ -7,6 +7,7 @@ require_relative 'internal_affairs/empty_line_between_expect_offense_and_correct
|
|
|
7
7
|
require_relative 'internal_affairs/example_description'
|
|
8
8
|
require_relative 'internal_affairs/example_heredoc_delimiter'
|
|
9
9
|
require_relative 'internal_affairs/inherit_deprecated_cop_class'
|
|
10
|
+
require_relative 'internal_affairs/invalid_node_pattern'
|
|
10
11
|
require_relative 'internal_affairs/itblock_handler'
|
|
11
12
|
require_relative 'internal_affairs/lambda_or_proc'
|
|
12
13
|
require_relative 'internal_affairs/location_exists'
|
|
@@ -18,6 +19,7 @@ require_relative 'internal_affairs/node_destructuring'
|
|
|
18
19
|
require_relative 'internal_affairs/node_first_or_last_argument'
|
|
19
20
|
require_relative 'internal_affairs/node_matcher_directive'
|
|
20
21
|
require_relative 'internal_affairs/node_pattern_groups'
|
|
22
|
+
require_relative 'internal_affairs/node_search_as_condition'
|
|
21
23
|
require_relative 'internal_affairs/node_type_group'
|
|
22
24
|
require_relative 'internal_affairs/node_type_multiple_predicates'
|
|
23
25
|
require_relative 'internal_affairs/node_type_predicate'
|
|
@@ -26,6 +28,7 @@ require_relative 'internal_affairs/offense_location_keyword'
|
|
|
26
28
|
require_relative 'internal_affairs/on_send_without_on_csend'
|
|
27
29
|
require_relative 'internal_affairs/operator_keyword'
|
|
28
30
|
require_relative 'internal_affairs/processed_source_buffer_name'
|
|
31
|
+
require_relative 'internal_affairs/redundant_autocorrect_incompatible_with'
|
|
29
32
|
require_relative 'internal_affairs/redundant_context_config_parameter'
|
|
30
33
|
require_relative 'internal_affairs/redundant_described_class_as_subject'
|
|
31
34
|
require_relative 'internal_affairs/redundant_expect_offense_arguments'
|
|
@@ -36,6 +36,10 @@ module RuboCop
|
|
|
36
36
|
|
|
37
37
|
RESTRICT_ON_SEND = MODULE_INCLUSION_METHODS
|
|
38
38
|
|
|
39
|
+
def on_new_investigation
|
|
40
|
+
@heredoc_end_lines = nil
|
|
41
|
+
end
|
|
42
|
+
|
|
39
43
|
def on_send(node)
|
|
40
44
|
return if node.receiver || node.arguments.empty?
|
|
41
45
|
return if node.parent&.type?(:send, :any_block, :array)
|
|
@@ -52,11 +56,19 @@ module RuboCop
|
|
|
52
56
|
private
|
|
53
57
|
|
|
54
58
|
def last_line_with_heredocs(node)
|
|
55
|
-
heredoc_end_lines
|
|
56
|
-
|
|
57
|
-
|
|
59
|
+
[node.last_line, heredoc_end_lines[node.last_line]].compact.max
|
|
60
|
+
end
|
|
61
|
+
|
|
62
|
+
# @return [Hash{Integer => Integer}] the last line of the heredocs
|
|
63
|
+
# that begin on a line
|
|
64
|
+
def heredoc_end_lines
|
|
65
|
+
@heredoc_end_lines ||=
|
|
66
|
+
processed_source.ast.each_node(:any_str).with_object({}) do |str, lines|
|
|
67
|
+
next unless str.heredoc?
|
|
58
68
|
|
|
59
|
-
|
|
69
|
+
end_line = str.loc.heredoc_end.line
|
|
70
|
+
lines[str.first_line] = [lines[str.first_line], end_line].compact.max
|
|
71
|
+
end
|
|
60
72
|
end
|
|
61
73
|
|
|
62
74
|
def autocorrect(corrector, last_line)
|
|
@@ -234,22 +234,40 @@ module RuboCop
|
|
|
234
234
|
|
|
235
235
|
def should_insert_line_before?(node)
|
|
236
236
|
return false if previous_line_empty?(node.first_line)
|
|
237
|
-
return true unless inside_block?(node) && no_empty_lines_around_block_body?
|
|
238
|
-
return true unless node.parent.begin_type?
|
|
239
237
|
|
|
240
|
-
|
|
238
|
+
statement = statement_of(node)
|
|
239
|
+
return true unless inside_block?(statement) && no_empty_lines_around_block_body?
|
|
240
|
+
return true unless statement.parent.begin_type?
|
|
241
|
+
|
|
242
|
+
statement.parent.children.first != statement
|
|
241
243
|
end
|
|
242
244
|
|
|
243
245
|
def should_insert_line_after?(node)
|
|
244
|
-
|
|
246
|
+
statement = statement_of(node)
|
|
247
|
+
return true unless inside_block?(statement) && no_empty_lines_around_block_body?
|
|
248
|
+
|
|
249
|
+
statement.parent.children.last != statement
|
|
250
|
+
end
|
|
251
|
+
|
|
252
|
+
def statement_of(node)
|
|
253
|
+
node = node.parent while wraps_on_the_same_line?(node.parent)
|
|
254
|
+
|
|
255
|
+
node
|
|
256
|
+
end
|
|
257
|
+
|
|
258
|
+
def wraps_on_the_same_line?(parent)
|
|
259
|
+
return false if parent.nil?
|
|
260
|
+
return parent.modifier_form? || parent.ternary? if parent.if_type?
|
|
245
261
|
|
|
246
|
-
|
|
262
|
+
parent.type?(:begin, :kwbegin) && parent.loc?(:begin) && parent.single_line?
|
|
247
263
|
end
|
|
248
264
|
|
|
249
265
|
def inside_block?(node)
|
|
250
|
-
|
|
266
|
+
parent = node.parent
|
|
267
|
+
return false if parent.nil?
|
|
268
|
+
return true if parent.any_block_type?
|
|
251
269
|
|
|
252
|
-
|
|
270
|
+
parent.begin_type? && parent.parent&.any_block_type?
|
|
253
271
|
end
|
|
254
272
|
|
|
255
273
|
def no_empty_lines_around_block_body?
|
|
@@ -163,11 +163,24 @@ module RuboCop
|
|
|
163
163
|
def register_endless_method_offense(line, line_index)
|
|
164
164
|
message = format(MSG, length: line_length(line), max: max)
|
|
165
165
|
loc = excess_range(nil, line, line_index)
|
|
166
|
+
node = endless_methods_by_line[line_index]
|
|
166
167
|
|
|
167
168
|
add_offense(loc, message: message) do |corrector|
|
|
168
169
|
self.max = line_length(line)
|
|
169
170
|
|
|
170
|
-
correct_to_multiline(corrector,
|
|
171
|
+
correct_to_multiline(corrector, node) unless heredoc_body_after_method?(node)
|
|
172
|
+
end
|
|
173
|
+
end
|
|
174
|
+
|
|
175
|
+
# The correction puts the body and `end` on lines of their own, which
|
|
176
|
+
# separates an opener before the body, or on the last line, from its
|
|
177
|
+
# heredoc body.
|
|
178
|
+
def heredoc_body_after_method?(node)
|
|
179
|
+
body_begin = node.body.source_range.begin_pos
|
|
180
|
+
|
|
181
|
+
heredoc_openers.any? do |opener|
|
|
182
|
+
(opener.line == node.first_line && opener.begin_pos < body_begin) ||
|
|
183
|
+
(opener.line == node.last_line && opener.begin_pos < node.source_range.end_pos)
|
|
171
184
|
end
|
|
172
185
|
end
|
|
173
186
|
|