-
-
Notifications
You must be signed in to change notification settings - Fork 3k
/
config_obsoletion.rb
283 lines (261 loc) · 11.2 KB
/
config_obsoletion.rb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
# frozen_string_literal: true
module RuboCop
# This class handles obsolete configuration.
# @api private
class ConfigObsoletion
RENAMED_COPS = {
'Layout/AlignArguments' => 'Layout/ArgumentAlignment',
'Layout/AlignArray' => 'Layout/ArrayAlignment',
'Layout/AlignHash' => 'Layout/HashAlignment',
'Layout/AlignParameters' => 'Layout/ParameterAlignment',
'Layout/IndentArray' => 'Layout/FirstArrayElementIndentation',
'Layout/IndentAssignment' => 'Layout/AssignmentIndentation',
'Layout/IndentFirstArgument' => 'Layout/FirstArgumentIndentation',
'Layout/IndentFirstArrayElement' => 'Layout/FirstArrayElementIndentation',
'Layout/IndentFirstHashElement' => 'Layout/FirstHashElementIndentation',
'Layout/IndentFirstParameter' => 'Layout/FirstParameterIndentation',
'Layout/IndentHash' => 'Layout/FirstHashElementIndentation',
'Layout/IndentHeredoc' => 'Layout/HeredocIndentation',
'Layout/LeadingBlankLines' => 'Layout/LeadingEmptyLines',
'Layout/Tab' => 'Layout/IndentationStyle',
'Layout/TrailingBlankLines' => 'Layout/TrailingEmptyLines',
'Lint/DuplicatedKey' => 'Lint/DuplicateHashKey',
'Lint/EndInMethod' => 'Style/EndBlock',
'Lint/HandleExceptions' => 'Lint/SuppressedException',
'Lint/MultipleCompare' => 'Lint/MultipleComparison',
'Lint/StringConversionInInterpolation' => 'Lint/RedundantStringCoercion',
'Lint/UnneededCopDisableDirective' => 'Lint/RedundantCopDisableDirective',
'Lint/UnneededCopEnableDirective' => 'Lint/RedundantCopEnableDirective',
'Lint/UnneededRequireStatement' => 'Lint/RedundantRequireStatement',
'Lint/UnneededSplatExpansion' => 'Lint/RedundantSplatExpansion',
'Naming/UncommunicativeBlockParamName' => 'Naming/BlockParameterName',
'Naming/UncommunicativeMethodParamName' => 'Naming/MethodParameterName',
'Style/DeprecatedHashMethods' => 'Style/PreferredHashMethods',
'Style/MethodCallParentheses' => 'Style/MethodCallWithoutArgsParentheses',
'Style/OpMethod' => 'Naming/BinaryOperatorParameterName',
'Style/SingleSpaceBeforeFirstArg' => 'Layout/SpaceBeforeFirstArg',
'Style/UnneededCapitalW' => 'Style/RedundantCapitalW',
'Style/UnneededCondition' => 'Style/RedundantCondition',
'Style/UnneededInterpolation' => 'Style/RedundantInterpolation',
'Style/UnneededPercentQ' => 'Style/RedundantPercentQ',
'Style/UnneededSort' => 'Style/RedundantSort'
}.map do |old_name, new_name|
[old_name, "The `#{old_name}` cop has been renamed to `#{new_name}`."]
end
MOVED_COPS = {
'Security' => 'Lint/Eval',
'Naming' => %w[Style/ClassAndModuleCamelCase Style/ConstantName
Style/FileName Style/MethodName Style/PredicateName
Style/VariableName Style/VariableNumber
Style/AccessorMethodName Style/AsciiIdentifiers],
'Layout' => %w[Lint/BlockAlignment Lint/EndAlignment
Lint/DefEndAlignment Metrics/LineLength],
'Lint' => 'Style/FlipFlop'
}.map do |new_department, old_names|
Array(old_names).map do |old_name|
[old_name, "The `#{old_name}` cop has been moved to " \
"`#{new_department}/#{old_name.split('/').last}`."]
end
end
REMOVED_COPS = {
'Layout/SpaceAfterControlKeyword' => 'Layout/SpaceAroundKeyword',
'Layout/SpaceBeforeModifierKeyword' => 'Layout/SpaceAroundKeyword',
'Lint/RescueWithoutErrorClass' => 'Style/RescueStandardError',
'Style/SpaceAfterControlKeyword' => 'Layout/SpaceAroundKeyword',
'Style/SpaceBeforeModifierKeyword' => 'Layout/SpaceAroundKeyword',
'Style/TrailingComma' => 'Style/TrailingCommaInArguments, ' \
'Style/TrailingCommaInArrayLiteral, and/or ' \
'Style/TrailingCommaInHashLiteral',
'Style/TrailingCommaInLiteral' => 'Style/TrailingCommaInArrayLiteral ' \
'and/or ' \
'Style/TrailingCommaInHashLiteral',
'Style/BracesAroundHashParameters' => nil
}.map do |old_name, other_cops|
if other_cops
more = ". Please use #{other_cops} instead".gsub(%r{[A-Z]\w+/\w+},
'`\&`')
end
[old_name, "The `#{old_name}` cop has been removed#{more}."]
end
REMOVED_COPS_WITH_REASON = {
'Lint/InvalidCharacterLiteral' => 'it was never being actually triggered',
'Lint/SpaceBeforeFirstArg' =>
'it was a duplicate of `Layout/SpaceBeforeFirstArg`. Please use ' \
'`Layout/SpaceBeforeFirstArg` instead',
'Style/MethodMissingSuper' => 'it has been superseded by `Lint/MissingSuper`. Please use ' \
'`Lint/MissingSuper` instead',
'Lint/UselessComparison' => 'it has been superseded by '\
'`Lint/BinaryOperatorWithIdenticalOperands`. Please use '\
'`Lint/BinaryOperatorWithIdenticalOperands` instead'
}.map do |cop_name, reason|
[cop_name, "The `#{cop_name}` cop has been removed since #{reason}."]
end
SPLIT_COPS = {
'Style/MethodMissing' =>
'The `Style/MethodMissing` cop has been split into ' \
'`Style/MethodMissingSuper` and `Style/MissingRespondToMissing`.'
}.to_a
OBSOLETE_COPS = Hash[*(RENAMED_COPS + MOVED_COPS + REMOVED_COPS +
REMOVED_COPS_WITH_REASON + SPLIT_COPS).flatten]
OBSOLETE_PARAMETERS = [
{
cops: %w[Layout/SpaceAroundOperators Style/SpaceAroundOperators],
parameters: 'MultiSpaceAllowedForOperators',
alternative: 'If your intention was to allow extra spaces for ' \
'alignment, please use AllowForAlignment: true instead.'
},
{
cops: 'Style/Encoding',
parameters: %w[EnforcedStyle SupportedStyles
AutoCorrectEncodingComment],
alternative: 'Style/Encoding no longer supports styles. ' \
'The "never" behavior is always assumed.'
},
{
cops: 'Style/IfUnlessModifier',
parameters: 'MaxLineLength',
alternative: '`Style/IfUnlessModifier: MaxLineLength` has been ' \
'removed. Use `Layout/LineLength: Max` instead'
},
{
cops: 'Style/WhileUntilModifier',
parameters: 'MaxLineLength',
alternative: '`Style/WhileUntilModifier: MaxLineLength` has been ' \
'removed. Use `Layout/LineLength: Max` instead'
},
{
cops: 'AllCops',
parameters: 'RunRailsCops',
alternative: "Use the following configuration instead:\n" \
"Rails:\n Enabled: true"
},
{
cops: 'Layout/CaseIndentation',
parameters: 'IndentWhenRelativeTo',
alternative: '`IndentWhenRelativeTo` has been renamed to ' \
'`EnforcedStyle`'
},
{
cops: %w[Lint/BlockAlignment Layout/BlockAlignment Lint/EndAlignment
Layout/EndAlignment Lint/DefEndAlignment
Layout/DefEndAlignment],
parameters: 'AlignWith',
alternative: '`AlignWith` has been renamed to `EnforcedStyleAlignWith`'
},
{
cops: 'Rails/UniqBeforePluck',
parameters: 'EnforcedMode',
alternative: '`EnforcedMode` has been renamed to `EnforcedStyle`'
},
{
cops: 'Style/MethodCallWithArgsParentheses',
parameters: 'IgnoredMethodPatterns',
alternative: '`IgnoredMethodPatterns` has been renamed to ' \
'`IgnoredPatterns`'
},
{
cops: %w[Performance/Count Performance/Detect],
parameters: 'SafeMode',
alternative: '`SafeMode` has been removed. ' \
'Use `SafeAutoCorrect` instead.'
},
{
cops: 'Bundler/GemComment',
parameters: 'Whitelist',
alternative: '`Whitelist` has been renamed to `IgnoredGems`.'
},
{
cops: %w[
Lint/SafeNavigationChain Lint/SafeNavigationConsistency
Style/NestedParenthesizedCalls Style/SafeNavigation
Style/TrivialAccessors
],
parameters: 'Whitelist',
alternative: '`Whitelist` has been renamed to `AllowedMethods`.'
},
{
cops: 'Style/IpAddresses',
parameters: 'Whitelist',
alternative: '`Whitelist` has been renamed to `AllowedAddresses`.'
},
{
cops: 'Naming/HeredocDelimiterNaming',
parameters: 'Blacklist',
alternative: '`Blacklist` has been renamed to `ForbiddenDelimiters`.'
},
{
cops: 'Naming/PredicateName',
parameters: 'NamePrefixBlacklist',
alternative: '`NamePrefixBlacklist` has been renamed to ' \
'`ForbiddenPrefixes`.'
},
{
cops: 'Naming/PredicateName',
parameters: 'NameWhitelist',
alternative: '`NameWhitelist` has been renamed to ' \
'`AllowedMethods`.'
}
].freeze
OBSOLETE_ENFORCED_STYLES = [
{
cop: 'Layout/IndentationConsistency',
parameter: 'EnforcedStyle',
enforced_style: 'rails',
alternative: '`EnforcedStyle: rails` has been renamed to ' \
'`EnforcedStyle: indented_internal_methods`'
}
].freeze
def initialize(config)
@config = config
end
def reject_obsolete_cops_and_parameters
messages = [obsolete_cops, obsolete_parameters,
obsolete_enforced_style].flatten.compact
return if messages.empty?
raise ValidationError, messages.join("\n")
end
private
def obsolete_cops
OBSOLETE_COPS.map do |cop_name, message|
next unless @config.key?(cop_name) ||
@config.key?(Cop::Badge.parse(cop_name).cop_name)
message + "\n(obsolete configuration found in " \
"#{smart_loaded_path}, please update it)"
end
end
def obsolete_enforced_style
OBSOLETE_ENFORCED_STYLES.map do |params|
obsolete_enforced_style_message(params[:cop], params[:parameter],
params[:enforced_style],
params[:alternative])
end
end
def obsolete_enforced_style_message(cop, param, enforced_style, alternative)
style = @config[cop]&.detect { |key, _| key.start_with?(param) }
return unless style && style[1] == enforced_style
"obsolete `#{param}: #{enforced_style}` (for #{cop}) found in " \
"#{smart_loaded_path}\n#{alternative}"
end
def obsolete_parameters
OBSOLETE_PARAMETERS.map do |params|
obsolete_parameter_message(params[:cops], params[:parameters],
params[:alternative])
end
end
def obsolete_parameter_message(cops, parameters, alternative)
Array(cops).map do |cop|
obsolete_parameters = Array(parameters).select do |param|
@config[cop]&.key?(param)
end
next if obsolete_parameters.empty?
obsolete_parameters.map do |parameter|
"obsolete parameter #{parameter} (for #{cop}) found in " \
"#{smart_loaded_path}\n#{alternative}"
end
end
end
def smart_loaded_path
PathUtil.smart_path(@config.loaded_path)
end
end
end