From 949ae519cc40ec7a390200aa69ab188d7a4f80c2 Mon Sep 17 00:00:00 2001 From: Kevin Newton Date: Fri, 29 Sep 2023 11:36:15 -0400 Subject: [PATCH] Update to latest parser, use Prism gem instead --- Gemfile | 2 +- Gemfile.lock | 4 ++-- app/models/analyzer.rb | 24 ++++++++++++------------ 3 files changed, 15 insertions(+), 15 deletions(-) diff --git a/Gemfile b/Gemfile index f4e28602..ca3fc77f 100644 --- a/Gemfile +++ b/Gemfile @@ -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" diff --git a/Gemfile.lock b/Gemfile.lock index 7f84403b..cb6806a9 100644 --- a/Gemfile.lock +++ b/Gemfile.lock @@ -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) @@ -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 @@ -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) @@ -357,7 +358,6 @@ DEPENDENCIES view_component (~> 3.6) web-console yard (~> 0.9.34) - yarp (~> 0.12) RUBY VERSION ruby 3.2.2p53 diff --git a/app/models/analyzer.rb b/app/models/analyzer.rb index 330028c4..50524de1 100644 --- a/app/models/analyzer.rb +++ b/app/models/analyzer.rb @@ -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. @@ -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 @@ -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}" @@ -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" @@ -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 @@ -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 @@ -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