rubocop-rails 2.15.0 → 2.15.2

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: 6e850e3f6c08903dd860f5865cb24cab1037189bbd7418c0db047d36094808d8
4
- data.tar.gz: a91a77471ae5e9a4c7a188d5fb958402844ba04c3641f14a6bf82161ea43ebf7
3
+ metadata.gz: 169ed04b1f5378e9e08fbffdca151afaf4b84fd54b1867a173740aadaede60b3
4
+ data.tar.gz: 1a2ac75c313cfcdeec1814d7e3aff4777c5013527d2cf4a2aa4f95aeb45446bf
5
5
  SHA512:
6
- metadata.gz: 753768200576274520f9f6a8d8ab959436e0b87c44a7b51a6eb20803fee0ec842fb1d0debb4c4fef997e8b51f7caee4198692bb8de3e1603ce16fdadf386692f
7
- data.tar.gz: eeabca82a842cea89cb8f3b5fbb54abe83c4d6405112a6493e30aeaa9ebb4df39b67c71a6e43342e77d80b2ca901978d48b77e7a52952254f20545ab9eaaf74a
6
+ metadata.gz: 51d495edd3e825db49ece8f51bfc118c73887681f414b86d31cd47d85dc5a1a22a385a2a17ed4ebf7d1c6332989795e2cde999a364d911c60f452993dc926c4b
7
+ data.tar.gz: 4546785b39af93e6c7fe5a6a3b637fa5c21d4b358c38ea8ad3ebd5c272d1d0a504e8206e8201cfe3771af9199c336649d033e32b324f16b06fc7d150054dc488
data/config/default.yml CHANGED
@@ -160,7 +160,7 @@ Rails/AttributeDefaultBlockValue:
160
160
  Enabled: pending
161
161
  VersionAdded: '2.9'
162
162
  Include:
163
- - 'models/**/*'
163
+ - 'app/models/**/*'
164
164
 
165
165
  Rails/BelongsTo:
166
166
  Description: >-
@@ -367,6 +367,7 @@ Rails/Exit:
367
367
 
368
368
  Rails/ExpandedDateRange:
369
369
  Description: 'Checks for expanded date range.'
370
+ StyleGuide: 'https://rails.rubystyle.guide/#date-time-range'
370
371
  Enabled: pending
371
372
  VersionAdded: '2.11'
372
373
 
@@ -54,6 +54,7 @@ module RuboCop
54
54
  # delegate :bar, to: :foo, prefix: true
55
55
  class Delegate < Base
56
56
  extend AutoCorrector
57
+ include VisibilityHelp
57
58
 
58
59
  MSG = 'Use `delegate` to define delegations.'
59
60
 
@@ -112,17 +113,11 @@ module RuboCop
112
113
  end
113
114
 
114
115
  def private_or_protected_delegation(node)
115
- line = node.first_line
116
- private_or_protected_before(line) ||
117
- private_or_protected_inline(line)
116
+ private_or_protected_inline(node) || node_visibility(node) != :public
118
117
  end
119
118
 
120
- def private_or_protected_before(line)
121
- (processed_source[0..line].map(&:strip) & %w[private protected]).any?
122
- end
123
-
124
- def private_or_protected_inline(line)
125
- processed_source[line - 1].strip.match?(/\A(private )|(protected )/)
119
+ def private_or_protected_inline(node)
120
+ processed_source[node.first_line - 1].strip.match?(/\A(private )|(protected )/)
126
121
  end
127
122
  end
128
123
  end
@@ -106,6 +106,8 @@ module RuboCop
106
106
 
107
107
  def on_send(node)
108
108
  any_manipulation?(node) do
109
+ next if node.method?(:keys) && target_rails_version <= 6.0
110
+
109
111
  add_offense(node) do |corrector|
110
112
  next unless AUTOCORECTABLE_METHODS.include?(node.method_name)
111
113
 
@@ -119,7 +121,7 @@ module RuboCop
119
121
  def autocorrect(corrector, node)
120
122
  receiver = node.receiver
121
123
 
122
- if receiver.receiver.method?(:messages)
124
+ if receiver.receiver.send_type? && receiver.receiver.method?(:messages)
123
125
  corrector.remove(receiver.receiver.loc.dot)
124
126
  corrector.remove(receiver.receiver.loc.selector)
125
127
  end
@@ -53,7 +53,7 @@ module RuboCop
53
53
  end
54
54
 
55
55
  def new_key(key_node, scope_node)
56
- "'#{scopes(scope_node).map(&:value).join('.')}.#{key_node.value}'"
56
+ "'#{scopes(scope_node).map(&:value).join('.')}.#{key_node.value}'".squeeze('.')
57
57
  end
58
58
 
59
59
  def scopes(scope_node)
@@ -20,8 +20,7 @@ module RuboCop
20
20
  class EnumHash < Base
21
21
  extend AutoCorrector
22
22
 
23
- MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. '\
24
- 'Use hash syntax instead.'
23
+ MSG = 'Enum defined as an array found in `%<enum>s` enum declaration. Use hash syntax instead.'
25
24
  RESTRICT_ON_SEND = %i[enum].freeze
26
25
 
27
26
  def_node_matcher :enum?, <<~PATTERN
@@ -29,14 +29,6 @@ module RuboCop
29
29
 
30
30
  minimum_target_rails_version 5.1
31
31
 
32
- def_node_matcher :expanded_date_range, <<~PATTERN
33
- (irange
34
- (send
35
- $_ {:beginning_of_day :beginning_of_week :beginning_of_month :beginning_of_quarter :beginning_of_year})
36
- (send
37
- $_ {:end_of_day :end_of_week :end_of_month :end_of_quarter :end_of_year}))
38
- PATTERN
39
-
40
32
  PREFERRED_METHODS = {
41
33
  beginning_of_day: 'all_day',
42
34
  beginning_of_week: 'all_week',
@@ -54,31 +46,55 @@ module RuboCop
54
46
  }.freeze
55
47
 
56
48
  def on_irange(node)
57
- return unless expanded_date_range(node)
58
-
59
49
  begin_node = node.begin
60
50
  end_node = node.end
61
- return unless same_receiver?(begin_node, end_node)
62
-
63
- beginning_method = begin_node.method_name
64
- end_method = end_node.method_name
65
- return unless use_mapped_methods?(beginning_method, end_method)
51
+ return if allow?(begin_node, end_node)
66
52
 
67
- preferred_method = "#{begin_node.receiver.source}.#{PREFERRED_METHODS[beginning_method]}"
53
+ preferred_method = preferred_method(begin_node)
54
+ if begin_node.method?(:beginning_of_week) && begin_node.arguments.one?
55
+ return unless same_argument?(begin_node, end_node)
68
56
 
69
- add_offense(node, message: format(MSG, preferred_method: preferred_method)) do |corrector|
70
- corrector.replace(node, preferred_method)
57
+ preferred_method << "(#{begin_node.first_argument.source})"
58
+ elsif any_arguments?(begin_node, end_node)
59
+ return
71
60
  end
61
+
62
+ register_offense(node, preferred_method)
72
63
  end
73
64
 
74
65
  private
75
66
 
76
- def same_receiver?(begin_node, end_node)
77
- begin_node.receiver.source == end_node.receiver.source
67
+ def allow?(begin_node, end_node)
68
+ return true unless (begin_source = receiver_source(begin_node))
69
+ return true unless (end_source = receiver_source(end_node))
70
+
71
+ begin_source != end_source || MAPPED_DATE_RANGE_METHODS[begin_node.method_name] != end_node.method_name
72
+ end
73
+
74
+ def receiver_source(node)
75
+ return if !node&.send_type? || node.receiver.nil?
76
+
77
+ node.receiver.source
78
78
  end
79
79
 
80
- def use_mapped_methods?(beginning_method, end_method)
81
- MAPPED_DATE_RANGE_METHODS[beginning_method] == end_method
80
+ def same_argument?(begin_node, end_node)
81
+ begin_node.first_argument.source == end_node.first_argument.source
82
+ end
83
+
84
+ def preferred_method(begin_node)
85
+ +"#{begin_node.receiver.source}.#{PREFERRED_METHODS[begin_node.method_name]}"
86
+ end
87
+
88
+ def any_arguments?(begin_node, end_node)
89
+ begin_node.arguments.any? || end_node.arguments.any?
90
+ end
91
+
92
+ def register_offense(node, preferred_method)
93
+ message = format(MSG, preferred_method: preferred_method)
94
+
95
+ add_offense(node, message: message) do |corrector|
96
+ corrector.replace(node, preferred_method)
97
+ end
82
98
  end
83
99
  end
84
100
  end
@@ -23,8 +23,7 @@ module RuboCop
23
23
  class OrderById < Base
24
24
  include RangeHelp
25
25
 
26
- MSG = 'Do not use the `id` column for ordering. '\
27
- 'Use a timestamp column to order chronologically.'
26
+ MSG = 'Do not use the `id` column for ordering. Use a timestamp column to order chronologically.'
28
27
  RESTRICT_ON_SEND = %i[order].freeze
29
28
 
30
29
  def_node_matcher :order_by_id?, <<~PATTERN
@@ -43,7 +43,7 @@ module RuboCop
43
43
 
44
44
  def register_offense(node, heredoc)
45
45
  add_offense(node) do |corrector|
46
- squiggly_heredoc = heredoc.source.sub(/\A<<-?/, '<<~')
46
+ squiggly_heredoc = heredoc.source.sub(/\A<<(-|~)?/, '<<~')
47
47
 
48
48
  corrector.replace(heredoc, squiggly_heredoc)
49
49
  corrector.remove(node.loc.dot)
@@ -22,12 +22,12 @@ module RuboCop
22
22
  MSG = 'Use `Time.use_zone` with block instead of `Time.zone=`.'
23
23
  RESTRICT_ON_SEND = %i[zone=].freeze
24
24
 
25
- def_node_matcher :time_zone_assignement?, <<~PATTERN
25
+ def_node_matcher :time_zone_assignment?, <<~PATTERN
26
26
  (send (const nil? :Time) :zone= ...)
27
27
  PATTERN
28
28
 
29
29
  def on_send(node)
30
- return unless time_zone_assignement?(node)
30
+ return unless time_zone_assignment?(node)
31
31
 
32
32
  add_offense(node)
33
33
  end
@@ -39,6 +39,7 @@ module RuboCop
39
39
  corrector.replace(node.loc.selector, style)
40
40
  end
41
41
  end
42
+ alias on_csend on_send
42
43
  end
43
44
  end
44
45
  end
@@ -6,7 +6,7 @@ module RuboCop
6
6
  # Checks for the use of exit statements (namely `return`,
7
7
  # `break` and `throw`) in transactions. This is due to the eventual
8
8
  # unexpected behavior when using ActiveRecord >= 7, where transactions
9
- # exitted using these statements are being rollbacked rather than
9
+ # exited using these statements are being rollbacked rather than
10
10
  # committed (pre ActiveRecord 7 behavior).
11
11
  #
12
12
  # As alternatives, it would be more intuitive to explicitly raise an
@@ -56,9 +56,9 @@ module RuboCop
56
56
 
57
57
  def with_index?(klass, table, names)
58
58
  # Compatibility for Rails 4.2.
59
- add_indicies = schema.add_indicies_by(table_name: table_name(klass))
59
+ add_indices = schema.add_indices_by(table_name: table_name(klass))
60
60
 
61
- (table.indices + add_indicies).any? do |index|
61
+ (table.indices + add_indices).any? do |index|
62
62
  index.unique &&
63
63
  (index.columns.to_set == names ||
64
64
  include_column_names_in_expression_index?(index, names))
@@ -5,11 +5,11 @@ module RuboCop
5
5
  module SchemaLoader
6
6
  # Represent db/schema.rb
7
7
  class Schema
8
- attr_reader :tables, :add_indicies
8
+ attr_reader :tables, :add_indices
9
9
 
10
10
  def initialize(ast)
11
11
  @tables = []
12
- @add_indicies = []
12
+ @add_indices = []
13
13
 
14
14
  build!(ast)
15
15
  end
@@ -20,8 +20,8 @@ module RuboCop
20
20
  end
21
21
  end
22
22
 
23
- def add_indicies_by(table_name:)
24
- add_indicies.select do |add_index|
23
+ def add_indices_by(table_name:)
24
+ add_indices.select do |add_index|
25
25
  add_index.table_name == table_name
26
26
  end
27
27
  end
@@ -39,7 +39,7 @@ module RuboCop
39
39
 
40
40
  # Compatibility for Rails 4.2.
41
41
  each_add_index(ast) do |add_index_def|
42
- @add_indicies << AddIndex.new(add_index_def)
42
+ @add_indices << AddIndex.new(add_index_def)
43
43
  end
44
44
  end
45
45
 
@@ -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.15.0'
7
+ STRING = '2.15.2'
8
8
 
9
9
  def self.document_version
10
10
  STRING.match('\d+\.\d+').to_s
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.15.0
4
+ version: 2.15.2
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: 2022-06-14 00:00:00.000000000 Z
13
+ date: 2022-07-07 00:00:00.000000000 Z
14
14
  dependencies:
15
15
  - !ruby/object:Gem::Dependency
16
16
  name: activesupport
@@ -227,7 +227,7 @@ required_rubygems_version: !ruby/object:Gem::Requirement
227
227
  - !ruby/object:Gem::Version
228
228
  version: '0'
229
229
  requirements: []
230
- rubygems_version: 3.4.0.dev
230
+ rubygems_version: 3.1.6
231
231
  signing_key:
232
232
  specification_version: 4
233
233
  summary: Automatic Rails code style checking tool.