Module: Tapioca::RBIFilesHelper

Extended by:
T::Helpers, T::Sig
Included in:
Commands::AbstractDsl, Commands::AbstractGem, Commands::CheckShims
Defined in:
lib/tapioca/helpers/rbi_files_helper.rb

Instance Method Summary collapse

Instance Method Details

#duplicated_nodes_from_index(index, shim_rbi_dir:, todo_rbi_file:) ⇒ Object



48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 48

def duplicated_nodes_from_index(index, shim_rbi_dir:, todo_rbi_file:)
  duplicates = {}
  say("Looking for duplicates... ")
  time = Benchmark.realtime do
    index.keys.each do |key|
      nodes = index[key]
      next unless shims_or_todos_have_duplicates?(nodes, shim_rbi_dir: shim_rbi_dir, todo_rbi_file: todo_rbi_file)

      duplicates[key] = nodes
    end
  end
  say(" Done ", :green)
  say("(#{time.round(2)}s)")
  duplicates
end

#index_rbi(index, kind, file) ⇒ Object



13
14
15
16
17
18
19
20
21
22
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 13

def index_rbi(index, kind, file)
  return unless File.exist?(file)

  say("Loading #{kind} RBIs from #{file}... ")
  time = Benchmark.realtime do
    parse_and_index_files(index, [file], number_of_workers: 1)
  end
  say(" Done ", :green)
  say("(#{time.round(2)}s)")
end

#index_rbis(index, kind, dir, number_of_workers:) ⇒ Object



25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 25

def index_rbis(index, kind, dir, number_of_workers:)
  return unless Dir.exist?(dir) && !Dir.empty?(dir)

  if kind == "payload"
    say("Loading Sorbet payload... ")
  else
    say("Loading #{kind} RBIs from #{dir}... ")
  end
  time = Benchmark.realtime do
    files = Dir.glob("#{dir}/**/*.rbi").sort
    parse_and_index_files(index, files, number_of_workers: number_of_workers)
  end
  say(" Done ", :green)
  say("(#{time.round(2)}s)")
end

#location_to_payload_url(loc, path_prefix:) ⇒ Object



65
66
67
68
69
70
71
72
73
74
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 65

def location_to_payload_url(loc, path_prefix:)
  return loc.to_s unless path_prefix

  url = loc.file || ""
  return loc.to_s unless url.start_with?(path_prefix)

  url = url.sub(path_prefix, SorbetHelper::SORBET_PAYLOAD_URL)
  url = "#{url}#L#{loc.begin_line}"
  url
end

#validate_rbi_files(command:, gem_dir:, dsl_dir:, auto_strictness:, gems: [], compilers: []) ⇒ Object



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
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 86

def validate_rbi_files(command:, gem_dir:, dsl_dir:, auto_strictness:, gems: [], compilers: [])
  error_url_base = Spoom::Sorbet::Errors::DEFAULT_ERROR_URL_BASE

  say("Checking generated RBI files... ")
  res = sorbet(
    "--no-config",
    "--error-url-base=#{error_url_base}",
    "--stop-after namer",
    dsl_dir,
    gem_dir,
  )
  say(" Done", :green)

  errors = Spoom::Sorbet::Errors::Parser.parse_string(res.err || "")

  if errors.empty?
    say("  No errors found\n\n", [:green, :bold])

    return
  end

  parse_errors = errors.select { |error| error.code < 4000 }

  error_messages = []

  if parse_errors.any?
    error_messages << set_color(<<~ERR, :red)
      ##### INTERNAL ERROR #####

      There are parse errors in the generated RBI files.

      This seems related to a bug in Tapioca.
      Please open an issue at https://github.com/Shopify/tapioca/issues/new with the following information:

      Tapioca v#{Tapioca::VERSION}

      Command:
        #{command}
    ERR

    error_messages << set_color(<<~ERR, :red) if gems.any?
      Gems:
      #{gems.map { |gem| "  #{gem.name} (#{gem.version})" }.join("\n")}
    ERR

    error_messages << set_color(<<~ERR, :red) if compilers.any?
      Compilers:
      #{compilers.map { |compiler| "  #{compiler.name}" }.join("\n")}
    ERR

    error_messages << set_color(<<~ERR, :red)
      Errors:
      #{parse_errors.map { |error| "  #{error}" }.join("\n")}

      ##########################
    ERR
  end

  if auto_strictness
    redef_errors = errors.select { |error| error.code == 4010 }
    update_gem_rbis_strictnesses(redef_errors, gem_dir)
  end

  Kernel.raise Thor::Error, error_messages.join("\n") if parse_errors.any?
end