rubocop 1.85.1 → 1.86.1
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 +9 -12
- data/lib/rubocop/cache_config.rb +1 -1
- data/lib/rubocop/cli/command/auto_generate_config.rb +1 -1
- data/lib/rubocop/cli/command/show_cops.rb +2 -2
- data/lib/rubocop/cli/command/show_docs_url.rb +1 -1
- data/lib/rubocop/config.rb +14 -10
- data/lib/rubocop/config_finder.rb +1 -1
- data/lib/rubocop/config_loader_resolver.rb +2 -1
- data/lib/rubocop/config_store.rb +1 -1
- data/lib/rubocop/config_validator.rb +1 -1
- data/lib/rubocop/cop/documentation.rb +2 -3
- data/lib/rubocop/cop/layout/argument_alignment.rb +1 -1
- data/lib/rubocop/cop/layout/dot_position.rb +1 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -2
- data/lib/rubocop/cop/layout/empty_line_between_defs.rb +1 -1
- data/lib/rubocop/cop/layout/empty_lines_around_attribute_accessor.rb +1 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +6 -3
- data/lib/rubocop/cop/layout/line_length.rb +5 -3
- data/lib/rubocop/cop/layout/multiline_method_call_indentation.rb +28 -3
- data/lib/rubocop/cop/layout/space_around_keyword.rb +3 -1
- data/lib/rubocop/cop/layout/space_in_lambda_literal.rb +1 -0
- data/lib/rubocop/cop/lint/constant_reassignment.rb +59 -9
- data/lib/rubocop/cop/lint/duplicate_methods.rb +55 -8
- data/lib/rubocop/cop/lint/empty_conditional_body.rb +6 -1
- data/lib/rubocop/cop/lint/empty_in_pattern.rb +8 -1
- data/lib/rubocop/cop/lint/empty_when.rb +8 -1
- data/lib/rubocop/cop/lint/number_conversion.rb +1 -1
- data/lib/rubocop/cop/lint/redundant_safe_navigation.rb +16 -0
- data/lib/rubocop/cop/lint/safe_navigation_chain.rb +17 -0
- data/lib/rubocop/cop/lint/syntax.rb +25 -1
- data/lib/rubocop/cop/lint/trailing_comma_in_attribute_declaration.rb +1 -0
- data/lib/rubocop/cop/lint/unused_method_argument.rb +10 -0
- data/lib/rubocop/cop/lint/useless_constant_scoping.rb +4 -4
- data/lib/rubocop/cop/lint/useless_default_value_argument.rb +2 -0
- data/lib/rubocop/cop/lint/utils/nil_receiver_checker.rb +22 -7
- data/lib/rubocop/cop/mixin/check_single_line_suitability.rb +1 -1
- data/lib/rubocop/cop/registry.rb +20 -13
- data/lib/rubocop/cop/style/access_modifier_declarations.rb +14 -2
- data/lib/rubocop/cop/style/and_or.rb +1 -0
- data/lib/rubocop/cop/style/arguments_forwarding.rb +25 -7
- data/lib/rubocop/cop/style/ascii_comments.rb +1 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +23 -31
- data/lib/rubocop/cop/style/collection_compact.rb +36 -16
- data/lib/rubocop/cop/style/concat_array_literals.rb +2 -0
- data/lib/rubocop/cop/style/conditional_assignment.rb +0 -4
- data/lib/rubocop/cop/style/empty_class_definition.rb +24 -2
- data/lib/rubocop/cop/style/file_open.rb +1 -1
- data/lib/rubocop/cop/style/global_vars.rb +1 -1
- data/lib/rubocop/cop/style/guard_clause.rb +9 -6
- data/lib/rubocop/cop/style/hash_lookup_method.rb +7 -0
- data/lib/rubocop/cop/style/if_inside_else.rb +1 -5
- data/lib/rubocop/cop/style/if_unless_modifier.rb +11 -0
- data/lib/rubocop/cop/style/if_with_semicolon.rb +7 -5
- data/lib/rubocop/cop/style/ip_addresses.rb +1 -2
- data/lib/rubocop/cop/style/magic_comment_format.rb +2 -2
- data/lib/rubocop/cop/style/method_call_with_args_parentheses/require_parentheses.rb +5 -3
- data/lib/rubocop/cop/style/module_member_existence_check.rb +1 -11
- data/lib/rubocop/cop/style/mutable_constant.rb +1 -1
- data/lib/rubocop/cop/style/non_nil_check.rb +5 -11
- data/lib/rubocop/cop/style/numeric_literals.rb +1 -1
- data/lib/rubocop/cop/style/one_class_per_file.rb +24 -4
- data/lib/rubocop/cop/style/raise_args.rb +1 -1
- data/lib/rubocop/cop/style/redundant_each.rb +3 -3
- data/lib/rubocop/cop/style/redundant_line_continuation.rb +16 -0
- data/lib/rubocop/cop/style/redundant_parentheses.rb +22 -20
- data/lib/rubocop/cop/style/redundant_percent_q.rb +4 -1
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +0 -5
- data/lib/rubocop/cop/style/redundant_struct_keyword_init.rb +10 -0
- data/lib/rubocop/cop/style/safe_navigation.rb +7 -7
- data/lib/rubocop/cop/style/single_line_block_params.rb +1 -1
- data/lib/rubocop/cop/style/symbol_proc.rb +4 -3
- data/lib/rubocop/cop/style/trailing_method_end_statement.rb +1 -0
- data/lib/rubocop/cop/style/yoda_expression.rb +1 -1
- data/lib/rubocop/formatter/disabled_config_formatter.rb +1 -1
- data/lib/rubocop/formatter/junit_formatter.rb +1 -1
- data/lib/rubocop/formatter/worst_offenders_formatter.rb +1 -1
- data/lib/rubocop/lsp/routes.rb +10 -3
- data/lib/rubocop/mcp/server.rb +27 -1
- data/lib/rubocop/path_util.rb +14 -2
- data/lib/rubocop/plugin/loader.rb +1 -1
- data/lib/rubocop/result_cache.rb +10 -1
- data/lib/rubocop/rspec/cop_helper.rb +8 -0
- data/lib/rubocop/rspec/shared_contexts.rb +11 -2
- data/lib/rubocop/runner.rb +8 -3
- data/lib/rubocop/server/core.rb +2 -0
- data/lib/rubocop/target_finder.rb +1 -1
- data/lib/rubocop/version.rb +2 -2
- metadata +5 -19
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: e7e3463c6020de496396b13ecf80f691f532af83ac8f8627286f3887f07b11b2
|
|
4
|
+
data.tar.gz: 9e8c332d0f33ea078abcc71e0ddb7cd009497842a026dfdcae103a63fc909ba6
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: 0f0d7cff4de9dc7d8f04cdea8f8f28d6e45d5c0676a40ca67daa4e3fd6e3397363d2c2f4b368b5bcbb165d7478bdfb33c5352c49d2fd52896f8aa6d488e017cc
|
|
7
|
+
data.tar.gz: 357c3047ffea023a5c10162e4e2f2ee782796b5ca87d7c4413bde296d284c41362033ca9d997609ac6d9eceea7227b46e240150340a8a26cad8071bc5cce33c7
|
data/config/default.yml
CHANGED
|
@@ -3,20 +3,16 @@
|
|
|
3
3
|
AllCops:
|
|
4
4
|
RubyInterpreters:
|
|
5
5
|
- ruby
|
|
6
|
-
- macruby
|
|
7
6
|
- rake
|
|
8
7
|
- jruby
|
|
9
|
-
- rbx
|
|
10
8
|
# Include common Ruby source files.
|
|
11
9
|
Include:
|
|
12
10
|
- '**/*.rb'
|
|
13
11
|
- '**/*.arb'
|
|
14
12
|
- '**/*.axlsx'
|
|
15
13
|
- '**/*.builder'
|
|
16
|
-
- '**/*.fcgi'
|
|
17
14
|
- '**/*.gemfile'
|
|
18
15
|
- '**/*.gemspec'
|
|
19
|
-
- '**/*.god'
|
|
20
16
|
- '**/*.jb'
|
|
21
17
|
- '**/*.jbuilder'
|
|
22
18
|
- '**/*.mspec'
|
|
@@ -25,15 +21,12 @@ AllCops:
|
|
|
25
21
|
- '**/*.podspec'
|
|
26
22
|
- '**/*.rabl'
|
|
27
23
|
- '**/*.rake'
|
|
28
|
-
- '**/*.rbuild'
|
|
29
24
|
- '**/*.rbw'
|
|
30
|
-
- '**/*.rbx'
|
|
31
25
|
- '**/*.ru'
|
|
32
26
|
- '**/*.ruby'
|
|
33
27
|
- '**/*.schema'
|
|
34
28
|
- '**/*.spec'
|
|
35
29
|
- '**/*.thor'
|
|
36
|
-
- '**/*.watchr'
|
|
37
30
|
- '**/.irbrc'
|
|
38
31
|
- '**/.pryrc'
|
|
39
32
|
- '**/.simplecov'
|
|
@@ -43,7 +36,6 @@ AllCops:
|
|
|
43
36
|
- '**/Brewfile'
|
|
44
37
|
- '**/Buildfile'
|
|
45
38
|
- '**/Capfile'
|
|
46
|
-
- '**/Cheffile'
|
|
47
39
|
- '**/Dangerfile'
|
|
48
40
|
- '**/Deliverfile'
|
|
49
41
|
- '**/Fastfile'
|
|
@@ -60,7 +52,6 @@ AllCops:
|
|
|
60
52
|
- '**/Snapfile'
|
|
61
53
|
- '**/Steepfile'
|
|
62
54
|
- '**/Thorfile'
|
|
63
|
-
- '**/Vagabondfile'
|
|
64
55
|
- '**/Vagrantfile'
|
|
65
56
|
Exclude:
|
|
66
57
|
- 'node_modules/**/*'
|
|
@@ -117,7 +108,7 @@ AllCops:
|
|
|
117
108
|
# line option.
|
|
118
109
|
UseCache: true
|
|
119
110
|
# Threshold for how many files can be stored in the result cache before some
|
|
120
|
-
# of the files are automatically removed.
|
|
111
|
+
# of the files are automatically removed. Set to false to disable cache pruning.
|
|
121
112
|
MaxFilesInCache: 20000
|
|
122
113
|
# The cache will be stored in "rubocop_cache" under this directory. If
|
|
123
114
|
# CacheRootDirectory is ~ (nil), which it is by default, the root will be
|
|
@@ -3993,11 +3984,13 @@ Style/EmptyClassDefinition:
|
|
|
3993
3984
|
Description: 'Enforces consistent style for empty class definitions.'
|
|
3994
3985
|
Enabled: pending
|
|
3995
3986
|
VersionAdded: '1.84'
|
|
3987
|
+
VersionChanged: '1.86'
|
|
3996
3988
|
EnforcedStyle: class_keyword
|
|
3997
3989
|
SupportedStyles:
|
|
3998
3990
|
- class_keyword
|
|
3999
3991
|
- class_new
|
|
4000
3992
|
- class_definition # Deprecated.
|
|
3993
|
+
AllowedParentClasses: []
|
|
4001
3994
|
|
|
4002
3995
|
Style/EmptyElse:
|
|
4003
3996
|
Description: 'Avoid empty else-clauses.'
|
|
@@ -4803,7 +4796,6 @@ Style/ModuleMemberExistenceCheck:
|
|
|
4803
4796
|
Description: 'Checks for usage of `Module` methods returning arrays that can be replaced with equivalent predicates.'
|
|
4804
4797
|
Enabled: pending
|
|
4805
4798
|
VersionAdded: '1.82'
|
|
4806
|
-
AllowedMethods: []
|
|
4807
4799
|
|
|
4808
4800
|
Style/MultilineBlockChain:
|
|
4809
4801
|
Description: 'Avoid multi-line chains of blocks.'
|
|
@@ -5119,7 +5111,11 @@ Style/OneClassPerFile:
|
|
|
5119
5111
|
Description: 'Checks that each source file defines at most one top-level class or module.'
|
|
5120
5112
|
Enabled: pending
|
|
5121
5113
|
VersionAdded: '1.85'
|
|
5114
|
+
VersionChanged: '1.86'
|
|
5122
5115
|
AllowedClasses: []
|
|
5116
|
+
Exclude:
|
|
5117
|
+
- 'spec/**/*'
|
|
5118
|
+
- 'test/**/*'
|
|
5123
5119
|
|
|
5124
5120
|
Style/OneLineConditional:
|
|
5125
5121
|
Description: >-
|
|
@@ -5564,9 +5560,10 @@ Style/RedundantStringEscape:
|
|
|
5564
5560
|
|
|
5565
5561
|
Style/RedundantStructKeywordInit:
|
|
5566
5562
|
Description: 'Checks for redundant `keyword_init` option for `Struct.new`.'
|
|
5567
|
-
Enabled:
|
|
5563
|
+
Enabled: false
|
|
5568
5564
|
SafeAutoCorrect: false
|
|
5569
5565
|
VersionAdded: '1.85'
|
|
5566
|
+
VersionChanged: '1.86'
|
|
5570
5567
|
|
|
5571
5568
|
Style/RegexpLiteral:
|
|
5572
5569
|
Description: 'Use / or %r around regular expressions.'
|
data/lib/rubocop/cache_config.rb
CHANGED
|
@@ -35,7 +35,7 @@ module RuboCop
|
|
|
35
35
|
root_dir do
|
|
36
36
|
next cache_root_override if cache_root_override
|
|
37
37
|
|
|
38
|
-
config_path = ConfigFinder.find_config_path(
|
|
38
|
+
config_path = ConfigFinder.find_config_path(PathUtil.pwd)
|
|
39
39
|
file_contents = File.read(config_path)
|
|
40
40
|
|
|
41
41
|
# Returns early if `CacheRootDirectory` is not used before requiring `erb` or `yaml`.
|
|
@@ -153,7 +153,7 @@ module RuboCop
|
|
|
153
153
|
def relative_path_to_todo_from_options_config
|
|
154
154
|
return AUTO_GENERATED_FILE unless @options[:config]
|
|
155
155
|
|
|
156
|
-
base = Pathname.new(
|
|
156
|
+
base = Pathname.new(PathUtil.pwd)
|
|
157
157
|
config_dir = Pathname.new(@options[:config]).realpath.dirname
|
|
158
158
|
|
|
159
159
|
# Don't have the path start with `/`
|
|
@@ -25,7 +25,7 @@ module RuboCop
|
|
|
25
25
|
super
|
|
26
26
|
|
|
27
27
|
# Load the configs so the require()s are done for custom cops
|
|
28
|
-
@config = @config_store.for(
|
|
28
|
+
@config = @config_store.for(PathUtil.pwd)
|
|
29
29
|
|
|
30
30
|
@cop_matchers = @options[:show_cops].map do |pattern|
|
|
31
31
|
if pattern.include?('*')
|
|
@@ -46,7 +46,7 @@ module RuboCop
|
|
|
46
46
|
registry = Cop::Registry.global
|
|
47
47
|
show_all = @cop_matchers.empty?
|
|
48
48
|
|
|
49
|
-
puts "# Available cops (#{registry.length}) + config for #{
|
|
49
|
+
puts "# Available cops (#{registry.length}) + config for #{PathUtil.pwd}: " if show_all
|
|
50
50
|
|
|
51
51
|
registry.departments.sort!.each do |department|
|
|
52
52
|
print_cops_of_department(registry, department, show_all)
|
data/lib/rubocop/config.rb
CHANGED
|
@@ -217,6 +217,9 @@ module RuboCop
|
|
|
217
217
|
for_all_cops['StringLiteralsFrozenByDefault']
|
|
218
218
|
end
|
|
219
219
|
|
|
220
|
+
# Returns true if the file matches any include pattern. If a block is given, the block is called
|
|
221
|
+
# to determine if the pattern is relevant (true returned by the block) or should be skipped
|
|
222
|
+
# (false returned).
|
|
220
223
|
def file_to_include?(file)
|
|
221
224
|
relative_file_path = path_relative_to_config(file)
|
|
222
225
|
|
|
@@ -228,11 +231,9 @@ module RuboCop
|
|
|
228
231
|
absolute_file_path = File.expand_path(file)
|
|
229
232
|
|
|
230
233
|
patterns_to_include.any? do |pattern|
|
|
231
|
-
if block_given?
|
|
232
|
-
|
|
233
|
-
|
|
234
|
-
match_path?(pattern, relative_file_path) || match_path?(pattern, absolute_file_path)
|
|
235
|
-
end
|
|
234
|
+
next if block_given? && !yield(pattern)
|
|
235
|
+
|
|
236
|
+
match_relative_or_absolute_path?(pattern, relative_file_path, absolute_file_path)
|
|
236
237
|
end
|
|
237
238
|
end
|
|
238
239
|
|
|
@@ -242,10 +243,7 @@ module RuboCop
|
|
|
242
243
|
# `bundler-console` conveys `Bundler::Console`).
|
|
243
244
|
return true if File.extname(file) == '.gemspec'
|
|
244
245
|
|
|
245
|
-
file_to_include?(file)
|
|
246
|
-
/[A-Z]/.match?(pattern.to_s) &&
|
|
247
|
-
(match_path?(pattern, relative_path) || match_path?(pattern, absolute_path))
|
|
248
|
-
end
|
|
246
|
+
file_to_include?(file) { |pattern| /[A-Z]/.match?(pattern.to_s) }
|
|
249
247
|
end
|
|
250
248
|
|
|
251
249
|
# Returns true if there's a chance that an Include pattern matches hidden
|
|
@@ -286,7 +284,7 @@ module RuboCop
|
|
|
286
284
|
loaded_path != File.join(Dir.home, ConfigLoader::DOTFILE)
|
|
287
285
|
File.expand_path(File.dirname(loaded_path))
|
|
288
286
|
else
|
|
289
|
-
|
|
287
|
+
PathUtil.pwd
|
|
290
288
|
end
|
|
291
289
|
end
|
|
292
290
|
|
|
@@ -345,6 +343,12 @@ module RuboCop
|
|
|
345
343
|
|
|
346
344
|
private
|
|
347
345
|
|
|
346
|
+
def match_relative_or_absolute_path?(pattern, relative_file_path, absolute_file_path)
|
|
347
|
+
should_use_absolute_path = absolute?(pattern.to_s) || pattern.to_s.start_with?('..') ||
|
|
348
|
+
relative_file_path.start_with?('..')
|
|
349
|
+
match_path?(pattern, should_use_absolute_path ? absolute_file_path : relative_file_path)
|
|
350
|
+
end
|
|
351
|
+
|
|
348
352
|
# @return [Float, nil] The Rails version as a `major.minor` Float.
|
|
349
353
|
def target_rails_version_from_bundler_lock_file
|
|
350
354
|
@target_rails_version_from_bundler_lock_file ||= read_rails_version_from_bundler_lock_file
|
|
@@ -45,6 +45,7 @@ module RuboCop
|
|
|
45
45
|
base_config.each do |k, v|
|
|
46
46
|
next unless v.is_a?(Hash)
|
|
47
47
|
|
|
48
|
+
only_base_has_include = v.key?('Include') && !hash.dig(k, 'Include')
|
|
48
49
|
if hash.key?(k)
|
|
49
50
|
v = merge(v, hash[k],
|
|
50
51
|
cop_name: k, file: file, debug: debug,
|
|
@@ -52,7 +53,7 @@ module RuboCop
|
|
|
52
53
|
inherit_mode: determine_inherit_mode(hash, k))
|
|
53
54
|
end
|
|
54
55
|
hash[k] = v
|
|
55
|
-
fix_include_paths(base_config.loaded_path, hash, path, k, v) if
|
|
56
|
+
fix_include_paths(base_config.loaded_path, hash, path, k, v) if only_base_has_include
|
|
56
57
|
end
|
|
57
58
|
end
|
|
58
59
|
end
|
data/lib/rubocop/config_store.rb
CHANGED
|
@@ -9,7 +9,7 @@ module RuboCop
|
|
|
9
9
|
|
|
10
10
|
# @api private
|
|
11
11
|
COMMON_PARAMS = %w[Exclude Include Severity inherit_mode AutoCorrect StyleGuide Details
|
|
12
|
-
Enabled Reference References].freeze
|
|
12
|
+
Enabled Reference References Safe SafeAutoCorrect].freeze
|
|
13
13
|
# @api private
|
|
14
14
|
INTERNAL_PARAMS = %w[Description StyleGuide
|
|
15
15
|
VersionAdded VersionChanged VersionRemoved
|
|
@@ -55,10 +55,9 @@ module RuboCop
|
|
|
55
55
|
|
|
56
56
|
# @api private
|
|
57
57
|
def builtin?(cop_class)
|
|
58
|
-
|
|
59
|
-
return false unless (m = cop_class.instance_methods(false).first)
|
|
58
|
+
return false unless (name = cop_class.name)
|
|
60
59
|
|
|
61
|
-
path, _line =
|
|
60
|
+
path, _line = Module.const_source_location(name)
|
|
62
61
|
path.start_with?(__dir__)
|
|
63
62
|
end
|
|
64
63
|
end
|
|
@@ -52,7 +52,7 @@ module RuboCop
|
|
|
52
52
|
'following the first line of a multi-line method call.'
|
|
53
53
|
|
|
54
54
|
def on_send(node)
|
|
55
|
-
return if !multiple_arguments?(node) || (node.
|
|
55
|
+
return if !multiple_arguments?(node) || (node.call_type? && node.method?(:[]=)) ||
|
|
56
56
|
autocorrect_incompatible_with_other_cops?
|
|
57
57
|
|
|
58
58
|
items = flattened_arguments(node)
|
|
@@ -112,7 +112,7 @@ module RuboCop
|
|
|
112
112
|
end
|
|
113
113
|
|
|
114
114
|
def last_heredoc_line(node)
|
|
115
|
-
if node.
|
|
115
|
+
if node.call_type?
|
|
116
116
|
node.arguments.select { |arg| heredoc?(arg) }.map { |arg| arg.loc.heredoc_end.line }.max
|
|
117
117
|
elsif heredoc?(node)
|
|
118
118
|
node.loc.heredoc_end.line
|
|
@@ -60,6 +60,11 @@ module RuboCop
|
|
|
60
60
|
END_OF_HEREDOC_LINE = 1
|
|
61
61
|
SIMPLE_DIRECTIVE_COMMENT_PATTERN = /\A# *:nocov:\z/.freeze
|
|
62
62
|
|
|
63
|
+
# @!method guard_clause_branch?(node)
|
|
64
|
+
def_node_matcher :guard_clause_branch?, <<~PATTERN
|
|
65
|
+
{(send nil? {:raise :fail} ...) return break next}
|
|
66
|
+
PATTERN
|
|
67
|
+
|
|
63
68
|
def on_if(node)
|
|
64
69
|
return if correct_style?(node)
|
|
65
70
|
return if multiple_statements_on_line?(node)
|
|
@@ -97,14 +102,16 @@ module RuboCop
|
|
|
97
102
|
end
|
|
98
103
|
|
|
99
104
|
def correct_style?(node)
|
|
100
|
-
!
|
|
105
|
+
!node.if_branch&.guard_clause? ||
|
|
101
106
|
next_line_rescue_or_ensure?(node) ||
|
|
102
107
|
next_sibling_parent_empty_or_else?(node) ||
|
|
103
108
|
next_sibling_empty_or_guard_clause?(node)
|
|
104
109
|
end
|
|
105
110
|
|
|
106
111
|
def contains_guard_clause?(node)
|
|
107
|
-
node.if_branch
|
|
112
|
+
return false unless (branch = node.if_branch)
|
|
113
|
+
|
|
114
|
+
branch.guard_clause? || guard_clause_branch?(branch)
|
|
108
115
|
end
|
|
109
116
|
|
|
110
117
|
def next_line_empty_or_allowed_directive_comment?(line)
|
|
@@ -185,7 +185,7 @@ module RuboCop
|
|
|
185
185
|
end
|
|
186
186
|
|
|
187
187
|
def empty_line_between_macros
|
|
188
|
-
cop_config.fetch('DefLikeMacros', []).map(&:to_sym)
|
|
188
|
+
@empty_line_between_macros ||= cop_config.fetch('DefLikeMacros', []).map(&:to_sym).freeze
|
|
189
189
|
end
|
|
190
190
|
|
|
191
191
|
def macro_candidate?(node)
|
|
@@ -123,20 +123,23 @@ module RuboCop
|
|
|
123
123
|
AlignmentCorrector.align_end(corrector, processed_source, node, alignment_node(node))
|
|
124
124
|
end
|
|
125
125
|
|
|
126
|
+
# rubocop:disable Metrics/CyclomaticComplexity
|
|
126
127
|
def check_assignment(node, rhs)
|
|
127
128
|
# If there are method calls chained to the right hand side of the
|
|
128
129
|
# assignment, we let rhs be the receiver of those method calls before
|
|
129
130
|
# we check if it's an if/unless/while/until.
|
|
130
131
|
return unless (rhs = first_part_of_call_chain(rhs))
|
|
131
132
|
|
|
132
|
-
# If `rhs` is a `begin` node
|
|
133
|
-
|
|
133
|
+
# If `rhs` is a `begin` node or a logical operator,
|
|
134
|
+
# unwrap to find the leading conditional.
|
|
135
|
+
rhs = rhs.child_nodes.first while rhs&.type?(:begin, :or, :and)
|
|
134
136
|
|
|
135
|
-
return unless rhs
|
|
137
|
+
return unless rhs&.conditional?
|
|
136
138
|
return if rhs.if_type? && rhs.ternary?
|
|
137
139
|
|
|
138
140
|
check_asgn_alignment(node, rhs)
|
|
139
141
|
end
|
|
142
|
+
# rubocop:enable Metrics/CyclomaticComplexity
|
|
140
143
|
|
|
141
144
|
def check_asgn_alignment(outer_node, inner_node)
|
|
142
145
|
align_with = {
|
|
@@ -406,9 +406,11 @@ module RuboCop
|
|
|
406
406
|
end
|
|
407
407
|
|
|
408
408
|
def string_delimiter(node)
|
|
409
|
-
delimiter = node.loc
|
|
410
|
-
|
|
411
|
-
|
|
409
|
+
delimiter = if node.loc?(:begin)
|
|
410
|
+
node.loc.begin
|
|
411
|
+
elsif node.parent&.dstr_type? && node.parent.loc?(:begin)
|
|
412
|
+
node.parent.loc.begin
|
|
413
|
+
end&.source
|
|
412
414
|
|
|
413
415
|
delimiter if %w[' "].include?(delimiter)
|
|
414
416
|
end
|
|
@@ -128,12 +128,15 @@ module RuboCop
|
|
|
128
128
|
if hash_pair_indented?(node, pair_ancestor, given_style)
|
|
129
129
|
return check_hash_pair_indented_style(rhs, pair_ancestor)
|
|
130
130
|
end
|
|
131
|
-
|
|
132
|
-
return false if !pair_ancestor && not_for_this_cop?(node)
|
|
131
|
+
return false if skip_for_context?(node, pair_ancestor)
|
|
133
132
|
|
|
134
133
|
check_regular_indentation(node, lhs, rhs, given_style)
|
|
135
134
|
end
|
|
136
135
|
|
|
136
|
+
def skip_for_context?(node, pair_ancestor)
|
|
137
|
+
pair_ancestor ? inside_multiline_chain_arg?(node) : not_for_this_cop?(node)
|
|
138
|
+
end
|
|
139
|
+
|
|
137
140
|
def hash_pair_aligned?(pair_ancestor, given_style)
|
|
138
141
|
pair_ancestor && given_style == :aligned
|
|
139
142
|
end
|
|
@@ -152,7 +155,10 @@ module RuboCop
|
|
|
152
155
|
end
|
|
153
156
|
|
|
154
157
|
def check_hash_pair_indentation(node, lhs, rhs)
|
|
155
|
-
@base = find_hash_pair_alignment_base(node)
|
|
158
|
+
@base = find_hash_pair_alignment_base(node)
|
|
159
|
+
return false if !@base && inside_multiline_chain_arg?(node)
|
|
160
|
+
|
|
161
|
+
@base ||= lhs.source_range
|
|
156
162
|
return if aligned_with_first_line_dot?(node, rhs)
|
|
157
163
|
|
|
158
164
|
calculate_column_delta_offense(rhs, @base.column)
|
|
@@ -166,6 +172,25 @@ module RuboCop
|
|
|
166
172
|
first_call.loc.dot.join(first_call.loc.selector)
|
|
167
173
|
end
|
|
168
174
|
|
|
175
|
+
def inside_multiline_chain_arg?(node)
|
|
176
|
+
enclosing_call = find_enclosing_chain_call(node)
|
|
177
|
+
return false unless enclosing_call
|
|
178
|
+
|
|
179
|
+
!same_line?(enclosing_call.loc.selector, enclosing_call.receiver.source_range)
|
|
180
|
+
end
|
|
181
|
+
|
|
182
|
+
def find_enclosing_chain_call(node)
|
|
183
|
+
hash_ancestor = find_pair_ancestor(node).parent
|
|
184
|
+
enclosing_call = hash_ancestor.parent
|
|
185
|
+
return unless hash_arg_in_chain?(enclosing_call, hash_ancestor)
|
|
186
|
+
|
|
187
|
+
enclosing_call
|
|
188
|
+
end
|
|
189
|
+
|
|
190
|
+
def hash_arg_in_chain?(call, hash_node)
|
|
191
|
+
call&.call_type? && call.receiver != hash_node && call.loc?(:dot)
|
|
192
|
+
end
|
|
193
|
+
|
|
169
194
|
def aligned_with_first_line_dot?(node, rhs)
|
|
170
195
|
return false unless rhs.source.start_with?('.', '&.')
|
|
171
196
|
|
|
@@ -47,9 +47,11 @@ module RuboCop
|
|
|
47
47
|
check(node, [:operator].freeze) if node.keyword?
|
|
48
48
|
end
|
|
49
49
|
|
|
50
|
-
def on_block(node)
|
|
50
|
+
def on_block(node)
|
|
51
51
|
check(node, %i[begin end].freeze)
|
|
52
52
|
end
|
|
53
|
+
alias on_numblock on_block
|
|
54
|
+
alias on_itblock on_block
|
|
53
55
|
|
|
54
56
|
def on_break(node)
|
|
55
57
|
check(node, [:keyword].freeze)
|
|
@@ -26,6 +26,7 @@ module RuboCop
|
|
|
26
26
|
|
|
27
27
|
MSG_REQUIRE_SPACE = 'Use a space between `->` and `(` in lambda literals.'
|
|
28
28
|
MSG_REQUIRE_NO_SPACE = 'Do not use spaces between `->` and `(` in lambda literals.'
|
|
29
|
+
RESTRICT_ON_SEND = %i[lambda].freeze
|
|
29
30
|
|
|
30
31
|
def on_send(node)
|
|
31
32
|
return unless arrow_lambda_with_args?(node)
|
|
@@ -6,8 +6,11 @@ module RuboCop
|
|
|
6
6
|
# Checks for constant reassignments.
|
|
7
7
|
#
|
|
8
8
|
# Emulates Ruby's runtime warning "already initialized constant X"
|
|
9
|
-
# when a constant is reassigned in the same file and namespace
|
|
10
|
-
#
|
|
9
|
+
# when a constant is reassigned in the same file and namespace.
|
|
10
|
+
#
|
|
11
|
+
# The cop tracks constants defined via `NAME = value` syntax as well as
|
|
12
|
+
# class/module keyword definitions. It detects reassignment when a constant
|
|
13
|
+
# is first defined one way and then redefined using the `NAME = value` syntax.
|
|
11
14
|
#
|
|
12
15
|
# The cop cannot catch all offenses, like, for example, when a constant
|
|
13
16
|
# is reassigned in another file, or when using metaprogramming (`Module#const_set`).
|
|
@@ -36,6 +39,14 @@ module RuboCop
|
|
|
36
39
|
# X = :bar
|
|
37
40
|
# end
|
|
38
41
|
#
|
|
42
|
+
# # bad
|
|
43
|
+
# class FooError < StandardError; end
|
|
44
|
+
# FooError = Class.new(RuntimeError)
|
|
45
|
+
#
|
|
46
|
+
# # bad
|
|
47
|
+
# module M; end
|
|
48
|
+
# M = 1
|
|
49
|
+
#
|
|
39
50
|
# # good - keep only one assignment
|
|
40
51
|
# X = :bar
|
|
41
52
|
#
|
|
@@ -69,16 +80,30 @@ module RuboCop
|
|
|
69
80
|
|
|
70
81
|
# @!method remove_constant(node)
|
|
71
82
|
def_node_matcher :remove_constant, <<~PATTERN
|
|
72
|
-
(send
|
|
83
|
+
(send {nil? self} :remove_const
|
|
73
84
|
({sym str} $_))
|
|
74
85
|
PATTERN
|
|
75
86
|
|
|
87
|
+
def on_class(node)
|
|
88
|
+
return unless unconditional_definition?(node)
|
|
89
|
+
|
|
90
|
+
constant_definitions[definition_name(node)] ||= :class
|
|
91
|
+
end
|
|
92
|
+
|
|
93
|
+
def on_module(node)
|
|
94
|
+
return unless unconditional_definition?(node)
|
|
95
|
+
|
|
96
|
+
constant_definitions[definition_name(node)] ||= :module
|
|
97
|
+
end
|
|
98
|
+
|
|
76
99
|
def on_casgn(node)
|
|
77
100
|
return unless fixed_constant_path?(node)
|
|
78
101
|
return unless simple_assignment?(node)
|
|
79
|
-
return if constant_names.add?(fully_qualified_constant_name(node))
|
|
80
102
|
|
|
81
|
-
|
|
103
|
+
name = fully_qualified_constant_name(node)
|
|
104
|
+
return constant_definitions[name] = :casgn unless constant_definitions.key?(name)
|
|
105
|
+
|
|
106
|
+
add_offense(node, message: format(MSG, constant: constant_display_name(node)))
|
|
82
107
|
end
|
|
83
108
|
|
|
84
109
|
def on_send(node)
|
|
@@ -90,7 +115,7 @@ module RuboCop
|
|
|
90
115
|
|
|
91
116
|
return if namespaces.none?
|
|
92
117
|
|
|
93
|
-
|
|
118
|
+
constant_definitions.delete(fully_qualified_name_for(namespaces, constant))
|
|
94
119
|
end
|
|
95
120
|
|
|
96
121
|
private
|
|
@@ -104,7 +129,7 @@ module RuboCop
|
|
|
104
129
|
return true if ancestor.type?(:module, :class)
|
|
105
130
|
|
|
106
131
|
ancestor.begin_type? || ancestor.literal? || ancestor.casgn_type? ||
|
|
107
|
-
freeze_method?(ancestor)
|
|
132
|
+
ancestor.type?(:masgn, :mlhs) || freeze_method?(ancestor)
|
|
108
133
|
end
|
|
109
134
|
end
|
|
110
135
|
|
|
@@ -128,6 +153,10 @@ module RuboCop
|
|
|
128
153
|
['', *namespaces, constant].join('::')
|
|
129
154
|
end
|
|
130
155
|
|
|
156
|
+
def constant_display_name(node)
|
|
157
|
+
[*constant_namespaces(node), node.name].join('::')
|
|
158
|
+
end
|
|
159
|
+
|
|
131
160
|
def constant_namespaces(node)
|
|
132
161
|
node.each_path.select(&:const_type?).map(&:short_name)
|
|
133
162
|
end
|
|
@@ -139,8 +168,29 @@ module RuboCop
|
|
|
139
168
|
.reverse
|
|
140
169
|
end
|
|
141
170
|
|
|
142
|
-
def
|
|
143
|
-
|
|
171
|
+
def unconditional_definition?(node)
|
|
172
|
+
node.each_ancestor.all? do |ancestor|
|
|
173
|
+
ancestor.type?(:begin, :module, :class)
|
|
174
|
+
end
|
|
175
|
+
end
|
|
176
|
+
|
|
177
|
+
def definition_name(node)
|
|
178
|
+
identifier = node.identifier
|
|
179
|
+
|
|
180
|
+
if identifier.namespace&.cbase_type?
|
|
181
|
+
fully_qualified_name_for([], identifier.short_name)
|
|
182
|
+
else
|
|
183
|
+
namespaces = ancestor_namespaces(node) + identifier_namespaces(identifier)
|
|
184
|
+
fully_qualified_name_for(namespaces, identifier.short_name)
|
|
185
|
+
end
|
|
186
|
+
end
|
|
187
|
+
|
|
188
|
+
def identifier_namespaces(identifier)
|
|
189
|
+
identifier.each_path.select(&:const_type?).map(&:short_name)
|
|
190
|
+
end
|
|
191
|
+
|
|
192
|
+
def constant_definitions
|
|
193
|
+
@constant_definitions ||= {}
|
|
144
194
|
end
|
|
145
195
|
end
|
|
146
196
|
end
|