-
-
Notifications
You must be signed in to change notification settings - Fork 268
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
Add new Rails/ValidationsGrouping
cop
#679
Open
fatkodima
wants to merge
1
commit into
rubocop:master
Choose a base branch
from
fatkodima:validations-grouping-cop
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
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 @@ | ||
* [#555](https://github.com/rubocop/rubocop-rails/issues/555): Add new `Rails/ValidationsGrouping` cop. ([@fatkodima][]) |
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 |
---|---|---|
@@ -0,0 +1,105 @@ | ||
# frozen_string_literal: true | ||
|
||
module RuboCop | ||
module Cop | ||
module Rails | ||
# Enforces the grouping of attribute validations. | ||
# | ||
# @example | ||
# # bad | ||
# validates :name, :email, :bio, presence: true | ||
# validates :email, format: { with: /@/ } | ||
# | ||
# # good | ||
# validates :name, :bio, presence: true | ||
# validates :email, presence: true, format: { with: /@/ } | ||
# | ||
# # good | ||
# validates :name, :email, :bio, presence: true, if: :some_condition? | ||
# validates :email, format: { with: /@/ } | ||
# | ||
# @example SingleAttributeValidations: true | ||
# # Enforces single attribute per validation. | ||
# | ||
# # bad | ||
# validates :name, :bio, presence: true | ||
# | ||
# # good | ||
# validates :name, presence: true | ||
# validates :bio, presence: true | ||
# | ||
class ValidationsGrouping < Base | ||
MSG = 'Group together all %<attribute>s validations.' | ||
SINGLE_ATTRIBUTE_MSG = 'Specify single attribute per validation.' | ||
|
||
VALIDATION_KEYS = %w[acceptance confirmation comparison exclusion format inclusion | ||
length numericality presence absence uniqueness].freeze | ||
|
||
RESTRICT_ON_SEND = %i[validates].freeze | ||
|
||
def_node_matcher :validates?, <<~PATTERN | ||
(send nil? :validates ${sym str} + hash) | ||
PATTERN | ||
|
||
def on_new_investigation | ||
# Hash is keyed by parent id and then by attribute name within it. | ||
@validations = Hash.new do |outer, parent| | ||
outer[parent] = Hash.new { |inner, attribute| inner[attribute] = [] } | ||
end | ||
@validations.compare_by_identity | ||
end | ||
|
||
def on_send(node) | ||
validates?(node) do |attribute_nodes| | ||
if attribute_nodes.size > 1 && cop_config['SingleAttributeValidations'] | ||
add_offense(node, message: SINGLE_ATTRIBUTE_MSG) | ||
end | ||
|
||
check_for_same_attributes_validations(node, attribute_nodes) | ||
end | ||
end | ||
|
||
private | ||
|
||
def check_for_same_attributes_validations(validation_node, attribute_nodes) | ||
attribute_nodes.each do |attribute_node| | ||
parent_node = validation_node.parent | ||
attribute_name = attribute_node.value.to_s | ||
@validations[parent_node][attribute_name] << validation_node | ||
|
||
validation_nodes = @validations[parent_node][attribute_name] | ||
next unless validation_nodes.size > 1 && same_options?(validation_nodes) | ||
|
||
message = format(MSG, attribute: attribute_name) | ||
|
||
validation_nodes.each do |node| | ||
add_offense(attribute_node(node, attribute_name), message: message) | ||
end | ||
end | ||
end | ||
|
||
def same_options?(validation_nodes) | ||
options = validation_nodes.map do |node| | ||
option_node = node.last_argument | ||
option_node.pairs.to_h do |pair| | ||
[pair.key.source, pair.value.source] | ||
end | ||
end | ||
|
||
options = options.map { |o| extra_options(o) } | ||
options.all?(options.first) | ||
end | ||
|
||
def extra_options(options) | ||
options.except(*VALIDATION_KEYS) | ||
end | ||
|
||
def attribute_node(validation_node, attribute_name) | ||
validation_node.arguments[0..-2].find do |argument| | ||
argument.value.to_s == attribute_name.to_s | ||
end | ||
end | ||
end | ||
end | ||
end | ||
end |
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 |
---|---|---|
@@ -0,0 +1,93 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::ValidationsGrouping, :config do | ||
let(:cop_config) { { 'SingleAttributeValidations' => false } } | ||
|
||
it 'registers an offense when attribute validations are split' do | ||
expect_offense(<<~RUBY) | ||
validates :name, :email, presence: true | ||
^^^^^^ Group together all email validations. | ||
validates :email, format: { with: /@/ } | ||
^^^^^^ Group together all email validations. | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when attribute validations are grouped' do | ||
expect_no_offenses(<<~RUBY) | ||
validates :email, presence: true, format: { with: /@/ } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense for not-literal attribute name validations' do | ||
expect_no_offenses(<<~RUBY) | ||
validates attribute, presence: true | ||
validates attribute, format: { with: /@/ } | ||
RUBY | ||
end | ||
|
||
it 'does not register an offense when attribute validations have different configuration options' do | ||
expect_no_offenses(<<~RUBY) | ||
validates :email, presence: true, if: :some_condition? | ||
validates :email, format: { with: /@/ } | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when attribute validations have same configuration options' do | ||
expect_offense(<<~RUBY) | ||
validates :email, presence: true, if: :some_condition? | ||
^^^^^^ Group together all email validations. | ||
validates :email, format: { with: /@/ }, if: :some_condition? | ||
^^^^^^ Group together all email validations. | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when attribute validations have different validation options' do | ||
expect_offense(<<~RUBY) | ||
validates :email, format: { with: /foo/ } | ||
^^^^^^ Group together all email validations. | ||
validates :email, format: { with: /bar/ } | ||
^^^^^^ Group together all email validations. | ||
RUBY | ||
end | ||
|
||
context 'with conditionals' do | ||
it 'registers an offense when attribute validations within conditional are split' do | ||
expect_offense(<<~RUBY) | ||
validates :email, uniqueness: true | ||
|
||
if condition? | ||
validates :name, :email, presence: true | ||
^^^^^^ Group together all email validations. | ||
validates :email, format: { with: /@/ } | ||
^^^^^^ Group together all email validations. | ||
end | ||
RUBY | ||
end | ||
|
||
it 'registers an offense when attribute validations outside conditional are split' do | ||
expect_offense(<<~RUBY) | ||
validates :name, :email, presence: true | ||
^^^^^^ Group together all email validations. | ||
validates :email, format: { with: /@/ } | ||
^^^^^^ Group together all email validations. | ||
|
||
if condition? | ||
validates :email, uniqueness: true | ||
end | ||
RUBY | ||
end | ||
end | ||
|
||
context 'SingleAttributeValidations is used' do | ||
let(:cop_config) { { 'SingleAttributeValidations' => true } } | ||
|
||
it 'registers and offense for multiple attributes per validation' do | ||
expect_offense(<<~RUBY) | ||
validates :name, :email, presence: true | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify single attribute per validation. | ||
validates :name, :email, presence: true, if: :some_condition? | ||
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Specify single attribute per validation. | ||
RUBY | ||
end | ||
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.
What would be the expected correction for this case?
I can't come up with something that doesn't make this more complicated.
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.