Class: RuboCop::Cop::Gusto::BootsnapLoadFile
- Inherits:
-
Base
- Object
- Base
- RuboCop::Cop::Gusto::BootsnapLoadFile
- Defined in:
- lib/rubocop/cop/gusto/bootsnap_load_file.rb
Overview
Prefer ‘YAML.load_file`/`JSON.load_file` over reading a file and then parsing its contents. Bootsnap caches the parsed result of `load_file`, so this improves load time.
Constant Summary collapse
- PROHIBITED_CONSTANTS =
Set[:YAML, :JSON].freeze
- RESTRICT_ON_SEND =
%i(load).freeze
Instance Method Summary collapse
- #file_read(node) ⇒ Object
- #load_inside_file_open(node) ⇒ Object
- #on_block(node) ⇒ Object (also: #on_itblock, #on_numblock)
- #on_send(node) ⇒ Object
- #yaml_or_json_load(node) ⇒ Object
Instance Method Details
#file_read(node) ⇒ Object
25 |
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 25 def_node_matcher :file_read, "(send (const nil? :File) :read $_)" |
#load_inside_file_open(node) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 |
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 28 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
41 42 43 44 45 |
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 41 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
49 50 51 52 53 |
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 49 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
22 |
# File 'lib/rubocop/cop/gusto/bootsnap_load_file.rb', line 22 def_node_matcher :yaml_or_json_load, "(send $(const nil? PROHIBITED_CONSTANTS) :load ...)" |