Class: RuboCop::Cop::Style::FileWrite
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
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
Instance Method Summary collapse
- #block_write?(node) ⇒ Object
- #evidence(node) ⇒ Object
- #file_open?(node) ⇒ Object
- #on_send(node) ⇒ Object
- #send_write?(node) ⇒ Object
Methods included from AutoCorrector
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
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
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| = format(MSG, write_method: write_method(mode)) add_offense(write_node, 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 |