Class: RbsActivesupport::Generator

Inherits:
Object
  • Object
show all
Includes:
AST
Defined in:
lib/rbs_activesupport/generator.rb

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Methods included from AST

#eval_args_with_options, #eval_include_args, #eval_node

Constructor Details

#initialize(pathname, rbs_builder) ⇒ Generator

Returns a new instance of Generator.



20
21
22
23
24
25
26
# File 'lib/rbs_activesupport/generator.rb', line 20

def initialize(pathname, rbs_builder) #: void
  @pathname = pathname

  method_searcher = MethodSearcher.new(rbs_builder)
  resolver = RBS::Resolver::TypeNameResolver.new(rbs_builder.env)
  @declaration_builder = DeclarationBuilder.new(resolver, method_searcher)
end

Instance Attribute Details

#declaration_builderObject (readonly)

: DeclarationBuilder



16
17
18
# File 'lib/rbs_activesupport/generator.rb', line 16

def declaration_builder
  @declaration_builder
end

#pathnameObject (readonly)

: Pathname



15
16
17
# File 'lib/rbs_activesupport/generator.rb', line 15

def pathname
  @pathname
end

Class Method Details

.generate(pathname, rbs_builder) ⇒ Object



7
8
9
10
11
# File 'lib/rbs_activesupport/generator.rb', line 7

def self.generate(pathname, rbs_builder) #: String?
  new(pathname, rbs_builder).generate
rescue StandardError
  warn "Failed to generate RBS for #{pathname}"
end

Instance Method Details

#generateObject

: String?



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/rbs_activesupport/generator.rb', line 28

def generate #: String?
  declarations = parse_source_code
  return if declarations.empty?

  definition = declarations.map do |namespace, method_calls|
    public_decls, private_decls = declaration_builder.build(namespace, method_calls)
    next if public_decls.empty? && private_decls.empty?

    "      # resolve-type-names: false\n\n      \#{header(namespace)}\n      \#{public_decls.join(\"\\n\")}\n\n      \#{\"private\" if private_decls.any?}\n\n      \#{private_decls.join(\"\\n\")}\n\n      \#{footer(namespace)}\n    RBS\n  end.compact.join(\"\\n\")\n\n  return if definition.empty?\n\n  format(definition)\nend\n"