Class: RuboCop::Cop::Style::FileWrite

Inherits:
Base
  • Object
show all
Extended by:
AutoCorrector
Includes:
RangeHelp
Defined in:
lib/rubocop/cop/style/file_write.rb

Overview

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

Examples:

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

# good
File.write(filename, content)

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

# good
File.binwrite(filename, content)

Constant Summary collapse

MSG =
'Use `File.%<write_method>s`.'
RESTRICT_ON_SEND =
%i[open].to_set.freeze
TRUNCATING_WRITE_MODES =
%w[w wt wb w+ w+t w+b].to_set.freeze

Constants included from RangeHelp

RangeHelp::BYTE_ORDER_MARK, RangeHelp::NOT_GIVEN

Constants inherited from Base

Base::DEPARTMENT_SEVERITIES

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source, #project_index

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, #always_autocorrect?, autocorrect_incompatible_with, badge, #begin_investigation, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #contextual_autocorrect?, #cop_config, #cop_name, cop_name, department, #disabled_autocorrect?, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, #inspect, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #parser_engine, #preview?, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, support_autocorrect?, support_multiple_source?, #target_gem_version, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

cop_dir_for, #exclude_limit, read_limits

Methods included from AutocorrectLogic

#autocorrect?, #autocorrect_enabled?, #autocorrect_requested?, #autocorrect_with_disable_uncorrectable?, #correctable?, #disable_uncorrectable?, #safe_autocorrect?, #skipped_unsafe_correction_with_disable_uncorrectable?

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

emulate_write_read_cycle, replace_file_contents, silence_warnings

Constructor Details

This class inherits a constructor from RuboCop::Cop::Base

Instance Method Details

#block_write?(node) ⇒ Object



65
66
67
68
69
70
71
# File 'lib/rubocop/cop/style/file_write.rb', line 65

def_node_matcher :block_write?, <<~PATTERN
  {
    (block _ (args (arg _name)) (send (lvar _name) :write $_))
    (numblock _ 1 (send (lvar :_1) :write $_))
    (itblock _ :it (send (lvar :it) :write $_))
  }
PATTERN

#evidence(node) ⇒ Object



92
93
94
95
96
97
98
# File 'lib/rubocop/cop/style/file_write.rb', line 92

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

#file_open?(node) ⇒ Object



49
50
51
52
53
54
55
56
57
# File 'lib/rubocop/cop/style/file_write.rb', line 49

def_node_matcher :file_open?, <<~PATTERN
  (send
    (const {nil? cbase} :File)
    :open
    $_
    (str $%TRUNCATING_WRITE_MODES)
    (block-pass (sym :write))?
  )
PATTERN

#on_send(node) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# File 'lib/rubocop/cop/style/file_write.rb', line 73

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 = replaced_range(node, write_node)
      heredocs = removed_heredocs(filename, content, write_node)
      next if foreign_heredoc_body?(range, heredocs)

      heredocs = restored_heredocs(heredocs, [filename, content])
      next unless restorable?(range, heredocs, write_node)

      replacement = "#{write_method(mode)}(#{filename.source}, #{content.source})"

      replace_and_restore_heredocs(corrector, range, replacement, heredocs)
    end
  end
end

#send_write?(node) ⇒ Object



60
61
62
# File 'lib/rubocop/cop/style/file_write.rb', line 60

def_node_matcher :send_write?, <<~PATTERN
  (send _ :write $_)
PATTERN