lib/phlex/compiler/emitter.rb
Lines
98%59 of 60 covered
Branches
90%20 of 22 covered
Methods
100%11 of 11 covered
- Not run
- 45
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # Lowers the output nodes MethodCompiler left in a method's tree to Ruby. | |
| 4 | # Each run of appends becomes one chain of `<<` on the buffer behind the | |
| 5 | # render check, with adjacent literals fused, and each enclosure becomes an | |
| 6 | # `ensure` that appends its closing text if the body exits early. | |
| 7 | 1 | class Phlex::Compiler::Emitter < Refract::MutationVisitor |
| 8 | 1 | include Phlex::Compiler::Builder |
| 9 | ||
| 10 | 1 | Output = Phlex::Compiler::Output |
| 11 | ||
| 12 | 1 | def initialize(locals) |
| 13 | 157 | super() |
| 14 | 157 | @locals = locals |
| 15 | end | |
| 16 | ||
| 17 | 1 | visit Refract::StatementsNode do |node| |
| 18 | 1682 | body = [] |
| 19 | ||
| 20 | 1682 | flatten(node.body).each do |child| |
| 21 | 2764 | case child |
| 22 | 2764 | in Output::Append if Output::Append === body.last |
| 23 | 280 | body[-1] = Output::Append.new(parts: body.last.parts + child.parts) |
| 24 | in Output::Append | |
| 25 | 345 | body << child |
| 26 | in Output::Enclosed | |
| 27 | 104 | body.concat(enclose(child)) |
| 28 | in Output::Opening | |
| 29 | 2 | body.concat(appends(child.parts, read(child.buffer))) |
| 30 | else | |
| 31 | 2033 | body << visit(child) |
| 32 | end | |
| 33 | end | |
| 34 | ||
| 35 | 4270 | node.copy(body: body.compact.map { |statement| (Output::Append === statement) ? append(statement.parts) : statement }) |
| 36 | end | |
| 37 | ||
| 38 | # Splices nested statements and enclosures that can't exit early into the | |
| 39 | # list. ASCII text at either end of an enclosure's body moves out of it, to | |
| 40 | # fuse with the text around it, since appending it can't fail or jump. An | |
| 41 | # enclosure whose body is then just another enclosure closes both at once. | |
| 42 | 1 | private def flatten(statements) |
| 43 | 2453 | statements.flat_map do |statement| |
| 44 | 3695 | case statement |
| 45 | 0 | in nil then []in never taken |
| 46 | 529 | in Refract::StatementsNode then flatten(statement.body) |
| 47 | 126 | in Output::Enclosed if Output.static?(statement) then flatten(statement.body) |
| 48 | in Output::Enclosed | |
| 49 | 116 | body = flatten(statement.body) |
| 50 | 248 | leading = body.take_while { |child| ascii_text?(child) } |
| 51 | 116 | rest = body.drop(leading.length) |
| 52 | 250 | trailing = rest.reverse.take_while { |child| ascii_text?(child) }.reverse |
| 53 | 116 | body = rest.take(rest.length - trailing.length) |
| 54 | ||
| 55 | 116 | enclosed = case body |
| 56 | in [Output::Enclosed => inner] if statement.closing.ascii_only? && inner.closing.ascii_only? | |
| 57 | 7 | inner.with(closing: inner.closing + statement.closing) |
| 58 | else | |
| 59 | 109 | statement.with(body:) |
| 60 | end | |
| 61 | ||
| 62 | 116 | [*leading, enclosed, *trailing] |
| 63 | 2924 | else [statement] |
| 64 | end | |
| 65 | end | |
| 66 | end | |
| 67 | ||
| 68 | 1 | private def ascii_text?(statement) |
| 69 | 320 | Output::Append === statement && statement.parts.all? { |part| Output::Literal === part && part.text.ascii_only? } |
| 70 | end | |
| 71 | ||
| 72 | 1 | private def enclose(node) |
| 73 | 104 | done = @locals.fresh(:done) |
| 74 | ||
| 75 | [ | |
| 76 | 104 | write(done, Refract::FalseNode.new), |
| 77 | begin_node( | |
| 78 | [*visit(statements(node.body)).body, write(done, Refract::TrueNode.new)], | |
| 79 | ensure_clause: ensure_node([unless_node(read(done), [append([Output::Literal.new(node.closing)])])]) | |
| 80 | ), | |
| 81 | ] | |
| 82 | end | |
| 83 | ||
| 84 | # Each part is appended in its own `<<` so that if one raises, everything | |
| 85 | # before it has already reached the buffer, as at runtime. The whole | |
| 86 | # statement evaluates to nil, like the element methods it replaces. | |
| 87 | 1 | private def append(parts) |
| 88 | 449 | parenthesized([if_node(should_render, appends(parts, buffer)), Refract::NilNode.new]) |
| 89 | end | |
| 90 | ||
| 91 | # One chain for each run of parts, and a choice of appends for each Choice. | |
| 92 | 1 | private def appends(parts, receiver) |
| 93 | 1104 | parts.slice_when { |a, b| Output::Choice === a || Output::Choice === b }.map do |run| |
| 94 | 509 | case run |
| 95 | in [Output::Choice => choice] | |
| 96 | 29 | if_node( |
| 97 | read(choice.escaped), | |
| 98 | [chain([Output::Literal.new(choice.prefix), Output::Expression.new(read(choice.escaped)), Output::Literal.new(choice.suffix)], receiver)], | |
| 99 | else_body: [call(receiver, :<<, read(choice.text))] | |
| 100 | ) | |
| 101 | else | |
| 102 | 480 | chain(run, receiver) |
| 103 | end | |
| 104 | end | |
| 105 | end | |
| 106 | ||
| 107 | 1 | private def chain(parts, receiver) |
| 108 | 1154 | fuse(parts).reduce(receiver) { |chained, part| call(chained, :<<, lower(part, receiver)) } |
| 109 | end | |
| 110 | ||
| 111 | 1 | private def fuse(parts) |
| 112 | 509 | parts.each_with_object([]) do |part, fused| |
| 113 | 1162 | if Output::Literal === part && Output::Literal === fused.last |
| 114 | 517 | fused[-1] = Output::Literal.new(fused.last.text + part.text) |
| 115 | else | |
| 116 | 645 | fused << part |
| 117 | end | |
| 118 | end | |
| 119 | end | |
| 120 | ||
| 121 | 1 | private def lower(part, receiver) |
| 122 | 690 | case partelse never taken |
| 123 | 581 | in Output::Literal then string(part.text) |
| 124 | 64 | in Output::Conditional | Output::Expression then part.node |
| 125 | 45 | in Output::Guarded then guard(part, receiver) |
| 126 | end | |
| 127 | end | |
| 128 | ||
| 129 | # The guard is an expression inside the append chain, so the opening tag's | |
| 130 | # text has already been appended when the closing text is. | |
| 131 | 1 | private def guard(part, receiver) |
| 132 | 45 | done = @locals.fresh(:done) |
| 133 | ||
| 134 | 45 | begin_node( |
| 135 | [write(done, Refract::FalseNode.new), *part.statements, write(done, Refract::TrueNode.new), lower(part.result, receiver)], | |
| 136 | ensure_clause: ensure_node([unless_node(read(done), [call(receiver, :<<, string(part.closing))], inline: true)]) | |
| 137 | ) | |
| 138 | end | |
| 139 | end |