Class: RuboCop::Cop::Ruact::NoIoInFlight

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/ruact/no_io_in_flight.rb

Overview

Prohibits I/O calls in lib/ruact/flight/** modules. Flight modules must be pure value transformations with no side effects (NFR10).

Examples:

# bad (in lib/ruact/flight/serializer.rb)
File.read("manifest.json")
Rails.logger.debug("serializing")
puts "debug"

# good
def serialize(value, request:)
  case value
  when NilClass then "null"
  end
end

Constant Summary collapse

MSG =
"I/O is not allowed in flight/** modules (NFR10). Move I/O to the imperative shell."
IO_METHODS =
%i[
  read write open puts print p pp warn
].freeze
IO_RECEIVERS =
%w[
  File IO Rails.logger Net Socket TCPSocket UDPSocket
].freeze

Instance Method Summary collapse

Instance Method Details

#io_send?(node) ⇒ Object



33
34
35
# File 'lib/rubocop/cop/ruact/no_io_in_flight.rb', line 33

def_node_matcher :io_send?, <<~PATTERN
  (send {nil? (const ...) (send ...)} {#{IO_METHODS.map { |m| ":#{m}" }.join(' ')}} ...)
PATTERN

#on_send(node) ⇒ Object



37
38
39
40
41
42
43
44
45
46
47
48
# File 'lib/rubocop/cop/ruact/no_io_in_flight.rb', line 37

def on_send(node)
  return unless flight_file?

  receiver = node.receiver
  method   = node.method_name

  io_via_nil_receiver = IO_METHODS.include?(method) && receiver.nil?
  io_via_logger       = IO_METHODS.include?(method) && !receiver.nil? && rails_logger_receiver?(receiver)
  io_via_class        = receiver && io_class_receiver?(receiver)

  add_offense(node) if io_via_nil_receiver || io_via_logger || io_via_class
end