lib/phlex/compiler/inline_scanner.rb

Lines

97%

34 of 35 covered

Branches

75%

3 of 4 covered

Methods

70%

12 of 17 covered

Not run
40
Never called
1# frozen_string_literal: true
2
3# Detects code that would change meaning if a block body were inlined into the
4# enclosing scope: `return` anywhere, `next`, `break` and `redo` belonging to
5# the block itself, or an assignment that would move a block local out of it.
61class Phlex::Compiler::InlineScanner < Refract::Visitor
71 def self.inlinable?(node)
8211 catch(:blocked) do
9211 new.visit(node)
10201 true
11 end
12 end
13
141 def initialize
15211 super
16211 @blocks = 0
17211 @loops = 0
18 end
19
201 visit Refract::ReturnNode do |node|
211 throw :blocked, false
22 end
23
241 [Refract::NextNode, Refract::BreakNode, Refract::RedoNode].each do |node_class|
253 visit node_class do |node|
2611 throw :blocked, false if @blocks == 0 && @loops == 0
276 super(node)
28 end
29 end
30
31 [
321 Refract::LocalVariableWriteNode,
33 Refract::LocalVariableTargetNode,
34 Refract::LocalVariableAndWriteNode,
35 Refract::LocalVariableOrWriteNode,
36 Refract::LocalVariableOperatorWriteNode,
37 ].each do |node_class|
385 visit node_class do |node|
394 throw :blocked, false if @blocks == 0else never taken
400 super(node)
41 end
42 end
43
44 # Before Ruby 4, a jump in a parameter default is only valid inside a block
45 # around the one it's in, so one there keeps the block being inlined a block.
461 [Refract::BlockNode, Refract::LambdaNode].each do |node_class|
472 visit node_class do |node|
4858 visit node.parameters
4958 @blocks += 1
5058 visit node.body
5158 @blocks -= 1
52 end
53 end
54
551 [Refract::WhileNode, Refract::UntilNode].each do |node_class|
562 visit node_class do |node|
571 visit node.predicate
581 @loops += 1
591 visit node.statements
601 @loops -= 1
61 end
62 end
63
64 # These bodies have scopes of their own, but their receivers are evaluated in
65 # the block's.
66
671 visit Refract::DefNode do |node|
681 visit node.receiver
69 end
70
711 visit Refract::SingletonClassNode do |node|
721 visit node.expression
73 end
74end