lib/phlex/compiler/file_compiler.rb
Lines
87%110 of 126 covered
Branches
75%44 of 58 covered
Methods
94%16 of 17 covered
- Not run
- 95110-111196206-208210-211238-239241-243245252
- Never called
| 1 | # frozen_string_literal: true | |
| 2 | ||
| 3 | # Finds the definitions in a file that are the live methods of the given | |
| 4 | # components, and compiles them. Which class a `class` statement names is | |
| 5 | # never worked out from its constant path: a definition is compiled only | |
| 6 | # when a component's method has that file and line as its source location, | |
| 7 | # and the enclosing class and module statements are then reopened with a | |
| 8 | # probe so Ruby itself confirms they name that component. | |
| 9 | 1 | class Phlex::Compiler::FileCompiler < Refract::Visitor |
| 10 | 1 | Result = Data.define(:namespace, :component, :compiled_snippets, :visibilities, :inlined, :inlined_svg) |
| 11 | ||
| 12 | # A statement in the file and the class and module statements around it. | |
| 13 | 1 | Scoped = Data.define(:namespace, :node) |
| 14 | ||
| 15 | 1 | def initialize(path, targets:, diagnostics: Phlex::Compiler::Diagnostics.new(path), recompile: false, inline: true) |
| 16 | 133 | super() |
| 17 | 133 | @path = path |
| 18 | 133 | @targets = targets |
| 19 | 133 | @diagnostics = diagnostics |
| 20 | 133 | @recompile = recompile |
| 21 | 133 | @inline = inline |
| 22 | 133 | @current_namespace = [] |
| 23 | 133 | @definitions = [] |
| 24 | 133 | @usings = [] |
| 25 | 133 | @keyword_flagged = [] |
| 26 | 133 | @compiled_lines = [] |
| 27 | end | |
| 28 | ||
| 29 | 1 | def compile(node) |
| 30 | 133 | @top_level = node.statements |
| 31 | 133 | visit(node) |
| 32 | 133 | results = definitions_by_namespace.flat_map do |namespace, definitions| |
| 33 | 127 | compile_namespace(namespace, definitions) |
| 34 | end | |
| 35 | 121 | return [].freeze unless refinements_reproducible?then never taken |
| 36 | ||
| 37 | 121 | results.freeze |
| 38 | end | |
| 39 | ||
| 40 | # The `using` statements to put before the compiled definitions. | |
| 41 | 1 | def usings |
| 42 | 221 | @usings.map(&:node) |
| 43 | end | |
| 44 | ||
| 45 | 1 | visit Refract::ModuleNode do |node| |
| 46 | 91 | @current_namespace.push(node) |
| 47 | 91 | super(node) |
| 48 | 91 | @current_namespace.pop |
| 49 | end | |
| 50 | ||
| 51 | 1 | visit Refract::ClassNode do |node| |
| 52 | 158 | @current_namespace.push(node) |
| 53 | 158 | super(node) |
| 54 | 158 | @current_namespace.pop |
| 55 | end | |
| 56 | ||
| 57 | 1 | visit Refract::DefNode do |node| |
| 58 | 331 | @definitions << Scoped.new(namespace: @current_namespace.dup.freeze, node:) unless node.receiver |
| 59 | end | |
| 60 | ||
| 61 | 1 | visit Refract::BlockNode do |node| |
| 62 | 33 | nil |
| 63 | end | |
| 64 | ||
| 65 | # A `using` is copied into the compiled source, so it has to be a plain | |
| 66 | # top-level statement naming a constant: one inside a conditional or | |
| 67 | # using a local would mean something else there. Ruby offers no way to | |
| 68 | # read the `ruby2_keywords` flag back, so a method marked with it is | |
| 69 | # refused if it's compiled. | |
| 70 | 1 | visit Refract::CallNode do |node| |
| 71 | 184 | if node.receiver.nil? |
| 72 | 200 | case node.name |
| 73 | in :using | |
| 74 | # A refused `using` isn't copied, since its constant or local may | |
| 75 | # not be reachable from the probe's fresh scope. | |
| 76 | 12 | if @stack[-2].equal?(@top_level) && node.arguments&.arguments in [Refract::ConstantReadNode | Refract::ConstantPathNode]receiver never nil |
| 77 | 11 | @usings << Scoped.new(namespace: @current_namespace.dup.freeze, node:) |
| 78 | else | |
| 79 | 1 | @diagnostics.refuse(node, "this `using` isn't a plain top-level statement naming a constant, which the compiler can't reproduce") |
| 80 | end | |
| 81 | in :ruby2_keywords | |
| 82 | 4 | @keyword_flagged.concat(ruby2_keywords_names(node).map { |name| Scoped.new(namespace: @current_namespace.dup.freeze, node: name) }) |
| 83 | else nil | |
| 84 | end | |
| 85 | end | |
| 86 | ||
| 87 | 184 | super(node) |
| 88 | end | |
| 89 | ||
| 90 | 1 | private def ruby2_keywords_names(node) |
| 91 | 2 | (node.arguments&.arguments || []).map do |argument|receiver never nil |
| 92 | 2 | case argument |
| 93 | 1 | in Refract::SymbolNode then argument.unescaped.to_sym |
| 94 | 1 | in Refract::DefNode then argument.name |
| 95 | 0 | else @diagnostics.refuse(node, "ruby2_keywords is given arguments the compiler can't read, so it can't tell which methods it marks")else never taken |
| 96 | end | |
| 97 | end | |
| 98 | end | |
| 99 | ||
| 100 | # A refinement applies from its `using` to the end of the scope, so it can | |
| 101 | # only be reproduced when every `using` comes before the definitions that | |
| 102 | # are replaced. Definitions above it that are left alone don't matter. | |
| 103 | 1 | private def refinements_reproducible? |
| 104 | 121 | first_definition = @compiled_lines.min |
| 105 | # A `using` on the same line as the first replaced definition may follow | |
| 106 | # it, so it's refused too. | |
| 107 | 129 | partial = @usings.find { |using| !using.namespace.empty? || (first_definition && using.node.start_line >= first_definition) } |
| 108 | 121 | return true unless partialelse never taken |
| 109 | ||
| 110 | 0 | @diagnostics.refuse(partial.node, "this `using` applies to only part of the file, which the compiler can't reproduce") |
| 111 | 0 | false |
| 112 | end | |
| 113 | ||
| 114 | # A definition with no live method at its line is usually just one that a | |
| 115 | # later definition replaced: a live method not made by `def`, such as one | |
| 116 | # from `attr_reader` or `define_method`, is never a target. It means the | |
| 117 | # file has changed since it was loaded when a live method of the same name | |
| 118 | # has no definition at its own line, in which case compiling from this | |
| 119 | # file would install the wrong code. A compiled method was always made by | |
| 120 | # `def`, so its line having no definition of it means the same, and | |
| 121 | # recompiling would leave the compiled method in place. | |
| 122 | 1 | private def definitions_by_namespace |
| 123 | 133 | orphaned = @targets.values.reject do |target| |
| 124 | 739 | @definitions.any? { |definition| definition.node.start_line == target.line && definition.node.name == target.name } |
| 125 | end | |
| 126 | ||
| 127 | 133 | orphaned.each do |target| |
| 128 | 16 | next unless target.replaced |
| 129 | ||
| 130 | 1 | @diagnostics.refuse(target.line, "#{target.name} was compiled from this line, which no longer defines it, so the file has changed since it was loaded") |
| 131 | end | |
| 132 | ||
| 133 | 132 | unique_definitions.group_by(&:namespace).filter_map do |namespace, definitions| |
| 134 | 154 | targeted, compiled, unmatched = partition_by_target(definitions) |
| 135 | ||
| 136 | 155 | compiled.each { |definition| @diagnostics.report(definition.node, "#{definition.node.name} is already compiled") } |
| 137 | ||
| 138 | 153 | unmatched.each do |definition| |
| 139 | 138 | next unless orphaned.any? { |target| target.name == definition.node.name } |
| 140 | ||
| 141 | 5 | @diagnostics.refuse(definition.node, "no live method is defined at this line, so the file has changed since it was loaded") |
| 142 | end | |
| 143 | ||
| 144 | 149 | [namespace, targeted] unless targeted.empty? |
| 145 | end.to_h | |
| 146 | end | |
| 147 | ||
| 148 | # Two definitions on one line share a source location, so a live method | |
| 149 | # there can't be matched to either, whatever their names or classes. Lines | |
| 150 | # with no live method on them aren't compiled, so they're left unmatched. | |
| 151 | 1 | private def unique_definitions |
| 152 | 410 | @definitions.group_by { |definition| definition.node.start_line }.flat_map do |line, definitions| |
| 153 | 268 | next definitions if definitions.one? || !@targets.key?(line) |
| 154 | ||
| 155 | 2 | @diagnostics.refuse(definitions.first.node, "more than one method is defined on this line, so the compiler can't tell which definition is live") |
| 156 | 1 | [] |
| 157 | end | |
| 158 | end | |
| 159 | ||
| 160 | 1 | private def partition_by_target(definitions) |
| 161 | 154 | definitions.each_with_object([[], [], []]) do |definition, (targeted, compiled, unmatched)| |
| 162 | 264 | target = @targets[definition.node.start_line] |
| 163 | ||
| 164 | 264 | if target.nil? || target.name != definition.node.name |
| 165 | 73 | unmatched << definition |
| 166 | 191 | elsif target.compiled |
| 167 | 2 | compiled << definition |
| 168 | 189 | elsif keyword_flagged?(target) |
| 169 | # Matched by name alone, since the mark can sit in a later reopening | |
| 170 | # of the class, whose statements are different nodes. | |
| 171 | 1 | @diagnostics.refuse(definition.node, "#{definition.node.name} is marked ruby2_keywords in this file, which the compiler can't preserve") |
| 172 | else | |
| 173 | 188 | targeted << definition |
| 174 | end | |
| 175 | end | |
| 176 | end | |
| 177 | ||
| 178 | # Marking an alias marks the body it shares with the method, so the | |
| 179 | # method's aliases are checked too. Like the method's own name, an alias's | |
| 180 | # is matched wherever in the file it's marked: telling which class a mark | |
| 181 | # is in would mean evaluating that class's statements. | |
| 182 | 1 | private def keyword_flagged?(target) |
| 183 | 189 | return false if @keyword_flagged.empty? |
| 184 | ||
| 185 | 5 | names = [target.name, *Phlex::Compiler.aliases_sharing(target.component, target.name).map { |_owner, name| name }] |
| 186 | 6 | @keyword_flagged.any? { |flagged| names.include?(flagged.node) } |
| 187 | end | |
| 188 | ||
| 189 | 1 | private def compile_namespace(namespace, definitions) |
| 190 | 127 | probe = Phlex::Compiler.probe(namespace, usings:) |
| 191 | 127 | environments = {}.compare_by_identity |
| 192 | ||
| 193 | # Restoring the original definitions inlines nothing, so calls nothing. | |
| 194 | 127 | if @inline |
| 195 | 126 | if (redefined = Phlex::Compiler::CoreCalls.redefined) |
| 196 | 0 | @diagnostics.refuse(definitions.first.node, "#{redefined} is redefined, and compiled code calls it where the runtime calls the original")then never taken |
| 197 | end | |
| 198 | ||
| 199 | 126 | if (refined = Phlex::Compiler::CoreCalls.refined(probe.refinements, probe.component, (Class === probe.component) ? Phlex::Compiler.descendants_of(probe.component) : []))else never taken |
| 200 | 3 | @diagnostics.refuse(@usings.first&.node || definitions.first.node, "this file refines #{refined}, which compiled code calls where the runtime calls it unrefined")receiver never nil |
| 201 | end | |
| 202 | end | |
| 203 | ||
| 204 | 308 | definitions.group_by { |definition| @targets[definition.node.start_line].component }.filter_map do |component, component_definitions| |
| 205 | 124 | unless probe.component.equal?(component) |
| 206 | 0 | copied, others = component_definitions.partition { |definition| copied?(component, probe.component, definition.node.name) }then never taken |
| 207 | 0 | copied.each { |definition| @diagnostics.report(definition.node, "#{component}##{definition.node.name} is copied from #{probe.component}'s definition, so it's left alone") } |
| 208 | 0 | next if others.empty? |
| 209 | ||
| 210 | 0 | first = others.first.node |
| 211 | 0 | raise Phlex::Compiler::Error, "#{@path}:#{first.start_line} defines #{component}##{first.name}, but reopening its class and module statements reaches #{probe.component.inspect}" |
| 212 | end | |
| 213 | ||
| 214 | 124 | environment = environments[component] ||= Phlex::Compiler::Environment.new(component, standard_set: probe.set.equal?(::Set), method_resolver: probe.method_resolver, inline: @inline) |
| 215 | 124 | snippets = [] |
| 216 | 124 | visibilities = {} |
| 217 | ||
| 218 | 124 | component_definitions.each do |definition| |
| 219 | # The original is reinstalled only over a compiled method. One that | |
| 220 | # was never replaced is already live, and copying it could put it | |
| 221 | # below a `using` it was above. | |
| 222 | 184 | keep_uncompiled = @recompile && @targets[definition.node.start_line].replaced |
| 223 | 184 | compiled = Phlex::Compiler::MethodCompiler.new(environment, @path, diagnostics: @diagnostics).compile(definition.node, keep_uncompiled:) |
| 224 | 182 | next unless compiled |
| 225 | ||
| 226 | 114 | snippets << compiled |
| 227 | 114 | @compiled_lines << definition.node.start_line |
| 228 | 114 | visibilities[definition.node.name] = visibility_of(component, definition.node.name) |
| 229 | end | |
| 230 | ||
| 231 | 122 | Result.new(namespace:, component:, compiled_snippets: snippets.freeze, visibilities: visibilities.freeze, inlined: environment.inlined, inlined_svg: environment.inlined_svg) |
| 232 | end | |
| 233 | end | |
| 234 | ||
| 235 | # A method given to `define_method` from another module keeps that | |
| 236 | # module's source location, so its definition is reached by reopening | |
| 237 | # that module rather than the component. | |
| 238 | 0 | private def copied?(component, owner, name) |
| 239 | 0 | return false unless Module === owner |
| 240 | ||
| 241 | 0 | defining = Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(owner, name) |
| 242 | 0 | live = Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(component, name) |
| 243 | 0 | defining.source_location == live.source_location && defining.original_name == live.original_name |
| 244 | rescue NameError | |
| 245 | 0 | false |
| 246 | end | |
| 247 | ||
| 248 | 1 | private def visibility_of(component, name) |
| 249 | 114 | if component.private_method_defined?(name, false) |
| 250 | 3 | :private |
| 251 | 111 | elsif component.protected_method_defined?(name, false) |
| 252 | 0 | :protectedthen never taken |
| 253 | else | |
| 254 | 111 | :public |
| 255 | end | |
| 256 | end | |
| 257 | end |