Class: Solargraph::Rspec::SpecWalker::NodeTypes

Inherits:
Object
  • Object
show all
Defined in:
lib/solargraph/rspec/spec_walker/node_types.rb

Class Method Summary collapse

Class Method Details

.a_block?(ast) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (Boolean)


9
10
11
12
13
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 9

def self.a_block?(ast)
  return false unless ast.is_a?(RubyVM::AbstractSyntaxTree::Node)

  %i[ITER LAMBDA].include?(ast.type)
end

.a_constant?(ast) ⇒ Boolean

Returns:

  • (Boolean)


47
48
49
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 47

def self.a_constant?(ast)
  %i[CONST COLON2].include?(ast.type)
end

.a_context_block?(block_ast) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (Boolean)


17
18
19
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 17

def self.a_context_block?(block_ast)
  Solargraph::Rspec::CONTEXT_METHODS.include?(method_with_block_name(block_ast))
end

.a_example_block?(block_ast, config) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)
  • config (Config)

Returns:

  • (Boolean)


30
31
32
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 30

def self.a_example_block?(block_ast, config)
  config.example_methods.map(&:to_s).include?(method_with_block_name(block_ast))
end

.a_hook_block?(block_ast) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (Boolean)


43
44
45
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 43

def self.a_hook_block?(block_ast)
  Solargraph::Rspec::HOOK_METHODS.include?(method_with_block_name(block_ast))
end

.a_let_block?(block_ast, config) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)
  • config (Config)

Returns:

  • (Boolean)


37
38
39
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 37

def self.a_let_block?(block_ast, config)
  config.let_methods.map(&:to_s).include?(method_with_block_name(block_ast))
end

.a_subject_block?(block_ast) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (Boolean)


23
24
25
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 23

def self.a_subject_block?(block_ast)
  Solargraph::Rspec::SUBJECT_METHODS.include?(method_with_block_name(block_ast))
end

.context_description_node(block_ast) ⇒ RubyVM::AbstractSyntaxTree::Node

Parameters:

  • block_ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (RubyVM::AbstractSyntaxTree::Node)


64
65
66
67
68
69
70
71
72
73
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 64

def self.context_description_node(block_ast)
  return nil unless a_context_block?(block_ast)

  case block_ast.children[0].type
  when :CALL # RSpec.describe "something" do end
    block_ast.children[0].children[2].children[0]
  when :FCALL # describe "something" do end
    block_ast.children[0].children[1].children[0]
  end
end

.let_method_name(block_ast) ⇒ String

Parameters:

  • block_ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (String)


77
78
79
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 77

def self.let_method_name(block_ast)
  block_ast.children[0].children[1]&.children&.[](0)&.children&.[](0)&.to_s
end

.method_with_block_name(block_ast) ⇒ String?

Parameters:

  • block_ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (String, nil)


53
54
55
56
57
58
59
60
# File 'lib/solargraph/rspec/spec_walker/node_types.rb', line 53

def self.method_with_block_name(block_ast)
  return nil unless a_block?(block_ast)

  method_call = %i[CALL FCALL].include?(block_ast.children[0].type)
  return nil unless method_call

  block_ast.children[0].children.select { |child| child.is_a?(Symbol) }.first&.to_s
end