rubocop-rails 2.2.1 → 2.3.0

Sign up to get free protection for your applications and to get access to all the features.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 492db5e3433993952c5b4b1df25b1a5b1a43531603eee5295d2c48f8d705b2c3
4
- data.tar.gz: 33d65e499186e463cf38b03397510fded02216053078289c639e80077c731b50
3
+ metadata.gz: f18a288648626076787a00773149df97edce3b5330a5dc650cf662af46214bd5
4
+ data.tar.gz: f945d031d0a5bc98eb0af95a6cdf8e931cb5a469a1c1ced6ab11c1fb20f09bb5
5
5
  SHA512:
6
- metadata.gz: 61cede5d16b9e4ec2451035108c7bf9e51ddf257ae2bc637d57827bc6eb16d4cb6e3408535ed4b47e39dbccbbc20ff881a076847e5e541980479ecb8867f10bb
7
- data.tar.gz: e80b95b67271d8e7708a0da90550afe30678dfb52ca20edd4e094ef21f414759ce5e09f141f83e34f9f0af0a3bb10d23982ca70e531ee57f88e1ffecad44d15c
6
+ metadata.gz: 24f03fcf8cd683c5ed3e2afc9243f113afaa8641fc5cbd938d5d2ff916b7eeed95a994b69b48509b20618341c5b5c39286bde009f1dc5b369f94dc249a65b148
7
+ data.tar.gz: 57d3fff4181c71750d0eccdde094607ad63f79b7a6b273f698224dcc687a707879df82a264df063b4ac8f133fff3f4fe23dfcb820b4d2863cbae6792c6e38906
@@ -9,6 +9,9 @@ AllCops:
9
9
  # application. If neither of those files exist, RuboCop will use Rails 5.0
10
10
  # as the default.
11
11
  TargetRailsVersion: ~
12
+ # When specifying style guide URLs, any paths and/or fragments will be
13
+ # evaluated relative to the base URL.
14
+ StyleGuideBaseURL: https://rails.rubystyle.guide
12
15
 
13
16
  Rails/ActionFilter:
14
17
  Description: 'Enforces consistent use of action filter methods.'
@@ -28,6 +31,7 @@ Rails/ActiveRecordAliases:
28
31
  Use `update!` instead of `update_attributes!`.
29
32
  Enabled: true
30
33
  VersionAdded: '0.53'
34
+ SafeAutoCorrect: false
31
35
 
32
36
  Rails/ActiveRecordOverride:
33
37
  Description: >-
@@ -136,12 +140,20 @@ Rails/DelegateAllowBlank:
136
140
 
137
141
  Rails/DynamicFindBy:
138
142
  Description: 'Use `find_by` instead of dynamic `find_by_*`.'
139
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#find_by'
143
+ StyleGuide: '#find_by'
140
144
  Enabled: true
141
145
  VersionAdded: '0.44'
142
146
  Whitelist:
143
147
  - find_by_sql
144
148
 
149
+ Rails/EnumHash:
150
+ Description: 'Prefer hash syntax over array syntax when defining enums.'
151
+ StyleGuide: '#enums'
152
+ Enabled: true
153
+ VersionAdded: '2.3'
154
+ Include:
155
+ - app/models/**/*.rb
156
+
145
157
  Rails/EnumUniqueness:
146
158
  Description: 'Avoid duplicate integers in hash-syntax `enum` declaration.'
147
159
  Enabled: true
@@ -180,7 +192,7 @@ Rails/FilePath:
180
192
 
181
193
  Rails/FindBy:
182
194
  Description: 'Prefer find_by over where.first.'
183
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#find_by'
195
+ StyleGuide: '#find_by'
184
196
  Enabled: true
185
197
  VersionAdded: '0.30'
186
198
  Include:
@@ -188,7 +200,7 @@ Rails/FindBy:
188
200
 
189
201
  Rails/FindEach:
190
202
  Description: 'Prefer all.find_each over all.find.'
191
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#find-each'
203
+ StyleGuide: '#find-each'
192
204
  Enabled: true
193
205
  VersionAdded: '0.30'
194
206
  Include:
@@ -196,7 +208,7 @@ Rails/FindEach:
196
208
 
197
209
  Rails/HasAndBelongsToMany:
198
210
  Description: 'Prefer has_many :through to has_and_belongs_to_many.'
199
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#has-many-through'
211
+ StyleGuide: '#has-many-through'
200
212
  Enabled: true
201
213
  VersionAdded: '0.12'
202
214
  Include:
@@ -204,7 +216,7 @@ Rails/HasAndBelongsToMany:
204
216
 
205
217
  Rails/HasManyOrHasOneDependent:
206
218
  Description: 'Define the dependent option to the has_many and has_one associations.'
207
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#has_many-has_one-dependent-option'
219
+ StyleGuide: '#has_many-has_one-dependent-option'
208
220
  Enabled: true
209
221
  VersionAdded: '0.50'
210
222
  Include:
@@ -251,7 +263,7 @@ Rails/InverseOf:
251
263
 
252
264
  Rails/LexicallyScopedActionFilter:
253
265
  Description: "Checks that methods specified in the filter's `only` or `except` options are explicitly defined in the controller."
254
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#lexically-scoped-action-filter'
266
+ StyleGuide: '#lexically-scoped-action-filter'
255
267
  Enabled: true
256
268
  Safe: false
257
269
  VersionAdded: '0.52'
@@ -316,7 +328,7 @@ Rails/ReadWriteAttribute:
316
328
  Description: >-
317
329
  Checks for read_attribute(:attr) and
318
330
  write_attribute(:attr, val).
319
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#read-attribute'
331
+ StyleGuide: '#read-attribute'
320
332
  Enabled: true
321
333
  VersionAdded: '0.20'
322
334
  VersionChanged: '0.29'
@@ -367,7 +379,7 @@ Rails/RequestReferer:
367
379
 
368
380
  Rails/ReversibleMigration:
369
381
  Description: 'Checks whether the change method of the migration file is reversible.'
370
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#reversible-migration'
382
+ StyleGuide: '#reversible-migration'
371
383
  Reference: 'https://api.rubyonrails.org/classes/ActiveRecord/Migration/CommandRecorder.html'
372
384
  Enabled: true
373
385
  VersionAdded: '0.47'
@@ -386,12 +398,13 @@ Rails/SafeNavigation:
386
398
 
387
399
  Rails/SaveBang:
388
400
  Description: 'Identifies possible cases where Active Record save! or related should be used.'
389
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#save-bang'
401
+ StyleGuide: '#save-bang'
390
402
  Enabled: false
391
403
  VersionAdded: '0.42'
392
404
  VersionChanged: '0.59'
393
405
  AllowImplicitReturn: true
394
406
  AllowedReceivers: []
407
+ SafeAutoCorrect: false
395
408
 
396
409
  Rails/ScopeArgs:
397
410
  Description: 'Checks the arguments of ActiveRecord scopes.'
@@ -424,7 +437,7 @@ Rails/SkipsModelValidations:
424
437
 
425
438
  Rails/TimeZone:
426
439
  Description: 'Checks the correct usage of time zone aware methods.'
427
- StyleGuide: 'https://github.com/rubocop-hq/rails-style-guide#time'
440
+ StyleGuide: '#time'
428
441
  Reference: 'http://danilenko.org/2012/7/6/rails_timezones'
429
442
  Enabled: true
430
443
  Safe: false
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Rails
6
6
  # This cop checks dynamic `find_by_*` methods.
7
7
  # Use `find_by` instead of dynamic method.
8
- # See. https://github.com/rubocop-hq/rails-style-guide#find_by
8
+ # See. https://rails.rubystyle.guide#find_by
9
9
  #
10
10
  # @example
11
11
  # # bad
@@ -0,0 +1,66 @@
1
+ # frozen_string_literal: true
2
+
3
+ module RuboCop
4
+ module Cop
5
+ module Rails
6
+ # This cop looks for enums written with array syntax.
7
+ #
8
+ # When using array syntax, adding an element in a
9
+ # position other than the last causes all previous
10
+ # definitions to shift. Explicitly specifying the
11
+ # value for each key prevents this from happening.
12
+ #
13
+ # @example
14
+ # # bad
15
+ # enum status: [:active, :archived]
16
+ #
17
+ # # good
18
+ # enum status: { active: 0, archived: 1 }
19
+ #
20
+ class EnumHash < Cop
21
+ MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '\
22
+ 'Use hash syntax instead.'
23
+
24
+ def_node_matcher :enum?, <<~PATTERN
25
+ (send nil? :enum (hash $...))
26
+ PATTERN
27
+
28
+ def_node_matcher :array_pair?, <<~PATTERN
29
+ (pair $_ $array)
30
+ PATTERN
31
+
32
+ def on_send(node)
33
+ enum?(node) do |pairs|
34
+ pairs.each do |pair|
35
+ key, array = array_pair?(pair)
36
+ next unless key
37
+
38
+ add_offense(array, message: format(MSG, enum: enum_name(key)))
39
+ end
40
+ end
41
+ end
42
+
43
+ def autocorrect(node)
44
+ range = node.loc.expression
45
+ hash = node
46
+ .children
47
+ .each_with_index
48
+ .map { |elem, index| [elem.children.first, index] }.to_h
49
+
50
+ ->(corrector) { corrector.replace(range, hash.to_s) }
51
+ end
52
+
53
+ private
54
+
55
+ def enum_name(key)
56
+ case key.type
57
+ when :sym, :str
58
+ key.value
59
+ else
60
+ key.source
61
+ end
62
+ end
63
+ end
64
+ end
65
+ end
66
+ end
@@ -23,22 +23,42 @@ module RuboCop
23
23
  MSG = 'Duplicate value `%<value>s` found in `%<enum>s` ' \
24
24
  'enum declaration.'
25
25
 
26
- def_node_matcher :enum_declaration, <<-PATTERN
27
- (send nil? :enum (hash (pair (_ $_) ${array hash})))
26
+ def_node_matcher :enum?, <<~PATTERN
27
+ (send nil? :enum (hash $...))
28
+ PATTERN
29
+
30
+ def_node_matcher :enum_values, <<~PATTERN
31
+ (pair $_ ${array hash})
28
32
  PATTERN
29
33
 
30
34
  def on_send(node)
31
- enum_declaration(node) do |name, args|
32
- items = args.values
35
+ enum?(node) do |pairs|
36
+ pairs.each do |pair|
37
+ enum_values(pair) do |key, args|
38
+ items = args.values
33
39
 
34
- return unless duplicates?(items)
40
+ next unless duplicates?(items)
35
41
 
36
- consecutive_duplicates(items).each do |item|
37
- add_offense(item, message: format(MSG, value: item.source,
38
- enum: name))
42
+ consecutive_duplicates(items).each do |item|
43
+ add_offense(item, message: format(
44
+ MSG, value: item.source, enum: enum_name(key)
45
+ ))
46
+ end
47
+ end
39
48
  end
40
49
  end
41
50
  end
51
+
52
+ private
53
+
54
+ def enum_name(key)
55
+ case key.type
56
+ when :sym, :str
57
+ key.value
58
+ else
59
+ key.source
60
+ end
61
+ end
42
62
  end
43
63
  end
44
64
  end
@@ -8,6 +8,7 @@ module RuboCop
8
8
  # save and an exception is better than unhandled failure.
9
9
  #
10
10
  # This will allow:
11
+ #
11
12
  # - update or save calls, assigned to a variable,
12
13
  # or used as a condition in an if/unless/case statement.
13
14
  # - create calls, assigned to a variable that then has a
@@ -107,7 +108,7 @@ module RuboCop
107
108
  CREATE_CONDITIONAL_MSG = '`%<current>s` returns a model which is ' \
108
109
  'always truthy.'
109
110
 
110
- CREATE_PERSIST_METHODS = %i[create
111
+ CREATE_PERSIST_METHODS = %i[create create_or_find_by
111
112
  first_or_create find_or_create_by].freeze
112
113
  MODIFY_PERSIST_METHODS = %i[save
113
114
  update update_attributes destroy].freeze
@@ -139,9 +140,9 @@ module RuboCop
139
140
  def on_send(node) # rubocop:disable Metrics/CyclomaticComplexity
140
141
  return unless persist_method?(node)
141
142
  return if return_value_assigned?(node)
143
+ return if implicit_return?(node)
142
144
  return if check_used_in_conditional(node)
143
145
  return if argument?(node)
144
- return if implicit_return?(node)
145
146
  return if explicit_return?(node)
146
147
 
147
148
  add_offense_for_node(node)
@@ -276,10 +277,18 @@ module RuboCop
276
277
  return false unless cop_config['AllowImplicitReturn']
277
278
 
278
279
  node = assignable_node(node)
279
- method = node.parent
280
+ method, sibling_index = find_method_with_sibling_index(node.parent)
280
281
  return unless method && (method.def_type? || method.block_type?)
281
282
 
282
- method.children.size == node.sibling_index + 1
283
+ method.children.size == node.sibling_index + sibling_index
284
+ end
285
+
286
+ def find_method_with_sibling_index(node, sibling_index = 1)
287
+ return node, sibling_index unless node&.or_type?
288
+
289
+ sibling_index += 1
290
+
291
+ find_method_with_sibling_index(node.parent, sibling_index)
283
292
  end
284
293
 
285
294
  def argument?(node)
@@ -5,7 +5,7 @@ module RuboCop
5
5
  module Rails
6
6
  # This cop checks for the use of Time methods without zone.
7
7
  #
8
- # Built on top of Ruby on Rails style guide (https://github.com/rubocop-hq/rails-style-guide#time)
8
+ # Built on top of Ruby on Rails style guide (https://rails.rubystyle.guide#time)
9
9
  # and the article http://danilenko.org/2012/7/6/rails_timezones/
10
10
  #
11
11
  # Two styles are supported for this cop. When EnforcedStyle is 'strict'
@@ -17,6 +17,7 @@ require_relative 'rails/date'
17
17
  require_relative 'rails/delegate'
18
18
  require_relative 'rails/delegate_allow_blank'
19
19
  require_relative 'rails/dynamic_find_by'
20
+ require_relative 'rails/enum_hash'
20
21
  require_relative 'rails/enum_uniqueness'
21
22
  require_relative 'rails/environment_comparison'
22
23
  require_relative 'rails/exit'
@@ -4,7 +4,7 @@ module RuboCop
4
4
  module Rails
5
5
  # This module holds the RuboCop Rails version information.
6
6
  module Version
7
- STRING = '2.2.1'
7
+ STRING = '2.3.0'
8
8
  end
9
9
  end
10
10
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: rubocop-rails
3
3
  version: !ruby/object:Gem::Version
4
- version: 2.2.1
4
+ version: 2.3.0
5
5
  platform: ruby
6
6
  authors:
7
7
  - Bozhidar Batsov
@@ -10,7 +10,7 @@ authors:
10
10
  autorequire:
11
11
  bindir: bin
12
12
  cert_chain: []
13
- date: 2019-07-13 00:00:00.000000000 Z
13
+ date: 2019-08-13 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: rack
@@ -71,6 +71,7 @@ files:
71
71
  - lib/rubocop/cop/rails/delegate.rb
72
72
  - lib/rubocop/cop/rails/delegate_allow_blank.rb
73
73
  - lib/rubocop/cop/rails/dynamic_find_by.rb
74
+ - lib/rubocop/cop/rails/enum_hash.rb
74
75
  - lib/rubocop/cop/rails/enum_uniqueness.rb
75
76
  - lib/rubocop/cop/rails/environment_comparison.rb
76
77
  - lib/rubocop/cop/rails/exit.rb