class RuboCop::Cop::Style::FileWrite

Favor ‘File.(bin)write` convenience methods.

NOTE: There are different method signatures between ‘File.write` (class method) and `File#write` (instance method). The following case will be allowed because static analysis does not know the contents of the splat argument:

source,ruby

File.open(filename, ‘w’) do |f|

f.write(*objects)

end


@example

## text mode
# bad
File.open(filename, 'w').write(content)
File.open(filename, 'w') do |f|
  f.write(content)
end

# good
File.write(filename, content)

@example

## binary mode
# bad
File.open(filename, 'wb').write(content)
File.open(filename, 'wb') do |f|
  f.write(content)
end

# good
File.binwrite(filename, content)

Constants

MSG
RESTRICT_ON_SEND
TRUNCATING_WRITE_MODES

Public Instance Methods

evidence(node) { |filename, mode, content, parent| ... } click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 85
def evidence(node)
  file_open?(node) do |filename, mode|
    file_open_write?(node.parent) do |content|
      yield(filename, mode, content, node.parent)
    end
  end
end
on_send(node) click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 72
def on_send(node)
  evidence(node) do |filename, mode, content, write_node|
    message = format(MSG, write_method: write_method(mode))

    add_offense(write_node, message: message) do |corrector|
      range = range_between(node.loc.selector.begin_pos, write_node.loc.expression.end_pos)
      replacement = replacement(mode, filename, content, write_node)

      corrector.replace(range, replacement)
    end
  end
end

Private Instance Methods

file_open_write?(node) { |content| ... } click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 95
def file_open_write?(node)
  content = send_write?(node) || block_write?(node) do |block_arg, lvar, write_arg|
    write_arg if block_arg == lvar
  end
  return false if content&.splat_type?

  yield(content) if content
end
heredoc?(write_node) click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 123
def heredoc?(write_node)
  write_node.block_type? && (first_argument = write_node.body.first_argument) &&
    first_argument.respond_to?(:heredoc?) && first_argument.heredoc?
end
heredoc_range(first_argument) click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 128
def heredoc_range(first_argument)
  range_between(
    first_argument.loc.heredoc_body.begin_pos, first_argument.loc.heredoc_end.end_pos
  )
end
replacement(mode, filename, content, write_node) click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 108
        def replacement(mode, filename, content, write_node)
          replacement = "#{write_method(mode)}(#{filename.source}, #{content.source})"

          if heredoc?(write_node)
            first_argument = write_node.body.first_argument

            <<~REPLACEMENT.chomp
              #{replacement}
              #{heredoc_range(first_argument).source}
            REPLACEMENT
          else
            replacement
          end
        end
write_method(mode) click to toggle source
# File lib/rubocop/cop/style/file_write.rb, line 104
def write_method(mode)
  mode.end_with?('b') ? :binwrite : :write
end