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

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, #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, 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, #ready, #relevant_file?, requires_gem, #string_literals_frozen_by_default?, 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



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

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

#evidence(node) ⇒ Object



82
83
84
85
86
87
88
# File 'lib/rubocop/cop/style/file_write.rb', line 82

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



69
70
71
72
73
74
75
76
77
78
79
80
# File 'lib/rubocop/cop/style/file_write.rb', line 69

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.source_range.end_pos)
      replacement = replacement(mode, filename, content, write_node)

      corrector.replace(range, replacement)
    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