Skip to content

Commit 7f169fd

Browse files
committed
Ignore malformed mixins and overloads (castwide#862)
* Ignore malformed mixins * Ignore malformed overload tags
1 parent 73fb1d5 commit 7f169fd

File tree

5 files changed

+40
-19
lines changed

5 files changed

+40
-19
lines changed

lib/solargraph/api_map.rb

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -249,9 +249,16 @@ def qualify tag, context_tag = ''
249249
return tag if ['Boolean', 'self', nil].include?(tag)
250250
type = ComplexType.parse(tag)
251251
return tag if type.literal?
252-
context_type = ComplexType.parse(context_tag)
252+
253+
context_type = ComplexType.try_parse(context_tag)
254+
return unless context_type
255+
256+
type = ComplexType.try_parse(tag)
257+
return unless type
258+
253259
fqns = qualify_namespace(type.rooted_namespace, context_type.rooted_namespace)
254-
return nil if fqns.nil?
260+
return unless fqns
261+
255262
fqns + type.substring
256263
end
257264

lib/solargraph/complex_type.rb

Lines changed: 16 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -218,21 +218,6 @@ def bottom?
218218
@items.all?(&:bot?)
219219
end
220220

221-
private
222-
223-
# @todo This is a quick and dirty hack that forces `self` keywords
224-
# to reference an instance of their class and never the class itself.
225-
# This behavior may change depending on which result is expected
226-
# from YARD conventions. See https://github.com/lsegal/yard/issues/1257
227-
# @param dst [String]
228-
# @return [String]
229-
def reduce_class dst
230-
while dst =~ /^(Class|Module)\<(.*?)\>$/
231-
dst = dst.sub(/^(Class|Module)\</, '').sub(/\>$/, '')
232-
end
233-
dst
234-
end
235-
236221
class << self
237222
# Parse type strings into a ComplexType.
238223
#
@@ -343,7 +328,7 @@ def parse *strings, partial: false
343328
def try_parse *strings
344329
parse *strings
345330
rescue ComplexTypeError => e
346-
Solargraph.logger.info "Error parsing complex type: #{e.message}"
331+
Solargraph.logger.info "Error parsing complex type `#{strings.join(', ')}`: #{e.message}"
347332
ComplexType::UNDEFINED
348333
end
349334
end
@@ -356,5 +341,20 @@ def try_parse *strings
356341
SELF = ComplexType.parse('self')
357342
BOOLEAN = ComplexType.parse('::Boolean')
358343
BOT = ComplexType.parse('bot')
344+
345+
private
346+
347+
# @todo This is a quick and dirty hack that forces `self` keywords
348+
# to reference an instance of their class and never the class itself.
349+
# This behavior may change depending on which result is expected
350+
# from YARD conventions. See https://github.com/lsegal/yard/issues/1257
351+
# @param dst [String]
352+
# @return [String]
353+
def reduce_class dst
354+
while dst =~ /^(Class|Module)\<(.*?)\>$/
355+
dst = dst.sub(/^(Class|Module)\</, '').sub(/\>$/, '')
356+
end
357+
dst
358+
end
359359
end
360360
end

lib/solargraph/pin/method.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -288,7 +288,9 @@ def try_merge! pin
288288

289289
# @return [::Array<Pin::Method>]
290290
def overloads
291-
@overloads ||= docstring.tags(:overload).map do |tag|
291+
# Ignore overload tags with nil parameters. If it's not an array, the
292+
# tag's source is likely malformed.
293+
@overloads ||= docstring.tags(:overload).select(&:parameters).map do |tag|
292294
Pin::Signature.new(
293295
generics,
294296
tag.parameters.map do |src|

spec/api_map_spec.rb

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -777,4 +777,11 @@ class Foo
777777
expect(baz).to be_a(Solargraph::Pin::Method)
778778
expect(baz.path).to eq('Foo#baz')
779779
end
780+
781+
it 'ignores malformed mixins' do
782+
closure = Solargraph::Pin::Namespace.new(name: 'Foo', closure: Solargraph::Pin::ROOT_PIN, type: :class)
783+
mixin = Solargraph::Pin::Reference::Include.new(name: 'defined?(DidYouMean::SpellChecker) && defined?(DidYouMean::Correctable)', closure: closure)
784+
api_map = Solargraph::ApiMap.new(pins: [closure, mixin])
785+
expect(api_map.get_method_stack('Foo', 'foo')).to be_empty
786+
end
780787
end

spec/pin/method_spec.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -569,5 +569,10 @@ def bar
569569
expect(pin.typify(api_map)).to be_undefined
570570
expect(pin.probe(api_map).simple_tags).to eq('Symbol, Float, String, Integer')
571571
end
572+
573+
it 'ignores malformed overload tags' do
574+
pin = Solargraph::Pin::Method.new(name: 'example', comments: "@overload\n @param")
575+
expect(pin.overloads).to be_empty
576+
end
572577
end
573578
end

0 commit comments

Comments
 (0)