2024-03-03 22:21:29 +00:00
|
|
|
# typed: strict
|
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
require_relative "../../../global"
|
|
|
|
require_relative "../../../utils/tty"
|
|
|
|
|
|
|
|
module Tapioca
|
|
|
|
module Compilers
|
|
|
|
class Tty < Tapioca::Dsl::Compiler
|
2024-03-17 20:28:54 +00:00
|
|
|
# FIXME: Enable cop again when https://github.com/sorbet/sorbet/issues/3532 is fixed.
|
|
|
|
# rubocop:disable Style/MutableConstant
|
|
|
|
# This should be a module whose singleton class contains RuboCop::AST::NodePattern::Macros,
|
|
|
|
# but I don't know how to express that in Sorbet.
|
2024-03-03 22:21:29 +00:00
|
|
|
ConstantType = type_member { { fixed: Module } }
|
2024-03-17 20:28:54 +00:00
|
|
|
# rubocop:enable Style/MutableConstant
|
2024-03-03 22:21:29 +00:00
|
|
|
|
|
|
|
sig { override.returns(T::Enumerable[Module]) }
|
|
|
|
def self.gather_constants
|
2024-03-17 20:28:54 +00:00
|
|
|
[::Tty]
|
2024-03-03 22:21:29 +00:00
|
|
|
end
|
|
|
|
|
|
|
|
sig { override.void }
|
|
|
|
def decorate
|
2024-03-15 21:10:08 -07:00
|
|
|
root.create_module(constant.name) do |mod|
|
2024-03-03 22:21:29 +00:00
|
|
|
dynamic_methods = ::Tty::COLOR_CODES.keys + ::Tty::STYLE_CODES.keys + ::Tty::SPECIAL_CODES.keys
|
|
|
|
methods = ::Tty.methods(false).sort.select { |method| dynamic_methods.include?(method) }
|
|
|
|
|
|
|
|
methods.each do |method|
|
|
|
|
return_type = (method.to_s.end_with?("?") ? "T::Boolean" : "String")
|
2024-03-15 21:10:08 -07:00
|
|
|
mod.create_method(method.to_s, return_type:)
|
2024-03-03 22:21:29 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|