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
Phlex::Compiler::InlineScanner#visit_local_variable_operator_write_nodePhlex::Compiler::InlineScanner#visit_local_variable_or_write_nodePhlex::Compiler::InlineScanner#visit_local_variable_and_write_nodePhlex::Compiler::InlineScanner#visit_local_variable_target_nodePhlex::Compiler::InlineScanner#visit_until_node
| 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. | |
| 6 | 1 | class Phlex::Compiler::InlineScanner < Refract::Visitor |
| 7 | 1 | def self.inlinable?(node) |
| 8 | 211 | catch(:blocked) do |
| 9 | 211 | new.visit(node) |
| 10 | 201 | true |
| 11 | end | |
| 12 | end | |
| 13 | ||
| 14 | 1 | def initialize |
| 15 | 211 | super |
| 16 | 211 | @blocks = 0 |
| 17 | 211 | @loops = 0 |
| 18 | end | |
| 19 | ||
| 20 | 1 | visit Refract::ReturnNode do |node| |
| 21 | 1 | throw :blocked, false |
| 22 | end | |
| 23 | ||
| 24 | 1 | [Refract::NextNode, Refract::BreakNode, Refract::RedoNode].each do |node_class| |
| 25 | 3 | visit node_class do |node| |
| 26 | 11 | throw :blocked, false if @blocks == 0 && @loops == 0 |
| 27 | 6 | super(node) |
| 28 | end | |
| 29 | end | |
| 30 | ||
| 31 | [ | |
| 32 | 1 | Refract::LocalVariableWriteNode, |
| 33 | Refract::LocalVariableTargetNode, | |
| 34 | Refract::LocalVariableAndWriteNode, | |
| 35 | Refract::LocalVariableOrWriteNode, | |
| 36 | Refract::LocalVariableOperatorWriteNode, | |
| 37 | ].each do |node_class| | |
| 38 | 5 | visit node_class do |node| |
| 39 | 4 | throw :blocked, false if @blocks == 0else never taken |
| 40 | 0 | 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. | |
| 46 | 1 | [Refract::BlockNode, Refract::LambdaNode].each do |node_class| |
| 47 | 2 | visit node_class do |node| |
| 48 | 58 | visit node.parameters |
| 49 | 58 | @blocks += 1 |
| 50 | 58 | visit node.body |
| 51 | 58 | @blocks -= 1 |
| 52 | end | |
| 53 | end | |
| 54 | ||
| 55 | 1 | [Refract::WhileNode, Refract::UntilNode].each do |node_class| |
| 56 | 2 | visit node_class do |node| |
| 57 | 1 | visit node.predicate |
| 58 | 1 | @loops += 1 |
| 59 | 1 | visit node.statements |
| 60 | 1 | @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 | ||
| 67 | 1 | visit Refract::DefNode do |node| |
| 68 | 1 | visit node.receiver |
| 69 | end | |
| 70 | ||
| 71 | 1 | visit Refract::SingletonClassNode do |node| |
| 72 | 1 | visit node.expression |
| 73 | end | |
| 74 | end |