Class: RubyLsp::Listeners::SpecStyle

Inherits:
TestDiscovery show all
Defined in:
lib/ruby_lsp/listeners/spec_style.rb

Defined Under Namespace

Classes: ClassGroup, DescribeGroup, Group

Constant Summary

Constants inherited from TestDiscovery

TestDiscovery::DYNAMIC_REFERENCE_MARKER

Instance Method Summary collapse

Methods included from Requests::Support::Common

#categorized_markdown_from_index_entries, #constant_name, #create_code_lens, #each_constant_path_part, #kind_for_entry, #markdown_from_index_entries, #namespace_constant_name, #not_in_dependencies?, #range_from_location, #range_from_node, #self_receiver?

Constructor Details

#initialize(response_builder, global_state, dispatcher, uri) ⇒ SpecStyle

: (ResponseBuilders::TestCollection, GlobalState, Prism::Dispatcher, URI::Generic) -> void



21
22
23
24
25
26
27
28
29
30
31
32
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 21

def initialize(response_builder, global_state, dispatcher, uri)
  super(response_builder, global_state, uri)

  @spec_group_id_stack = [] #: Array[Group?]

  register_events(
    dispatcher,
    :on_class_node_enter,
    :on_call_node_enter,
    :on_call_node_leave,
  )
end

Instance Method Details

#on_call_node_enter(node) ⇒ Object

: (Prism::CallNode) -> void



60
61
62
63
64
65
66
67
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 60

def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
  case node.name
  when :describe
    handle_describe(node)
  when :it, :specify
    handle_example(node) if in_spec_context?
  end
end

#on_call_node_leave(node) ⇒ Object

: (Prism::CallNode) -> void



70
71
72
73
74
75
76
77
78
79
80
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 70

def on_call_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
  return unless node.name == :describe && !node.receiver

  current_group = @spec_group_id_stack.last
  return unless current_group.is_a?(DescribeGroup)

  description = extract_description(node)
  return unless description && current_group.id.end_with?(description)

  @spec_group_id_stack.pop
end

#on_class_node_enter(node) ⇒ Object

: (Prism::ClassNode) -> void



35
36
37
38
39
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 35

def on_class_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
  with_test_ancestor_tracking(node) do |name, ancestors|
    @spec_group_id_stack << (ancestors.include?("Minitest::Spec") ? ClassGroup.new(name) : nil)
  end
end

#on_class_node_leave(node) ⇒ Object

: (Prism::ClassNode) -> void



42
43
44
45
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 42

def on_class_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
  @spec_group_id_stack.pop
  super
end

#on_module_node_enter(node) ⇒ Object

: (Prism::ModuleNode) -> void



48
49
50
51
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 48

def on_module_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
  @spec_group_id_stack << nil
  super
end

#on_module_node_leave(node) ⇒ Object

: (Prism::ModuleNode) -> void



54
55
56
57
# File 'lib/ruby_lsp/listeners/spec_style.rb', line 54

def on_module_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
  @spec_group_id_stack.pop
  super
end