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
Phlex::Compiler::ReparseScanner::ParameterNames#visit_optional_parameter_nodePhlex::Compiler::ReparseScanner::ParameterNames#visit_rest_parameter_nodePhlex::Compiler::ReparseScanner::ParameterNames#visit_required_keyword_parameter_nodePhlex::Compiler::ReparseScanner::ParameterNames#visit_optional_keyword_parameter_nodePhlex::Compiler::ReparseScanner::ParameterNames#visit_keyword_rest_parameter_nodePhlex::Compiler::ReparseScanner::ParameterNames#visit_block_parameter_nodePhlex::Compiler::ReparseScanner::ParameterNames#visit_block_local_variable_nodePhlex::Compiler::ReparseScanner#visit_implicit_nodePhlex::Compiler::ReparseScanner#visit_it_local_variable_read_nodePhlex::Compiler::ReparseScanner#visit_local_variable_target_nodePhlex::Compiler::ReparseScanner#visit_local_variable_and_write_nodePhlex::Compiler::ReparseScanner#visit_local_variable_or_write_nodePhlex::Compiler::ReparseScanner#visit_lambda_nodePhlex::Compiler::ReparseScanner#visit_flip_flop_nodePhlex::Compiler::ReparseScanner#visit_interpolated_regular_expression_nodePhlex::Compiler::ReparseScanner#visit_interpolated_match_last_line_nodePhlex::Compiler::ReparseScanner#visit_def_nodePhlex::Compiler::ReparseScanner#visit_singleton_class_node
| 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. | |
| 8 | 1 | class Phlex::Compiler::ReparseScanner < Refract::Visitor |
| 9 | 10 | 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. | |
| 13 | 1 | class ParameterNames < Refract::Visitor |
| 14 | 1 | def self.of(parameters) |
| 15 | 182 | scanner = new |
| 16 | 182 | scanner.visit(parameters) |
| 17 | 182 | scanner.names |
| 18 | end | |
| 19 | ||
| 20 | 1 | attr_reader :names |
| 21 | ||
| 22 | 1 | def initialize |
| 23 | 182 | super |
| 24 | 182 | @names = Set.new |
| 25 | end | |
| 26 | ||
| 27 | [ | |
| 28 | 1 | 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| | |
| 37 | 8 | visit node_class do |node| |
| 38 | 31 | @names << node.name if node.nameelse never taken |
| 39 | end | |
| 40 | end | |
| 41 | end | |
| 42 | ||
| 43 | 1 | def self.conflict(block) |
| 44 | 111 | scanner = new(block.parameters) |
| 45 | 111 | scanner.visit(block.body) |
| 46 | 111 | scanner.conflict |
| 47 | end | |
| 48 | ||
| 49 | 1 | def initialize(parameters) |
| 50 | 111 | super() |
| 51 | 111 | @own = ParameterNames.of(parameters) |
| 52 | 111 | @own.merge(NUMBERED_PARAMETERS) if Refract::NumberedParametersNode === parametersthen never taken |
| 53 | 111 | @uses = {} |
| 54 | 111 | @stateful = nil |
| 55 | 111 | @scopes = [] |
| 56 | end | |
| 57 | ||
| 58 | 1 | def conflict |
| 59 | 111 | return @stateful if @statefulthen never taken |
| 60 | ||
| 61 | 111 | @uses.each do |name, reason| |
| 62 | 4 | return reason if @own.include?(name) |
| 63 | end | |
| 64 | ||
| 65 | 110 | nil |
| 66 | end | |
| 67 | ||
| 68 | 1 | visit Refract::CallNode do |node| |
| 69 | 176 | use(node.name, "`#{node.name}` is called before it's assigned") if node.variable_call |
| 70 | 176 | super(node) |
| 71 | end | |
| 72 | ||
| 73 | 0 | visit Refract::ImplicitNode do |node| |
| 74 | 0 | if Phlex::Compiler::BareCallScanner.bare?(node.value) |
| 75 | 0 | use(node.value.name, "`#{node.value.name}` is called before it's assigned") |
| 76 | end | |
| 77 | ||
| 78 | 0 | super(node) |
| 79 | end | |
| 80 | ||
| 81 | 0 | visit Refract::ItLocalVariableReadNode do |node| |
| 82 | 0 | use(:it, "`it` is assigned after it's read as a parameter") |
| 83 | end | |
| 84 | ||
| 85 | [ | |
| 86 | 1 | Refract::LocalVariableReadNode, |
| 87 | Refract::LocalVariableWriteNode, | |
| 88 | Refract::LocalVariableTargetNode, | |
| 89 | Refract::LocalVariableAndWriteNode, | |
| 90 | Refract::LocalVariableOrWriteNode, | |
| 91 | Refract::LocalVariableOperatorWriteNode, | |
| 92 | ].each do |node_class| | |
| 93 | 6 | visit node_class do |node| |
| 94 | 69 | super(node) |
| 95 | 69 | owner = @scopes.size - node.depth |
| 96 | ||
| 97 | 69 | if owner == 0 |
| 98 | 28 | @own << node.name |
| 99 | 41 | elsif owner > 0 && !@scopes[owner - 1].include?(node.name) |
| 100 | 0 | 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 | ||
| 105 | 1 | [Refract::BlockNode, Refract::LambdaNode].each do |node_class| |
| 106 | 2 | visit node_class do |node| |
| 107 | 71 | @scopes.push(ParameterNames.of(node.parameters)) |
| 108 | 71 | super(node) |
| 109 | 71 | @scopes.pop |
| 110 | end | |
| 111 | end | |
| 112 | ||
| 113 | 0 | visit Refract::FlipFlopNode do |node| |
| 114 | 0 | @stateful ||= "it has a flip-flop" |
| 115 | 0 | super(node) |
| 116 | end | |
| 117 | ||
| 118 | 1 | [Refract::InterpolatedRegularExpressionNode, Refract::InterpolatedMatchLastLineNode].each do |node_class| |
| 119 | 0 | visit node_class do |node| |
| 120 | 0 | @stateful ||= "a regular expression in it is interpolated only once" if node.once |
| 121 | 0 | super(node) |
| 122 | end | |
| 123 | end | |
| 124 | ||
| 125 | 1 | [Refract::DefNode, Refract::SingletonClassNode].each do |node_class| |
| 126 | 0 | visit node_class do |node| |
| 127 | 0 | @stateful ||= "it defines methods" |
| 128 | end | |
| 129 | end | |
| 130 | ||
| 131 | 1 | private def use(name, reason) |
| 132 | 4 | @uses[name] ||= reason |
| 133 | end | |
| 134 | end |