Module: Phlex::Compiler

Defined in:
lib/phlex/compiler.rb,
lib/phlex/compiler/method_compiler.rb

Defined Under Namespace

Classes: ClassCompiler, Compactor, Concat, FileCompiler, MethodCompiler

Constant Summary collapse

MAP =
{}
Error =
Class.new(StandardError)

Class Method Summary collapse

Class Method Details

.compile(component) ⇒ Object



15
16
17
18
# File 'lib/phlex/compiler.rb', line 15

def self.compile(component)
	path, line = Object.const_source_location(component.name)
	compile_file(path)
end

.compile_file(path) ⇒ Object



20
21
22
23
24
25
26
27
28
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
# File 'lib/phlex/compiler.rb', line 20

def self.compile_file(path)
	unless File.exist?(path)
		raise ArgumentError, "Can’t compile #{path} because it doesn’t exist."
	end

	require(path)

	source = File.read(path)
	tree = Prism.parse(source).value
	refract = Refract::Converter.new.visit(tree)

	last_line = source.count("\n")

	starting_line = last_line + 1

	results = FileCompiler.new(path).compile(refract)

	result = Refract::StatementsNode.new(
		body: results.map do |result|
			result.namespace.reverse_each.reduce(
				result.compiled_snippets
			) do |body, scope|
				scope.copy(
					body: Refract::StatementsNode.new(
						body: [body]
					)
				)
			end
		end
	)

	formatting_result = Refract::Formatter.new(starting_line:).format_node(result)

	MAP[path] = formatting_result.source_map

	eval("# frozen_string_literal: true\n#{formatting_result.source}", TOPLEVEL_BINDING, path, starting_line - 1)
end