Class: Steep::Drivers::Check

Inherits:
Object
  • Object
show all
Includes:
Utils::DriverHelper
Defined in:
lib/steep/drivers/check.rb

Constant Summary collapse

LSP =
LanguageServer::Protocol
PLURALIZE =
{
  "diagnostic" => "diagnostics",
  "expression" => "expressions",
  "file" => "files",
  "problem" => "problems",
}.freeze

Instance Attribute Summary collapse

Attributes included from Utils::DriverHelper

#disable_install_collection, #steepfile

Instance Method Summary collapse

Methods included from Utils::DriverHelper

#install_collection, #keep_diagnostic?, #load_config, #request_id, #shutdown_exit, #wait_for_message, #wait_for_response_id

Constructor Details

#initialize(stdout:, stderr:) ⇒ Check

Returns a new instance of Check.



32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
# File 'lib/steep/drivers/check.rb', line 32

def initialize(stdout:, stderr:)
  @stdout = stdout
  @stderr = stderr
  @command_line_patterns = []
  @severity_level = :warning
  @jobs_option = Utils::JobsOption.new()
  @active_group_names = []
  @type_check_code = true
  @validate_group_signatures = true
  @validate_project_signatures = false
  @validate_library_signatures = false
  @formatter = 'code'
  @use_daemon = true
  @expressions = []
end

Instance Attribute Details

#active_group_namesObject (readonly)

Returns the value of attribute active_group_names.



14
15
16
# File 'lib/steep/drivers/check.rb', line 14

def active_group_names
  @active_group_names
end

#command_line_patternsObject (readonly)

Returns the value of attribute command_line_patterns.



8
9
10
# File 'lib/steep/drivers/check.rb', line 8

def command_line_patterns
  @command_line_patterns
end

#expressionsObject (readonly)

Returns the value of attribute expressions.



21
22
23
# File 'lib/steep/drivers/check.rb', line 21

def expressions
  @expressions
end

#formatterObject

Returns the value of attribute formatter.



19
20
21
# File 'lib/steep/drivers/check.rb', line 19

def formatter
  @formatter
end

#jobs_optionObject (readonly)

Returns the value of attribute jobs_option.



12
13
14
# File 'lib/steep/drivers/check.rb', line 12

def jobs_option
  @jobs_option
end

#save_expectations_pathObject

Returns the value of attribute save_expectations_path.



10
11
12
# File 'lib/steep/drivers/check.rb', line 10

def save_expectations_path
  @save_expectations_path
end

#severity_levelObject

Returns the value of attribute severity_level.



11
12
13
# File 'lib/steep/drivers/check.rb', line 11

def severity_level
  @severity_level
end

#stderrObject (readonly)

Returns the value of attribute stderr.



7
8
9
# File 'lib/steep/drivers/check.rb', line 7

def stderr
  @stderr
end

#stdoutObject (readonly)

Returns the value of attribute stdout.



6
7
8
# File 'lib/steep/drivers/check.rb', line 6

def stdout
  @stdout
end

#targetsObject (readonly)

Returns the value of attribute targets.



13
14
15
# File 'lib/steep/drivers/check.rb', line 13

def targets
  @targets
end

#type_check_codeObject

Returns the value of attribute type_check_code.



15
16
17
# File 'lib/steep/drivers/check.rb', line 15

def type_check_code
  @type_check_code
end

#use_daemonObject

Returns the value of attribute use_daemon.



20
21
22
# File 'lib/steep/drivers/check.rb', line 20

def use_daemon
  @use_daemon
end

#validate_group_signaturesObject

Returns the value of attribute validate_group_signatures.



16
17
18
# File 'lib/steep/drivers/check.rb', line 16

def validate_group_signatures
  @validate_group_signatures
end

#validate_library_signaturesObject

Returns the value of attribute validate_library_signatures.



18
19
20
# File 'lib/steep/drivers/check.rb', line 18

def validate_library_signatures
  @validate_library_signatures
end

#validate_project_signaturesObject

Returns the value of attribute validate_project_signatures.



17
18
19
# File 'lib/steep/drivers/check.rb', line 17

def validate_project_signatures
  @validate_project_signatures
end

#with_expectations_pathObject

Returns the value of attribute with_expectations_path.



9
10
11
# File 'lib/steep/drivers/check.rb', line 9

def with_expectations_path
  @with_expectations_path
end

Instance Method Details

#active_group?(group) ⇒ Boolean

Returns:

  • (Boolean)


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

def active_group?(group)
  return true if active_group_names.empty?

  case group
  when Project::Target
    active_group_names.any? {|target_name, group_name|
      target_name == group.name && (group_name == nil || group_name == true)
    }
  when Project::Group
    active_group_names.any? {|target_name, group_name|
      target_name == group.target.name &&
        (group_name == group.name || group_name == true)
    }
  end
end

#load_files(files, target, group, params:) ⇒ Object



162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
# File 'lib/steep/drivers/check.rb', line 162

def load_files(files, target, group, params:)
  if type_check_code
    files.source_paths.each_group_path(group) do |path,|
      params[:code_paths] << [target.name.to_s, target.project.absolute_path(path).to_s]
    end
    files.inline_paths.each_group_path(group) do |path,|
      params[:inline_paths] << [target.name.to_s, target.project.absolute_path(path).to_s]
    end
  end
  if validate_group_signatures
    files.signature_paths.each_group_path(group) do |path,|
      params[:signature_paths] << [target.name.to_s, target.project.absolute_path(path).to_s]
    end
  end
  if validate_project_signatures
    files.signature_paths.each_project_path(except: target) do |path, path_target|
      params[:signature_paths] << [path_target.name.to_s, target.project.absolute_path(path).to_s]
    end
    if group.is_a?(Project::Group)
      files.signature_paths.each_target_path(target, except: group) do |path,|
        params[:signature_paths] << [target.name.to_s, target.project.absolute_path(path).to_s]
      end
    end
  end
  if validate_library_signatures
    files.each_library_path(target) do |path|
      params[:library_paths] << [target.name.to_s, path.to_s]
    end
  end
end

#pluralize(string, count) ⇒ Object



294
295
296
297
298
299
300
# File 'lib/steep/drivers/check.rb', line 294

def pluralize(string, count)
  if count == 1
    string
  else
    PLURALIZE.fetch(string)
  end
end


193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
# File 'lib/steep/drivers/check.rb', line 193

def print_expectations(project:, all_files:, expectations_path:, notifications:)
  expectations = Expectations.load(path: expectations_path, content: expectations_path.read)

  expected_count = 0
  unexpected_count = 0
  missing_count = 0

  ns = notifications.each.with_object({}) do |notification, hash| #$ Hash[Pathname, Array[Expectations::Diagnostic]]
    path = project.relative_path(Steep::PathHelper.to_pathname(notification[:uri]) || raise)
    hash[path] = notification[:diagnostics].map do |diagnostic|
      Expectations::Diagnostic.from_lsp(diagnostic)
    end
  end

  all_files.sort.each do |path|
    test = expectations.test(path: path, diagnostics: ns[path] || [])

    buffer = RBS::Buffer.new(name: path, content: path.read)
    printer = DiagnosticPrinter.new(buffer: buffer, stdout: stdout)

    test.each_diagnostics.each do |type, diag|
      case type
      when :expected
        expected_count += 1
      when :unexpected
        unexpected_count += 1
        printer.print(diag.to_lsp, prefix: Rainbow("+ ").green)
      when :missing
        missing_count += 1
        printer.print(diag.to_lsp, prefix: Rainbow("- ").red, source: false)
      end
    end
  end

  if unexpected_count > 0 || missing_count > 0
    stdout.puts

    stdout.puts Rainbow("Expectations unsatisfied:").bold.red
    stdout.puts "  #{expected_count} expected #{pluralize("diagnostic", expected_count)}"
    stdout.puts Rainbow("  + #{unexpected_count} unexpected #{pluralize("diagnostic", unexpected_count)}").green
    stdout.puts Rainbow("  - #{missing_count} missing #{pluralize("diagnostic", missing_count)}").red
    1
  else
    stdout.puts Rainbow("Expectations satisfied:").bold.green
    stdout.puts "  #{expected_count} expected #{pluralize("diagnostic", expected_count)}"
    0
  end
end


269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
# File 'lib/steep/drivers/check.rb', line 269

def print_result(project:, notifications:)
  if notifications.all? {|notification| notification[:diagnostics].empty? }
    emoji = %w(🫖 🫖 🫖 🫖 🫖 🫖 🫖 🫖 🍵 🧋 🧉).sample
    stdout.puts Rainbow("No type error detected. #{emoji}").green.bold
    0
  else
    errors = notifications.reject {|notification| notification[:diagnostics].empty? }
    total = errors.sum {|notification| notification[:diagnostics].size }

    errors.each do |notification|
      path = Steep::PathHelper.to_pathname(notification[:uri]) or raise
      buffer = RBS::Buffer.new(name: project.relative_path(path), content: path.read)
      printer = DiagnosticPrinter.new(buffer: buffer, stdout: stdout, formatter: formatter)

      notification[:diagnostics].each do |diag|
        printer.print(diag)
        stdout.puts
      end
    end

    stdout.puts Rainbow("Detected #{total} #{pluralize("problem", total)} from #{errors.size} #{pluralize("file", errors.size)}").red.bold
    1
  end
end

#runObject



64
65
66
67
68
69
70
71
72
73
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
# File 'lib/steep/drivers/check.rb', line 64

def run
  project = load_config()

  unless expressions.empty?
    return run_expressions(project)
  end

  params = build_typecheck_params(project)

  diagnostic_notifications = [] #: Array[LanguageServer::Protocol::Interface::PublishDiagnosticsParams]
  error_messages = [] #: Array[String]

  setup_connection(project) do |reader, writer|
    request_guid = SecureRandom.uuid
    writer.write(Server::CustomMethods::TypeCheck.request(request_guid, params))

    wait_for_response_id(reader: reader, id: request_guid) do |message|
      case message[:method]
      when "textDocument/publishDiagnostics"
        ds = message[:params][:diagnostics]
        ds.select! { |d| keep_diagnostic?(d, severity_level: severity_level) }
        stdout.print(ds.empty? ? "." : "F")
        diagnostic_notifications << message[:params]
        stdout.flush
      when "window/showMessage"
        if message[:params][:type] == LSP::Constant::MessageType::ERROR
          error_messages << message[:params][:message]
        end
      end
    end
  end

  stdout.puts
  stdout.puts

  print_typecheck_result(project: project, diagnostic_notifications: diagnostic_notifications, error_messages: error_messages)
rescue Errno::EPIPE => error
  stdout.puts Rainbow("Steep shutdown with an error: #{error.inspect}").red.bold
  1
end

#run_expressions(project) ⇒ Object



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
151
152
153
154
155
156
157
158
159
160
# File 'lib/steep/drivers/check.rb', line 105

def run_expressions(project)
  target = project.targets.first or raise "No targets configured"

  stdout.puts Rainbow("# Type checking expression:").bold
  stdout.puts

  loader = Project::Target.construct_env_loader(options: target.options, project: project)
  signature_service = Services::SignatureService.load_from(loader, implicitly_returns_nil: target.implicitly_returns_nil)
  subtyping = signature_service.current_subtyping or raise "Failed to build subtyping"

  lsp_formatter = Diagnostic::LSPFormatter.new(target.code_diagnostics_config)
  total = 0

  expressions.each_with_index do |expr, index|
    expr_path = Pathname(expressions.size > 1 ? "(expression:#{index})" : "(expression)")

    begin
      source = Source.parse(expr, path: expr_path, factory: subtyping.factory)
    rescue ::Parser::SyntaxError => exn
      stdout.puts Rainbow("Syntax error in #{expr_path}: #{exn.message}").red.bold
      total += 1
      next
    end

    typing = Services::TypeCheckService.type_check(
      source: source,
      subtyping: subtyping,
      constant_resolver: signature_service.latest_constant_resolver,
      cursor: nil
    )

    diagnostics = typing.errors.filter_map { |error| lsp_formatter.format(error) }
    diagnostics.select! { |d| keep_diagnostic?(d, severity_level: severity_level) }

    unless diagnostics.empty?
      buffer = RBS::Buffer.new(name: expr_path, content: expr)
      printer = DiagnosticPrinter.new(buffer: buffer, stdout: stdout, formatter: self.formatter)

      diagnostics.each do |diag|
        printer.print(diag)
        stdout.puts
      end

      total += diagnostics.size
    end
  end

  if total == 0
    emoji = %w(🫖 🫖 🫖 🫖 🫖 🫖 🫖 🫖 🍵 🧋 🧉).sample
    stdout.puts Rainbow("No type error detected. #{emoji}").green.bold
    0
  else
    stdout.puts Rainbow("Detected #{total} #{pluralize("problem", total)} from #{expressions.size} #{pluralize("expression", expressions.size)}").red.bold
    1
  end
end

#save_expectations(project:, all_files:, expectations_path:, notifications:) ⇒ Object



242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
# File 'lib/steep/drivers/check.rb', line 242

def save_expectations(project:, all_files:, expectations_path:, notifications:)
  expectations = if expectations_path.file?
                   Expectations.load(path: expectations_path, content: expectations_path.read)
                 else
                   Expectations.empty()
                 end

  ns = notifications.each.with_object({}) do |notification, hash| #$ Hash[Pathname, Array[Expectations::Diagnostic]]
    path = project.relative_path(Steep::PathHelper.to_pathname(notification[:uri]) || raise)
    hash[path] = notification[:diagnostics].map {|diagnostic| Expectations::Diagnostic.from_lsp(diagnostic) }
  end

  all_files.sort.each do |path|
    ds = ns[path] || []

    if ds.empty?
      expectations.diagnostics.delete(path)
    else
      expectations.diagnostics[path] = ds
    end
  end

  expectations_path.write(expectations.to_yaml)
  stdout.puts Rainbow("Saved expectations in #{expectations_path}...").bold
  0
end