Skip to content

Commit

Permalink
Update to latest parser, use Prism gem instead (#96)
Browse files Browse the repository at this point in the history
  • Loading branch information
kddnewton authored Sep 29, 2023
1 parent df75e3f commit 4af2af7
Show file tree
Hide file tree
Showing 3 changed files with 15 additions and 15 deletions.
2 changes: 1 addition & 1 deletion Gemfile
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ gem "httparty", "~> 0.21.0"
gem "view_component", "~> 3.6"

# Interact with the Ruby syntax tree
gem "yarp", "~> 0.12"
gem "prism", "~> 0.13"

# A pure Ruby code highlighter that is compatible with Pygments
gem "rouge", "~> 4.1"
Expand Down
4 changes: 2 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -197,6 +197,7 @@ GEM
ast (~> 2.4.1)
racc
pg (1.5.4)
prism (0.13.0)
psych (5.1.0)
stringio
public_suffix (5.0.3)
Expand Down Expand Up @@ -315,7 +316,6 @@ GEM
websocket-extensions (>= 0.1.0)
websocket-extensions (0.1.5)
yard (0.9.34)
yarp (0.12.0)
zeitwerk (2.6.11)

PLATFORMS
Expand All @@ -338,6 +338,7 @@ DEPENDENCIES
jsbundling-rails
meta-tags
pg (~> 1.5)
prism (~> 0.13)
puma (~> 6.4)
rails (~> 7.1.0.beta1)
rails-html-sanitizer (~> 1.6)
Expand All @@ -357,7 +358,6 @@ DEPENDENCIES
view_component (~> 3.6)
web-console
yard (~> 0.9.34)
yarp (~> 0.12)

RUBY VERSION
ruby 3.2.2p53
Expand Down
24 changes: 12 additions & 12 deletions app/models/analyzer.rb
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# frozen_string_literal: true

class Analyzer
class Visitor < YARP::Visitor
class Visitor < Prism::Visitor
# This object represents a set of comments being returned from the parser
# for a given file. It can be used to quickly access a set of comments at
# a given line number.
Expand Down Expand Up @@ -79,14 +79,14 @@ def with(node)

# Returns the components of the constant path for the given node.
def path_for(node)
case node
when YARP::ConstantPathNode
case node.type
when :constant_path_node
if node.parent
path_for(node.parent).concat(path_for(node.child))
else
[ROOT].concat(path_for(node.child))
end
when YARP::ConstantReadNode
when :constant_read_node
[node.location.slice]
else
:unprocessable
Expand Down Expand Up @@ -151,12 +151,12 @@ def visit_class_node(node)
end

statements =
case node.body
case node.body&.type
when nil
[]
when YARP::StatementsNode
when :statements_node
node.body.body
when YARP::BeginNode
when :begin_node
node.body.statements.body
else
raise "Unexpected statements node: #{node.body.inspect}"
Expand All @@ -169,13 +169,13 @@ def visit_class_node(node)
# being called as a top-level statement inside of a class definition.
statements.each do |statement|
# Only looking at calls.
next unless statement.is_a?(YARP::CallNode)
next unless statement.is_a?(Prism::CallNode)

# Only looking at #include and #extend.
next unless ["include", "extend"].include?(statement.message)

# Only looking for calls that have arguments.
next unless statement.arguments.is_a?(YARP::ArgumentsNode)
next unless statement.arguments.is_a?(Prism::ArgumentsNode)

target =
if statement.message == "include"
Expand Down Expand Up @@ -211,7 +211,7 @@ def visit_class_node(node)
end

def visit_constant_write_node(node)
constant_nesting.with(YARP::ConstantReadNode.new(node.name, node.name_loc)) do |constant_path|
constant_nesting.with(Prism::ConstantReadNode.new(node.name, node.name_loc)) do |constant_path|
analyzer.consts << constant_path.join("::")

super
Expand Down Expand Up @@ -240,7 +240,7 @@ def visit_def_node(node)
case node.receiver
when nil
context.instance_methods << InstanceMethod.new(**kwargs)
when YARP::SelfNode
when Prism::SelfNode
context.class_methods << ClassMethod.new(**kwargs)
else # rubocop:disable Style/EmptyElse
# In this case, we don't actually know what to do. The receiver of the
Expand Down Expand Up @@ -356,7 +356,7 @@ def to_s
end

def analyze_code(path, code)
result = YARP.parse(code)
result = Prism.parse(code)
result.value.accept(Visitor.new(self, @gem, path, result.comments))
self
end
Expand Down

0 comments on commit 4af2af7

Please sign in to comment.