class RuboCop::Cop::Itamae::CdInExecute

Check that `cd` in `execute`.

@example

# bad
execute 'cd /tmp && rm -rf /tmp/*'

# good
execute 'rm -rf /tmp/*' do
  cwd '/tmp'
end

Constants

MSG

Public Instance Methods

on_block(node) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 41
def on_block(node)
  find_execute_with_block(node) do |name, param_nodes|
    add_offence_for_execute_block_name(node, name)

    param_nodes.compact.each do |param_node|
      find_command(param_node) do |command|
        add_offense_for_command_param(param_node, command)
      end
    end
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 53
def on_send(node)
  return if node.parent&.block_type?

  find_execute_without_block(node) do |name|
    add_offense_for_execute_name(node, name)
  end
end

Private Instance Methods

add_offence_for_execute_block_name(node, name) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 116
def add_offence_for_execute_block_name(node, name)
  dir = cd_dir_in_command(name)
  return unless dir

  loc = cd_location(node.child_nodes.first.child_nodes.first, name)
  add_offense(loc, message: format(MSG, dir: dir))
end
add_offense_for_command_param(param_node, command) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 124
def add_offense_for_command_param(param_node, command)
  dir = cd_dir_in_command(command)
  return unless dir

  command_node =
    if param_node.child_nodes.first.child_nodes.empty?
      param_node.child_nodes.first
    else
      param_node.child_nodes.first.child_nodes.first
    end

  loc = cd_location(command_node, command)
  add_offense(loc, message: format(MSG, dir: dir))
end
add_offense_for_execute_name(node, name) click to toggle source

def autocorrect(node)

if node.block_type?
  lambda do |corrector|
    find_execute_with_block(node) do |name, param_nodes|
      if offence_command?(name)
        corrector.remove(cd_location(node.child_nodes.first.child_nodes.first, name))

        # TODO: insert cwd
        next
      end
    end
  end
elsif node.begin_type?
  lambda do |corrector|
    find_command(node) do |command|
      next unless offence_command?(command)

      command_node = node.child_nodes.first.child_nodes.first
      corrector.remove(cd_location(command_node, command))

      # TODO: insert cwd
    end
  end
elsif node.send_type?
  lambda do |corrector|
    find_execute_without_block(node) do |name|
      next unless offence_command?(name)

      corrector.remove(cd_location(node.child_nodes.first, name))

      # TODO: insert cwd
    end

    find_command(node) do |command|
      next unless offence_command?(command)

      command_node = node.child_nodes.first
      corrector.remove(cd_location(command_node, command))

      # TODO: insert cwd
    end
  end
end

end

# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 108
def add_offense_for_execute_name(node, name)
  dir = cd_dir_in_command(name)
  return unless dir

  loc = cd_location(node.child_nodes.first, name)
  add_offense(loc, message: format(MSG, dir: dir))
end
cd_dir_in_command(command) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 139
def cd_dir_in_command(command)
  dir, = parse_command(command)
  dir
end
cd_location(node, command) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 153
def cd_location(node, command)
  _, second_command = parse_command(command)
  second_command_pos = command.index(second_command)
  begin_pos = node.loc.begin.end_pos
  end_pos = begin_pos + second_command_pos
  Parser::Source::Range.new(node.loc.expression.source_buffer,
                            begin_pos, end_pos)
end
parse_command(command) click to toggle source
# File lib/rubocop/cop/itamae/cd_in_execute.rb, line 144
def parse_command(command)
  command =~ /^\s*cd\s+(.+?)&&(.+)$/
  if Regexp.last_match(1) && Regexp.last_match(2)
    [Regexp.last_match(1).strip, Regexp.last_match(2).strip]
  else
    [nil, nil]
  end
end