Module: Solargraph::TypeChecker::Checks
- Included in:
- Solargraph::TypeChecker
- Defined in:
- lib/solargraph/type_checker/checks.rb
Class Method Summary collapse
- .any_types_match?(api_map, expected, inferred) ⇒ Boolean
- .duck_types_match?(api_map, expected, inferred) ⇒ Boolean
- .either_way?(api_map, cls1, cls2) ⇒ Boolean
- .fuzz(type) ⇒ String
-
.types_match?(api_map, expected, inferred) ⇒ Boolean
Compare an expected type with an inferred type.
Class Method Details
.any_types_match?(api_map, expected, inferred) ⇒ Boolean
48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/solargraph/type_checker/checks.rb', line 48 def any_types_match? api_map, expected, inferred return duck_types_match?(api_map, expected, inferred) if expected.duck_type? expected.each do |exp| next if exp.duck_type? inferred.each do |inf| # return true if exp == inf || api_map.super_and_sub?(fuzz(inf), fuzz(exp)) return true if exp == inf || either_way?(api_map, inf, exp) end end false end |
.duck_types_match?(api_map, expected, inferred) ⇒ Boolean
64 65 66 67 68 69 70 71 72 |
# File 'lib/solargraph/type_checker/checks.rb', line 64 def duck_types_match? api_map, expected, inferred raise ArgumentError, "Expected type must be duck type" unless expected.duck_type? expected.each do |exp| next unless exp.duck_type? quack = exp.to_s[1..-1] return false if api_map.get_method_stack(inferred.namespace, quack, scope: inferred.scope).empty? end true end |
.either_way?(api_map, cls1, cls2) ⇒ Boolean
88 89 90 91 92 |
# File 'lib/solargraph/type_checker/checks.rb', line 88 def either_way?(api_map, cls1, cls2) f1 = fuzz(cls1) f2 = fuzz(cls2) api_map.super_and_sub?(f1, f2) || api_map.super_and_sub?(f2, f1) end |
.fuzz(type) ⇒ String
76 77 78 79 80 81 82 |
# File 'lib/solargraph/type_checker/checks.rb', line 76 def fuzz type if type.parameters? type.name else type.tag end end |
.types_match?(api_map, expected, inferred) ⇒ Boolean
Compare an expected type with an inferred type. Common usage is to check if the type declared in a method’s @return tag matches the type inferred from static analysis of the code.
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 |
# File 'lib/solargraph/type_checker/checks.rb', line 14 def types_match? api_map, expected, inferred return true if expected.to_s == inferred.to_s matches = [] expected.each do |exp| found = false inferred.each do |inf| # if api_map.super_and_sub?(fuzz(inf), fuzz(exp)) if either_way?(api_map, inf, exp) found = true matches.push inf break end end return false unless found end inferred.each do |inf| next if matches.include?(inf) found = false expected.each do |exp| # if api_map.super_and_sub?(fuzz(inf), fuzz(exp)) if either_way?(api_map, inf, exp) found = true break end end return false unless found end true end |