Skip to content

Commit 6bc9b1c

Browse files
authored
Merge pull request #1337 from Earlopain/validation-error
Fix an error for `Rails/Validation` when passing no arguments
2 parents aa2bf3e + 5e2237a commit 6bc9b1c

File tree

3 files changed

+144
-188
lines changed

3 files changed

+144
-188
lines changed
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
* [#1337](https://github.com/rubocop/rubocop-rails/pull/1337): Fix an error for `Rails/Validation` when passing no arguments. ([@earlopain][])

lib/rubocop/cop/rails/validation.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,11 +59,11 @@ class Validation < Base
5959

6060
def on_send(node)
6161
return if node.receiver
62+
return unless (last_argument = node.last_argument)
6263

6364
range = node.loc.selector
6465

6566
add_offense(range, message: message(node)) do |corrector|
66-
last_argument = node.last_argument
6767
return if !last_argument.literal? && !last_argument.splat_type? && !frozen_array_argument?(last_argument)
6868

6969
corrector.replace(range, 'validates')
Lines changed: 142 additions & 187 deletions
Original file line numberDiff line numberDiff line change
@@ -1,225 +1,180 @@
11
# frozen_string_literal: true
22

33
RSpec.describe RuboCop::Cop::Rails::Validation, :config do
4-
it 'accepts new style validations' do
5-
expect_no_offenses('validates :name')
6-
end
7-
8-
described_class::RESTRICT_ON_SEND.each_with_index do |validation, number|
9-
it "registers an offense for #{validation}" do
10-
offenses = inspect_source("#{validation} :name")
11-
expect(offenses.first.message.include?(described_class::ALLOWLIST[number])).to be(true)
4+
described_class::TYPES.each do |type|
5+
it "registers an offense for with validates_#{type}_of" do
6+
type = 'length' if type == 'size'
7+
expect_offense(<<~RUBY, type: type)
8+
validates_#{type}_of :full_name, :birth_date
9+
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`.
10+
RUBY
11+
12+
expect_correction(<<~RUBY)
13+
validates :full_name, :birth_date, #{type}: true
14+
RUBY
1215
end
13-
end
1416

15-
describe '#autocorrect' do
16-
shared_examples 'autocorrects' do
17-
it 'autocorrects' do
18-
expect(autocorrect_source(source)).to eq(autocorrected_source)
19-
end
20-
end
17+
it "registers an offense for with validates_#{type}_of when method arguments are enclosed in parentheses" do
18+
type = 'length' if type == 'size'
19+
expect_offense(<<~RUBY, type: type)
20+
validates_#{type}_of(:full_name, :birth_date)
21+
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`.
22+
RUBY
2123

22-
shared_examples 'does not autocorrect' do
23-
it 'does not autocorrect' do
24-
expect(autocorrect_source(source)).to eq(source)
25-
end
24+
expect_correction(<<~RUBY)
25+
validates(:full_name, :birth_date, #{type}: true)
26+
RUBY
2627
end
2728

28-
described_class::TYPES.each do |type|
29-
context "with validates_#{type}_of" do
30-
let(:autocorrected_source) do
31-
type = 'length' if type == 'size'
32-
33-
"validates :full_name, :birth_date, #{type}: true"
34-
end
35-
36-
let(:source) do
37-
"validates_#{type}_of :full_name, :birth_date"
38-
end
39-
40-
include_examples 'autocorrects'
41-
end
42-
43-
context "with validates_#{type}_of when method arguments are enclosed in parentheses" do
44-
let(:autocorrected_source) do
45-
type = 'length' if type == 'size'
46-
47-
"validates(:full_name, :birth_date, #{type}: true)"
48-
end
49-
50-
let(:source) do
51-
"validates_#{type}_of(:full_name, :birth_date)"
52-
end
53-
54-
include_examples 'autocorrects'
55-
end
56-
57-
context "with validates_#{type}_of when attributes are specified with array literal" do
58-
let(:autocorrected_source) do
59-
type = 'length' if type == 'size'
60-
61-
"validates :full_name, :birth_date, #{type}: true"
62-
end
63-
64-
let(:source) do
65-
"validates_#{type}_of [:full_name, :birth_date]"
66-
end
67-
68-
include_examples 'autocorrects'
69-
end
70-
71-
context "with validates_#{type}_of when attributes are specified with frozen array literal" do
72-
let(:autocorrected_source) do
73-
type = 'length' if type == 'size'
74-
75-
"validates :full_name, :birth_date, #{type}: true"
76-
end
77-
78-
let(:source) do
79-
"validates_#{type}_of [:full_name, :birth_date].freeze"
80-
end
81-
82-
include_examples 'autocorrects'
83-
end
84-
85-
context "with validates_#{type}_of when attributes are specified with symbol array literal" do
86-
let(:autocorrected_source) do
87-
type = 'length' if type == 'size'
88-
89-
"validates :full_name, :birth_date, #{type}: true"
90-
end
91-
92-
let(:source) do
93-
"validates_#{type}_of %i[full_name birth_date]"
94-
end
95-
96-
include_examples 'autocorrects'
97-
end
98-
99-
context "with validates_#{type}_of when attributes are specified with frozen symbol array literal" do
100-
let(:autocorrected_source) do
101-
type = 'length' if type == 'size'
29+
it "registers an offense for with validates_#{type}_of when attributes are specified with array literal" do
30+
type = 'length' if type == 'size'
31+
expect_offense(<<~RUBY, type: type)
32+
validates_#{type}_of [:full_name, :birth_date]
33+
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`.
34+
RUBY
10235

103-
"validates :full_name, :birth_date, #{type}: true"
104-
end
105-
106-
let(:source) do
107-
"validates_#{type}_of %i[full_name birth_date].freeze"
108-
end
109-
110-
include_examples 'autocorrects'
111-
end
36+
expect_correction(<<~RUBY)
37+
validates :full_name, :birth_date, #{type}: true
38+
RUBY
11239
end
11340

114-
context 'with single attribute name' do
115-
let(:autocorrected_source) do
116-
'validates :a, numericality: true'
117-
end
118-
119-
let(:source) do
120-
'validates_numericality_of :a'
121-
end
41+
it "registers an offense for with validates_#{type}_of when attributes are specified with frozen array literal" do
42+
type = 'length' if type == 'size'
43+
expect_offense(<<~RUBY, type: type)
44+
validates_#{type}_of [:full_name, :birth_date].freeze
45+
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`.
46+
RUBY
12247

123-
include_examples 'autocorrects'
48+
expect_correction(<<~RUBY)
49+
validates :full_name, :birth_date, #{type}: true
50+
RUBY
12451
end
12552

126-
context 'with multi attribute names' do
127-
let(:autocorrected_source) do
128-
'validates :a, :b, numericality: true'
129-
end
53+
it "registers an offense for with validates_#{type}_of when attributes are specified with symbol array literal" do
54+
type = 'length' if type == 'size'
55+
expect_offense(<<~RUBY, type: type)
56+
validates_#{type}_of %i[full_name birth_date]
57+
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`.
58+
RUBY
13059

131-
let(:source) do
132-
'validates_numericality_of :a, :b'
133-
end
134-
135-
include_examples 'autocorrects'
60+
expect_correction(<<~RUBY)
61+
validates :full_name, :birth_date, #{type}: true
62+
RUBY
13663
end
13764

138-
context 'with non-braced hash literal' do
139-
let(:autocorrected_source) do
140-
'validates :a, :b, numericality: { minimum: 1 }'
141-
end
142-
143-
let(:source) do
144-
'validates_numericality_of :a, :b, minimum: 1'
145-
end
146-
147-
include_examples 'autocorrects'
65+
it "registers an offense for with validates_#{type}_of when " \
66+
'attributes are specified with frozen symbol array literal' do
67+
type = 'length' if type == 'size'
68+
expect_offense(<<~RUBY, type: type)
69+
validates_#{type}_of %i[full_name birth_date].freeze
70+
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`.
71+
RUBY
72+
73+
expect_correction(<<~RUBY)
74+
validates :full_name, :birth_date, #{type}: true
75+
RUBY
14876
end
77+
end
14978

150-
context 'with braced hash literal' do
151-
let(:autocorrected_source) do
152-
'validates :a, :b, numericality: { minimum: 1 }'
153-
end
79+
it 'registers an offense with single attribute name' do
80+
expect_offense(<<~RUBY)
81+
validates_numericality_of :a
82+
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
83+
RUBY
15484

155-
let(:source) do
156-
'validates_numericality_of :a, :b, { minimum: 1 }'
157-
end
85+
expect_correction(<<~RUBY)
86+
validates :a, numericality: true
87+
RUBY
88+
end
15889

159-
include_examples 'autocorrects'
160-
end
90+
it 'registers an offense with multi attribute names' do
91+
expect_offense(<<~RUBY)
92+
validates_numericality_of :a, :b
93+
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
94+
RUBY
16195

162-
context 'with a proc' do
163-
let(:autocorrected_source) do
164-
'validates :a, :b, comparison: { greater_than: -> { Time.zone.today } }'
165-
end
96+
expect_correction(<<~RUBY)
97+
validates :a, :b, numericality: true
98+
RUBY
99+
end
166100

167-
let(:source) do
168-
'validates_comparison_of :a, :b, greater_than: -> { Time.zone.today }'
169-
end
101+
it 'registers an offense with non-braced hash literal' do
102+
expect_offense(<<~RUBY)
103+
validates_numericality_of :a, :b, minimum: 1
104+
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
105+
RUBY
170106

171-
include_examples 'autocorrects'
172-
end
107+
expect_correction(<<~RUBY)
108+
validates :a, :b, numericality: { minimum: 1 }
109+
RUBY
110+
end
173111

174-
context 'with splat' do
175-
let(:autocorrected_source) do
176-
'validates :a, *b, numericality: true'
177-
end
112+
it 'registers an offense with braced hash literal' do
113+
expect_offense(<<~RUBY)
114+
validates_numericality_of :a, :b, { minimum: 1 }
115+
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
116+
RUBY
178117

179-
let(:source) do
180-
'validates_numericality_of :a, *b'
181-
end
118+
expect_correction(<<~RUBY)
119+
validates :a, :b, numericality: { minimum: 1 }
120+
RUBY
121+
end
182122

183-
include_examples 'autocorrects'
184-
end
123+
it 'registers an offense with a proc' do
124+
expect_offense(<<~RUBY)
125+
validates_comparison_of :a, :b, greater_than: -> { Time.zone.today }
126+
^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
127+
RUBY
185128

186-
context 'with splat and options' do
187-
let(:autocorrected_source) do
188-
'validates :a, *b, :c, numericality: { minimum: 1 }'
189-
end
129+
expect_correction(<<~RUBY)
130+
validates :a, :b, comparison: { greater_than: -> { Time.zone.today } }
131+
RUBY
132+
end
190133

191-
let(:source) do
192-
'validates_numericality_of :a, *b, :c, minimum: 1'
193-
end
134+
it 'registers an offense with a splat' do
135+
expect_offense(<<~RUBY)
136+
validates_numericality_of :a, *b
137+
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
138+
RUBY
194139

195-
include_examples 'autocorrects'
196-
end
140+
expect_correction(<<~RUBY)
141+
validates :a, *b, numericality: true
142+
RUBY
143+
end
197144

198-
context 'with trailing send node' do
199-
let(:source) do
200-
'validates_numericality_of :a, b'
201-
end
145+
it 'registers an offense with a splat and options' do
146+
expect_offense(<<~RUBY)
147+
validates_numericality_of :a, *b, :c, minimum: 1
148+
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...]
149+
RUBY
202150

203-
include_examples 'does not autocorrect'
204-
end
151+
expect_correction(<<~RUBY)
152+
validates :a, *b, :c, numericality: { minimum: 1 }
153+
RUBY
154+
end
205155

206-
context 'with trailing constant' do
207-
let(:source) do
208-
'validates_numericality_of :a, B'
209-
end
156+
it 'registers no offense with trailing send node' do
157+
expect_no_offenses(<<~RUBY)
158+
validates_numericality_of :a, b
159+
RUBY
160+
end
210161

211-
include_examples 'does not autocorrect'
212-
end
162+
it 'registers no offense with trailing constant' do
163+
expect_no_offenses(<<~RUBY)
164+
validates_numericality_of :a, B
165+
RUBY
166+
end
213167

214-
context 'with trailing local variable' do
215-
let(:source) do
216-
<<~RUBY
217-
b = { minimum: 1 }
218-
validates_numericality_of :a, b
219-
RUBY
220-
end
168+
it 'registers no offense with trailing local variable' do
169+
expect_no_offenses(<<~RUBY)
170+
b = { minimum: 1 }
171+
validates_numericality_of :a, b
172+
RUBY
173+
end
221174

222-
include_examples 'does not autocorrect'
223-
end
175+
it 'registers no offense when no arguments are passed' do
176+
expect_no_offenses(<<~RUBY)
177+
validates_numericality_of
178+
RUBY
224179
end
225180
end

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy