Class: StrictIvars::BaseProcessor

Inherits:
Prism::Visitor
  • Object
show all
Defined in:
lib/strict_ivars/base_processor.rb

Direct Known Subclasses

Processor

Constant Summary collapse

EVAL_METHODS =
Set[:class_eval, :module_eval, :instance_eval, :eval].freeze

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initializeBaseProcessor

Returns a new instance of BaseProcessor.



21
22
23
24
# File 'lib/strict_ivars/base_processor.rb', line 21

def initialize
	@context = Set[]
	@annotations = []
end

Instance Attribute Details

#annotationsObject (readonly)

: Array[[Integer, String]]



27
28
29
# File 'lib/strict_ivars/base_processor.rb', line 27

def annotations
  @annotations
end

Class Method Details

.call(source) ⇒ Object

: (String) -> String



7
8
9
10
11
12
13
14
15
16
17
18
19
# File 'lib/strict_ivars/base_processor.rb', line 7

def self.call(source)
	visitor = new
	visitor.visit(Prism.parse(source).value)
	buffer = source.dup
	annotations = visitor.annotations
	annotations.sort_by!(&:first)

	annotations.reverse_each do |offset, string|
		buffer.insert(offset, string)
	end

	buffer
end

Instance Method Details

#visit_call_node(node) ⇒ Object



29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
# File 'lib/strict_ivars/base_processor.rb', line 29

def visit_call_node(node)
	name = node.name

	if EVAL_METHODS.include?(name) && (arguments = node.arguments)
		location = arguments.location

		closing = if arguments.contains_forwarding?
			")), &(::StrictIvars.__eval_block_from_forwarding__(...))"
		else
			"))"
		end

		if node.receiver
			receiver_local = "__eval_receiver_#{SecureRandom.hex(8)}__"
			receiver_location = node.receiver.location

			@annotations.push(
				[receiver_location.start_character_offset, "(#{receiver_local} = "],
				[receiver_location.end_character_offset, ")"],
				[location.start_character_offset, "*(::StrictIvars.__process_eval_args__(#{receiver_local}, :#{name}, "],
				[location.end_character_offset, closing]
			)
		else
			@annotations.push(
				[location.start_character_offset, "*(::StrictIvars.__process_eval_args__(self, :#{name}, "],
				[location.end_character_offset, closing]
			)
		end
	end

	super
end