Class: BitClust::RbsSigImporter

Inherits:
Object
  • Object
show all
Defined in:
lib/bitclust/rbs_sig_importer.rb

Instance Method Summary collapse

Constructor Details

#initialize(core_root: nil, repository_root: nil, sig_dirs: []) ⇒ RbsSigImporter

core_root: ruby/rbs チェックアウトの core/(組み込みクラスの型) repository_root: 同 stdlib/(core_root を与えると rbs が stringio を 暗黙に要求するので、core_root とセットで渡す) sig_dirs: 素の .rbs ディレクトリ(テスト・追加分)



30
31
32
33
34
35
# File 'lib/bitclust/rbs_sig_importer.rb', line 30

def initialize(core_root: nil, repository_root: nil, sig_dirs: [])
  @core_root = core_root
  @repository_root = repository_root
  @sig_dirs = sig_dirs
  @signatures = nil
end

Instance Method Details

#apply(db) ⇒ Object

DB の全メソッドエントリに対応表を引き、ヒットしたものへ rbs_sig property(JSON 1 行)を書き込む。書き方は MethodSinceCalculator#apply と 同じ(変更があったエントリだけ save)



66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
# File 'lib/bitclust/rbs_sig_importer.rb', line 66

def apply(db)
  stats = { entries_updated: 0, sigs_matched: 0, methods_missed: 0 } #: stats
  db.classes.each do |c|
    c.entries.each do |m|
      next if m.kind == :undefined
      next unless %w[i s m].include?(m.typechar)
      overloads = lookup(c.name, m.typechar, m.names)
      unless overloads
        stats[:methods_missed] += 1
        next
      end
      stats[:sigs_matched] += 1
      json = JSON.generate({'overloads' => overloads})
      next if m.rbs_sig == json
      m.rbs_sig = json
      m.save
      stats[:entries_updated] += 1
    end
  end
  stats
end

#lookup(class_name, typechar, names) ⇒ Object

bitclust の typechar('i'/'s'/'m')と名前の並び(別名)で対応表を引く。 モジュール関数(m)は def self?. 由来の # キーを先に、new は Class.new → Class#initialize の順で引く。見つからなければ nil



45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
# File 'lib/bitclust/rbs_sig_importer.rb', line 45

def lookup(class_name, typechar, names)
  names.each do |name|
    case typechar
    when 'i'
      sig = signatures["#{class_name}##{name}"]
    when 's'
      sig = signatures["#{class_name}.#{name}"]
      sig ||= signatures["#{class_name}#initialize"] if name == 'new'
    when 'm'
      sig = signatures["#{class_name}##{name}"] || signatures["#{class_name}.#{name}"]
    else
      return nil
    end
    return sig if sig
  end
  nil
end

#signaturesObject

"Class#method"/"Class.method" → オーバーロード配列



38
39
40
# File 'lib/bitclust/rbs_sig_importer.rb', line 38

def signatures
  @signatures ||= build_signatures
end