|
| 1 | +# typed: strict |
| 2 | +# frozen_string_literal: true |
| 3 | + |
| 4 | +module RubyLsp |
| 5 | + module Listeners |
| 6 | + class TestStyle |
| 7 | + extend T::Sig |
| 8 | + include Requests::Support::Common |
| 9 | + |
| 10 | + ACCESS_MODIFIERS = [:public, :private, :protected].freeze |
| 11 | + DYNAMIC_REFERENCE_MARKER = "<dynamic_reference>" |
| 12 | + |
| 13 | + sig do |
| 14 | + params( |
| 15 | + response_builder: ResponseBuilders::TestCollection, |
| 16 | + global_state: GlobalState, |
| 17 | + dispatcher: Prism::Dispatcher, |
| 18 | + uri: URI::Generic, |
| 19 | + ).void |
| 20 | + end |
| 21 | + def initialize(response_builder, global_state, dispatcher, uri) |
| 22 | + @response_builder = response_builder |
| 23 | + @global_state = global_state |
| 24 | + @uri = uri |
| 25 | + @index = T.let(global_state.index, RubyIndexer::Index) |
| 26 | + |
| 27 | + @visibility_stack = T.let([:public], T::Array[Symbol]) |
| 28 | + @nesting = T.let([], T::Array[String]) |
| 29 | + |
| 30 | + dispatcher.register( |
| 31 | + self, |
| 32 | + :on_class_node_enter, |
| 33 | + :on_class_node_leave, |
| 34 | + :on_module_node_enter, |
| 35 | + :on_module_node_leave, |
| 36 | + :on_def_node_enter, |
| 37 | + :on_call_node_enter, |
| 38 | + :on_call_node_leave, |
| 39 | + ) |
| 40 | + end |
| 41 | + |
| 42 | + sig { params(node: Prism::ClassNode).void } |
| 43 | + def on_class_node_enter(node) |
| 44 | + @visibility_stack << :public |
| 45 | + name = constant_name(node.constant_path) |
| 46 | + name ||= name_with_dynamic_reference(node.constant_path) |
| 47 | + |
| 48 | + fully_qualified_name = RubyIndexer::Index.actual_nesting(@nesting, name).join("::") |
| 49 | + |
| 50 | + attached_ancestors = begin |
| 51 | + @index.linearized_ancestors_of(fully_qualified_name) |
| 52 | + rescue RubyIndexer::Index::NonExistingNamespaceError |
| 53 | + # When there are dynamic parts in the constant path, we will not have indexed the namespace. We can still |
| 54 | + # provide test functionality if the class inherits directly from Test::Unit::TestCase or Minitest::Test |
| 55 | + [node.superclass&.slice].compact |
| 56 | + end |
| 57 | + |
| 58 | + if attached_ancestors.include?("Test::Unit::TestCase") || |
| 59 | + non_declarative_minitest?(attached_ancestors, fully_qualified_name) |
| 60 | + |
| 61 | + @response_builder.add(Requests::Support::TestItem.new( |
| 62 | + fully_qualified_name, |
| 63 | + fully_qualified_name, |
| 64 | + @uri, |
| 65 | + range_from_node(node), |
| 66 | + )) |
| 67 | + end |
| 68 | + |
| 69 | + @nesting << name |
| 70 | + end |
| 71 | + |
| 72 | + sig { params(node: Prism::ModuleNode).void } |
| 73 | + def on_module_node_enter(node) |
| 74 | + @visibility_stack << :public |
| 75 | + |
| 76 | + name = constant_name(node.constant_path) |
| 77 | + name ||= name_with_dynamic_reference(node.constant_path) |
| 78 | + |
| 79 | + @nesting << name |
| 80 | + end |
| 81 | + |
| 82 | + sig { params(node: Prism::ModuleNode).void } |
| 83 | + def on_module_node_leave(node) |
| 84 | + @visibility_stack.pop |
| 85 | + @nesting.pop |
| 86 | + end |
| 87 | + |
| 88 | + sig { params(node: Prism::ClassNode).void } |
| 89 | + def on_class_node_leave(node) |
| 90 | + @visibility_stack.pop |
| 91 | + @nesting.pop |
| 92 | + end |
| 93 | + |
| 94 | + sig { params(node: Prism::DefNode).void } |
| 95 | + def on_def_node_enter(node) |
| 96 | + return if @visibility_stack.last != :public |
| 97 | + |
| 98 | + name = node.name.to_s |
| 99 | + return unless name.start_with?("test_") |
| 100 | + |
| 101 | + current_group_name = RubyIndexer::Index.actual_nesting(@nesting, nil).join("::") |
| 102 | + |
| 103 | + # If we're finding a test method, but for the wrong framework, then the group test item will not have been |
| 104 | + # previously pushed and thus we return early and avoid adding items for a framework this listener is not |
| 105 | + # interested in |
| 106 | + test_item = @response_builder[current_group_name] |
| 107 | + return unless test_item |
| 108 | + |
| 109 | + test_item.add(Requests::Support::TestItem.new( |
| 110 | + "#{current_group_name}##{name}", |
| 111 | + name, |
| 112 | + @uri, |
| 113 | + range_from_node(node), |
| 114 | + )) |
| 115 | + end |
| 116 | + |
| 117 | + sig { params(node: Prism::CallNode).void } |
| 118 | + def on_call_node_enter(node) |
| 119 | + name = node.name |
| 120 | + return unless ACCESS_MODIFIERS.include?(name) |
| 121 | + |
| 122 | + @visibility_stack << name |
| 123 | + end |
| 124 | + |
| 125 | + sig { params(node: Prism::CallNode).void } |
| 126 | + def on_call_node_leave(node) |
| 127 | + name = node.name |
| 128 | + return unless ACCESS_MODIFIERS.include?(name) |
| 129 | + return unless node.arguments&.arguments |
| 130 | + |
| 131 | + @visibility_stack.pop |
| 132 | + end |
| 133 | + |
| 134 | + private |
| 135 | + |
| 136 | + sig { params(attached_ancestors: T::Array[String], fully_qualified_name: String).returns(T::Boolean) } |
| 137 | + def non_declarative_minitest?(attached_ancestors, fully_qualified_name) |
| 138 | + return false unless attached_ancestors.include?("Minitest::Test") |
| 139 | + |
| 140 | + # We only support regular Minitest tests. The declarative syntax provided by ActiveSupport is handled by the |
| 141 | + # Rails add-on |
| 142 | + name_parts = fully_qualified_name.split("::") |
| 143 | + singleton_name = "#{name_parts.join("::")}::<Class:#{name_parts.last}>" |
| 144 | + !@index.linearized_ancestors_of(singleton_name).include?("ActiveSupport::Testing::Declarative") |
| 145 | + rescue RubyIndexer::Index::NonExistingNamespaceError |
| 146 | + true |
| 147 | + end |
| 148 | + |
| 149 | + sig do |
| 150 | + params( |
| 151 | + node: T.any( |
| 152 | + Prism::ConstantPathNode, |
| 153 | + Prism::ConstantReadNode, |
| 154 | + Prism::ConstantPathTargetNode, |
| 155 | + Prism::CallNode, |
| 156 | + Prism::MissingNode, |
| 157 | + ), |
| 158 | + ).returns(String) |
| 159 | + end |
| 160 | + def name_with_dynamic_reference(node) |
| 161 | + slice = node.slice |
| 162 | + slice.gsub(/((?<=::)|^)[a-z]\w*/, DYNAMIC_REFERENCE_MARKER) |
| 163 | + end |
| 164 | + end |
| 165 | + end |
| 166 | +end |
0 commit comments