Module: Transpec::Util
- Included in:
- DynamicAnalyzer::Rewriter, StaticContextInspector, Syntax::MethodStub, Syntax::OperatorMatcher, Syntax::RSpecConfigure, Syntax::Should
- Defined in:
- lib/transpec/util.rb
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
- .proc_literal?(node) ⇒ Boolean
Class Method Details
.const_name(node) ⇒ Object
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 |
# File 'lib/transpec/util.rb', line 22 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
47 48 49 |
# File 'lib/transpec/util.rb', line 47 def contain_here_document?(node) here_document?(node) || node.each_descendent_node.any? { |n| here_document?(n) } end |
.here_document?(node) ⇒ Boolean
40 41 42 43 44 45 |
# File 'lib/transpec/util.rb', line 40 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
51 52 53 54 55 |
# File 'lib/transpec/util.rb', line 51 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
57 58 59 60 61 62 63 64 65 66 67 |
# File 'lib/transpec/util.rb', line 57 def indentation_of_line(arg) range = case arg when AST::Node then arg.loc.expression when Parser::Source::Range then arg else fail ArgumentError, "Invalid argument #{arg}" end line = range.source_line /^(?<indentation>\s*)\S/ =~ line indentation end |
.proc_literal?(node) ⇒ Boolean
7 8 9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/transpec/util.rb', line 7 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 |