Module: Spoom::Sorbet::Sigils

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(/^#[[:blank:]]*typed:[[:blank:]]*(\S*)/, Regexp)

Class Method Summary collapse

Class Method Details

.change_sigil_in_file(path, new_strictness) ⇒ Object

changes the sigil in the file at the passed path to the specified new strictness : ((String | Pathname) path, String new_strictness) -> bool



68
69
70
71
72
73
74
75
# File 'lib/spoom/sorbet/sigils.rb', line 68

def 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, encoding: Encoding::ASCII_8BIT)

  strictness_in_content(new_content) == new_strictness
end

.change_sigil_in_files(path_list, new_strictness) ⇒ Object

changes the sigil to have a new strictness in a list of files : (Array path_list, String new_strictness) -> Array



79
80
81
82
83
# File 'lib/spoom/sorbet/sigils.rb', line 79

def 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

returns a string containing the strictness of a sigil in a file at the passed path

  • returns nil if no sigil

: ((String | Pathname) path) -> String?



59
60
61
62
63
64
# File 'lib/spoom/sorbet/sigils.rb', line 59

def file_strictness(path)
  return unless File.file?(path)

  content = File.read(path, encoding: Encoding::ASCII_8BIT)
  strictness_in_content(content)
end

.sigil_string(strictness) ⇒ Object

returns the full sigil comment string for the passed strictness : (String strictness) -> String



34
35
36
# File 'lib/spoom/sorbet/sigils.rb', line 34

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

.strictness_in_content(content) ⇒ Object

returns the strictness of a sigil in the passed file content string (nil if no sigil) : (String content) -> String?



46
47
48
# File 'lib/spoom/sorbet/sigils.rb', line 46

def strictness_in_content(content)
  SIGIL_REGEXP.match(content)&.[](1)
end

.update_sigil(content, new_strictness) ⇒ Object

returns a string which is the passed content but with the sigil updated to a new strictness : (String content, String new_strictness) -> String



52
53
54
# File 'lib/spoom/sorbet/sigils.rb', line 52

def update_sigil(content, new_strictness)
  content.sub(SIGIL_REGEXP, sigil_string(new_strictness))
end

.valid_strictness?(strictness) ⇒ Boolean

returns true if the passed string is a valid strictness (else false) : (String strictness) -> bool

Returns:

  • (Boolean)


40
41
42
# File 'lib/spoom/sorbet/sigils.rb', line 40

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