Class: LittleGhost::CodeMode::Javascript::Catalog

Inherits:
CodeMode::Catalog
  • Object
show all
Defined in:
lib/little_ghost/code_mode/javascript/catalog.rb

Overview

:nodoc: all

Defined Under Namespace

Modules: TypeScript

Constant Summary collapse

IDENTIFIER_CHARACTER =
/[^A-Za-z0-9_$]/
RESERVED_NAMES =
%w[exec wait stop].freeze

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(specifications) ⇒ Catalog

Returns a new instance of Catalog.



11
12
13
# File 'lib/little_ghost/code_mode/javascript/catalog.rb', line 11

def initialize(specifications)
  super(specifications, normalize: method(:normalize), reserved: RESERVED_NAMES)
end

Class Method Details

.normalize(name) ⇒ Object



31
32
33
34
35
36
# File 'lib/little_ghost/code_mode/javascript/catalog.rb', line 31

def self.normalize(name)
  normalized = name.to_s.gsub(IDENTIFIER_CHARACTER, "_")
  normalized = "_#{normalized}" unless normalized.match?(/\A[A-Za-z_$]/)
  normalized = "_then" if normalized == "then"
  normalized
end

Instance Method Details

#declarationsObject



21
22
23
24
25
26
27
28
29
# File 'lib/little_ghost/code_mode/javascript/catalog.rb', line 21

def declarations
  body = @definitions.map do |definition|
    description = TypeScript.comment(definition.fetch("description"))
    signature = "#{definition.fetch("name")}(args: " \
      "#{TypeScript.render(definition.fetch("input_schema"))}): Promise<unknown>;"
    [description, signature].compact.join("\n")
  end.join("\n")
  "declare const tools: {\n#{TypeScript.indent(body)}\n};"
end

#host_definitionsObject



15
16
17
18
19
# File 'lib/little_ghost/code_mode/javascript/catalog.rb', line 15

def host_definitions
  @host_definitions ||= @definitions.map do |definition|
    definition.slice("name", "description", "input_schema").freeze
  end.freeze
end

#normalize(name) ⇒ Object



38
# File 'lib/little_ghost/code_mode/javascript/catalog.rb', line 38

def normalize(name) = self.class.normalize(name)