Created
February 15, 2024 07:44
-
-
Save drwl/bcd6104da2c42efe244a6680c920eb5b to your computer and use it in GitHub Desktop.
unexpected ripper output when subscribing to events
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
file = <<~FILE | |
# typed: strong | |
# == Schema Information | |
# | |
# Table name: users | |
# | |
# id :bigint not null, primary key | |
# | |
module Admin | |
class User < ApplicationRecord | |
end | |
end | |
FILE | |
=begin | |
[10] pry(main)> Ripper.sexp(file) | |
=> [:program, | |
[[:module, | |
[:const_ref, [:@const, "Admin", [9, 7]]], | |
[:bodystmt, | |
[[:void_stmt], | |
[:class, | |
[:const_ref, [:@const, "User", [10, 8]]], | |
[:var_ref, [:@const, "ApplicationRecord", [10, 15]]], | |
[:bodystmt, [[:void_stmt]], nil, nil, nil]]], | |
nil, | |
nil, | |
nil]]]] | |
=end | |
require "ripper" | |
class CustomParser < Ripper | |
# Overview of Ripper: https://kddnewton.com/2022/02/14/formatting-ruby-part-1.html | |
# Ripper API: https://kddnewton.com/ripper-docs/events | |
class << self | |
def parse(string) | |
parser = new(string).tap { |p| p.parse } | |
parser.items | |
end | |
end | |
attr_reader :items | |
def initialize(...) | |
super | |
@items = [] | |
end | |
def on_module(const, _bodystmt)= @items << [const, lineno] | |
def on_class(const, _superclass, _bodystmt)= @items << [const, lineno] | |
end | |
=begin | |
[12] pry(main)> CustomParser.parse(file) | |
=> [["User", 11], ["Admin", 12]] | |
=end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment