Module: Spoom::Sorbet::Sigils

Extended by:
T::Sig
Defined in:
lib/spoom/sorbet/sigils.rb

Constant Summary collapse

STRICTNESS_IGNORE =
"ignore"
STRICTNESS_FALSE =
"false"
STRICTNESS_TRUE =
"true"
STRICTNESS_STRICT =
"strict"
STRICTNESS_STRONG =
"strong"
STRICTNESS_INTERNAL =
"__STDLIB_INTERNAL"
VALID_STRICTNESS =
T.let([
  STRICTNESS_IGNORE,
  STRICTNESS_FALSE,
  STRICTNESS_TRUE,
  STRICTNESS_STRICT,
  STRICTNESS_STRONG,
  STRICTNESS_INTERNAL,
].freeze, T::Array[String])
SIGIL_REGEXP =
T.let(/^#\s*typed\s*:\s*(\w*)\s*$/.freeze, Regexp)

Class Method Summary collapse

Class Method Details

.change_sigil_in_file(path, new_strictness) ⇒ Object



65
66
67
68
69
70
71
72
# 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) ⇒ Object



76
77
78
79
80
# 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) ⇒ Object



57
58
59
60
61
# 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") ⇒ Object



90
91
92
93
94
95
# 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) ⇒ Object



32
33
34
# File 'lib/spoom/sorbet/sigils.rb', line 32

def self.sigil_string(strictness)
  "# typed: #{strictness}"
end

.strictness_in_content(content) ⇒ Object



44
45
46
# 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) ⇒ Object



50
51
52
# 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) ⇒ Boolean

Returns:

  • (Boolean)


38
39
40
# File 'lib/spoom/sorbet/sigils.rb', line 38

def self.valid_strictness?(strictness)
  VALID_STRICTNESS.include?(strictness.strip)
end