Skip to content
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 array and hash literal offenders to AttributeDefaultBlockValue #369

Merged
merged 1 commit into from
Oct 14, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
35 changes: 28 additions & 7 deletions docs/modules/ROOT/pages/cops_rails.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -452,8 +452,9 @@ assert_not x
|===

This cop looks for `attribute` class methods that specify a `:default` option
which value is a method call without a block.
It will accept all other values, such as literals and constants.
which value is an array, string literal or method call without a block.
It will accept all other values, such as string, symbol, integer and float literals
as well as constants.

=== Examples

Expand All @@ -465,27 +466,47 @@ class User < ApplicationRecord
end

# good
class User < ActiveRecord::Base
class User < ApplicationRecord
attribute :confirmed_at, :datetime, default: -> { Time.zone.now }
end

# bad
class User < ApplicationRecord
attribute :roles, :string, array: true, default: []
end

# good
class User < ActiveRecord::Base
class User < ApplicationRecord
attribute :roles, :string, array: true, default: -> { [] }
end

# bad
class User < ApplicationRecord
attribute :configuration, default: {}
end

# good
class User < ApplicationRecord
attribute :configuration, default: -> { {} }
end

# good
class User < ApplicationRecord
attribute :role, :string, default: :customer
end

# good
class User < ActiveRecord::Base
class User < ApplicationRecord
attribute :activated, :boolean, default: false
end

# good
class User < ActiveRecord::Base
class User < ApplicationRecord
attribute :login_count, :integer, default: 0
end

# good
class User < ActiveRecord::Base
class User < ApplicationRecord
FOO = 123
attribute :custom_attribute, :integer, default: FOO
end
Expand Down
41 changes: 32 additions & 9 deletions lib/rubocop/cop/rails/attribute_default_block_value.rb
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ module RuboCop
module Cop
module Rails
# This cop looks for `attribute` class methods that specify a `:default` option
# which value is a method call without a block.
# It will accept all other values, such as literals and constants.
# which value is an array, string literal or method call without a block.
# It will accept all other values, such as string, symbol, integer and float literals
# as well as constants.
#
# @example
# # bad
Expand All @@ -14,44 +15,66 @@ module Rails
# end
#
# # good
# class User < ActiveRecord::Base
# class User < ApplicationRecord
# attribute :confirmed_at, :datetime, default: -> { Time.zone.now }
# end
#
# # bad
# class User < ApplicationRecord
# attribute :roles, :string, array: true, default: []
# end
#
# # good
# class User < ActiveRecord::Base
# class User < ApplicationRecord
# attribute :roles, :string, array: true, default: -> { [] }
# end
#
# # bad
# class User < ApplicationRecord
# attribute :configuration, default: {}
# end
#
# # good
# class User < ApplicationRecord
# attribute :configuration, default: -> { {} }
# end
#
# # good
# class User < ApplicationRecord
# attribute :role, :string, default: :customer
# end
#
# # good
# class User < ActiveRecord::Base
# class User < ApplicationRecord
# attribute :activated, :boolean, default: false
# end
#
# # good
# class User < ActiveRecord::Base
# class User < ApplicationRecord
# attribute :login_count, :integer, default: 0
# end
#
# # good
# class User < ActiveRecord::Base
# class User < ApplicationRecord
# FOO = 123
# attribute :custom_attribute, :integer, default: FOO
# end
class AttributeDefaultBlockValue < Cop
MSG = 'Pass method in a block to `:default` option.'
TYPE_OFFENDERS = %i[send array hash].freeze

def_node_matcher :default_attribute, <<~PATTERN
(send nil? :attribute _ _ (hash <$#attribute ...>))
(send nil? :attribute _ ?_ (hash <$#attribute ...>))
PATTERN

def_node_matcher :attribute, '(pair (sym :default) $_)'

def on_send(node)
default_attribute(node) do |attribute|
value = attribute.children.last
return unless TYPE_OFFENDERS.any? { |type| value.type == type }

add_offense(node, location: value) if value.send_type?
add_offense(node, location: value)
end
end

Expand Down
14 changes: 14 additions & 0 deletions spec/rubocop/cop/rails/attribute_default_block_value_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,20 @@ def bar
RUBY
end

it 'disallows array literals' do
expect_offense(<<~RUBY)
attribute :foo, :string, array: true, default: []
^^ #{message}
RUBY
end

it 'disallows hash literals' do
expect_offense(<<~RUBY)
attribute :foo, default: {}
^^ #{message}
RUBY
end

it 'autocorrects `:default` method value in same row' do
expect_offense(<<~RUBY)
attribute :foo, :string, default: Foo.bar
Expand Down