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.
91class Phlex::Compiler::FileCompiler < Refract::Visitor
101 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.
131 Scoped = Data.define(:namespace, :node)
14
151 def initialize(path, targets:, diagnostics: Phlex::Compiler::Diagnostics.new(path), recompile: false, inline: true)
16133 super()
17133 @path = path
18133 @targets = targets
19133 @diagnostics = diagnostics
20133 @recompile = recompile
21133 @inline = inline
22133 @current_namespace = []
23133 @definitions = []
24133 @usings = []
25133 @keyword_flagged = []
26133 @compiled_lines = []
27 end
28
291 def compile(node)
30133 @top_level = node.statements
31133 visit(node)
32133 results = definitions_by_namespace.flat_map do |namespace, definitions|
33127 compile_namespace(namespace, definitions)
34 end
35121 return [].freeze unless refinements_reproducible?then never taken
36
37121 results.freeze
38 end
39
40 # The `using` statements to put before the compiled definitions.
411 def usings
42221 @usings.map(&:node)
43 end
44
451 visit Refract::ModuleNode do |node|
4691 @current_namespace.push(node)
4791 super(node)
4891 @current_namespace.pop
49 end
50
511 visit Refract::ClassNode do |node|
52158 @current_namespace.push(node)
53158 super(node)
54158 @current_namespace.pop
55 end
56
571 visit Refract::DefNode do |node|
58331 @definitions << Scoped.new(namespace: @current_namespace.dup.freeze, node:) unless node.receiver
59 end
60
611 visit Refract::BlockNode do |node|
6233 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.
701 visit Refract::CallNode do |node|
71184 if node.receiver.nil?
72200 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.
7612 if @stack[-2].equal?(@top_level) && node.arguments&.arguments in [Refract::ConstantReadNode | Refract::ConstantPathNode]receiver never nil
7711 @usings << Scoped.new(namespace: @current_namespace.dup.freeze, node:)
78 else
791 @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
824 @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
87184 super(node)
88 end
89
901 private def ruby2_keywords_names(node)
912 (node.arguments&.arguments || []).map do |argument|receiver never nil
922 case argument
931 in Refract::SymbolNode then argument.unescaped.to_sym
941 in Refract::DefNode then argument.name
950 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.
1031 private def refinements_reproducible?
104121 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.
107129 partial = @usings.find { |using| !using.namespace.empty? || (first_definition && using.node.start_line >= first_definition) }
108121 return true unless partialelse never taken
109
1100 @diagnostics.refuse(partial.node, "this `using` applies to only part of the file, which the compiler can't reproduce")
1110 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.
1221 private def definitions_by_namespace
123133 orphaned = @targets.values.reject do |target|
124739 @definitions.any? { |definition| definition.node.start_line == target.line && definition.node.name == target.name }
125 end
126
127133 orphaned.each do |target|
12816 next unless target.replaced
129
1301 @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
133132 unique_definitions.group_by(&:namespace).filter_map do |namespace, definitions|
134154 targeted, compiled, unmatched = partition_by_target(definitions)
135
136155 compiled.each { |definition| @diagnostics.report(definition.node, "#{definition.node.name} is already compiled") }
137
138153 unmatched.each do |definition|
139138 next unless orphaned.any? { |target| target.name == definition.node.name }
140
1415 @diagnostics.refuse(definition.node, "no live method is defined at this line, so the file has changed since it was loaded")
142 end
143
144149 [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.
1511 private def unique_definitions
152410 @definitions.group_by { |definition| definition.node.start_line }.flat_map do |line, definitions|
153268 next definitions if definitions.one? || !@targets.key?(line)
154
1552 @diagnostics.refuse(definitions.first.node, "more than one method is defined on this line, so the compiler can't tell which definition is live")
1561 []
157 end
158 end
159
1601 private def partition_by_target(definitions)
161154 definitions.each_with_object([[], [], []]) do |definition, (targeted, compiled, unmatched)|
162264 target = @targets[definition.node.start_line]
163
164264 if target.nil? || target.name != definition.node.name
16573 unmatched << definition
166191 elsif target.compiled
1672 compiled << definition
168189 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.
1711 @diagnostics.refuse(definition.node, "#{definition.node.name} is marked ruby2_keywords in this file, which the compiler can't preserve")
172 else
173188 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.
1821 private def keyword_flagged?(target)
183189 return false if @keyword_flagged.empty?
184
1855 names = [target.name, *Phlex::Compiler.aliases_sharing(target.component, target.name).map { |_owner, name| name }]
1866 @keyword_flagged.any? { |flagged| names.include?(flagged.node) }
187 end
188
1891 private def compile_namespace(namespace, definitions)
190127 probe = Phlex::Compiler.probe(namespace, usings:)
191127 environments = {}.compare_by_identity
192
193 # Restoring the original definitions inlines nothing, so calls nothing.
194127 if @inline
195126 if (redefined = Phlex::Compiler::CoreCalls.redefined)
1960 @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
199126 if (refined = Phlex::Compiler::CoreCalls.refined(probe.refinements, probe.component, (Class === probe.component) ? Phlex::Compiler.descendants_of(probe.component) : []))else never taken
2003 @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
204308 definitions.group_by { |definition| @targets[definition.node.start_line].component }.filter_map do |component, component_definitions|
205124 unless probe.component.equal?(component)
2060 copied, others = component_definitions.partition { |definition| copied?(component, probe.component, definition.node.name) }then never taken
2070 copied.each { |definition| @diagnostics.report(definition.node, "#{component}##{definition.node.name} is copied from #{probe.component}'s definition, so it's left alone") }
2080 next if others.empty?
209
2100 first = others.first.node
2110 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
214124 environment = environments[component] ||= Phlex::Compiler::Environment.new(component, standard_set: probe.set.equal?(::Set), method_resolver: probe.method_resolver, inline: @inline)
215124 snippets = []
216124 visibilities = {}
217
218124 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.
222184 keep_uncompiled = @recompile && @targets[definition.node.start_line].replaced
223184 compiled = Phlex::Compiler::MethodCompiler.new(environment, @path, diagnostics: @diagnostics).compile(definition.node, keep_uncompiled:)
224182 next unless compiled
225
226114 snippets << compiled
227114 @compiled_lines << definition.node.start_line
228114 visibilities[definition.node.name] = visibility_of(component, definition.node.name)
229 end
230
231122 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.
2380 private def copied?(component, owner, name)
2390 return false unless Module === owner
240
2410 defining = Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(owner, name)
2420 live = Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(component, name)
2430 defining.source_location == live.source_location && defining.original_name == live.original_name
244 rescue NameError
2450 false
246 end
247
2481 private def visibility_of(component, name)
249114 if component.private_method_defined?(name, false)
2503 :private
251111 elsif component.protected_method_defined?(name, false)
2520 :protectedthen never taken
253 else
254111 :public
255 end
256 end
257end