class RuboCop::Cop::Rails::RakeEnvironment


end
do_something
task foo: :environment do
# good
end
do_something
task :foo do
# bad
@example
loaded to run will run significantly faster without loading the whole application.
will break a behavior. It’s also slower. E.g. some task that only needs one gem to be
Probably not a problem in most cases, but it is possible that calling ‘:environment` task
@safety
* The task invokes the `:environment` task.
* The task does not need application code.
following conditions:
You can ignore the offense if the task satisfies at least one of the
models.
Rake tasks. Without it, tasks cannot make use of application code like
dependency. The `:environment` task loads application code for other
Checks for Rake tasks without the `:environment` task

def correct_task_arguments_dependency(task_method)

def correct_task_arguments_dependency(task_method)
  "#{task_arguments(task_method).source} => :environment"
end

def correct_task_dependency(task_name)

def correct_task_dependency(task_name)
  if task_name.sym_type?
    "#{task_name.source.delete(':|\'|"')}: :environment"
  else
    "#{task_name.source} => :environment"
  end
end

def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler

rubocop:disable InternalAffairs/NumblockHandler
def on_block(node) # rubocop:disable InternalAffairs/NumblockHandler
  task_definition?(node) do |task_method|
    return if task_name(task_method) == :default
    return if with_dependencies?(task_method)
    add_offense(task_method) do |corrector|
      if with_arguments?(task_method)
        new_task_dependency = correct_task_arguments_dependency(task_method)
        corrector.replace(task_arguments(task_method), new_task_dependency)
      else
        task_name = task_method.first_argument
        new_task_dependency = correct_task_dependency(task_name)
        corrector.replace(task_name, new_task_dependency)
      end
    end
  end
end

def task_arguments(node)

def task_arguments(node)
  node.arguments[1]
end

def task_name(node)

def task_name(node)
  first_arg = node.first_argument
  case first_arg&.type
  when :sym, :str
    first_arg.value.to_sym
  when :hash
    return nil if first_arg.children.size != 1
    pair = first_arg.children.first
    key = pair.children.first
    case key.type
    when :sym, :str
      key.value.to_sym
    end
  end
end

def with_arguments?(node)

def with_arguments?(node)
  node.arguments.size > 1 && node.arguments[1].array_type?
end

def with_dependencies?(node)

def with_dependencies?(node)
  first_arg = node.first_argument
  return false unless first_arg
  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?
    with_hash_style_dependencies?(task_args)
  end
end

def with_hash_style_dependencies?(hash_node)

def with_hash_style_dependencies?(hash_node)
  deps = hash_node.pairs.first&.value
  return false unless deps
  case deps.type
  when :array
    !deps.values.empty?
  else
    true
  end
end