Skip to content

Commit 85dad40

Browse files
committed
Only compile methods that are still owned by the class in the file
1 parent 3afb750 commit 85dad40

File tree

5 files changed

+23
-8
lines changed

5 files changed

+23
-8
lines changed

lib/phlex.rb

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,8 @@ module Phlex
2727
CACHED_FILES = Set.new
2828
ATTRIBUTE_CACHE = FIFO.new
2929

30+
UNBOUND_INSTANCE_METHOD_METHOD = Module.instance_method(:instance_method)
31+
3032
def self.__expand_attribute_cache__(file_path)
3133
unless CACHED_FILES.include?(file_path)
3234
CACHED_FILES << file_path

lib/phlex/compiler.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ def self.compile_file(path)
2727

2828
starting_line = last_line + 1
2929

30-
results = FileCompiler.new.compile(refract)
30+
results = FileCompiler.new(path).compile(refract)
3131

3232
result = Refract::StatementsNode.new(
3333
body: results.map do |result|

lib/phlex/compiler/class_compiler.rb

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,10 @@
11
# frozen_string_literal: true
22

33
class Phlex::Compiler::ClassCompiler < Refract::Visitor
4-
def initialize(component)
4+
def initialize(component, path)
55
super()
66
@component = component
7+
@path = path
78
@compiled_snippets = []
89
end
910

@@ -16,6 +17,17 @@ def compile(node)
1617
return if node.name == :initialize
1718
return if node.receiver
1819

20+
method = begin
21+
Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(@component, node.name)
22+
rescue NameError
23+
nil
24+
end
25+
26+
return unless method
27+
path, lineno = method.source_location
28+
return unless @path == path
29+
return unless node.start_line == lineno
30+
1931
@compiled_snippets << Phlex::Compiler::MethodCompiler.new(
2032
@component
2133
).compile(node)

lib/phlex/compiler/file_compiler.rb

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,9 @@
33
class Phlex::Compiler::FileCompiler < Refract::Visitor
44
Result = Data.define(:namespace, :compiled_snippets)
55

6-
def initialize
7-
super
6+
def initialize(path)
7+
super()
8+
@path = path
89
@current_namespace = []
910
@results = []
1011
end
@@ -32,7 +33,7 @@ def compile(node)
3233
if Class === const && Phlex::SGML > const
3334
@results << Result.new(
3435
namespace: @current_namespace.dup.freeze,
35-
compiled_snippets: Phlex::Compiler::ClassCompiler.new(const).compile(node)
36+
compiled_snippets: Phlex::Compiler::ClassCompiler.new(const, @path).compile(node)
3637
)
3738
else
3839
super(node)

lib/phlex/compiler/method_compiler.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -430,7 +430,7 @@ def compile_raw_helper(node)
430430

431431
private def standard_element?(node)
432432
if (tag = Phlex::HTML::StandardElements.__registered_elements__[node.name]) &&
433-
(Phlex::HTML::StandardElements == @component.instance_method(node.name).owner)
433+
(Phlex::HTML::StandardElements == Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(@component, node.name).owner)
434434

435435
tag
436436
else
@@ -440,7 +440,7 @@ def compile_raw_helper(node)
440440

441441
private def void_element?(node)
442442
if (tag = Phlex::HTML::VoidElements.__registered_elements__[node.name]) &&
443-
(Phlex::HTML::VoidElements == @component.instance_method(node.name).owner)
443+
(Phlex::HTML::VoidElements == Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(@component, node.name).owner)
444444

445445
tag
446446
else
@@ -506,7 +506,7 @@ def compile_raw_helper(node)
506506

507507
ALLOWED_OWNERS = Set[Phlex::SGML, Phlex::HTML, Phlex::SVG]
508508
private def own_method_without_scope?(node)
509-
ALLOWED_OWNERS.include?(@component.instance_method(node.name).owner)
509+
ALLOWED_OWNERS.include?(Phlex::UNBOUND_INSTANCE_METHOD_METHOD.bind_call(@component, node.name).owner)
510510
end
511511

512512
private def state_local

0 commit comments

Comments
 (0)