-
-
Notifications
You must be signed in to change notification settings - Fork 268
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #339 from cilim/attribute_default_block_value-cop
Add new `AttributeDefaultBlockValue` cop
- Loading branch information
Showing
7 changed files
with
279 additions
and
0 deletions.
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
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
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,68 @@ | ||
# frozen_string_literal: true | ||
|
||
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. | ||
# | ||
# @example | ||
# # bad | ||
# class User < ApplicationRecord | ||
# attribute :confirmed_at, :datetime, default: Time.zone.now | ||
# end | ||
# | ||
# # good | ||
# class User < ActiveRecord::Base | ||
# attribute :confirmed_at, :datetime, default: -> { Time.zone.now } | ||
# end | ||
# | ||
# # good | ||
# class User < ActiveRecord::Base | ||
# attribute :role, :string, default: :customer | ||
# end | ||
# | ||
# # good | ||
# class User < ActiveRecord::Base | ||
# attribute :activated, :boolean, default: false | ||
# end | ||
# | ||
# # good | ||
# class User < ActiveRecord::Base | ||
# attribute :login_count, :integer, default: 0 | ||
# end | ||
# | ||
# # good | ||
# class User < ActiveRecord::Base | ||
# FOO = 123 | ||
# attribute :custom_attribute, :integer, default: FOO | ||
# end | ||
class AttributeDefaultBlockValue < Cop | ||
MSG = 'Pass method in a block to `:default` option.' | ||
|
||
def_node_matcher :default_attribute, <<~PATTERN | ||
(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 | ||
|
||
add_offense(node, location: value) if value.send_type? | ||
end | ||
end | ||
|
||
def autocorrect(node) | ||
expression = default_attribute(node).children.last | ||
|
||
lambda do |corrector| | ||
corrector.replace(expression, "-> { #{expression.source} }") | ||
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
138 changes: 138 additions & 0 deletions
138
spec/rubocop/cop/rails/attribute_default_block_value_spec.rb
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,138 @@ | ||
# frozen_string_literal: true | ||
|
||
RSpec.describe RuboCop::Cop::Rails::AttributeDefaultBlockValue do | ||
subject(:cop) { described_class.new(config) } | ||
|
||
let(:config) { RuboCop::Config.new } | ||
let(:message) { 'Pass method in a block to `:default` option.' } | ||
|
||
context 'when `:default` option is last' do | ||
it 'disallows method' do | ||
expect_offense(<<~RUBY) | ||
def bar | ||
end | ||
attribute :foo, :string, default: bar | ||
^^^ #{message} | ||
RUBY | ||
end | ||
|
||
it 'disallows method called from other instance object' do | ||
expect_offense(<<~RUBY) | ||
attribute :foo, :string, default: Foo.new.bar | ||
^^^^^^^^^^^ #{message} | ||
RUBY | ||
end | ||
|
||
it 'autocorrects `:default` method value in same row' do | ||
expect_offense(<<~RUBY) | ||
attribute :foo, :string, default: Foo.bar | ||
^^^^^^^ #{message} | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
attribute :foo, :string, default: -> { Foo.bar } | ||
RUBY | ||
end | ||
|
||
it 'autocorrects `:default` method value in next row' do | ||
expect_offense(<<~RUBY) | ||
attribute :foo, :string, limit: 1, | ||
default: Foo.bar | ||
^^^^^^^ #{message} | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
attribute :foo, :string, limit: 1, | ||
default: -> { Foo.bar } | ||
RUBY | ||
end | ||
|
||
it 'allows boolean false' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :boolean, default: false | ||
RUBY | ||
end | ||
|
||
it 'allows boolean true' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :boolean, default: true | ||
RUBY | ||
end | ||
|
||
it 'allows symbol' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :string, default: :bar | ||
RUBY | ||
end | ||
|
||
it 'allows int' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :integer, default: 1 | ||
RUBY | ||
end | ||
|
||
it 'allows decimal' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :decimal, default: 3.14 | ||
RUBY | ||
end | ||
|
||
it 'allows constant' do | ||
expect_no_offenses(<<~RUBY) | ||
CONSTANT = :foo | ||
attribute :bar, :string, default: CONSTANT | ||
RUBY | ||
end | ||
|
||
it 'allows block' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :datetime, default: -> { Time.zone.now } | ||
RUBY | ||
end | ||
|
||
it 'allows without default value' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :datetime | ||
RUBY | ||
end | ||
|
||
it 'allows with array option' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :bar, array: FooBar.array | ||
RUBY | ||
end | ||
|
||
it 'allows with range option' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :bar, range: FooBar.range | ||
RUBY | ||
end | ||
|
||
it 'allows with limit option' do | ||
expect_no_offenses(<<~RUBY) | ||
attribute :foo, :bar, limit: FooBar.limit | ||
RUBY | ||
end | ||
end | ||
|
||
context 'when `:default` option is next to last' do | ||
it 'disallows `:default` method value' do | ||
expect_offense(<<~RUBY) | ||
attribute :foo, :string, range: true, default: Foo.bar, limit: 10 | ||
^^^^^^^ #{message} | ||
RUBY | ||
end | ||
|
||
it 'autocorrects `:default` method value' do | ||
expect_offense(<<~RUBY) | ||
attribute :foo, :string, default: Foo.bar, limit: 1 | ||
^^^^^^^ #{message} | ||
RUBY | ||
|
||
expect_correction(<<~RUBY) | ||
attribute :foo, :string, default: -> { Foo.bar }, limit: 1 | ||
RUBY | ||
end | ||
end | ||
end |