Module: Empirical

Defined in:
lib/empirical.rb,
lib/empirical/version.rb,
lib/empirical/eval_processor.rb

Defined Under Namespace

Classes: BaseProcessor, ClassCallbacksProcessor, Configuration, EvalProcessor, IvarProcessor, NameError, SignatureProcessor, TypeError, VoidClass

Constant Summary collapse

Void =
VoidClass.new
EMPTY_ARRAY =
[].freeze
EVERYTHING =
["**/*"].freeze
METHOD_METHOD =
Module.instance_method(:method)
CONFIG =
Configuration.new
PROCESSORS =
[
	IvarProcessor,
	SignatureProcessor,
	ClassCallbacksProcessor,
]
TypedSignatureError =
Class.new(SyntaxError)
NeverError =
Class.new(RuntimeError)
VERSION =
"0.0.3"

Class Method Summary collapse

Class Method Details

.__eval_block_from_forwarding__(&block) ⇒ Object

: () { () -> void } -> Proc



45
46
47
# File 'lib/empirical/eval_processor.rb', line 45

def self.__eval_block_from_forwarding__(*, &block)
	block
end

.__process_eval_args__(receiver, method_name, *args) ⇒ Object

For internal use only. This method pre-processes arguments to an eval method. : (Object, Symbol, *untyped)



6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
# File 'lib/empirical/eval_processor.rb', line 6

def self.__process_eval_args__(receiver, method_name, *args)
	method = METHOD_METHOD.bind_call(receiver, method_name)
	owner = method.owner

	source, file = nil

	case method_name
	when :class_eval, :module_eval
		if Module == owner
			source, file = args
		end
	when :instance_eval
		if BasicObject == owner
			source, file = args
		end
	when :eval
		if Kernel == owner
			source, _binding, file = args
		elsif Binding == owner
			source, file = args
		end
	end

	if String === source
		file ||= caller_locations(1, 1).first.path

		if CONFIG.match?(file)
			args[0] = process(source, with: PROCESSORS)
		else
			args[0] = process(source)
		end
	end

	args
rescue ::NameError
	args
end

.init(include: EMPTY_ARRAY, exclude: EMPTY_ARRAY) ⇒ Object

Initializes Empirical so that code loaded after this point will:

1. be guarded against undefined instance variable reads,
2. permit users to define type checked method definitions, and
3. permit users to define class/module defined callbacks

You can pass an array of globs to include: and exclude:.

Empirical.init(
  include: ["#{Dir.pwd}/**/*"],
  exclude: ["#{Dir.pwd}/vendor/**/*"]
)

: (include: Array, exclude: Array) -> void



55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
# File 'lib/empirical.rb', line 55

def self.init(include: EMPTY_ARRAY, exclude: EMPTY_ARRAY)
	CONFIG.include(*include)
	CONFIG.exclude(*exclude)

	RequireHooks.source_transform(
		patterns: EVERYTHING,
		exclude_patterns: EMPTY_ARRAY
	) do |path, source|
		source ||= File.read(path)

		if CONFIG.match?(path)
			process(source, with: PROCESSORS)
		else
			process(source)
		end
	end
end

.process(source, with: []) ⇒ Object



73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/empirical.rb', line 73

def self.process(source, with: [])
	annotations = []
	tree = Prism.parse(source).value

	Array(with).each do |processor|
		processor.new(annotations:).visit(tree)
	end

	Empirical::EvalProcessor.new(annotations:).visit(tree)

	buffer = source.dup
	annotations.sort_by!(&:first)

	annotations.reverse_each do |offset, length, string|
		buffer[offset, length] = string
	end

	buffer
end