# frozen_string_literal: true module RuboCop module Cop module Style # Favor `File.(bin)write` convenience methods. # # @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) # class FileWrite < Base extend AutoCorrector include RangeHelp MSG = 'Use `File.%s`.' RESTRICT_ON_SEND = %i[open].to_set.freeze TRUNCATING_WRITE_MODES = %w[w wt wb w+ w+t w+b].to_set.freeze # @!method file_open?(node) def_node_matcher :file_open?, <<~PATTERN (send (const {nil? cbase} :File) :open $_ (str $%TRUNCATING_WRITE_MODES) (block-pass (sym :write))? ) PATTERN # @!method send_write?(node) def_node_matcher :send_write?, <<~PATTERN (send _ :write $_) PATTERN # @!method block_write?(node) def_node_matcher :block_write?, <<~PATTERN (block _ (args (arg $_)) (send (lvar $_) :write $_)) PATTERN 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 = "#{write_method(mode)}(#{filename.source}, #{content.source})" corrector.replace(range, replacement) end end end 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 private 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 yield(content) if content end def write_method(mode) mode.end_with?('b') ? :binwrite : :write end end end end end