-
-
Notifications
You must be signed in to change notification settings - Fork 263
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Fix an error for Rails/Validation
when passing no arguments
#1337
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
* [#1337](https://github.com/rubocop/rubocop-rails/pull/1337): Fix an error for `Rails/Validation` when passing no arguments. ([@earlopain][]) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,225 +1,180 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::Validation, :config do | ||
it 'accepts new style validations' do | ||
expect_no_offenses('validates :name') | ||
end | ||
|
||
described_class::RESTRICT_ON_SEND.each_with_index do |validation, number| | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I removed these tests, they are duplicated with the loop below. |
||
it "registers an offense for #{validation}" do | ||
offenses = inspect_source("#{validation} :name") | ||
expect(offenses.first.message.include?(described_class::ALLOWLIST[number])).to be(true) | ||
described_class::TYPES.each do |type| | ||
it "registers an offense for with validates_#{type}_of" do | ||
type = 'length' if type == 'size' | ||
expect_offense(<<~RUBY, type: type) | ||
validates_#{type}_of :full_name, :birth_date | ||
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
validates :full_name, :birth_date, #{type}: true | ||
RUBY | ||
end | ||
end | ||
|
||
describe '#autocorrect' do | ||
shared_examples 'autocorrects' do | ||
it 'autocorrects' do | ||
expect(autocorrect_source(source)).to eq(autocorrected_source) | ||
end | ||
end | ||
it "registers an offense for with validates_#{type}_of when method arguments are enclosed in parentheses" do | ||
type = 'length' if type == 'size' | ||
expect_offense(<<~RUBY, type: type) | ||
validates_#{type}_of(:full_name, :birth_date) | ||
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. | ||
RUBY | ||
|
||
shared_examples 'does not autocorrect' do | ||
it 'does not autocorrect' do | ||
expect(autocorrect_source(source)).to eq(source) | ||
end | ||
expect_correction(<<~RUBY) | ||
validates(:full_name, :birth_date, #{type}: true) | ||
RUBY | ||
end | ||
|
||
described_class::TYPES.each do |type| | ||
context "with validates_#{type}_of" do | ||
let(:autocorrected_source) do | ||
type = 'length' if type == 'size' | ||
|
||
"validates :full_name, :birth_date, #{type}: true" | ||
end | ||
|
||
let(:source) do | ||
"validates_#{type}_of :full_name, :birth_date" | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
|
||
context "with validates_#{type}_of when method arguments are enclosed in parentheses" do | ||
let(:autocorrected_source) do | ||
type = 'length' if type == 'size' | ||
|
||
"validates(:full_name, :birth_date, #{type}: true)" | ||
end | ||
|
||
let(:source) do | ||
"validates_#{type}_of(:full_name, :birth_date)" | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
|
||
context "with validates_#{type}_of when attributes are specified with array literal" do | ||
let(:autocorrected_source) do | ||
type = 'length' if type == 'size' | ||
|
||
"validates :full_name, :birth_date, #{type}: true" | ||
end | ||
|
||
let(:source) do | ||
"validates_#{type}_of [:full_name, :birth_date]" | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
|
||
context "with validates_#{type}_of when attributes are specified with frozen array literal" do | ||
let(:autocorrected_source) do | ||
type = 'length' if type == 'size' | ||
|
||
"validates :full_name, :birth_date, #{type}: true" | ||
end | ||
|
||
let(:source) do | ||
"validates_#{type}_of [:full_name, :birth_date].freeze" | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
|
||
context "with validates_#{type}_of when attributes are specified with symbol array literal" do | ||
let(:autocorrected_source) do | ||
type = 'length' if type == 'size' | ||
|
||
"validates :full_name, :birth_date, #{type}: true" | ||
end | ||
|
||
let(:source) do | ||
"validates_#{type}_of %i[full_name birth_date]" | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
|
||
context "with validates_#{type}_of when attributes are specified with frozen symbol array literal" do | ||
let(:autocorrected_source) do | ||
type = 'length' if type == 'size' | ||
it "registers an offense for with validates_#{type}_of when attributes are specified with array literal" do | ||
type = 'length' if type == 'size' | ||
expect_offense(<<~RUBY, type: type) | ||
validates_#{type}_of [:full_name, :birth_date] | ||
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. | ||
RUBY | ||
|
||
"validates :full_name, :birth_date, #{type}: true" | ||
end | ||
|
||
let(:source) do | ||
"validates_#{type}_of %i[full_name birth_date].freeze" | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :full_name, :birth_date, #{type}: true | ||
RUBY | ||
end | ||
|
||
context 'with single attribute name' do | ||
let(:autocorrected_source) do | ||
'validates :a, numericality: true' | ||
end | ||
|
||
let(:source) do | ||
'validates_numericality_of :a' | ||
end | ||
it "registers an offense for with validates_#{type}_of when attributes are specified with frozen array literal" do | ||
type = 'length' if type == 'size' | ||
expect_offense(<<~RUBY, type: type) | ||
validates_#{type}_of [:full_name, :birth_date].freeze | ||
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. | ||
RUBY | ||
|
||
include_examples 'autocorrects' | ||
expect_correction(<<~RUBY) | ||
validates :full_name, :birth_date, #{type}: true | ||
RUBY | ||
end | ||
|
||
context 'with multi attribute names' do | ||
let(:autocorrected_source) do | ||
'validates :a, :b, numericality: true' | ||
end | ||
it "registers an offense for with validates_#{type}_of when attributes are specified with symbol array literal" do | ||
type = 'length' if type == 'size' | ||
expect_offense(<<~RUBY, type: type) | ||
validates_#{type}_of %i[full_name birth_date] | ||
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. | ||
RUBY | ||
|
||
let(:source) do | ||
'validates_numericality_of :a, :b' | ||
end | ||
|
||
include_examples 'autocorrects' | ||
expect_correction(<<~RUBY) | ||
validates :full_name, :birth_date, #{type}: true | ||
RUBY | ||
end | ||
|
||
context 'with non-braced hash literal' do | ||
let(:autocorrected_source) do | ||
'validates :a, :b, numericality: { minimum: 1 }' | ||
end | ||
|
||
let(:source) do | ||
'validates_numericality_of :a, :b, minimum: 1' | ||
end | ||
|
||
include_examples 'autocorrects' | ||
it "registers an offense for with validates_#{type}_of when " \ | ||
'attributes are specified with frozen symbol array literal' do | ||
type = 'length' if type == 'size' | ||
expect_offense(<<~RUBY, type: type) | ||
validates_#{type}_of %i[full_name birth_date].freeze | ||
^^^^^^^^^^^{type}^^^ Prefer the new style validations `validates :column, #{type}: value` over `validates_#{type}_of`. | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
validates :full_name, :birth_date, #{type}: true | ||
RUBY | ||
end | ||
end | ||
|
||
context 'with braced hash literal' do | ||
let(:autocorrected_source) do | ||
'validates :a, :b, numericality: { minimum: 1 }' | ||
end | ||
it 'registers an offense with single attribute name' do | ||
expect_offense(<<~RUBY) | ||
validates_numericality_of :a | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
let(:source) do | ||
'validates_numericality_of :a, :b, { minimum: 1 }' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, numericality: true | ||
RUBY | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
it 'registers an offense with multi attribute names' do | ||
expect_offense(<<~RUBY) | ||
validates_numericality_of :a, :b | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
context 'with a proc' do | ||
let(:autocorrected_source) do | ||
'validates :a, :b, comparison: { greater_than: -> { Time.zone.today } }' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, :b, numericality: true | ||
RUBY | ||
end | ||
|
||
let(:source) do | ||
'validates_comparison_of :a, :b, greater_than: -> { Time.zone.today }' | ||
end | ||
it 'registers an offense with non-braced hash literal' do | ||
expect_offense(<<~RUBY) | ||
validates_numericality_of :a, :b, minimum: 1 | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
include_examples 'autocorrects' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, :b, numericality: { minimum: 1 } | ||
RUBY | ||
end | ||
|
||
context 'with splat' do | ||
let(:autocorrected_source) do | ||
'validates :a, *b, numericality: true' | ||
end | ||
it 'registers an offense with braced hash literal' do | ||
expect_offense(<<~RUBY) | ||
validates_numericality_of :a, :b, { minimum: 1 } | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
let(:source) do | ||
'validates_numericality_of :a, *b' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, :b, numericality: { minimum: 1 } | ||
RUBY | ||
end | ||
|
||
include_examples 'autocorrects' | ||
end | ||
it 'registers an offense with a proc' do | ||
expect_offense(<<~RUBY) | ||
validates_comparison_of :a, :b, greater_than: -> { Time.zone.today } | ||
^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
context 'with splat and options' do | ||
let(:autocorrected_source) do | ||
'validates :a, *b, :c, numericality: { minimum: 1 }' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, :b, comparison: { greater_than: -> { Time.zone.today } } | ||
RUBY | ||
end | ||
|
||
let(:source) do | ||
'validates_numericality_of :a, *b, :c, minimum: 1' | ||
end | ||
it 'registers an offense with a splat' do | ||
expect_offense(<<~RUBY) | ||
validates_numericality_of :a, *b | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
include_examples 'autocorrects' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, *b, numericality: true | ||
RUBY | ||
end | ||
|
||
context 'with trailing send node' do | ||
let(:source) do | ||
'validates_numericality_of :a, b' | ||
end | ||
it 'registers an offense with a splat and options' do | ||
expect_offense(<<~RUBY) | ||
validates_numericality_of :a, *b, :c, minimum: 1 | ||
^^^^^^^^^^^^^^^^^^^^^^^^^ Prefer the new style [...] | ||
RUBY | ||
|
||
include_examples 'does not autocorrect' | ||
end | ||
expect_correction(<<~RUBY) | ||
validates :a, *b, :c, numericality: { minimum: 1 } | ||
RUBY | ||
end | ||
|
||
context 'with trailing constant' do | ||
let(:source) do | ||
'validates_numericality_of :a, B' | ||
end | ||
it 'registers no offense with trailing send node' do | ||
expect_no_offenses(<<~RUBY) | ||
validates_numericality_of :a, b | ||
RUBY | ||
end | ||
|
||
include_examples 'does not autocorrect' | ||
end | ||
it 'registers no offense with trailing constant' do | ||
expect_no_offenses(<<~RUBY) | ||
validates_numericality_of :a, B | ||
RUBY | ||
end | ||
|
||
context 'with trailing local variable' do | ||
let(:source) do | ||
<<~RUBY | ||
b = { minimum: 1 } | ||
validates_numericality_of :a, b | ||
RUBY | ||
end | ||
it 'registers no offense with trailing local variable' do | ||
expect_no_offenses(<<~RUBY) | ||
b = { minimum: 1 } | ||
validates_numericality_of :a, b | ||
RUBY | ||
end | ||
|
||
include_examples 'does not autocorrect' | ||
end | ||
it 'registers no offense when no arguments are passed' do | ||
expect_no_offenses(<<~RUBY) | ||
validates_numericality_of | ||
RUBY | ||
end | ||
end |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I removed this test,
expect_correction
would raise anInfiniteLoopError
if this wouldn't pass