Class: RuboCop::Cop::Style::FileRead

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

Overview

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

Examples:

# bad - text mode
File.open(filename).read
File.open(filename, &:read)
File.open(filename) { |f| f.read }
File.open(filename) do |f|
  f.read
end
File.open(filename, 'r').read
File.open(filename, 'r', &:read)
File.open(filename, 'r') do |f|
  f.read
end

# good
File.read(filename)

# bad - binary mode
File.open(filename, 'rb').read
File.open(filename, 'rb', &:read)
File.open(filename, 'rb') do |f|
  f.read
end

# good
File.binread(filename)

Constant Summary collapse

MSG =
'Use `File.%<read_method>s`.'
RESTRICT_ON_SEND =
%i[open].freeze
READ_FILE_START_TO_FINISH_MODES =
%w[r rt rb r+ r+t r+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_read?(node) ⇒ Object



62
63
64
# File 'lib/rubocop/cop/style/file_read.rb', line 62

def_node_matcher :block_read?, <<~PATTERN
  (block _ (args (arg _name)) (send (lvar _name) :read))
PATTERN

#file_open?(node) ⇒ Object



46
47
48
49
50
51
52
53
54
# File 'lib/rubocop/cop/style/file_read.rb', line 46

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

#on_send(node) ⇒ Object



66
67
68
69
70
71
72
73
74
75
76
77
# File 'lib/rubocop/cop/style/file_read.rb', line 66

def on_send(node)
  evidence(node) do |filename, mode, read_node|
    message = format(MSG, read_method: read_method(mode))

    add_offense(read_node, message: message) do |corrector|
      range = range_between(node.loc.selector.begin_pos, read_node.source_range.end_pos)
      replacement = "#{read_method(mode)}(#{filename.source})"

      corrector.replace(range, replacement)
    end
  end
end

#send_read?(node) ⇒ Object



57
58
59
# File 'lib/rubocop/cop/style/file_read.rb', line 57

def_node_matcher :send_read?, <<~PATTERN
  (send _ :read)
PATTERN