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

Class Method Summary collapse

Class Method Details

.change_sigil_in_file(path, new_strictness) ⇒ Object



72
73
74
75
76
77
78
79
# File 'lib/spoom/sorbet/sigils.rb', line 72

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



83
84
85
86
87
# File 'lib/spoom/sorbet/sigils.rb', line 83

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



63
64
65
66
67
68
# File 'lib/spoom/sorbet/sigils.rb', line 63

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



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

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

.strictness_in_content(content) ⇒ Object



50
51
52
# File 'lib/spoom/sorbet/sigils.rb', line 50

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

.update_sigil(content, new_strictness) ⇒ Object



56
57
58
# File 'lib/spoom/sorbet/sigils.rb', line 56

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

.valid_strictness?(strictness) ⇒ Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/spoom/sorbet/sigils.rb', line 44

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