File tree Expand file tree Collapse file tree 6 files changed +53
-9
lines changed
Expand file tree Collapse file tree 6 files changed +53
-9
lines changed Original file line number Diff line number Diff line change @@ -12,3 +12,5 @@ gem "minitest", "~> 5.16"
1212gem "rubocop" , "~> 1.21"
1313
1414gem "thor" , "~> 1.3"
15+
16+ gem "prism" , "~> 1.4"
Original file line number Diff line number Diff line change @@ -46,6 +46,7 @@ PLATFORMS
4646
4747DEPENDENCIES
4848 minitest (~> 5.16 )
49+ prism (~> 1.4 )
4950 rake (~> 13.0 )
5051 rbi_generator !
5152 rubocop (~> 1.21 )
Original file line number Diff line number Diff line change 11require "thor"
2-
2+ require_relative "generator"
33module RbiGenerator
44 # Generate will take a filepath and then we generate the rbi file beside it
55 class CLI < Thor
66 desc "generate FILEPATH" , "Generate an RBI file for the given Ruby file"
77 def generate ( file_path )
8- if File . exist? ( file_path )
9- # Here you would implement the logic to generate the RBI file
10- # For now, we will just print a message
11- puts "Generating RBI for #{ file_path } ..."
12- # Example: File.write("#{file_path}.rbi", "Generated RBI content")
13- else
8+ unless File . exist? ( file_path )
149 puts "File not found: #{ file_path } "
10+ exit ( 1 )
1511 end
16- rescue StandardError => e
17- puts "An error occurred: #{ e . message } "
12+ Generator . new ( file_path ) . generate
1813 end
1914 end
2015end
Original file line number Diff line number Diff line change 1+ # frozen_string_literal: true
2+
3+ require "ripper"
4+ require "prism"
5+ require "pp"
6+
7+ module RbiGenerator
8+ class Generator
9+ def initialize ( file_path )
10+ @file_path = file_path
11+ @rbi_path = @file_path . sub ( /\. rb$/ , ".rbi" )
12+ end
13+
14+ def generate
15+ content = File . read ( @file_path )
16+ ast = Ripper . sexp ( content )
17+ pp ast
18+ class_name = extract_class_name ( ast )
19+ method_names = extract_method_names ( ast )
20+ puts "Class Name: #{ class_name } "
21+ puts "Method Names: #{ method_names . join ( ", " ) } "
22+ # puts "Method Names: #{method_names.join(", ")}"
23+ # puts Ripper.sexp(content)
24+ end
25+
26+ # Takes the syntax tree and extracts the class name
27+ def extract_class_name ( ast )
28+ class_node = ast [ 1 ] . find { |node | node [ 0 ] == :class }
29+ class_node [ 1 ] [ 1 ] if class_node
30+ end
31+
32+ def extract_method_names ( ast )
33+ class_node = ast [ 1 ] . find { |node | node [ 0 ] == :class }
34+ body = class_node [ 3 ] # :bodystmt
35+ return [ ] unless body && body [ 0 ] . is_a? ( Array )
36+
37+ method_defs = body [ 0 ] . select { |node | node [ 0 ] == :def }
38+ method_defs . map { |def_node | def_node [ 1 ] [ 1 ] } # => ["hello", "goodbye"]
39+ end
40+ end
41+ end
Original file line number Diff line number Diff line change 1+ class MyTest
2+ def hello ; end
3+
4+ def test ( x ) ; end
5+ end
You can’t perform that action at this time.
0 commit comments