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:

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

# good
File.write(filename, content)
## binary mode
# bad
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

Instance Attribute Summary

Attributes inherited from Base

#config, #processed_source

Instance Method Summary collapse

Methods included from AutoCorrector

support_autocorrect?

Methods inherited from Base

#active_support_extensions_enabled?, #add_global_offense, #add_offense, autocorrect_incompatible_with, badge, callbacks_needed, #callbacks_needed, #config_to_allow_offenses, #config_to_allow_offenses=, #cop_config, #cop_name, cop_name, department, documentation_url, exclude_from_registry, #excluded_file?, #external_dependency_checksum, inherited, #initialize, joining_forces, lint?, match?, #message, #offenses, #on_investigation_end, #on_new_investigation, #on_other_file, #parse, #ready, #relevant_file?, support_autocorrect?, support_multiple_source?, #target_rails_version, #target_ruby_version

Methods included from ExcludeLimit

#exclude_limit

Methods included from AutocorrectLogic

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

Methods included from IgnoredNode

#ignore_node, #ignored_node?, #part_of_ignored_node?

Methods included from Util

silence_warnings

Constructor Details

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

Instance Method Details

#block_write?(node) ⇒ Object


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

def_node_matcher :block_write?, <<~PATTERN
  (block _ (args (arg $_)) (send (lvar $_) :write $_))
PATTERN

#evidence(node) ⇒ Object


85
86
87
88
89
90
91
# 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

#file_open?(node) ⇒ Object


52
53
54
55
56
57
58
59
60
# File 'lib/rubocop/cop/style/file_write.rb', line 52

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


72
73
74
75
76
77
78
79
80
81
82
83
# 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

#send_write?(node) ⇒ Object


63
64
65
# File 'lib/rubocop/cop/style/file_write.rb', line 63

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