Module: Tapioca::RBIFilesHelper

Included in:
Commands::AbstractDsl, Commands::AbstractGem, Commands::CheckShims
Defined in:
lib/tapioca/helpers/rbi_files_helper.rb

Overview

@requires_ancestor: Thor::Shell @requires_ancestor: SorbetHelper

Instance Method Summary collapse

Instance Method Details

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

: (RBI::Index index, shim_rbi_dir: String, todo_rbi_file: String) -> Hash[String, Array]



38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 38

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

: (RBI::Index index, String kind, String file) -> void



9
10
11
12
13
14
15
16
17
18
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 9

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

: (RBI::Index index, String kind, String dir, number_of_workers: Integer?) -> void



21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 21

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

: (RBI::Loc loc, path_prefix: String?) -> String



55
56
57
58
59
60
61
62
63
64
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 55

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

: ( | command: String, | gem_dir: String, | dsl_dir: String, | auto_strictness: bool, | ?gems: Array, | ?compilers: Enumerable | ) -> void



74
75
76
77
78
79
80
81
82
83
84
85
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
# File 'lib/tapioca/helpers/rbi_files_helper.rb', line 74

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 Tapioca::Error, error_messages.join("\n") if parse_errors.any?
end