rubocop 1.75.1 → 1.75.3
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/config/default.yml +29 -18
- data/lib/rubocop/config_validator.rb +6 -6
- data/lib/rubocop/cop/internal_affairs/example_description.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/node_pattern_groups.rb +1 -0
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +6 -1
- data/lib/rubocop/cop/layout/block_alignment.rb +1 -2
- data/lib/rubocop/cop/layout/def_end_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +2 -2
- data/lib/rubocop/cop/layout/hash_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/multiline_operation_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +2 -4
- data/lib/rubocop/cop/lint/deprecated_open_ssl_constant.rb +1 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +2 -3
- data/lib/rubocop/cop/lint/missing_cop_enable_directive.rb +1 -1
- data/lib/rubocop/cop/lint/nested_method_definition.rb +1 -1
- data/lib/rubocop/cop/lint/non_local_exit_from_iterator.rb +2 -2
- data/lib/rubocop/cop/lint/or_assignment_to_constant.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_type_conversion.rb +7 -4
- data/lib/rubocop/cop/lint/return_in_void_context.rb +7 -2
- data/lib/rubocop/cop/lint/suppressed_exception.rb +1 -1
- data/lib/rubocop/cop/lint/to_enum_arguments.rb +1 -1
- data/lib/rubocop/cop/lint/top_level_return_with_argument.rb +1 -1
- data/lib/rubocop/cop/lint/useless_rescue.rb +1 -1
- data/lib/rubocop/cop/message_annotator.rb +7 -3
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
- data/lib/rubocop/cop/mixin/def_node.rb +1 -1
- data/lib/rubocop/cop/mixin/empty_lines_around_body.rb +1 -1
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +0 -1
- data/lib/rubocop/cop/mixin/multiline_expression_indentation.rb +2 -0
- data/lib/rubocop/cop/mixin/trailing_comma.rb +3 -3
- data/lib/rubocop/cop/naming/memoized_instance_variable_name.rb +1 -1
- data/lib/rubocop/cop/naming/method_name.rb +1 -1
- data/lib/rubocop/cop/style/arguments_forwarding.rb +4 -4
- data/lib/rubocop/cop/style/class_and_module_children.rb +7 -1
- data/lib/rubocop/cop/style/class_equality_comparison.rb +1 -1
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -2
- data/lib/rubocop/cop/style/conditional_assignment.rb +16 -4
- data/lib/rubocop/cop/style/double_negation.rb +1 -1
- data/lib/rubocop/cop/style/empty_literal.rb +4 -0
- data/lib/rubocop/cop/style/eval_with_location.rb +3 -3
- data/lib/rubocop/cop/style/explicit_block_argument.rb +2 -2
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +3 -2
- data/lib/rubocop/cop/style/global_std_stream.rb +3 -0
- data/lib/rubocop/cop/style/hash_fetch_chain.rb +0 -1
- data/lib/rubocop/cop/style/hash_syntax.rb +3 -0
- data/lib/rubocop/cop/style/hash_transform_keys.rb +2 -2
- data/lib/rubocop/cop/style/hash_transform_values.rb +2 -2
- data/lib/rubocop/cop/style/if_with_boolean_literal_branches.rb +1 -1
- data/lib/rubocop/cop/style/keyword_parameters_order.rb +1 -1
- data/lib/rubocop/cop/style/lambda_call.rb +7 -2
- data/lib/rubocop/cop/style/map_into_array.rb +3 -1
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/omit_parentheses.rb +1 -1
- data/lib/rubocop/cop/style/redundant_condition.rb +13 -1
- data/lib/rubocop/cop/style/redundant_current_directory_in_path.rb +1 -1
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +0 -3
- data/lib/rubocop/cop/style/redundant_parentheses.rb +12 -3
- data/lib/rubocop/cop/style/return_nil.rb +2 -2
- data/lib/rubocop/cop/style/safe_navigation.rb +18 -3
- data/lib/rubocop/cop/style/super_arguments.rb +1 -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/util.rb +1 -1
- data/lib/rubocop/cop/variable_force/variable.rb +1 -1
- data/lib/rubocop/cops_documentation_generator.rb +6 -2
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/pacman_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +8 -0
- data/lib/rubocop/server/cache.rb +13 -10
- data/lib/rubocop/target_finder.rb +6 -2
- data/lib/rubocop/version.rb +1 -1
- metadata +5 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: a997a626f52ebff384613dfc5885815cf0bcee74a22f53485f77a5ea95205a60
|
4
|
+
data.tar.gz: b79e9c7f1b9b0716cf38b317174b49075c94dc39edd3d310a1a00d4fa6304a4f
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: 9e2948dd0e2d446e78113d6575bf5fb2f96a2d40239db066b9e6476479efd22996081675bcf47925a8ce0fb6012495e678701ab7383cf1a534255747fc2a6d00
|
7
|
+
data.tar.gz: 944bf135b7e8855c53172f14e33b040326cde4b0c14a8cad3656434765f1b9f9c2097e455437317aba2026f51c0ff0beb2a119b5e262bb4173664ab408e8aa8a
|
data/config/default.yml
CHANGED
@@ -274,7 +274,8 @@ Bundler/OrderedGems:
|
|
274
274
|
Gemspec/AddRuntimeDependency:
|
275
275
|
Description: 'Prefer `add_dependency` over `add_runtime_dependency`.'
|
276
276
|
StyleGuide: '#add_dependency_vs_add_runtime_dependency'
|
277
|
-
|
277
|
+
References:
|
278
|
+
- https://github.com/rubygems/rubygems/issues/7799#issuecomment-2192720316
|
278
279
|
Enabled: pending
|
279
280
|
VersionAdded: '1.65'
|
280
281
|
Include:
|
@@ -343,7 +344,7 @@ Gemspec/RequireMFA:
|
|
343
344
|
Severity: warning
|
344
345
|
VersionAdded: '1.23'
|
345
346
|
VersionChanged: '1.40'
|
346
|
-
|
347
|
+
References:
|
347
348
|
- https://guides.rubygems.org/mfa-requirement-opt-in/
|
348
349
|
Include:
|
349
350
|
- '**/*.gemspec'
|
@@ -604,7 +605,7 @@ Layout/EmptyLineAfterMultilineCondition:
|
|
604
605
|
# This is disabled, because this style is not very common in practice.
|
605
606
|
Enabled: false
|
606
607
|
VersionAdded: '0.90'
|
607
|
-
|
608
|
+
References:
|
608
609
|
- https://github.com/airbnb/ruby#multiline-if-newline
|
609
610
|
|
610
611
|
Layout/EmptyLineBetweenDefs:
|
@@ -639,7 +640,7 @@ Layout/EmptyLinesAroundAccessModifier:
|
|
639
640
|
SupportedStyles:
|
640
641
|
- around
|
641
642
|
- only_before
|
642
|
-
|
643
|
+
References:
|
643
644
|
# A reference to `EnforcedStyle: only_before`.
|
644
645
|
- https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
|
645
646
|
|
@@ -996,7 +997,7 @@ Layout/IndentationConsistency:
|
|
996
997
|
SupportedStyles:
|
997
998
|
- normal
|
998
999
|
- indented_internal_methods
|
999
|
-
|
1000
|
+
References:
|
1000
1001
|
# A reference to `EnforcedStyle: indented_internal_methods`.
|
1001
1002
|
- https://edgeguides.rubyonrails.org/contributing_to_ruby_on_rails.html#follow-the-coding-conventions
|
1002
1003
|
|
@@ -2035,7 +2036,8 @@ Lint/InterpolationCheck:
|
|
2035
2036
|
|
2036
2037
|
Lint/ItWithoutArgumentsInBlock:
|
2037
2038
|
Description: 'Checks uses of `it` calls without arguments in block.'
|
2038
|
-
|
2039
|
+
References:
|
2040
|
+
- 'https://bugs.ruby-lang.org/issues/18980'
|
2039
2041
|
Enabled: pending
|
2040
2042
|
VersionAdded: '1.59'
|
2041
2043
|
|
@@ -2673,7 +2675,7 @@ Metrics/AbcSize:
|
|
2673
2675
|
Description: >-
|
2674
2676
|
A calculated magnitude based on number of assignments,
|
2675
2677
|
branches, and conditions.
|
2676
|
-
|
2678
|
+
References:
|
2677
2679
|
- http://c2.com/cgi/wiki?AbcMetric
|
2678
2680
|
- https://en.wikipedia.org/wiki/ABC_Software_Metric
|
2679
2681
|
Enabled: true
|
@@ -3150,7 +3152,8 @@ Security/JSONLoad:
|
|
3150
3152
|
Description: >-
|
3151
3153
|
Prefer usage of `JSON.parse` over `JSON.load` due to potential
|
3152
3154
|
security issues. See reference for more information.
|
3153
|
-
|
3155
|
+
References:
|
3156
|
+
- 'https://ruby-doc.org/stdlib-2.7.0/libdoc/json/rdoc/JSON.html#method-i-load'
|
3154
3157
|
Enabled: true
|
3155
3158
|
VersionAdded: '0.43'
|
3156
3159
|
VersionChanged: '1.22'
|
@@ -3162,7 +3165,8 @@ Security/MarshalLoad:
|
|
3162
3165
|
Description: >-
|
3163
3166
|
Avoid using of `Marshal.load` or `Marshal.restore` due to potential
|
3164
3167
|
security issues. See reference for more information.
|
3165
|
-
|
3168
|
+
References:
|
3169
|
+
- 'https://ruby-doc.org/core-2.7.0/Marshal.html#module-Marshal-label-Security+considerations'
|
3166
3170
|
Enabled: true
|
3167
3171
|
VersionAdded: '0.47'
|
3168
3172
|
|
@@ -3177,7 +3181,8 @@ Security/YAMLLoad:
|
|
3177
3181
|
Description: >-
|
3178
3182
|
Prefer usage of `YAML.safe_load` over `YAML.load` due to potential
|
3179
3183
|
security issues. See reference for more information.
|
3180
|
-
|
3184
|
+
References:
|
3185
|
+
- 'https://ruby-doc.org/stdlib-2.7.0/libdoc/yaml/rdoc/YAML.html#module-YAML-label-Security'
|
3181
3186
|
Enabled: true
|
3182
3187
|
VersionAdded: '0.47'
|
3183
3188
|
SafeAutoCorrect: false
|
@@ -3271,7 +3276,8 @@ Style/ArrayCoercion:
|
|
3271
3276
|
|
3272
3277
|
Style/ArrayFirstLast:
|
3273
3278
|
Description: 'Use `arr.first` and `arr.last` instead of `arr[0]` and `arr[-1]`.'
|
3274
|
-
|
3279
|
+
References:
|
3280
|
+
- '#first-and-last'
|
3275
3281
|
Enabled: false
|
3276
3282
|
VersionAdded: '1.58'
|
3277
3283
|
Safe: false
|
@@ -4009,7 +4015,7 @@ Style/ExponentialNotation:
|
|
4009
4015
|
Style/FetchEnvVar:
|
4010
4016
|
Description: >-
|
4011
4017
|
Suggests `ENV.fetch` for the replacement of `ENV[]`.
|
4012
|
-
|
4018
|
+
References:
|
4013
4019
|
- https://rubystyle.guide/#hash-fetch-defaults
|
4014
4020
|
Enabled: pending
|
4015
4021
|
VersionAdded: '1.28'
|
@@ -4050,7 +4056,8 @@ Style/FileWrite:
|
|
4050
4056
|
Style/FloatDivision:
|
4051
4057
|
Description: 'For performing float division, coerce one side only.'
|
4052
4058
|
StyleGuide: '#float-division'
|
4053
|
-
|
4059
|
+
References:
|
4060
|
+
- 'https://blog.rubystyle.guide/ruby/2019/06/21/float-division.html'
|
4054
4061
|
Enabled: true
|
4055
4062
|
VersionAdded: '0.72'
|
4056
4063
|
VersionChanged: '1.9'
|
@@ -4147,7 +4154,8 @@ Style/GlobalStdStream:
|
|
4147
4154
|
Style/GlobalVars:
|
4148
4155
|
Description: 'Do not introduce global variables.'
|
4149
4156
|
StyleGuide: '#instance-vars'
|
4150
|
-
|
4157
|
+
References:
|
4158
|
+
- 'https://www.zenspider.com/ruby/quickref.html'
|
4151
4159
|
Enabled: true
|
4152
4160
|
VersionAdded: '0.13'
|
4153
4161
|
# Built-in global variables are allowed by default.
|
@@ -4958,7 +4966,7 @@ Style/OpenStructUse:
|
|
4958
4966
|
Description: >-
|
4959
4967
|
Avoid using OpenStruct. As of Ruby 3.0, use is officially discouraged due to performance,
|
4960
4968
|
version compatibility, and potential security issues.
|
4961
|
-
|
4969
|
+
References:
|
4962
4970
|
- https://docs.ruby-lang.org/en/3.0.0/OpenStruct.html#class-OpenStruct-label-Caveats
|
4963
4971
|
|
4964
4972
|
Enabled: pending
|
@@ -5203,7 +5211,8 @@ Style/RedundantFetchBlock:
|
|
5203
5211
|
Description: >-
|
5204
5212
|
Use `fetch(key, value)` instead of `fetch(key) { value }`
|
5205
5213
|
when value has Numeric, Rational, Complex, Symbol or String type, `false`, `true`, `nil` or is a constant.
|
5206
|
-
|
5214
|
+
References:
|
5215
|
+
- 'https://github.com/fastruby/fast-ruby#hashfetch-with-argument-vs-hashfetch--block-code'
|
5207
5216
|
Enabled: true
|
5208
5217
|
Safe: false
|
5209
5218
|
# If enabled, this cop will autocorrect usages of
|
@@ -5445,7 +5454,8 @@ Style/Sample:
|
|
5445
5454
|
Description: >-
|
5446
5455
|
Use `sample` instead of `shuffle.first`,
|
5447
5456
|
`shuffle.last`, and `shuffle[Integer]`.
|
5448
|
-
|
5457
|
+
References:
|
5458
|
+
- 'https://github.com/fastruby/fast-ruby#arrayshufflefirst-vs-arraysample-code'
|
5449
5459
|
Enabled: true
|
5450
5460
|
VersionAdded: '0.30'
|
5451
5461
|
|
@@ -5940,7 +5950,8 @@ Style/YAMLFileRead:
|
|
5940
5950
|
|
5941
5951
|
Style/YodaCondition:
|
5942
5952
|
Description: 'Forbid or enforce yoda conditions.'
|
5943
|
-
|
5953
|
+
References:
|
5954
|
+
- 'https://en.wikipedia.org/wiki/Yoda_conditions'
|
5944
5955
|
Enabled: true
|
5945
5956
|
EnforcedStyle: forbid_for_all_comparison_operators
|
5946
5957
|
SupportedStyles:
|
@@ -9,16 +9,16 @@ module RuboCop
|
|
9
9
|
|
10
10
|
# @api private
|
11
11
|
COMMON_PARAMS = %w[Exclude Include Severity inherit_mode AutoCorrect StyleGuide Details
|
12
|
-
Enabled Reference].freeze
|
12
|
+
Enabled Reference References].freeze
|
13
13
|
# @api private
|
14
14
|
INTERNAL_PARAMS = %w[Description StyleGuide
|
15
15
|
VersionAdded VersionChanged VersionRemoved
|
16
|
-
Reference Safe SafeAutoCorrect].freeze
|
16
|
+
Reference References Safe SafeAutoCorrect].freeze
|
17
17
|
# @api private
|
18
18
|
NEW_COPS_VALUES = %w[pending disable enable].freeze
|
19
19
|
|
20
20
|
# @api private
|
21
|
-
CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect].to_set.freeze
|
21
|
+
CONFIG_CHECK_KEYS = %w[Enabled Safe SafeAutoCorrect AutoCorrect References].to_set.freeze
|
22
22
|
CONFIG_CHECK_DEPARTMENTS = %w[pending override_department].freeze
|
23
23
|
CONFIG_CHECK_AUTOCORRECTS = %w[always contextual disabled].freeze
|
24
24
|
private_constant :CONFIG_CHECK_KEYS, :CONFIG_CHECK_DEPARTMENTS
|
@@ -260,8 +260,7 @@ module RuboCop
|
|
260
260
|
end
|
261
261
|
end
|
262
262
|
|
263
|
-
# rubocop:disable Metrics/
|
264
|
-
def check_cop_config_value(hash, parent = nil)
|
263
|
+
def check_cop_config_value(hash, parent = nil) # rubocop:disable Metrics/AbcSize, Metrics/CyclomaticComplexity, Metrics/PerceivedComplexity
|
265
264
|
hash.each do |key, value|
|
266
265
|
check_cop_config_value(value, key) if value.is_a?(Hash)
|
267
266
|
|
@@ -271,6 +270,8 @@ module RuboCop
|
|
271
270
|
supposed_values = 'a boolean'
|
272
271
|
elsif key == 'AutoCorrect' && !CONFIG_CHECK_AUTOCORRECTS.include?(value)
|
273
272
|
supposed_values = '`always`, `contextual`, `disabled`, or a boolean'
|
273
|
+
elsif key == 'References'
|
274
|
+
supposed_values = 'an array of strings'
|
274
275
|
else
|
275
276
|
next
|
276
277
|
end
|
@@ -278,7 +279,6 @@ module RuboCop
|
|
278
279
|
raise ValidationError, param_error_message(parent, key, value, supposed_values)
|
279
280
|
end
|
280
281
|
end
|
281
|
-
# rubocop:enable Metrics/PerceivedComplexity, Metrics/CyclomaticComplexity
|
282
282
|
|
283
283
|
# FIXME: Handling colors in exception messages like this is ugly.
|
284
284
|
def param_error_message(parent, key, value, supposed_values)
|
@@ -28,6 +28,7 @@ module RuboCop
|
|
28
28
|
RESTRICT_ON_SEND = %i[def_node_matcher def_node_search].freeze
|
29
29
|
NODE_GROUPS = {
|
30
30
|
any_block: %i[block numblock itblock],
|
31
|
+
any_def: %i[def defs],
|
31
32
|
argument: %i[arg optarg restarg kwarg kwoptarg kwrestarg blockarg forward_arg shadowarg],
|
32
33
|
boolean: %i[true false],
|
33
34
|
call: %i[send csend],
|
@@ -10,7 +10,12 @@ module RuboCop
|
|
10
10
|
extend FileFinder
|
11
11
|
|
12
12
|
ALLOWED_CONFIGURATIONS = %w[
|
13
|
-
Safe SafeAutoCorrect AutoCorrect
|
13
|
+
Safe SafeAutoCorrect AutoCorrect
|
14
|
+
Severity
|
15
|
+
StyleGuide
|
16
|
+
Details
|
17
|
+
Reference References
|
18
|
+
Include Exclude
|
14
19
|
].freeze
|
15
20
|
RESTRICT_ON_SEND = %i[[] fetch].freeze
|
16
21
|
MSG = '`%<name>s` is not defined in the configuration for `%<cop>s` ' \
|
@@ -48,7 +48,7 @@ module RuboCop
|
|
48
48
|
def on_send(node)
|
49
49
|
return unless node.def_modifier?
|
50
50
|
|
51
|
-
method_def = node.each_descendant(:
|
51
|
+
method_def = node.each_descendant(:any_def).first
|
52
52
|
expr = node.source_range
|
53
53
|
|
54
54
|
line_start = range_between(expr.begin_pos, method_def.loc.keyword.end_pos)
|
@@ -180,7 +180,7 @@ module RuboCop
|
|
180
180
|
end
|
181
181
|
|
182
182
|
def method_candidate?(node)
|
183
|
-
cop_config['EmptyLineBetweenMethodDefs'] && node.
|
183
|
+
cop_config['EmptyLineBetweenMethodDefs'] && node.any_def_type?
|
184
184
|
end
|
185
185
|
|
186
186
|
def class_candidate?(node)
|
@@ -252,7 +252,7 @@ module RuboCop
|
|
252
252
|
end
|
253
253
|
|
254
254
|
def end_loc(node)
|
255
|
-
if node.
|
255
|
+
if node.any_def_type? && node.endless?
|
256
256
|
node.source_range.end
|
257
257
|
else
|
258
258
|
node.loc.end
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
# * ignore_implicit (without curly braces)
|
20
20
|
#
|
21
21
|
# Alternatively you can specify multiple allowed styles. That's done by
|
22
|
-
# passing a list of styles to
|
22
|
+
# passing a list of styles to EnforcedHashRocketStyle and EnforcedColonStyle.
|
23
23
|
#
|
24
24
|
# @example EnforcedHashRocketStyle: key (default)
|
25
25
|
# # bad
|
@@ -29,8 +29,7 @@ module RuboCop
|
|
29
29
|
MSG = '`%<kw_loc>s` at %<kw_loc_line>d, %<kw_loc_column>d is not ' \
|
30
30
|
'aligned with `%<beginning>s` at ' \
|
31
31
|
'%<begin_loc_line>d, %<begin_loc_column>d.'
|
32
|
-
ANCESTOR_TYPES = %i[kwbegin
|
33
|
-
ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS = %i[def defs].freeze
|
32
|
+
ANCESTOR_TYPES = %i[kwbegin any_def class module any_block].freeze
|
34
33
|
ALTERNATIVE_ACCESS_MODIFIERS = %i[public_class_method private_class_method].freeze
|
35
34
|
|
36
35
|
def on_resbody(node)
|
@@ -162,8 +161,7 @@ module RuboCop
|
|
162
161
|
end
|
163
162
|
|
164
163
|
def access_modifier_node(node)
|
165
|
-
return nil unless
|
166
|
-
ANCESTOR_TYPES_WITH_ACCESS_MODIFIERS.include?(node.type)
|
164
|
+
return nil unless node.any_def_type?
|
167
165
|
|
168
166
|
access_modifier_node = node.ancestors.first
|
169
167
|
return nil unless access_modifier?(access_modifier_node)
|
@@ -134,7 +134,7 @@ module RuboCop
|
|
134
134
|
if NO_ARG_ALGORITHM.include?(algorithm_parts.first.upcase) && no_arguments
|
135
135
|
"'#{algorithm_parts.first}'"
|
136
136
|
else
|
137
|
-
mode = 'cbc'
|
137
|
+
mode = 'cbc' if size_and_mode.empty?
|
138
138
|
|
139
139
|
"'#{(algorithm_parts + size_and_mode + [mode]).compact.take(3).join('-')}'"
|
140
140
|
end
|
@@ -42,7 +42,6 @@ module RuboCop
|
|
42
42
|
class DuplicateMethods < Base
|
43
43
|
MSG = 'Method `%<method>s` is defined at both %<defined>s and %<current>s.'
|
44
44
|
RESTRICT_ON_SEND = %i[alias_method attr_reader attr_writer attr_accessor attr].freeze
|
45
|
-
DEF_TYPES = %i[def defs].freeze
|
46
45
|
|
47
46
|
def initialize(config = nil, options = nil)
|
48
47
|
super
|
@@ -162,7 +161,7 @@ module RuboCop
|
|
162
161
|
end
|
163
162
|
|
164
163
|
def method_key(node, method_name)
|
165
|
-
if (ancestor_def = node.each_ancestor(
|
164
|
+
if (ancestor_def = node.each_ancestor(:any_def).first)
|
166
165
|
"#{ancestor_def.method_name}.#{method_name}"
|
167
166
|
else
|
168
167
|
method_name
|
@@ -170,7 +169,7 @@ module RuboCop
|
|
170
169
|
end
|
171
170
|
|
172
171
|
def location(node)
|
173
|
-
if
|
172
|
+
if node.any_def_type?
|
174
173
|
node.loc.keyword.join(node.loc.name)
|
175
174
|
else
|
176
175
|
node.source_range
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
52
52
|
each_missing_enable do |cop, line_range|
|
53
53
|
next if acceptable_range?(cop, line_range)
|
54
54
|
|
55
|
-
range = source_range(processed_source.buffer, line_range.min,
|
55
|
+
range = source_range(processed_source.buffer, line_range.min, 0..0)
|
56
56
|
comment = processed_source.comment_at_line(line_range.begin)
|
57
57
|
|
58
58
|
add_offense(range, message: message(cop, comment))
|
@@ -98,7 +98,7 @@ module RuboCop
|
|
98
98
|
subject, = *node # rubocop:disable InternalAffairs/NodeDestructuring
|
99
99
|
return if node.defs_type? && allowed_subject_type?(subject)
|
100
100
|
|
101
|
-
def_ancestor = node.each_ancestor(:
|
101
|
+
def_ancestor = node.each_ancestor(:any_def).first
|
102
102
|
return unless def_ancestor
|
103
103
|
|
104
104
|
within_scoping_def =
|
@@ -46,7 +46,7 @@ module RuboCop
|
|
46
46
|
def on_return(return_node)
|
47
47
|
return if return_value?(return_node)
|
48
48
|
|
49
|
-
return_node.each_ancestor(:any_block, :
|
49
|
+
return_node.each_ancestor(:any_block, :any_def) do |node|
|
50
50
|
break if scoped_node?(node)
|
51
51
|
|
52
52
|
# if a proc is passed to `Module#define_method` or
|
@@ -66,7 +66,7 @@ module RuboCop
|
|
66
66
|
private
|
67
67
|
|
68
68
|
def scoped_node?(node)
|
69
|
-
node.
|
69
|
+
node.any_def_type? || node.lambda?
|
70
70
|
end
|
71
71
|
|
72
72
|
def return_value?(return_node)
|
@@ -27,7 +27,8 @@ module RuboCop
|
|
27
27
|
# In all cases, chaining one same `to_*` conversion methods listed above is redundant.
|
28
28
|
#
|
29
29
|
# The cop can also register an offense for chaining conversion methods on methods that are
|
30
|
-
# expected to return a specific type regardless of receiver (eg. `foo.inspect.to_s`
|
30
|
+
# expected to return a specific type regardless of receiver (eg. `foo.inspect.to_s` and
|
31
|
+
# `foo.to_json.to_s`).
|
31
32
|
#
|
32
33
|
# @example
|
33
34
|
# # bad
|
@@ -69,10 +70,12 @@ module RuboCop
|
|
69
70
|
# foo.to_s
|
70
71
|
#
|
71
72
|
# # bad - chaining a conversion to a method that is expected to return the same type
|
72
|
-
# inspect.to_s
|
73
|
+
# foo.inspect.to_s
|
74
|
+
# foo.to_json.to_s
|
73
75
|
#
|
74
76
|
# # good
|
75
|
-
# inspect
|
77
|
+
# foo.inspect
|
78
|
+
# foo.to_json
|
76
79
|
#
|
77
80
|
class RedundantTypeConversion < Base
|
78
81
|
extend AutoCorrector
|
@@ -108,7 +111,7 @@ module RuboCop
|
|
108
111
|
|
109
112
|
# Methods that already are expected to return a given type, which makes a further
|
110
113
|
# conversion redundant.
|
111
|
-
TYPED_METHODS = { to_s: %i[inspect] }.freeze
|
114
|
+
TYPED_METHODS = { to_s: %i[inspect to_json] }.freeze
|
112
115
|
|
113
116
|
CONVERSION_METHODS = Set[*LITERAL_NODE_TYPES.keys].freeze
|
114
117
|
RESTRICT_ON_SEND = CONVERSION_METHODS + [:to_d]
|
@@ -32,12 +32,17 @@ module RuboCop
|
|
32
32
|
class ReturnInVoidContext < Base
|
33
33
|
MSG = 'Do not return a value in `%<method>s`.'
|
34
34
|
|
35
|
+
# Returning out of these methods only exits the block itself.
|
36
|
+
SCOPE_CHANGING_METHODS = %i[lambda define_method define_singleton_method].freeze
|
37
|
+
|
35
38
|
def on_return(return_node)
|
36
39
|
return unless return_node.descendants.any?
|
37
40
|
|
38
|
-
def_node = return_node.each_ancestor(:
|
41
|
+
def_node = return_node.each_ancestor(:any_def).first
|
39
42
|
return unless def_node&.void_context?
|
40
|
-
return if return_node.each_ancestor(:any_block).any?
|
43
|
+
return if return_node.each_ancestor(:any_block).any? do |block_node|
|
44
|
+
SCOPE_CHANGING_METHODS.include?(block_node.method_name)
|
45
|
+
end
|
41
46
|
|
42
47
|
add_offense(
|
43
48
|
return_node.loc.keyword,
|
@@ -116,7 +116,7 @@ module RuboCop
|
|
116
116
|
private
|
117
117
|
|
118
118
|
def comment_between_rescue_and_end?(node)
|
119
|
-
ancestor = node.each_ancestor(:kwbegin, :
|
119
|
+
ancestor = node.each_ancestor(:kwbegin, :any_def, :any_block).first
|
120
120
|
return false unless ancestor
|
121
121
|
|
122
122
|
end_line = ancestor.loc.end&.line || ancestor.loc.last_line
|
@@ -40,7 +40,7 @@ module RuboCop
|
|
40
40
|
# top-level return node's ancestors should not be of block, def, or
|
41
41
|
# defs type.
|
42
42
|
def top_level_return?(return_node)
|
43
|
-
return_node.each_ancestor(:block, :
|
43
|
+
return_node.each_ancestor(:block, :any_def).none?
|
44
44
|
end
|
45
45
|
end
|
46
46
|
end
|
@@ -32,7 +32,7 @@ module RuboCop
|
|
32
32
|
# @param [String] cop_name for specific cop name
|
33
33
|
# @param [Hash] cop_config configs for specific cop, from config#for_cop
|
34
34
|
# @option cop_config [String] :StyleGuide Extension of base styleguide URL
|
35
|
-
# @option cop_config [String] :
|
35
|
+
# @option cop_config [String] :References Full reference URLs
|
36
36
|
# @option cop_config [String] :Details
|
37
37
|
#
|
38
38
|
# @param [Hash, nil] options optional
|
@@ -100,8 +100,12 @@ module RuboCop
|
|
100
100
|
end
|
101
101
|
|
102
102
|
def reference_urls
|
103
|
-
urls =
|
104
|
-
|
103
|
+
urls = cop_config
|
104
|
+
.values_at('References', 'Reference') # Support legacy Reference key
|
105
|
+
.flat_map { Array(_1) }
|
106
|
+
.reject(&:empty?)
|
107
|
+
|
108
|
+
urls unless urls.empty?
|
105
109
|
end
|
106
110
|
|
107
111
|
def extra_details?
|
@@ -48,7 +48,7 @@ module RuboCop
|
|
48
48
|
|
49
49
|
args = process_args(node.arguments)
|
50
50
|
return extract_breakable_node_from_elements(node, args, max)
|
51
|
-
elsif node.
|
51
|
+
elsif node.any_def_type?
|
52
52
|
return extract_breakable_node_from_elements(node, node.arguments, max)
|
53
53
|
elsif node.type?(:array, :hash)
|
54
54
|
return extract_breakable_node_from_elements(node, node.children, max)
|
@@ -220,7 +220,7 @@ module RuboCop
|
|
220
220
|
|
221
221
|
# @api private
|
222
222
|
def already_on_multiple_lines?(node)
|
223
|
-
return node.first_line != node.last_argument.last_line if node.
|
223
|
+
return node.first_line != node.last_argument.last_line if node.any_def_type?
|
224
224
|
|
225
225
|
!node.single_line?
|
226
226
|
end
|
@@ -40,7 +40,7 @@ module RuboCop
|
|
40
40
|
end
|
41
41
|
|
42
42
|
def safe_to_split?(node)
|
43
|
-
node.each_descendant(:if, :case, :kwbegin, :
|
43
|
+
node.each_descendant(:if, :case, :kwbegin, :any_def).none? &&
|
44
44
|
node.each_descendant(:dstr, :str).none? { |n| n.heredoc? || n.value.include?("\n") } &&
|
45
45
|
node.each_descendant(:begin, :sym).none? { |b| !b.single_line? }
|
46
46
|
end
|
@@ -19,7 +19,7 @@ module RuboCop
|
|
19
19
|
|
20
20
|
# @!method non_public_modifier?(node)
|
21
21
|
def_node_matcher :non_public_modifier?, <<~PATTERN
|
22
|
-
(send nil? {:private :protected :private_class_method} (
|
22
|
+
(send nil? {:private :protected :private_class_method} (any_def ...))
|
23
23
|
PATTERN
|
24
24
|
end
|
25
25
|
end
|
@@ -21,7 +21,7 @@ module RuboCop
|
|
21
21
|
|
22
22
|
# @!method empty_line_required?(node)
|
23
23
|
def_node_matcher :empty_line_required?,
|
24
|
-
'{
|
24
|
+
'{any_def class module (send nil? {:private :protected :public})}'
|
25
25
|
|
26
26
|
def check(node, body, adjusted_first_line: nil)
|
27
27
|
return if valid_body_style?(body)
|
@@ -6,7 +6,6 @@ module RuboCop
|
|
6
6
|
module FrozenStringLiteral
|
7
7
|
module_function
|
8
8
|
|
9
|
-
FROZEN_STRING_LITERAL_REGEXP = /#\s*frozen[-_]?string[-_]?literal:/i.freeze
|
10
9
|
FROZEN_STRING_LITERAL_ENABLED = '# frozen_string_literal: true'
|
11
10
|
FROZEN_STRING_LITERAL_TYPES_RUBY27 = %i[str dstr].freeze
|
12
11
|
|
@@ -136,9 +136,9 @@ module RuboCop
|
|
136
136
|
end
|
137
137
|
|
138
138
|
def last_item_precedes_newline?(node)
|
139
|
-
after_last_item =
|
140
|
-
|
141
|
-
after_last_item.source
|
139
|
+
after_last_item = node.children.last.source_range.end.join(node.loc.end.begin)
|
140
|
+
|
141
|
+
after_last_item.source.start_with?(/,?\s*(#.*)?\n/)
|
142
142
|
end
|
143
143
|
|
144
144
|
def avoid_comma(kind, comma_begin_pos, extra_info)
|