rubocop 1.18.3 → 1.20.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 +46 -7
- data/lib/rubocop/cli.rb +18 -0
- data/lib/rubocop/config_loader.rb +2 -2
- data/lib/rubocop/config_loader_resolver.rb +21 -6
- data/lib/rubocop/config_validator.rb +18 -5
- data/lib/rubocop/cop/bundler/gem_filename.rb +103 -0
- data/lib/rubocop/cop/bundler/ordered_gems.rb +1 -1
- data/lib/rubocop/cop/correctors/require_library_corrector.rb +23 -0
- data/lib/rubocop/cop/documentation.rb +1 -1
- data/lib/rubocop/cop/gemspec/ordered_dependencies.rb +1 -1
- data/lib/rubocop/cop/internal_affairs/inherit_deprecated_cop_class.rb +34 -0
- data/lib/rubocop/cop/internal_affairs/undefined_config.rb +71 -0
- data/lib/rubocop/cop/internal_affairs.rb +2 -0
- data/lib/rubocop/cop/layout/class_structure.rb +5 -1
- data/lib/rubocop/cop/layout/empty_line_after_guard_clause.rb +9 -0
- data/lib/rubocop/cop/layout/end_alignment.rb +10 -2
- data/lib/rubocop/cop/layout/first_argument_indentation.rb +1 -1
- data/lib/rubocop/cop/layout/hash_alignment.rb +22 -18
- data/lib/rubocop/cop/layout/heredoc_indentation.rb +0 -7
- data/lib/rubocop/cop/layout/indentation_style.rb +2 -2
- data/lib/rubocop/cop/layout/leading_comment_space.rb +1 -1
- data/lib/rubocop/cop/layout/line_end_string_concatenation_indentation.rb +33 -14
- data/lib/rubocop/cop/layout/multiline_method_argument_line_breaks.rb +3 -0
- data/lib/rubocop/cop/layout/rescue_ensure_alignment.rb +22 -9
- data/lib/rubocop/cop/layout/space_around_operators.rb +8 -1
- data/lib/rubocop/cop/layout/space_before_comment.rb +1 -1
- data/lib/rubocop/cop/layout/space_inside_parens.rb +5 -5
- data/lib/rubocop/cop/layout/trailing_whitespace.rb +24 -1
- data/lib/rubocop/cop/lint/ambiguous_range.rb +105 -0
- data/lib/rubocop/cop/lint/ambiguous_regexp_literal.rb +5 -2
- data/lib/rubocop/cop/lint/debugger.rb +2 -2
- data/lib/rubocop/cop/lint/duplicate_branch.rb +2 -1
- data/lib/rubocop/cop/lint/duplicate_methods.rb +8 -5
- data/lib/rubocop/cop/lint/shadowed_argument.rb +1 -1
- data/lib/rubocop/cop/mixin/annotation_comment.rb +57 -34
- data/lib/rubocop/cop/mixin/check_line_breakable.rb +2 -2
- data/lib/rubocop/cop/mixin/documentation_comment.rb +5 -2
- data/lib/rubocop/cop/mixin/frozen_string_literal.rb +14 -1
- data/lib/rubocop/cop/mixin/hash_transform_method.rb +6 -1
- data/lib/rubocop/cop/mixin/heredoc.rb +7 -0
- data/lib/rubocop/cop/mixin/percent_array.rb +13 -7
- data/lib/rubocop/cop/mixin/require_library.rb +59 -0
- data/lib/rubocop/cop/mixin/space_before_punctuation.rb +1 -1
- data/lib/rubocop/cop/naming/inclusive_language.rb +18 -1
- data/lib/rubocop/cop/style/block_delimiters.rb +39 -6
- data/lib/rubocop/cop/style/comment_annotation.rb +25 -39
- data/lib/rubocop/cop/style/commented_keyword.rb +2 -1
- data/lib/rubocop/cop/style/conditional_assignment.rb +19 -5
- data/lib/rubocop/cop/style/double_cop_disable_directive.rb +1 -7
- data/lib/rubocop/cop/style/double_negation.rb +12 -1
- data/lib/rubocop/cop/style/encoding.rb +26 -15
- data/lib/rubocop/cop/style/eval_with_location.rb +1 -1
- data/lib/rubocop/cop/style/explicit_block_argument.rb +32 -7
- data/lib/rubocop/cop/style/frozen_string_literal_comment.rb +1 -1
- data/lib/rubocop/cop/style/hash_as_last_array_item.rb +11 -0
- data/lib/rubocop/cop/style/hash_except.rb +4 -3
- data/lib/rubocop/cop/style/hash_transform_keys.rb +0 -3
- data/lib/rubocop/cop/style/identical_conditional_branches.rb +30 -5
- data/lib/rubocop/cop/style/method_def_parentheses.rb +10 -1
- data/lib/rubocop/cop/style/missing_else.rb +7 -0
- data/lib/rubocop/cop/style/mutable_constant.rb +73 -13
- data/lib/rubocop/cop/style/redundant_begin.rb +25 -0
- data/lib/rubocop/cop/style/redundant_freeze.rb +4 -3
- data/lib/rubocop/cop/style/redundant_self_assignment_branch.rb +83 -0
- data/lib/rubocop/cop/style/redundant_sort.rb +2 -2
- data/lib/rubocop/cop/style/semicolon.rb +32 -24
- data/lib/rubocop/cop/style/single_line_block_params.rb +3 -1
- data/lib/rubocop/cop/style/single_line_methods.rb +14 -9
- data/lib/rubocop/cop/style/sole_nested_conditional.rb +4 -0
- data/lib/rubocop/cop/style/special_global_vars.rb +21 -0
- data/lib/rubocop/cop/style/struct_inheritance.rb +3 -0
- data/lib/rubocop/cop/style/symbol_array.rb +3 -3
- data/lib/rubocop/cop/style/word_array.rb +23 -5
- data/lib/rubocop/cop/util.rb +7 -2
- data/lib/rubocop/formatter/git_hub_actions_formatter.rb +1 -1
- data/lib/rubocop/magic_comment.rb +44 -15
- data/lib/rubocop/options.rb +1 -1
- data/lib/rubocop/version.rb +1 -1
- data/lib/rubocop.rb +6 -1
- metadata +12 -5
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: e89a054e431c4e739e8e4b0bcdc36452704f1cffd6bda1e5710241e6712161a9
|
4
|
+
data.tar.gz: 632d9503cddcb39443ad8fc8b31bd1f1576803e9490ae77d22ee06f34d40784b
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: b765c92c6118f7e8e958d0787714764c392453efa450048629002a69634fe032648a6f88654afc522d9ca01b2fb2c7f70c573d6bd9a227e85168cfcbe5613091
|
7
|
+
data.tar.gz: 22d0b80509def399c10586f579f08520e47c1d03a7eab14e0c9b998d9fea0b7a477333677c0af2aed3e768bb9c9d6a30f51a88f7d9e4973acd8057389ac01357
|
data/README.md
CHANGED
@@ -54,7 +54,7 @@ To prevent an unwanted RuboCop update you might want to use a conservative versi
|
|
54
54
|
in your `Gemfile`:
|
55
55
|
|
56
56
|
```rb
|
57
|
-
gem 'rubocop', '~> 1.
|
57
|
+
gem 'rubocop', '~> 1.20', require: false
|
58
58
|
```
|
59
59
|
|
60
60
|
See [our versioning policy](https://docs.rubocop.org/rubocop/versioning.html) for further details.
|
data/config/default.yml
CHANGED
@@ -174,6 +174,20 @@ Bundler/GemComment:
|
|
174
174
|
IgnoredGems: []
|
175
175
|
OnlyFor: []
|
176
176
|
|
177
|
+
Bundler/GemFilename:
|
178
|
+
Description: 'Enforces the filename for managing gems.'
|
179
|
+
Enabled: true
|
180
|
+
VersionAdded: '1.20'
|
181
|
+
EnforcedStyle: 'Gemfile'
|
182
|
+
SupportedStyles:
|
183
|
+
- 'Gemfile'
|
184
|
+
- 'gems.rb'
|
185
|
+
Include:
|
186
|
+
- '**/Gemfile'
|
187
|
+
- '**/gems.rb'
|
188
|
+
- '**/Gemfile.lock'
|
189
|
+
- '**/gems.locked'
|
190
|
+
|
177
191
|
Bundler/GemVersion:
|
178
192
|
Description: 'Requires or forbids specifying gem versions.'
|
179
193
|
Enabled: false
|
@@ -1434,6 +1448,13 @@ Lint/AmbiguousOperator:
|
|
1434
1448
|
VersionAdded: '0.17'
|
1435
1449
|
VersionChanged: '0.83'
|
1436
1450
|
|
1451
|
+
Lint/AmbiguousRange:
|
1452
|
+
Description: Checks for ranges with ambiguous boundaries.
|
1453
|
+
Enabled: pending
|
1454
|
+
VersionAdded: '1.19'
|
1455
|
+
SafeAutoCorrect: false
|
1456
|
+
RequireParenthesesForMethodChains: false
|
1457
|
+
|
1437
1458
|
Lint/AmbiguousRegexpLiteral:
|
1438
1459
|
Description: >-
|
1439
1460
|
Checks for ambiguous regexp literals in the first argument of
|
@@ -1512,6 +1533,11 @@ Lint/Debugger:
|
|
1512
1533
|
Capybara:
|
1513
1534
|
- save_and_open_page
|
1514
1535
|
- save_and_open_screenshot
|
1536
|
+
debug.rb:
|
1537
|
+
- binding.b
|
1538
|
+
- binding.break
|
1539
|
+
- Kernel.binding.b
|
1540
|
+
- Kernel.binding.break
|
1515
1541
|
Pry:
|
1516
1542
|
- binding.pry
|
1517
1543
|
- binding.remote_pry
|
@@ -1520,6 +1546,8 @@ Lint/Debugger:
|
|
1520
1546
|
Rails:
|
1521
1547
|
- debugger
|
1522
1548
|
- Kernel.debugger
|
1549
|
+
RubyJard:
|
1550
|
+
- jard
|
1523
1551
|
WebConsole:
|
1524
1552
|
- binding.console
|
1525
1553
|
|
@@ -1824,7 +1852,6 @@ Lint/MultipleComparison:
|
|
1824
1852
|
Enabled: true
|
1825
1853
|
VersionAdded: '0.47'
|
1826
1854
|
VersionChanged: '1.1'
|
1827
|
-
AllowMethodComparison: true
|
1828
1855
|
|
1829
1856
|
Lint/NestedMethodDefinition:
|
1830
1857
|
Description: 'Do not use nested method definitions.'
|
@@ -2557,6 +2584,7 @@ Naming/InclusiveLanguage:
|
|
2557
2584
|
- denylist
|
2558
2585
|
- block
|
2559
2586
|
slave:
|
2587
|
+
WholeWord: true
|
2560
2588
|
Suggestions: ['replica', 'secondary', 'follower']
|
2561
2589
|
|
2562
2590
|
Naming/MemoizedInstanceVariableName:
|
@@ -3142,7 +3170,7 @@ Style/CommentAnnotation:
|
|
3142
3170
|
StyleGuide: '#annotate-keywords'
|
3143
3171
|
Enabled: true
|
3144
3172
|
VersionAdded: '0.10'
|
3145
|
-
VersionChanged: '1.
|
3173
|
+
VersionChanged: '1.20'
|
3146
3174
|
Keywords:
|
3147
3175
|
- TODO
|
3148
3176
|
- FIXME
|
@@ -3155,8 +3183,9 @@ Style/CommentAnnotation:
|
|
3155
3183
|
Style/CommentedKeyword:
|
3156
3184
|
Description: 'Do not place comments on the same line as certain keywords.'
|
3157
3185
|
Enabled: true
|
3186
|
+
SafeAutoCorrect: false
|
3158
3187
|
VersionAdded: '0.51'
|
3159
|
-
VersionChanged: '1.
|
3188
|
+
VersionChanged: '1.19'
|
3160
3189
|
|
3161
3190
|
Style/ConditionalAssignment:
|
3162
3191
|
Description: >-
|
@@ -3603,8 +3632,9 @@ Style/IdenticalConditionalBranches:
|
|
3603
3632
|
line at the end of each branch, which can validly be moved
|
3604
3633
|
out of the conditional.
|
3605
3634
|
Enabled: true
|
3635
|
+
SafeAutoCorrect: false
|
3606
3636
|
VersionAdded: '0.36'
|
3607
|
-
VersionChanged: '1.
|
3637
|
+
VersionChanged: '1.19'
|
3608
3638
|
|
3609
3639
|
Style/IfInsideElse:
|
3610
3640
|
Description: 'Finds if nodes inside else, which can be converted to elsif.'
|
@@ -3929,6 +3959,7 @@ Style/MultipleComparison:
|
|
3929
3959
|
Enabled: true
|
3930
3960
|
VersionAdded: '0.49'
|
3931
3961
|
VersionChanged: '1.1'
|
3962
|
+
AllowMethodComparison: true
|
3932
3963
|
|
3933
3964
|
Style/MutableConstant:
|
3934
3965
|
Description: 'Do not assign mutable objects to constants.'
|
@@ -4152,6 +4183,7 @@ Style/OptionHash:
|
|
4152
4183
|
- args
|
4153
4184
|
- params
|
4154
4185
|
- parameters
|
4186
|
+
Allowlist: []
|
4155
4187
|
|
4156
4188
|
Style/OptionalArguments:
|
4157
4189
|
Description: >-
|
@@ -4404,6 +4436,11 @@ Style/RedundantSelfAssignment:
|
|
4404
4436
|
Safe: false
|
4405
4437
|
VersionAdded: '0.90'
|
4406
4438
|
|
4439
|
+
Style/RedundantSelfAssignmentBranch:
|
4440
|
+
Description: 'Checks for places where conditional branch makes redundant self-assignment.'
|
4441
|
+
Enabled: pending
|
4442
|
+
VersionAdded: '1.19'
|
4443
|
+
|
4407
4444
|
Style/RedundantSort:
|
4408
4445
|
Description: >-
|
4409
4446
|
Use `min` instead of `sort.first`,
|
@@ -4575,6 +4612,7 @@ Style/SpecialGlobalVars:
|
|
4575
4612
|
VersionAdded: '0.13'
|
4576
4613
|
VersionChanged: '0.36'
|
4577
4614
|
SafeAutoCorrect: false
|
4615
|
+
RequireEnglish: true
|
4578
4616
|
EnforcedStyle: use_english_names
|
4579
4617
|
SupportedStyles:
|
4580
4618
|
- use_perl_names
|
@@ -4675,8 +4713,9 @@ Style/StructInheritance:
|
|
4675
4713
|
Description: 'Checks for inheritance from Struct.new.'
|
4676
4714
|
StyleGuide: '#no-extend-struct-new'
|
4677
4715
|
Enabled: true
|
4716
|
+
SafeAutoCorrect: false
|
4678
4717
|
VersionAdded: '0.29'
|
4679
|
-
VersionChanged: '
|
4718
|
+
VersionChanged: '1.20'
|
4680
4719
|
|
4681
4720
|
Style/SwapValues:
|
4682
4721
|
Description: 'This cop enforces the use of shorthand-style swapping of 2 variables.'
|
@@ -4895,7 +4934,7 @@ Style/VariableInterpolation:
|
|
4895
4934
|
|
4896
4935
|
Style/WhenThen:
|
4897
4936
|
Description: 'Use when x then ... for one-line cases.'
|
4898
|
-
StyleGuide: '#
|
4937
|
+
StyleGuide: '#no-when-semicolons'
|
4899
4938
|
Enabled: true
|
4900
4939
|
VersionAdded: '0.9'
|
4901
4940
|
|
@@ -4919,7 +4958,7 @@ Style/WordArray:
|
|
4919
4958
|
StyleGuide: '#percent-w'
|
4920
4959
|
Enabled: true
|
4921
4960
|
VersionAdded: '0.9'
|
4922
|
-
VersionChanged: '
|
4961
|
+
VersionChanged: '1.19'
|
4923
4962
|
EnforcedStyle: percent
|
4924
4963
|
SupportedStyles:
|
4925
4964
|
# percent style: %w(word1 word2)
|
data/lib/rubocop/cli.rb
CHANGED
@@ -8,6 +8,11 @@ module RuboCop
|
|
8
8
|
STATUS_OFFENSES = 1
|
9
9
|
STATUS_ERROR = 2
|
10
10
|
STATUS_INTERRUPTED = 128 + Signal.list['INT']
|
11
|
+
DEFAULT_PARALLEL_OPTIONS = %i[
|
12
|
+
color debug display_style_guide display_time display_only_fail_level_offenses
|
13
|
+
display_only_failed except extra_details fail_level fix_layout format
|
14
|
+
ignore_disable_comments lint only only_guide_cops require safe
|
15
|
+
].freeze
|
11
16
|
|
12
17
|
class Finished < RuntimeError; end
|
13
18
|
|
@@ -37,6 +42,7 @@ module RuboCop
|
|
37
42
|
else
|
38
43
|
act_on_options
|
39
44
|
validate_options_vs_config
|
45
|
+
parallel_by_default!
|
40
46
|
apply_default_formatter
|
41
47
|
execute_runners
|
42
48
|
end
|
@@ -84,6 +90,18 @@ module RuboCop
|
|
84
90
|
'with AllCops: UseCache: false is not allowed.'
|
85
91
|
end
|
86
92
|
|
93
|
+
def parallel_by_default!
|
94
|
+
# See https://github.com/rubocop/rubocop/pull/4537 for JRuby and Windows constraints.
|
95
|
+
return if RUBY_ENGINE != 'ruby' || RuboCop::Platform.windows?
|
96
|
+
|
97
|
+
if (@options.keys - DEFAULT_PARALLEL_OPTIONS).empty? &&
|
98
|
+
@config_store.for_pwd.for_all_cops['UseCache'] != false
|
99
|
+
puts 'Use parallel by default.' if @options[:debug]
|
100
|
+
|
101
|
+
@options[:parallel] = true
|
102
|
+
end
|
103
|
+
end
|
104
|
+
|
87
105
|
def act_on_options
|
88
106
|
set_options_to_config_loader
|
89
107
|
|
@@ -143,7 +143,7 @@ module RuboCop
|
|
143
143
|
PENDING_BANNER = <<~BANNER
|
144
144
|
The following cops were added to RuboCop, but are not configured. Please set Enabled to either `true` or `false` in your `.rubocop.yml` file.
|
145
145
|
|
146
|
-
Please also note that can
|
146
|
+
Please also note that you can opt-in to new cops by default by adding this to your config:
|
147
147
|
AllCops:
|
148
148
|
NewCops: enable
|
149
149
|
BANNER
|
@@ -161,7 +161,7 @@ module RuboCop
|
|
161
161
|
def warn_pending_cop(cop)
|
162
162
|
version = cop.metadata['VersionAdded'] || 'N/A'
|
163
163
|
|
164
|
-
warn Rainbow("#{cop.name}: #
|
164
|
+
warn Rainbow("#{cop.name}: # new in #{version}").yellow
|
165
165
|
warn Rainbow(' Enabled: true').yellow
|
166
166
|
end
|
167
167
|
|
@@ -108,7 +108,7 @@ module RuboCop
|
|
108
108
|
result.delete(key)
|
109
109
|
elsif merge_hashes?(base_hash, derived_hash, key)
|
110
110
|
result[key] = merge(base_hash[key], derived_hash[key], **opts)
|
111
|
-
elsif should_union?(
|
111
|
+
elsif should_union?(derived_hash, base_hash, opts[:inherit_mode], key)
|
112
112
|
result[key] = base_hash[key] | derived_hash[key]
|
113
113
|
elsif opts[:debug]
|
114
114
|
warn_on_duplicate_setting(base_hash, derived_hash, key, **opts)
|
@@ -183,11 +183,26 @@ module RuboCop
|
|
183
183
|
local_inherit || hash['inherit_mode'] || {}
|
184
184
|
end
|
185
185
|
|
186
|
-
def should_union?(base_hash,
|
187
|
-
base_hash[key].is_a?(Array)
|
188
|
-
|
189
|
-
|
190
|
-
|
186
|
+
def should_union?(derived_hash, base_hash, root_mode, key)
|
187
|
+
return false unless base_hash[key].is_a?(Array)
|
188
|
+
|
189
|
+
derived_mode = derived_hash['inherit_mode']
|
190
|
+
return false if should_override?(derived_mode, key)
|
191
|
+
return true if should_merge?(derived_mode, key)
|
192
|
+
|
193
|
+
base_mode = base_hash['inherit_mode']
|
194
|
+
return false if should_override?(base_mode, key)
|
195
|
+
return true if should_merge?(base_mode, key)
|
196
|
+
|
197
|
+
should_merge?(root_mode, key)
|
198
|
+
end
|
199
|
+
|
200
|
+
def should_merge?(mode, key)
|
201
|
+
mode && mode['merge'] && mode['merge'].include?(key)
|
202
|
+
end
|
203
|
+
|
204
|
+
def should_override?(mode, key)
|
205
|
+
mode && mode['override'] && mode['override'].include?(key)
|
191
206
|
end
|
192
207
|
|
193
208
|
def merge_hashes?(base_hash, derived_hash, key)
|
@@ -104,12 +104,9 @@ module RuboCop
|
|
104
104
|
# to do so than to pass the value around to various methods.
|
105
105
|
next if name == 'inherit_mode'
|
106
106
|
|
107
|
-
suggestions = NameSimilarity.find_similar_names(name, Cop::Registry.global.map(&:cop_name))
|
108
|
-
suggestion = "Did you mean `#{suggestions.join('`, `')}`?" if suggestions.any?
|
109
|
-
|
110
107
|
message = <<~MESSAGE.rstrip
|
111
|
-
unrecognized cop #{name} found in #{smart_loaded_path}
|
112
|
-
#{suggestion}
|
108
|
+
unrecognized cop or department #{name} found in #{smart_loaded_path}
|
109
|
+
#{suggestion(name)}
|
113
110
|
MESSAGE
|
114
111
|
|
115
112
|
unknown_cops << message
|
@@ -117,6 +114,22 @@ module RuboCop
|
|
117
114
|
raise ValidationError, unknown_cops.join("\n") if unknown_cops.any?
|
118
115
|
end
|
119
116
|
|
117
|
+
def suggestion(name)
|
118
|
+
registry = Cop::Registry.global
|
119
|
+
departments = registry.departments.map(&:to_s)
|
120
|
+
suggestions = NameSimilarity.find_similar_names(name, departments + registry.map(&:cop_name))
|
121
|
+
if suggestions.any?
|
122
|
+
"Did you mean `#{suggestions.join('`, `')}`?"
|
123
|
+
else
|
124
|
+
# Department names can contain slashes, e.g. Chef/Correctness, but there's no support for
|
125
|
+
# the concept of higher level departments in RuboCop. It's a flat structure. So if the user
|
126
|
+
# tries to configure a "top level department", we hint that it's the bottom level
|
127
|
+
# departments that should be configured.
|
128
|
+
suggestions = departments.select { |department| department.start_with?("#{name}/") }
|
129
|
+
"#{name} is not a department. Use `#{suggestions.join('`, `')}`." if suggestions.any?
|
130
|
+
end
|
131
|
+
end
|
132
|
+
|
120
133
|
def validate_syntax_cop
|
121
134
|
syntax_config = @config['Lint/Syntax']
|
122
135
|
default_config = ConfigLoader.default_configuration['Lint/Syntax']
|
@@ -0,0 +1,103 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
module Bundler
|
6
|
+
# This cop verifies that a project contains Gemfile or gems.rb file and correct
|
7
|
+
# associated lock file based on the configuration.
|
8
|
+
#
|
9
|
+
# @example EnforcedStyle: Gemfile (default)
|
10
|
+
# # bad
|
11
|
+
# Project contains gems.rb and gems.locked files
|
12
|
+
#
|
13
|
+
# # bad
|
14
|
+
# Project contains Gemfile and gems.locked file
|
15
|
+
#
|
16
|
+
# # good
|
17
|
+
# Project contains Gemfile and Gemfile.lock
|
18
|
+
#
|
19
|
+
# @example EnforcedStyle: gems.rb
|
20
|
+
# # bad
|
21
|
+
# Project contains Gemfile and Gemfile.lock files
|
22
|
+
#
|
23
|
+
# # bad
|
24
|
+
# Project contains gems.rb and Gemfile.lock file
|
25
|
+
#
|
26
|
+
# # good
|
27
|
+
# Project contains gems.rb and gems.locked files
|
28
|
+
class GemFilename < Base
|
29
|
+
include ConfigurableEnforcedStyle
|
30
|
+
include RangeHelp
|
31
|
+
|
32
|
+
MSG_GEMFILE_REQUIRED = '`gems.rb` file was found but `Gemfile` is required '\
|
33
|
+
'(file path: %<file_path>s).'
|
34
|
+
MSG_GEMS_RB_REQUIRED = '`Gemfile` was found but `gems.rb` file is required '\
|
35
|
+
'(file path: %<file_path>s).'
|
36
|
+
MSG_GEMFILE_MISMATCHED = 'Expected a `Gemfile.lock` with `Gemfile` but found '\
|
37
|
+
'`gems.locked` file (file path: %<file_path>s).'
|
38
|
+
MSG_GEMS_RB_MISMATCHED = 'Expected a `gems.locked` file with `gems.rb` but found '\
|
39
|
+
'`Gemfile.lock` (file path: %<file_path>s).'
|
40
|
+
GEMFILE_FILES = %w[Gemfile Gemfile.lock].freeze
|
41
|
+
GEMS_RB_FILES = %w[gems.rb gems.locked].freeze
|
42
|
+
|
43
|
+
def on_new_investigation
|
44
|
+
file_path = processed_source.file_path
|
45
|
+
basename = File.basename(file_path)
|
46
|
+
return if expected_gemfile?(basename)
|
47
|
+
|
48
|
+
register_offense(file_path, basename)
|
49
|
+
end
|
50
|
+
|
51
|
+
private
|
52
|
+
|
53
|
+
def register_offense(file_path, basename)
|
54
|
+
register_gemfile_offense(file_path, basename) if gemfile_offense?(basename)
|
55
|
+
register_gems_rb_offense(file_path, basename) if gems_rb_offense?(basename)
|
56
|
+
end
|
57
|
+
|
58
|
+
def register_gemfile_offense(file_path, basename)
|
59
|
+
message = case basename
|
60
|
+
when 'gems.rb'
|
61
|
+
MSG_GEMFILE_REQUIRED
|
62
|
+
when 'gems.locked'
|
63
|
+
MSG_GEMFILE_MISMATCHED
|
64
|
+
end
|
65
|
+
|
66
|
+
add_global_offense(format(message, file_path: file_path))
|
67
|
+
end
|
68
|
+
|
69
|
+
def register_gems_rb_offense(file_path, basename)
|
70
|
+
message = case basename
|
71
|
+
when 'Gemfile'
|
72
|
+
MSG_GEMS_RB_REQUIRED
|
73
|
+
when 'Gemfile.lock'
|
74
|
+
MSG_GEMS_RB_MISMATCHED
|
75
|
+
end
|
76
|
+
|
77
|
+
add_global_offense(format(message, file_path: file_path))
|
78
|
+
end
|
79
|
+
|
80
|
+
def gemfile_offense?(basename)
|
81
|
+
gemfile_required? && GEMS_RB_FILES.include?(basename)
|
82
|
+
end
|
83
|
+
|
84
|
+
def gems_rb_offense?(basename)
|
85
|
+
gems_rb_required? && GEMFILE_FILES.include?(basename)
|
86
|
+
end
|
87
|
+
|
88
|
+
def expected_gemfile?(basename)
|
89
|
+
(gemfile_required? && GEMFILE_FILES.include?(basename)) ||
|
90
|
+
(gems_rb_required? && GEMS_RB_FILES.include?(basename))
|
91
|
+
end
|
92
|
+
|
93
|
+
def gemfile_required?
|
94
|
+
style == :Gemfile
|
95
|
+
end
|
96
|
+
|
97
|
+
def gems_rb_required?
|
98
|
+
style == :'gems.rb'
|
99
|
+
end
|
100
|
+
end
|
101
|
+
end
|
102
|
+
end
|
103
|
+
end
|
@@ -0,0 +1,23 @@
|
|
1
|
+
# frozen_string_literal: true
|
2
|
+
|
3
|
+
module RuboCop
|
4
|
+
module Cop
|
5
|
+
# This class ensures a require statement is present for a standard library
|
6
|
+
# determined by the variable library_name
|
7
|
+
class RequireLibraryCorrector
|
8
|
+
extend RangeHelp
|
9
|
+
|
10
|
+
class << self
|
11
|
+
def correct(corrector, node, library_name)
|
12
|
+
node = node.parent while node.parent?
|
13
|
+
node = node.children.first if node.begin_type?
|
14
|
+
corrector.insert_before(node, require_statement(library_name))
|
15
|
+
end
|
16
|
+
|
17
|
+
def require_statement(library_name)
|
18
|
+
"require '#{library_name}'\n"
|
19
|
+
end
|
20
|
+
end
|
21
|
+
end
|
22
|
+
end
|
23
|
+
end
|