-
Notifications
You must be signed in to change notification settings - Fork 267
Expand file tree
/
Copy pathenhancements_test.rb
More file actions
325 lines (270 loc) · 8.87 KB
/
Copy pathenhancements_test.rb
File metadata and controls
325 lines (270 loc) · 8.87 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
# typed: true
# frozen_string_literal: true
require_relative "test_case"
module RubyIndexer
class EnhancementTest < TestCase
def teardown
super
Enhancement.clear
end
def test_enhancing_indexing_included_hook
Class.new(Enhancement) do
def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
owner = @listener.current_owner
return unless owner
return unless call_node.name == :extend
arguments = call_node.arguments&.arguments
return unless arguments
arguments.each do |node|
next unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
module_name = node.full_name
next unless module_name == "ActiveSupport::Concern"
@listener.register_included_hook do |index, base|
class_methods_name = "#{owner.name}::ClassMethods"
if index.indexed?(class_methods_name)
singleton = index.existing_or_new_singleton_class(base.name)
singleton.mixin_operations << Entry::Include.new(class_methods_name)
end
end
@listener.add_method(
"new_method",
call_node.location,
[Entry::Signature.new([Entry::RequiredParameter.new(name: :a)])],
)
rescue Prism::ConstantPathNode::DynamicPartsInConstantPathError,
Prism::ConstantPathNode::MissingNodesInConstantPathError
# Do nothing
end
end
end
index(<<~RUBY)
module ActiveSupport
module Concern
def self.extended(base)
base.class_eval("def new_method(a); end")
end
end
end
module ActiveRecord
module Associations
extend ActiveSupport::Concern
module ClassMethods
def belongs_to(something); end
end
end
class Base
include Associations
end
end
class User < ActiveRecord::Base
end
RUBY
assert_equal(
[
"User::<User>",
"ActiveRecord::Base::<Base>",
"ActiveRecord::Associations::ClassMethods",
"Object::<Object>",
"BasicObject::<BasicObject>",
"Class",
"Module",
"Object",
"Kernel",
"BasicObject",
],
@index.linearized_ancestors_of("User::<User>"),
)
assert_entry("new_method", Entry::Method, "/fake/path/foo.rb:10-4:10-33")
end
def test_enhancing_indexing_configuration_dsl
Class.new(Enhancement) do
def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
return unless @listener.current_owner
name = node.name
return unless name == :has_many
arguments = node.arguments&.arguments
return unless arguments
association_name = arguments.first
return unless association_name.is_a?(Prism::SymbolNode)
@listener.add_method(
association_name.value, #: as !nil
association_name.location,
[],
)
end
end
index(<<~RUBY)
module ActiveSupport
module Concern
def self.extended(base)
base.class_eval("def new_method(a); end")
end
end
end
module ActiveRecord
module Associations
extend ActiveSupport::Concern
module ClassMethods
def belongs_to(something); end
end
end
class Base
include Associations
end
end
class User < ActiveRecord::Base
has_many :posts
end
RUBY
assert_entry("posts", Entry::Method, "/fake/path/foo.rb:23-11:23-17")
end
def test_error_handling_in_on_call_node_enter_enhancement
Class.new(Enhancement) do
def on_call_node_enter(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
raise "Error"
end
class << self
def name
"TestEnhancement"
end
end
end
_stdout, stderr = capture_io do
index(<<~RUBY)
module ActiveSupport
module Concern
def self.extended(base)
base.class_eval("def new_method(a); end")
end
end
end
RUBY
end
assert_match(
%r{Indexing error in file:///fake/path/foo\.rb with 'TestEnhancement' on call node enter enhancement},
stderr,
)
# The module should still be indexed
assert_entry("ActiveSupport::Concern", Entry::Module, "/fake/path/foo.rb:1-2:5-5")
end
def test_error_handling_in_on_call_node_leave_enhancement
Class.new(Enhancement) do
def on_call_node_leave(node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
raise "Error"
end
class << self
def name
"TestEnhancement"
end
end
end
_stdout, stderr = capture_io do
index(<<~RUBY)
module ActiveSupport
module Concern
def self.extended(base)
base.class_eval("def new_method(a); end")
end
end
end
RUBY
end
assert_match(
%r{Indexing error in file:///fake/path/foo\.rb with 'TestEnhancement' on call node leave enhancement},
stderr,
)
# The module should still be indexed
assert_entry("ActiveSupport::Concern", Entry::Module, "/fake/path/foo.rb:1-2:5-5")
end
def test_advancing_namespace_stack_from_enhancement
Class.new(Enhancement) do
def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
owner = @listener.current_owner
return unless owner
case call_node.name
when :class_methods
@listener.add_module("ClassMethods", call_node.location, call_node.location)
when :extend
arguments = call_node.arguments&.arguments
return unless arguments
arguments.each do |node|
next unless node.is_a?(Prism::ConstantReadNode) || node.is_a?(Prism::ConstantPathNode)
module_name = node.full_name
next unless module_name == "ActiveSupport::Concern"
@listener.register_included_hook do |index, base|
class_methods_name = "#{owner.name}::ClassMethods"
if index.indexed?(class_methods_name)
singleton = index.existing_or_new_singleton_class(base.name)
singleton.mixin_operations << Entry::Include.new(class_methods_name)
end
end
end
end
end
def on_call_node_leave(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
return unless call_node.name == :class_methods
@listener.pop_namespace_stack
end
end
index(<<~RUBY)
module ActiveSupport
module Concern
end
end
module MyConcern
extend ActiveSupport::Concern
class_methods do
def foo; end
end
end
class User
include MyConcern
end
RUBY
assert_equal(
[
"User::<User>",
"MyConcern::ClassMethods",
"Object::<Object>",
"BasicObject::<BasicObject>",
"Class",
"Module",
"Object",
"Kernel",
"BasicObject",
],
@index.linearized_ancestors_of("User::<User>"),
)
refute_nil(@index.resolve_method("foo", "User::<User>"))
end
def test_creating_anonymous_classes_from_enhancement
Class.new(Enhancement) do
def on_call_node_enter(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
case call_node.name
when :context
arguments = call_node.arguments&.arguments
first_argument = arguments&.first
return unless first_argument.is_a?(Prism::StringNode)
@listener.add_class(
"<RSpec:#{first_argument.content}>",
call_node.location,
first_argument.location,
)
when :subject
@listener.add_method("subject", call_node.location, [])
end
end
def on_call_node_leave(call_node) # rubocop:disable RubyLsp/UseRegisterWithHandlerMethod
return unless call_node.name == :context
@listener.pop_namespace_stack
end
end
index(<<~RUBY)
context "does something" do
subject { call_whatever }
end
RUBY
refute_nil(@index.resolve_method("subject", "<RSpec:does something>"))
end
end
end