Skip to content

Commit 10eda01

Browse files
committed
playing with Ripper
1 parent 1c08e3f commit 10eda01

File tree

6 files changed

+53
-9
lines changed

6 files changed

+53
-9
lines changed

Gemfile

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,3 +12,5 @@ gem "minitest", "~> 5.16"
1212
gem "rubocop", "~> 1.21"
1313

1414
gem "thor", "~> 1.3"
15+
16+
gem "prism", "~> 1.4"

Gemfile.lock

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,7 @@ PLATFORMS
4646

4747
DEPENDENCIES
4848
minitest (~> 5.16)
49+
prism (~> 1.4)
4950
rake (~> 13.0)
5051
rbi_generator!
5152
rubocop (~> 1.21)

exe/rbi_generator

100644100755
File mode changed.

lib/rbi_generator/cli.rb

Lines changed: 4 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,15 @@
11
require "thor"
2-
2+
require_relative "generator"
33
module 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
2015
end

lib/rbi_generator/generator.rb

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
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

test.rb

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
class MyTest
2+
def hello; end
3+
4+
def test(x); end
5+
end

0 commit comments

Comments
 (0)