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)


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

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) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (Boolean)


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

def self.a_example_block?(block_ast)
  Solargraph::Rspec::EXAMPLE_METHODS.include?(method_with_block_name(block_ast))
end

.a_hook_block?(block_ast) ⇒ Boolean

Parameters:

  • ast (RubyVM::AbstractSyntaxTree::Node)

Returns:

  • (Boolean)


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

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)


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

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)


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

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)


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

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)


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

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