forked from castwide/solargraph
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathchain_spec.rb
384 lines (350 loc) · 12.6 KB
/
chain_spec.rb
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
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
describe Solargraph::Source::Chain do
it "gets empty definitions for undefined links" do
chain = described_class.new([Solargraph::Source::Chain::Link.new])
expect(chain.define(nil, nil, nil)).to be_empty
end
it "infers undefined types for undefined links" do
chain = described_class.new([Solargraph::Source::Chain::Link.new])
expect(chain.infer(nil, nil, nil)).to be_undefined
end
it "calls itself undefined if any of its links are undefined" do
chain = described_class.new([Solargraph::Source::Chain::Link.new])
expect(chain).to be_undefined
end
it "returns undefined bases for single links" do
chain = described_class.new([Solargraph::Source::Chain::Link.new])
expect(chain.base).to be_undefined
end
it "defines constants from core classes" do
api_map = Solargraph::ApiMap.new
chain = described_class.new([Solargraph::Source::Chain::Constant.new('String')])
pins = chain.define(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(pins.first).to be_a(Solargraph::Pin::Namespace)
expect(pins.first.path).to eq('String')
end
it "infers types from core classes" do
api_map = Solargraph::ApiMap.new
chain = described_class.new([Solargraph::Source::Chain::Constant.new('String')])
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.namespace).to eq('String')
expect(type.scope).to eq(:class)
end
it "infers types from core methods" do
api_map = Solargraph::ApiMap.new
chain = described_class.new([Solargraph::Source::Chain::Constant.new('String'), Solargraph::Source::Chain::Call.new('new')])
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.namespace).to eq('String')
expect(type.scope).to eq(:instance)
end
it "recognizes literals" do
chain = described_class.new([Solargraph::Source::Chain::Literal.new('String')])
expect(chain.literal?).to be(true)
end
it "recognizes constants" do
chain = described_class.new([Solargraph::Source::Chain::Constant.new('String')])
expect(chain.constant?).to be(true)
end
it "recognizes unfinished constants" do
chain = described_class.new([Solargraph::Source::Chain::Constant.new('String'), Solargraph::Source::Chain::Constant.new('<undefined>')])
expect(chain.constant?).to be(true)
expect(chain.base.constant?).to be(true)
expect(chain.undefined?).to be(true)
expect(chain.base.undefined?).to be(false)
end
it "infers types from new subclass calls without a subclass initialize method" do
code = %(
class Sup
def initialize; end
def meth; end
end
class Sub < Sup
def meth; end
end
)
map = Solargraph::SourceMap.load_string(code)
api_map = Solargraph::ApiMap.new
api_map.index map.pins
sig = Solargraph::Source.load_string('Sub.new')
chain = Solargraph::Source::SourceChainer.chain(sig, Solargraph::Position.new(0, 5))
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.name).to eq('Sub')
end
it "follows constant chains" do
source = Solargraph::Source.load_string(%(
module Mixin; end
module Container
class Foo; end
end
Container::Foo::Mixin
))
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(5, 23))
pins = chain.define(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(pins).to be_empty
end
it "rebases inner constants chains" do
source = Solargraph::Source.load_string(%(
class Foo
class Bar; end
::Foo::Bar
end
))
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(3, 16))
pins = chain.define(api_map, Solargraph::Pin::ProxyType.new(closure: Solargraph::Pin::Namespace.new(name: 'Foo'), return_type: Solargraph::ComplexType.parse('Class<Foo>')), [])
expect(pins.first.path).to eq('Foo::Bar')
end
it "resolves relative constant paths" do
source = Solargraph::Source.load_string(%(
class Foo
class Bar
class Baz; end
end
module Other
Bar::Baz
end
end
))
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(6, 16))
pins = chain.define(api_map, Solargraph::Pin::ProxyType.anonymous(Solargraph::ComplexType.parse('Class<Foo::Other>')), [])
expect(pins.first.path).to eq('Foo::Bar::Baz')
end
it "avoids recursive variable assignments" do
source = Solargraph::Source.load_string(%(
@foo = @bar
@bar = @foo.quz
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(2, 18))
expect {
chain.define(api_map, Solargraph::Pin::ROOT_PIN, [])
}.not_to raise_error
end
it "pulls types from multiple lines of code" do
source = Solargraph::Source.load_string(%(
123
'abc'
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(2, 11))
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.to_s).to eq('String')
end
it "uses last line of a begin expression as return type" do
source = Solargraph::Source.load_string(%(
begin
123
'abc'
end
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(4, 9))
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.to_s).to eq('String')
end
it "matches constants on complete symbols" do
source = Solargraph::Source.load_string(%(
class Correct; end
class NotCorrect; end
Correct
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(3, 6))
result = chain.define(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(result.map(&:path)).to eq(['Correct'])
end
it 'infers booleans from or-nodes passed to !' do
source = Solargraph::Source.load_string(%(
!([].include?('.') || [].include?('#'))
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Parser.chain(source.node, source.filename)
type = chain.infer(api_map, api_map.pins.first, [])
expect(type.tag).to eq('Boolean')
end
it 'infers last type from and-nodes' do
source = Solargraph::Source.load_string(%(
[] && ''
))
api_map = Solargraph::ApiMap.new
chain = Solargraph::Parser.chain(source.node)
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.to_s).to eq('String')
end
it 'infers multiple types from or-nodes' do
source = Solargraph::Source.load_string(%(
[] || ''
))
api_map = Solargraph::ApiMap.new
chain = Solargraph::Parser.chain(source.node)
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.to_s).to eq('Array, String')
end
it 'infers Procs from block-pass nodes' do
source = Solargraph::Source.load_string(%(
x = []
x.map(&:foo)
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
node = source.node_at(2, 12)
chain = Solargraph::Parser.chain(node, 'test.rb')
pin = chain.define(api_map, Solargraph::Pin::ROOT_PIN, []).first
expect(pin.return_type.tag).to eq('Proc')
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.tag).to eq('Proc')
end
it 'infers Boolean from true' do
source = Solargraph::Source.load_string(%(
@x = true
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
node = source.node_at(1, 6)
# chain = Solargraph::Source::NodeChainer.chain(node, 'test.rb')
chain = Solargraph::Parser.chain(node, 'test.rb')
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.tag).to eq('Boolean')
end
it 'infers self from Object#freeze' do
source = Solargraph::Source.load_string(%(
Array.new.freeze
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
node = source.node_at(1, 16)
chain = Solargraph::Parser.chain(node, 'test.rb')
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.tag).to eq('Array')
end
it 'infers the nearest constants first' do
source = Solargraph::Source.load_string(%(
module Outer
class String; end
end
module Outer
module Inner
def self.outer_string
String
end
end
end
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
closure = api_map.get_path_pins('Outer::Inner').first
outer_node = api_map.get_path_pins('Outer::Inner.outer_string').first.send(:method_body_node)
outer_chain = Solargraph::Parser.chain(outer_node)
outer_type = outer_chain.infer(api_map, closure, [])
expect(outer_type.tag).to eq('Class<Outer::String>')
end
it 'infers rooted constants' do
source = Solargraph::Source.load_string(%(
module Outer
class String; end
end
module Outer
module Inner
def self.core_string
::String
end
end
end
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
closure = api_map.get_path_pins('Outer::Inner').first
core_node = api_map.get_path_pins('Outer::Inner.core_string').first.send(:method_body_node)
core_chain = Solargraph::Parser.chain(core_node)
core_type = core_chain.infer(api_map, closure, [])
expect(core_type.tag).to eq('Class<String>')
end
it 'infers String from interpolated strings' do
source = Solargraph::Source.load_string('"#{Object}"', 'test.rb')
node = source.node
api_map = Solargraph::ApiMap.new
api_map.map source
chain = Solargraph::Parser.chain(node)
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.tag).to eq('String')
end
it 'infers namespaces from constant aliases' do
source = Solargraph::Source.load_string(%(
class Foo
class Bar; end
end
Alias = Foo
Alias::Bar.new
), 'test.rb')
api_map = Solargraph::ApiMap.new
api_map.map source
node = source.node_at(5, 17)
chain = Solargraph::Parser.chain(node, 'test.rb')
type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(type.tag).to eq('Foo::Bar')
end
it 'detects blocks in multiple calls' do
source = Solargraph::Source.load_string(%(
foo { |x| x }.bar { |y| y }
))
chain = Solargraph::Parser.chain(source.node)
expect(chain.links.length).to eq(2)
expect(chain.links[0]).to be_with_block
expect(chain.links[1]).to be_with_block
end
it 'infers instance variables from multiple assignments' do
source = Solargraph::Source.load_string(%(
def foo
@foo = nil
@foo = 'foo'
end
))
api_map = Solargraph::ApiMap.new
api_map.map source
pin = api_map.get_path_pins('#foo').first
type = pin.probe(api_map)
expect(type.tag).to eq('String')
end
it 'recognizes nil safe navigation' do
source = Solargraph::Source.load_string(%(
String.new&.strip
), 'test.rb')
api_map = Solargraph::ApiMap.new.map(source)
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(1, 18))
tag = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(tag.to_s).to eq('String, nil')
end
it 'infers Class<self> from Object#class' do
source = Solargraph::Source.load_string(%(
String.new.class
), 'test.rb')
api_map = Solargraph::ApiMap.new.map(source)
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(1, 17))
tag = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
expect(tag.to_s).to eq('Class<String>')
end
it 'gracefully handles requests for type of generic method in chain' do
source = Solargraph::Source.load_string(%(
# @generic T
# @param x [generic<T>]
# @return [generic<T>]}
def foo(x); x; end
foo('string')
), 'test.rb')
api_map = Solargraph::ApiMap.new.map(source)
chain = Solargraph::Source::SourceChainer.chain(source, Solargraph::Position.new(5, 7))
expect { chain.infer(api_map, Solargraph::Pin::ROOT_PIN, []) }.not_to raise_error
# @todo get this to also return a defined type
# type = chain.infer(api_map, Solargraph::Pin::ROOT_PIN, [])
# expect(type.to_s).to eq('String')
end
end