Skip to content

Commit

Permalink
Make RakeEnvironment cop less aggressive mode
Browse files Browse the repository at this point in the history
  • Loading branch information
pocke committed Sep 21, 2019
1 parent 869dd38 commit 7ff07b7
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 44 deletions.
43 changes: 15 additions & 28 deletions lib/rubocop/cop/rails/rake_environment.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ module Rails
#
# * The task does not need application code.
# * The task invokes :environment task.
# * The task's dependencies invoke :environment task.
#
# @example
# # bad
Expand All @@ -36,9 +35,7 @@ class RakeEnvironment < Cop
def on_send(node)
return unless task_definition?(node)
return if task_name(node) == :default

deps = dependencies(node)
return if environment_dependency_will_apply?(deps)
return if with_dependencies?(node)

add_offense(node)
end
Expand All @@ -62,40 +59,30 @@ def task_name(node)
end
end

def environment_dependency_will_apply?(deps)
return false if deps.empty?

dep = deps.first
case dep.type
when :str, :sym
dep.value.to_sym == :environment
else
true
end
end

def dependencies(node)
def with_dependencies?(node)
first_arg = node.arguments[0]
return [] unless first_arg

return hash_style_dependencies(first_arg) if first_arg.hash_type?
return false unless first_arg

task_args = node.arguments[1]
return [] unless task_args
return [] unless task_args.hash_type?
if first_arg.hash_type?
with_hash_style_dependencies?(first_arg)
else
task_args = node.arguments[1]
return false unless task_args
return false unless task_args.hash_type?

hash_style_dependencies(task_args)
with_hash_style_dependencies?(task_args)
end
end

def hash_style_dependencies(hash_node)
def with_hash_style_dependencies?(hash_node)
deps = hash_node.pairs.first&.value
return [] unless deps
return false unless deps

case deps.type
when :array
deps.children
!deps.values.empty?
else
[deps]
true
end
end
end
Expand Down
29 changes: 13 additions & 16 deletions spec/rubocop/cop/rails/rake_environment_spec.rb
Original file line number Diff line number Diff line change
Expand Up @@ -13,35 +13,32 @@
RUBY
end

it 'registers an offense to task with an dependency' do
expect_offense(<<~RUBY)
task foo: :bar do
^^^^^^^^^^^^^^ Set :environment task as a dependency to all rake task.
it 'does not register an offense to task with :environment ' \
'but it has other dependency before it' do
expect_no_offenses(<<~RUBY)
task foo: [:bar, :environment] do
end
RUBY
end

it 'registers an offense to task with dependencies' do
expect_offense(<<~RUBY)
task foo: [:foo, :bar] do
^^^^^^^^^^^^^^^^^^^^^^ Set :environment task as a dependency to all rake task.
it 'does not register an offense to task with an dependency' do
expect_no_offenses(<<~RUBY)
task foo: :bar do
end
RUBY
end

it 'registers an offense to task with :environment ' \
'but it has other dependency before it' do
expect_offense(<<~RUBY)
task foo: [:bar, :environment] do
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^ Set :environment task as a dependency to all rake task.
it 'does not register an offense to task with dependencies' do
expect_no_offenses(<<~RUBY)
task foo: [:foo, :bar] do
end
RUBY
end

it 'registers an offense to task with a dependency as a method call' do
expect_offense(<<~RUBY)
it 'does not register an offense to task with a dependency ' \
'as a method call' do
expect_no_offenses(<<~RUBY)
task foo: [:bar, dep]
^^^^^^^^^^^^^^^^^^^^^ Set :environment task as a dependency to all rake task.
RUBY
end

Expand Down

0 comments on commit 7ff07b7

Please sign in to comment.