Module: RbsMacros
- Defined in:
- lib/rbs_macros.rb,
lib/rbs_macros/macro.rb,
lib/rbs_macros/config.rb,
lib/rbs_macros/project.rb,
lib/rbs_macros/version.rb,
lib/rbs_macros/exec_ctx.rb,
lib/rbs_macros/environment.rb,
lib/rbs_macros/meta_module.rb,
lib/rbs_macros/library_registry.rb,
lib/rbs_macros/macros/forwardable.rb
Overview
RbsMacros is a utility that looks for metaprogramming-related method invocation in your Ruby code and generates RBS files for them.
Defined Under Namespace
Modules: Macros Classes: AbstractProject, Config, Environment, ExecCtx, FakeProject, LibraryRegistry, Macro, MetaModule, Project
Constant Summary collapse
- VERSION =
"0.1.0"
- MetaClass =
MetaModule
Class Method Summary collapse
Class Method Details
.run(&block) ⇒ Object
18 19 20 21 22 23 24 25 26 27 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 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 |
# File 'lib/rbs_macros.rb', line 18 def self.run(&block) config = Config.new block&.(config) env = Environment.new config.loader.load(env: env.rbs) if config.use_loader config.macros.each do |macro| macro.setup(env) end config.project.glob(ext: ".rbs", include: config.sigs, exclude: [config.output_dir]) do |filename| source = config.project.read(filename) buffer = RBS::Buffer.new(name: filename, content: source) _, directives, decls = RBS::Parser.parse_signature(buffer) env.rbs.add_signature(buffer:, directives:, decls:) end # TODO: streamline this private method invocation env.instance_variable_set(:@rbs, env.rbs.resolve_type_names) config.project.glob(ext: ".rb", include: config.load_dirs, exclude: []) do |filename| relative_filename = filename config.load_dirs.each do |load_dir| if filename.start_with?("#{load_dir}/") relative_filename = filename.delete_prefix("#{load_dir}/") break end end source = config.project.read(filename) env.(source, filename: relative_filename.sub(/\.rb\z/, "")) end files = {} # : Hash[String, Array[RBS::AST::Declarations::t]] env.decls.each do |entry| file_decls = (files[entry.file] ||= []) current_mod = env.object_class container = nil # : (RBS::AST::Declarations::Module | RBS::AST::Declarations::Class)? (entry.mod.name || "").split("::").each do |name| inner_mod = current_mod.(name.to_sym) raise "Not found: #{current_mod.name}::#{name}" unless inner_mod raise "Not a module: #{current_mod.name}::#{name}" unless inner_mod.is_a?(MetaModule) current_mod = inner_mod current_decls = container&.members || file_decls container = nil current_decls.each do |decl| if (decl.is_a?(RBS::AST::Declarations::Class) || decl.is_a?(RBS::AST::Declarations::Module)) \ && decl.name.name == name.to_sym container = decl end end next if container if inner_mod.is_class container = c = RBS::AST::Declarations::Class.new( name: RBS::TypeName.new(name: name.to_sym, namespace: RBS::Namespace.empty), type_params: [], super_class: nil, members: [], annotations: [], location: nil, comment: nil ) current_decls << c else container = m = RBS::AST::Declarations::Module.new( name: RBS::TypeName.new(name: name.to_sym, namespace: RBS::Namespace.empty), type_params: [], members: [], self_types: [], annotations: [], location: nil, comment: nil ) current_decls << m end end if container container.members << entry.declaration else d = entry.declaration case d when RBS::AST::Declarations::Class, RBS::AST::Declarations::Module, RBS::AST::Declarations::Interface, RBS::AST::Declarations::Constant, RBS::AST::Declarations::Global, RBS::AST::Declarations::TypeAlias, RBS::AST::Declarations::ClassAlias, RBS::AST::Declarations::ModuleAlias file_decls << d else raise "Not allowed here: #{d.class}" end end end decls = [] # : Array[RBS::AST::Declarations::t] env.object_class..each do |name| value = env.object_class.(name) next unless value.is_a?(MetaModule) decls << RBS::AST::Declarations::Module.new( name: RBS::TypeName.new(name:, namespace: RBS::Namespace.empty), type_params: [], members: [], self_types: [], annotations: [], location: nil, comment: nil ) end files.each do |filename, file_decls| out = StringIO.new(+"", "w") writer = RBS::Writer.new(out:) writer.write file_decls config.project.write("#{config.output_dir}/#{filename}.rbs", out.string) end end |