Module: Transpec::Util
- Included in:
- DynamicAnalyzer::Rewriter, StaticContextInspector, Syntax::CurrentExample, Syntax::ExampleGroup, Syntax::Have::SourceBuilder, Syntax::Its, Syntax::MethodStub, Syntax::OnelinerShould, Syntax::Operator, Syntax::Pending, Syntax::RSpecConfigure::ConfigModification, Syntax::Should, Syntax::ShouldReceive
- Defined in:
- lib/transpec/util.rb
Constant Summary collapse
- LITERAL_TYPES =
%w( true false nil int float str sym regexp ).map(&:to_sym).freeze
- WHITESPACES =
[' ', "\t"].freeze
Class Method Summary collapse
- .beginning_of_line_range(arg) ⇒ Object
- .block_node_taken_by_method(node) ⇒ Object
- .chainable_source(node) ⇒ Object
- .const_name(node) ⇒ Object
- .contain_here_document?(node) ⇒ Boolean
- .each_backward_chained_node(origin_node, mode = nil) {|origin_node| ... } ⇒ Object
- .each_forward_chained_node(origin_node, mode = nil) {|origin_node| ... } ⇒ Object
- .each_line_range(arg) ⇒ Object
- .expand_range_to_adjacent_whitespaces(range, direction = :both) ⇒ Object
- .find_consecutive_whitespace_position(source, origin, method) ⇒ Object
- .first_block_arg_name(block_node) ⇒ Object
- .here_document?(node) ⇒ Boolean
- .in_explicit_parentheses?(node) ⇒ Boolean
- .indentation_of_line(arg) ⇒ Object
- .line_range(arg) ⇒ Object
- .literal?(node) ⇒ Boolean
- .proc_literal?(node) ⇒ Boolean
- .range_from_arg(arg) ⇒ Object
Class Method Details
.beginning_of_line_range(arg) ⇒ Object
134 135 136 137 138 |
# File 'lib/transpec/util.rb', line 134 def beginning_of_line_range(arg) range = range_from_arg(arg) begin_pos = range.begin_pos - range.column Parser::Source::Range.new(range.source_buffer, begin_pos, begin_pos) end |
.block_node_taken_by_method(node) ⇒ Object
69 70 71 72 73 74 75 |
# File 'lib/transpec/util.rb', line 69 def block_node_taken_by_method(node) parent_node = node.parent return nil unless parent_node return nil unless parent_node.block_type? return nil unless parent_node.children.first.equal?(node) parent_node end |
.chainable_source(node) ⇒ Object
215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 |
# File 'lib/transpec/util.rb', line 215 def chainable_source(node) fail "Invalid argument #{node}" unless node.send_type? map = node.loc source = map.expression.source return source if map.selector.source.start_with?('[') arg_node = node.children[2] return source unless arg_node left_of_arg_range = map.selector.end.join(arg_node.loc.expression.begin) return source if left_of_arg_range.source.include?('(') if map.selector.source.match(/^\w/) relative_index = left_of_arg_range.begin_pos - map.expression.begin_pos source[relative_index, left_of_arg_range.length] = '(' source << ')' else "(#{source})" end end |
.const_name(node) ⇒ Object
30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/transpec/util.rb', line 30 def const_name(node) return nil if node.nil? || !node.const_type? const_names = [] const_node = node loop do namespace_node, name = *const_node const_names << name break unless namespace_node break unless namespace_node.is_a?(Parser::AST::Node) break if namespace_node.cbase_type? const_node = namespace_node end const_names.reverse.join('::') end |
.contain_here_document?(node) ⇒ Boolean
53 54 55 |
# File 'lib/transpec/util.rb', line 53 def contain_here_document?(node) node.each_node.any? { |n| here_document?(n) } end |
.each_backward_chained_node(origin_node, mode = nil) {|origin_node| ... } ⇒ Object
102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 |
# File 'lib/transpec/util.rb', line 102 def each_backward_chained_node(origin_node, mode = nil) return to_enum(__method__, origin_node, mode) unless block_given? yield origin_node if mode == :include_origin origin_node.each_ancestor(:send, :block).reduce(origin_node) do |child_node, parent_node| break unless parent_node.children.first.equal?(child_node) if mode == :child_as_second_arg yield parent_node, child_node else yield parent_node end parent_node end nil end |
.each_forward_chained_node(origin_node, mode = nil) {|origin_node| ... } ⇒ Object
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 |
# File 'lib/transpec/util.rb', line 77 def each_forward_chained_node(origin_node, mode = nil) return to_enum(__method__, origin_node, mode) unless block_given? yield origin_node if mode == :include_origin parent_node = origin_node loop do child_node = parent_node.children.first return if !child_node || !child_node.is_a?(AST::Node) return unless [:send, :block].include?(child_node.type) if mode == :parent_as_second_arg yield child_node, parent_node else yield child_node end parent_node = child_node end nil end |
.each_line_range(arg) ⇒ Object
145 146 147 148 149 150 151 152 153 |
# File 'lib/transpec/util.rb', line 145 def each_line_range(arg) multiline_range = range_from_arg(arg) range = line_range(multiline_range) while range.line <= multiline_range.end.line yield range range = line_range(range.end) end end |
.expand_range_to_adjacent_whitespaces(range, direction = :both) ⇒ Object
178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 |
# File 'lib/transpec/util.rb', line 178 def (range, direction = :both) source = range.source_buffer.source begin_pos = if [:both, :begin].include?(direction) find_consecutive_whitespace_position(source, range.begin_pos, :downto) else range.begin_pos end end_pos = if [:both, :end].include?(direction) find_consecutive_whitespace_position(source, range.end_pos - 1, :upto) + 1 else range.end_pos end Parser::Source::Range.new(range.source_buffer, begin_pos, end_pos) end |
.find_consecutive_whitespace_position(source, origin, method) ⇒ Object
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 |
# File 'lib/transpec/util.rb', line 195 def find_consecutive_whitespace_position(source, origin, method) from, to = case method when :upto [origin + 1, source.length - 1] when :downto [origin - 1, 0] else fail "Invalid method #{method}" end from.send(method, to).reduce(origin) do |previous_position, position| character = source[position] if WHITESPACES.include?(character) position else return previous_position end end end |
.first_block_arg_name(block_node) ⇒ Object
63 64 65 66 67 |
# File 'lib/transpec/util.rb', line 63 def first_block_arg_name(block_node) args_node = block_node.children[1] first_arg_node = args_node.children.first first_arg_node.children.first end |
.here_document?(node) ⇒ Boolean
48 49 50 51 |
# File 'lib/transpec/util.rb', line 48 def here_document?(node) return false unless [:str, :dstr].include?(node.type) node.loc.respond_to?(:heredoc_end) end |
.in_explicit_parentheses?(node) ⇒ Boolean
57 58 59 60 61 |
# File 'lib/transpec/util.rb', line 57 def in_explicit_parentheses?(node) return false unless node.begin_type? source = node.loc.expression.source source[0] == '(' && source[-1] == ')' end |
.indentation_of_line(arg) ⇒ Object
122 123 124 125 126 127 128 129 130 131 132 |
# File 'lib/transpec/util.rb', line 122 def indentation_of_line(arg) line = case arg when AST::Node then arg.loc.expression.source_line when Parser::Source::Range then arg.source_line when String then arg else fail ArgumentError, "Invalid argument #{arg}" end /^(?<indentation>\s*)\S/ =~ line indentation end |
.line_range(arg) ⇒ Object
140 141 142 143 |
# File 'lib/transpec/util.rb', line 140 def line_range(arg) range = range_from_arg(arg) beginning_of_line_range(range).resize(range.source_line.size + 1) end |
.literal?(node) ⇒ Boolean
163 164 165 166 167 168 169 170 171 172 173 174 175 176 |
# File 'lib/transpec/util.rb', line 163 def literal?(node) case node.type when :array, :irange, :erange node.children.all? { |n| literal?(n) } when :hash node.children.all? do |pair_node| pair_node.children.all? { |n| literal?(n) } end when *LITERAL_TYPES true else false end end |
.proc_literal?(node) ⇒ Boolean
15 16 17 18 19 20 21 22 23 24 25 26 27 28 |
# File 'lib/transpec/util.rb', line 15 def proc_literal?(node) return false unless node.block_type? send_node = node.children.first receiver_node, method_name, = *send_node if receiver_node.nil? || const_name(receiver_node) == 'Kernel' [:lambda, :proc].include?(method_name) elsif const_name(receiver_node) == 'Proc' method_name == :new else false end end |