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 =
[
  STRICTNESS_IGNORE,
  STRICTNESS_FALSE,
  STRICTNESS_TRUE,
  STRICTNESS_STRICT,
  STRICTNESS_STRONG,
  STRICTNESS_INTERNAL,
].freeze
SIGIL_REGEXP =

: Array

/^#[[:blank:]]*typed:[[:blank:]]*(\S*)/

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



65
66
67
68
69
70
71
72
# File 'lib/spoom/sorbet/sigils.rb', line 65

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



76
77
78
79
80
# File 'lib/spoom/sorbet/sigils.rb', line 76

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?



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

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



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

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?



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

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



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

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)


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

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