lib/phlex/compiler/builder.rb

Lines

100%

32 of 32 covered

Branches

100%

2 of 2 covered

Methods

100%

15 of 15 covered

1# frozen_string_literal: true
2
3# Constructors for the Refract nodes the compiler generates, with the
4# defaults it always wants. Bodies are given as arrays of statements.
51module Phlex::Compiler::Builder
61 private def call(receiver, name, *arguments, block: nil)
74624 Refract::CallNode.new(
8 receiver:,
9 name:,
104624 arguments: arguments.empty? ? nil : Refract::ArgumentsNode.new(arguments:),
11 block:
12 )
13 end
14
15 # Looked up from the top level, so a component can't shadow it.
161 private def constant(path)
174522 path.split("::").reduce(nil) { |parent, name| Refract::ConstantPathNode.new(parent:, name:) }
18 end
19
201 private def string(text)
21853 Refract::StringNode.new(unescaped: text)
22 end
23
24 # Generated locals aren't resolved to a scope, so their depth is unknown.
251 private def read(name)
262301 Refract::LocalVariableReadNode.new(name:, depth: nil)
27 end
28
291 private def write(name, value)
301089 Refract::LocalVariableWriteNode.new(name:, value:, depth: nil)
31 end
32
331 private def statements(body)
343823 Refract::StatementsNode.new(body:)
35 end
36
371 private def parenthesized(body)
38489 Refract::ParenthesesNode.new(body: statements(body))
39 end
40
411 private def if_node(predicate, body, else_body: nil, inline: false)
42835 Refract::IfNode.new(
43 inline:,
44 predicate:,
45 statements: statements(body),
46 subsequent: else_body && Refract::ElseNode.new(statements: statements(else_body))
47 )
48 end
49
501 private def unless_node(predicate, body, inline: false)
51149 Refract::UnlessNode.new(inline:, predicate:, statements: statements(body), else_clause: nil)
52 end
53
541 private def begin_node(body, rescue_clause: nil, ensure_clause: nil)
55498 Refract::BeginNode.new(statements: statements(body), rescue_clause:, else_clause: nil, ensure_clause:)
56 end
57
581 private def rescue_node(exceptions, reference, body)
59348 Refract::RescueNode.new(exceptions:, reference:, statements: statements(body), subsequent: nil)
60 end
61
621 private def ensure_node(body)
63150 Refract::EnsureNode.new(statements: statements(body))
64 end
65
661 private def buffer
67546 call(read(state_local), :buffer)
68 end
69
701 private def should_render
71549 call(read(state_local), :should_render?)
72 end
73
74 # The local holding `@_state`. Overridden where reading it must also
75 # declare it.
761 private def state_local
77898 Phlex::Compiler::Locals::STATE
78 end
79end