From 63e9699ad9a8d0d19b20ae687a1521649ee68bc4 Mon Sep 17 00:00:00 2001 From: Masataka Pocke Kuwabara Date: Sat, 21 Sep 2019 13:18:29 +0900 Subject: [PATCH] Make RakeEnvironment cop less aggressive mode --- lib/rubocop/cop/rails/rake_environment.rb | 43 +++++++------------ manual/cops_rails.md | 1 - .../cop/rails/rake_environment_spec.rb | 29 ++++++------- 3 files changed, 28 insertions(+), 45 deletions(-) diff --git a/lib/rubocop/cop/rails/rake_environment.rb b/lib/rubocop/cop/rails/rake_environment.rb index 5532fdfce4..8956a37f82 100644 --- a/lib/rubocop/cop/rails/rake_environment.rb +++ b/lib/rubocop/cop/rails/rake_environment.rb @@ -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 @@ -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 @@ -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 diff --git a/manual/cops_rails.md b/manual/cops_rails.md index 93d1f8912d..a245346e2a 100644 --- a/manual/cops_rails.md +++ b/manual/cops_rails.md @@ -1676,7 +1676,6 @@ following conditions: * The task does not need application code. * The task invokes :environment task. -* The task's dependencies invoke :environment task. ### Examples diff --git a/spec/rubocop/cop/rails/rake_environment_spec.rb b/spec/rubocop/cop/rails/rake_environment_spec.rb index 87525bbdae..e1f6e0c855 100644 --- a/spec/rubocop/cop/rails/rake_environment_spec.rb +++ b/spec/rubocop/cop/rails/rake_environment_spec.rb @@ -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