class Jazzy::SymbolGraph::Constraint

Constraint is a tidied-up JSON object, used by both Symbol and Relationship, and key to reconstructing extensions.

Constants

KIND_MAP

Attributes

kind[RW]
lhs[RW]
rhs[RW]

Public Class Methods

new(kind, lhs, rhs) click to toggle source
# File lib/jazzy/symbol_graph/constraint.rb, line 14
def initialize(kind, lhs, rhs)
  self.kind = kind # "==" or ":"
  self.lhs = lhs
  self.rhs = rhs
end
new_declaration(decl) click to toggle source

Init from a Swift declaration fragment eg. ‘A : B’

# File lib/jazzy/symbol_graph/constraint.rb, line 39
def self.new_declaration(decl)
  decl =~ /^(.*?)\s*([:<=]+)\s*(.*)$/
  new(Regexp.last_match[2],
      Regexp.last_match[1],
      Regexp.last_match[3])
end
new_hash(hash) click to toggle source

Init from a JSON hash

# File lib/jazzy/symbol_graph/constraint.rb, line 29
def self.new_hash(hash)
  kind = KIND_MAP[hash[:kind]]
  raise "Unknown constraint kind '#{kind}'" unless kind

  lhs = hash[:lhs].sub(/^Self\./, '')
  rhs = hash[:rhs].sub(/^Self\./, '')
  new(kind, lhs, rhs)
end
new_list(hash_list) click to toggle source
# File lib/jazzy/symbol_graph/constraint.rb, line 55
def self.new_list(hash_list)
  hash_list.map { |h| Constraint.new_hash(h) }.sort.uniq
end
new_list_for_symbol(hash_list, path_components) click to toggle source

Swift protocols and reqs have an implementation/hidden conformance to their own protocol: we don’t want to think about this in docs.

# File lib/jazzy/symbol_graph/constraint.rb, line 61
def self.new_list_for_symbol(hash_list, path_components)
  hash_list.map do |hash|
    if hash[:lhs] == 'Self' &&
       hash[:kind] == 'conformance' &&
       path_components.include?(hash[:rhs])
      next nil
    end

    Constraint.new_hash(hash)
  end.compact
end
new_list_from_declaration(decl) click to toggle source

Workaround Swift 5.3 bug with missing constraint rels, eg. extension P {

func f<C>(a: C) where C: P {}

}

# File lib/jazzy/symbol_graph/constraint.rb, line 77
def self.new_list_from_declaration(decl)
  return [] if decl.include?('(')

  decl.split(/\s*,\s*/).map { |cons| Constraint.new_declaration(cons) }
end

Public Instance Methods

<=>(other) click to toggle source
# File lib/jazzy/symbol_graph/constraint.rb, line 86
def <=>(other)
  to_swift <=> other.to_swift
end
hash() click to toggle source
# File lib/jazzy/symbol_graph/constraint.rb, line 92
def hash
  to_swift.hash
end
to_swift() click to toggle source
# File lib/jazzy/symbol_graph/constraint.rb, line 46
def to_swift
  "#{lhs} #{kind} #{rhs}"
end
type_names() click to toggle source

The first component of types in the constraint

# File lib/jazzy/symbol_graph/constraint.rb, line 51
def type_names
  Set.new([lhs, rhs].map { |n| n.sub(/\..*$/, '') })
end