lib/phlex/compiler/output.rb
Lines
91%21 of 23 covered
Branches
100%5 of 5 covered
Methods
50%2 of 4 covered
- Not run
- 29-30
- Never called
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # What a compiled method appends to the buffer, before it's lowered to Ruby. | |
| 4 | # MethodCompiler leaves these in the method's Refract tree where the output | |
| 5 | # goes, and the Emitter replaces each run of them with one guarded append. | |
| 6 | 1 | module Phlex::Compiler::Output |
| 7 | # Text known at compile time. | |
| 8 | 1 | Literal = Data.define(:text) |
| 9 | ||
| 10 | # A conditional choosing between literal texts whose predicate can't raise, | |
| 11 | # so it can't exit early. | |
| 12 | 1 | Conditional = Data.define(:node) |
| 13 | ||
| 14 | # A Ruby expression that evaluates to the text to append, and may raise. | |
| 15 | 1 | Expression = Data.define(:node) |
| 16 | ||
| 17 | # An attribute whose value was serialised into locals: a String's escaped | |
| 18 | # text, appended between the prefix and suffix, or for any other value the | |
| 19 | # whole text. Appended as statements, so the String needn't be copied. | |
| 20 | 1 | Choice = Data.define(:escaped, :text, :prefix, :suffix) |
| 21 | ||
| 22 | # Statements that serialise attributes into locals, then the part to | |
| 23 | # append, with the closing text appended instead if the statements exit | |
| 24 | # early. The runtime closes an opening tag however serialisation exits. | |
| 25 | 1 | Guarded = Data.define(:statements, :result, :closing) |
| 26 | ||
| 27 | # Stands in for a statement in the method's tree until it's emitted. | |
| 28 | 1 | module Statement |
| 29 | 0 | def start_line = nil |
| 30 | 0 | def accept(visitor) = self |
| 31 | end | |
| 32 | ||
| 33 | # Parts appended to the buffer in order. | |
| 34 | 1 | Append = Data.define(:parts) do |
| 35 | 1 | include Statement |
| 36 | ||
| 37 | 138 | def static? = parts.all? { |part| part in Literal | Conditional } |
| 38 | end | |
| 39 | ||
| 40 | # Parts appended to a buffer read into a local, where rendering is already | |
| 41 | # known, as an element method appends its opening tag. | |
| 42 | 1 | Opening = Data.define(:parts, :buffer) do |
| 43 | 1 | include Statement |
| 44 | end | |
| 45 | ||
| 46 | # Statements whose output is followed by the closing text however they | |
| 47 | # exit, unless none of it can exit early. | |
| 48 | 1 | Enclosed = Data.define(:body, :closing) do |
| 49 | 1 | include Statement |
| 50 | end | |
| 51 | ||
| 52 | # Whether the statements can only append text that's known not to raise | |
| 53 | # or jump, so they need no guard. | |
| 54 | 1 | def self.static?(node) |
| 55 | 826 | case node |
| 56 | 584 | in Array then node.all? { |child| static?(child) } |
| 57 | 120 | in Append then node.static? |
| 58 | 259 | in Enclosed then static?(node.body) |
| 59 | 36 | in Refract::StatementsNode then static?(node.body) |
| 60 | 116 | else false |
| 61 | end | |
| 62 | end | |
| 63 | end |