Class: RuboCop::Cop::Gusto::BootsnapLoadFile

Inherits:
Base
  • Object
show all
Defined in:
lib/rubocop/cop/gusto/bootsnap_load_file.rb

Overview

Do not use Bootsnap to load files. Use ‘require` instead.

Constant Summary collapse

PROHIBITED_CONSTANTS =
Set[:YAML, :JSON].freeze
RESTRICT_ON_SEND =
%i(load).freeze

Instance Method Summary collapse

Instance Method Details

#file_read(node) ⇒ Object



15
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 15

def_node_matcher :file_read, "(send (const nil? :File) :read $_)"

#load_inside_file_open(node) ⇒ Object



18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 18

def_node_matcher :load_inside_file_open, <<~PATTERN
  (block
    (send
      (const nil? :File) :open
      $(str _))
    (args
      (arg _file))
    (send
      $(const nil? :YAML) :load
      (lvar _file))
  )
PATTERN

#on_block(node) ⇒ Object Also known as: on_itblock, on_numblock



31
32
33
34
35
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 31

def on_block(node)
  load_inside_file_open(node) do |file_path_node, constant_node|
    add_offense(node, message: "Use #{constant_node.source}.load_file(#{file_path_node.source}) to improve load time with bootsnap")
  end
end

#on_send(node) ⇒ Object



39
40
41
42
43
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 39

def on_send(node)
  yaml_or_json_load(node) do |constant_node|
    on_load(node, constant_node)
  end
end

#yaml_or_json_load(node) ⇒ Object



12
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 12

def_node_matcher :yaml_or_json_load, "(send $(const nil? PROHIBITED_CONSTANTS) :load ...)"