Module: Transpec::Util
- Included in:
- DynamicAnalyzer::Rewriter, StaticContextInspector, Syntax::Its, Syntax::MethodStub, Syntax::OperatorMatcher, Syntax::RSpecConfigure, Syntax::Should
- Defined in:
- lib/transpec/util.rb
Constant Summary collapse
- LITERAL_TYPES =
%w( true false nil int float str sym regexp ).map(&:to_sym).freeze
Class Method Summary collapse
- .const_name(node) ⇒ Object
- .contain_here_document?(node) ⇒ Boolean
- .here_document?(node) ⇒ Boolean
- .in_parentheses?(node) ⇒ Boolean
- .indentation_of_line(arg) ⇒ Object
- .literal?(node) ⇒ Boolean
- .proc_literal?(node) ⇒ Boolean
Class Method Details
.const_name(node) ⇒ Object
28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 |
# File 'lib/transpec/util.rb', line 28 def const_name(node) return nil if node.nil? || node.type != :const 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.type == :cbase 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) here_document?(node) || node.each_descendent_node.any? { |n| here_document?(n) } end |
.here_document?(node) ⇒ Boolean
46 47 48 49 50 51 |
# File 'lib/transpec/util.rb', line 46 def here_document?(node) return false unless [:str, :dstr].include?(node.type) map = node.loc return false if !map.respond_to?(:begin) || map.begin.nil? map.begin.source.start_with?('<<') end |
.in_parentheses?(node) ⇒ Boolean
57 58 59 60 61 |
# File 'lib/transpec/util.rb', line 57 def in_parentheses?(node) return false unless node.type == :begin source = node.loc.expression.source source[0] == '(' && source[-1] == ')' end |
.indentation_of_line(arg) ⇒ Object
63 64 65 66 67 68 69 70 71 72 73 |
# File 'lib/transpec/util.rb', line 63 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 |
.literal?(node) ⇒ Boolean
75 76 77 78 79 80 81 82 83 84 85 86 87 88 |
# File 'lib/transpec/util.rb', line 75 def literal?(node) case node.type when *LITERAL_TYPES true 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 else false end end |
.proc_literal?(node) ⇒ Boolean
13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
# File 'lib/transpec/util.rb', line 13 def proc_literal?(node) return false unless node.type == :block 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 |