lib/phlex/compiler/reparse_scanner.rb

Lines

76%

49 of 64 covered

Branches

55%

10 of 18 covered

Methods

40%

12 of 30 covered

Not run
73-757881-82100113-115119-121126-127
Never called
1# frozen_string_literal: true
2
3# Finds code in a block body that would behave differently if Ruby parsed it a
4# second time with the block's locals already declared, as it does the original
5# body kept after the compiled one. Code with state of its own, a flip-flop, a
6# regular expression interpolated only once or a method definition, would also
7# be split between the two copies.
81class Phlex::Compiler::ReparseScanner < Refract::Visitor
910 NUMBERED_PARAMETERS = (1..9).map { |n| :"_#{n}" }.freeze
10
11 # The names a parameter list declares as parameters, but not locals assigned
12 # in their defaults.
131 class ParameterNames < Refract::Visitor
141 def self.of(parameters)
15182 scanner = new
16182 scanner.visit(parameters)
17182 scanner.names
18 end
19
201 attr_reader :names
21
221 def initialize
23182 super
24182 @names = Set.new
25 end
26
27 [
281 Refract::RequiredParameterNode,
29 Refract::OptionalParameterNode,
30 Refract::RestParameterNode,
31 Refract::RequiredKeywordParameterNode,
32 Refract::OptionalKeywordParameterNode,
33 Refract::KeywordRestParameterNode,
34 Refract::BlockParameterNode,
35 Refract::BlockLocalVariableNode,
36 ].each do |node_class|
378 visit node_class do |node|
3831 @names << node.name if node.nameelse never taken
39 end
40 end
41 end
42
431 def self.conflict(block)
44111 scanner = new(block.parameters)
45111 scanner.visit(block.body)
46111 scanner.conflict
47 end
48
491 def initialize(parameters)
50111 super()
51111 @own = ParameterNames.of(parameters)
52111 @own.merge(NUMBERED_PARAMETERS) if Refract::NumberedParametersNode === parametersthen never taken
53111 @uses = {}
54111 @stateful = nil
55111 @scopes = []
56 end
57
581 def conflict
59111 return @stateful if @statefulthen never taken
60
61111 @uses.each do |name, reason|
624 return reason if @own.include?(name)
63 end
64
65110 nil
66 end
67
681 visit Refract::CallNode do |node|
69176 use(node.name, "`#{node.name}` is called before it's assigned") if node.variable_call
70176 super(node)
71 end
72
730 visit Refract::ImplicitNode do |node|
740 if Phlex::Compiler::BareCallScanner.bare?(node.value)
750 use(node.value.name, "`#{node.value.name}` is called before it's assigned")
76 end
77
780 super(node)
79 end
80
810 visit Refract::ItLocalVariableReadNode do |node|
820 use(:it, "`it` is assigned after it's read as a parameter")
83 end
84
85 [
861 Refract::LocalVariableReadNode,
87 Refract::LocalVariableWriteNode,
88 Refract::LocalVariableTargetNode,
89 Refract::LocalVariableAndWriteNode,
90 Refract::LocalVariableOrWriteNode,
91 Refract::LocalVariableOperatorWriteNode,
92 ].each do |node_class|
936 visit node_class do |node|
9469 super(node)
9569 owner = @scopes.size - node.depth
96
9769 if owner == 0
9828 @own << node.name
9941 elsif owner > 0 && !@scopes[owner - 1].include?(node.name)
1000 use(node.name, "`#{node.name}` is a local of both the block and a block inside it")then never taken
101 end
102 end
103 end
104
1051 [Refract::BlockNode, Refract::LambdaNode].each do |node_class|
1062 visit node_class do |node|
10771 @scopes.push(ParameterNames.of(node.parameters))
10871 super(node)
10971 @scopes.pop
110 end
111 end
112
1130 visit Refract::FlipFlopNode do |node|
1140 @stateful ||= "it has a flip-flop"
1150 super(node)
116 end
117
1181 [Refract::InterpolatedRegularExpressionNode, Refract::InterpolatedMatchLastLineNode].each do |node_class|
1190 visit node_class do |node|
1200 @stateful ||= "a regular expression in it is interpolated only once" if node.once
1210 super(node)
122 end
123 end
124
1251 [Refract::DefNode, Refract::SingletonClassNode].each do |node_class|
1260 visit node_class do |node|
1270 @stateful ||= "it defines methods"
128 end
129 end
130
1311 private def use(name, reason)
1324 @uses[name] ||= reason
133 end
134end