module Spoom::Sorbet::Sigils

Constants

SIGIL_REGEXP
STRICTNESS_FALSE
STRICTNESS_IGNORE
STRICTNESS_INTERNAL
STRICTNESS_STRICT
STRICTNESS_STRONG
STRICTNESS_TRUE
VALID_STRICTNESS

Public Class Methods

change_sigil_in_file(path, new_strictness) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 65
def self.change_sigil_in_file(path, new_strictness)
  content = File.read(path, encoding: Encoding::ASCII_8BIT)
  new_content = update_sigil(content, new_strictness)

  File.write(path, new_content)

  strictness_in_content(new_content) == new_strictness
end
change_sigil_in_files(path_list, new_strictness) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 76
def self.change_sigil_in_files(path_list, new_strictness)
  path_list.filter do |path|
    change_sigil_in_file(path, new_strictness)
  end
end
file_strictness(path) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 57
def self.file_strictness(path)
  return nil unless File.file?(path)
  content = File.read(path, encoding: Encoding::ASCII_8BIT)
  strictness_in_content(content)
end
files_with_sigil_strictness(directory, strictness, extension: ".rb") click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 90
def self.files_with_sigil_strictness(directory, strictness, extension: ".rb")
  paths = Dir.glob("#{File.expand_path(directory)}/**/*#{extension}").sort.uniq
  paths.filter do |path|
    file_strictness(path) == strictness
  end
end
sigil_string(strictness) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 32
def self.sigil_string(strictness)
  "# typed: #{strictness}"
end
strictness_in_content(content) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 44
def self.strictness_in_content(content)
  SIGIL_REGEXP.match(content)&.[](1)
end
update_sigil(content, new_strictness) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 50
def self.update_sigil(content, new_strictness)
  content.sub(SIGIL_REGEXP, sigil_string(new_strictness))
end
valid_strictness?(strictness) click to toggle source
# File lib/spoom/sorbet/sigils.rb, line 38
def self.valid_strictness?(strictness)
  VALID_STRICTNESS.include?(strictness.strip)
end