Skip to content

Commit

Permalink
Migrate to Prism (#1090)
Browse files Browse the repository at this point in the history
* Migrate to Prism

* Update ShowSyntaxTreeTest

* Remove obsolete submodule

* Remove obsolete overrides
  • Loading branch information
st0012 authored Oct 13, 2023
1 parent 71e5cb3 commit 28955f3
Show file tree
Hide file tree
Showing 52 changed files with 22,328 additions and 20,264 deletions.
2 changes: 1 addition & 1 deletion .github/dependabot.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ updates:
- "minor"
- "patch"
exclude-patterns:
- "yarp"
- "prism"
- package-ecosystem: "gitsubmodule"
directory: "/"
schedule:
Expand Down
6 changes: 3 additions & 3 deletions .gitmodules
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
[submodule "test/fixtures/yarp"]
path = test/fixtures/yarp
url = https://github.com/ruby/yarp.git
[submodule "test/fixtures/prism"]
path = test/fixtures/prism
url = https://github.com/ruby/prism.git
2 changes: 1 addition & 1 deletion ADDONS.md
Original file line number Diff line number Diff line change
Expand Up @@ -157,7 +157,7 @@ module RubyLsp

# Listeners must define methods for each event they registered with the emitter. In this case, we have to define
# `on_const` to specify what this listener should do every time we find a constant
sig { params(node: YARP::ConstantReadNode).void }
sig { params(node: Prism::ConstantReadNode).void }
def on_constant_read(node)
# Certain helpers are made available to listeners to build LSP responses. The classes under `RubyLsp::Interface`
# are generally used to build responses and they match exactly what the specification requests.
Expand Down
5 changes: 3 additions & 2 deletions Gemfile.lock
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ PATH
specs:
ruby-lsp (0.11.2)
language_server-protocol (~> 3.17.0)
prism (>= 0.13, < 0.14)
sorbet-runtime (>= 0.5.5685)
yarp (>= 0.12, < 0.13)

GEM
remote: https://rubygems.org/
Expand Down Expand Up @@ -37,6 +37,7 @@ GEM
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
prism (0.13.0)
psych (5.1.0)
stringio
racc (1.7.1)
Expand Down Expand Up @@ -106,7 +107,7 @@ GEM
yard-sorbet (0.8.1)
sorbet-runtime (>= 0.5)
yard (>= 0.9)
yarp (0.12.0)
yarp (0.13.0)

PLATFORMS
arm64-darwin
Expand Down
2 changes: 1 addition & 1 deletion Rakefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ require "ruby_lsp/check_docs"
Rake::TestTask.new(:test) do |t|
t.libs << "test"
t.libs << "lib"
t.test_files = FileList["test/**/*_test.rb", "lib/ruby_indexer/test/**/*_test.rb"].exclude("test/fixtures/yarp/**/*")
t.test_files = FileList["test/**/*_test.rb", "lib/ruby_indexer/test/**/*_test.rb"].exclude("test/fixtures/prism/**/*")
end

RDoc::Task.new do |rdoc|
Expand Down
2 changes: 1 addition & 1 deletion bin/test
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
#!/usr/bin/env bash

PRISM_FIXTURES_DIR=test/fixtures/yarp/test/prism/fixtures
PRISM_FIXTURES_DIR=test/fixtures/prism/test/prism/fixtures

if [ ! -d "$PRISM_FIXTURES_DIR" ]; then
echo "$PRISM_FIXTURES_DIR does not exist."
Expand Down
8 changes: 4 additions & 4 deletions lib/ruby_indexer/lib/ruby_indexer/index.rb
Original file line number Diff line number Diff line change
Expand Up @@ -179,7 +179,7 @@ def index_all(indexable_paths: RubyIndexer.configuration.indexables, &block)
sig { params(indexable_path: IndexablePath, source: T.nilable(String)).void }
def index_single(indexable_path, source = nil)
content = source || File.read(indexable_path.full_path)
result = YARP.parse(content)
result = Prism.parse(content)
visitor = IndexVisitor.new(self, result, indexable_path.full_path)
result.value.accept(visitor)

Expand Down Expand Up @@ -262,7 +262,7 @@ class Entry
sig { returns(String) }
attr_reader :file_path

sig { returns(YARP::Location) }
sig { returns(Prism::Location) }
attr_reader :location

sig { returns(T::Array[String]) }
Expand All @@ -271,7 +271,7 @@ class Entry
sig { returns(Symbol) }
attr_accessor :visibility

sig { params(name: String, file_path: String, location: YARP::Location, comments: T::Array[String]).void }
sig { params(name: String, file_path: String, location: Prism::Location, comments: T::Array[String]).void }
def initialize(name, file_path, location, comments)
@name = name
@file_path = file_path
Expand Down Expand Up @@ -326,7 +326,7 @@ class UnresolvedAlias < Entry
nesting: T::Array[String],
name: String,
file_path: String,
location: YARP::Location,
location: Prism::Location,
comments: T::Array[String],
).void
end
Expand Down
90 changes: 45 additions & 45 deletions lib/ruby_indexer/lib/ruby_indexer/visitor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@
# frozen_string_literal: true

module RubyIndexer
class IndexVisitor < YARP::Visitor
class IndexVisitor < Prism::Visitor
extend T::Sig

sig { params(index: Index, parse_result: YARP::ParseResult, file_path: String).void }
sig { params(index: Index, parse_result: Prism::ParseResult, file_path: String).void }
def initialize(index, parse_result, file_path)
@index = index
@file_path = file_path
Expand All @@ -14,125 +14,125 @@ def initialize(index, parse_result, file_path)
parse_result.comments.to_h do |c|
[c.location.start_line, c]
end,
T::Hash[Integer, YARP::Comment],
T::Hash[Integer, Prism::Comment],
)

super()
end

sig { override.params(node: YARP::ClassNode).void }
sig { override.params(node: Prism::ClassNode).void }
def visit_class_node(node)
add_index_entry(node, Index::Entry::Class)
end

sig { override.params(node: YARP::ModuleNode).void }
sig { override.params(node: Prism::ModuleNode).void }
def visit_module_node(node)
add_index_entry(node, Index::Entry::Module)
end

sig { override.params(node: YARP::MultiWriteNode).void }
sig { override.params(node: Prism::MultiWriteNode).void }
def visit_multi_write_node(node)
value = node.value
values = value.is_a?(YARP::ArrayNode) && value.opening_loc ? value.elements : []
values = value.is_a?(Prism::ArrayNode) && value.opening_loc ? value.elements : []

node.targets.each_with_index do |target, i|
current_value = values[i]
# The moment we find a splat on the right hand side of the assignment, we can no longer figure out which value
# gets assigned to what
values.clear if current_value.is_a?(YARP::SplatNode)
values.clear if current_value.is_a?(Prism::SplatNode)

case target
when YARP::ConstantTargetNode
when Prism::ConstantTargetNode
add_constant(target, fully_qualify_name(target.name.to_s), current_value)
when YARP::ConstantPathTargetNode
when Prism::ConstantPathTargetNode
add_constant(target, fully_qualify_name(target.slice), current_value)
end
end
end

sig { override.params(node: YARP::ConstantPathWriteNode).void }
sig { override.params(node: Prism::ConstantPathWriteNode).void }
def visit_constant_path_write_node(node)
# ignore variable constants like `var::FOO` or `self.class::FOO`
target = node.target
return unless target.parent.nil? || target.parent.is_a?(YARP::ConstantReadNode)
return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

name = fully_qualify_name(target.location.slice)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantPathOrWriteNode).void }
sig { override.params(node: Prism::ConstantPathOrWriteNode).void }
def visit_constant_path_or_write_node(node)
# ignore variable constants like `var::FOO` or `self.class::FOO`
target = node.target
return unless target.parent.nil? || target.parent.is_a?(YARP::ConstantReadNode)
return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

name = fully_qualify_name(target.location.slice)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantPathOperatorWriteNode).void }
sig { override.params(node: Prism::ConstantPathOperatorWriteNode).void }
def visit_constant_path_operator_write_node(node)
# ignore variable constants like `var::FOO` or `self.class::FOO`
target = node.target
return unless target.parent.nil? || target.parent.is_a?(YARP::ConstantReadNode)
return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

name = fully_qualify_name(target.location.slice)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantPathAndWriteNode).void }
sig { override.params(node: Prism::ConstantPathAndWriteNode).void }
def visit_constant_path_and_write_node(node)
# ignore variable constants like `var::FOO` or `self.class::FOO`
target = node.target
return unless target.parent.nil? || target.parent.is_a?(YARP::ConstantReadNode)
return unless target.parent.nil? || target.parent.is_a?(Prism::ConstantReadNode)

name = fully_qualify_name(target.location.slice)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantWriteNode).void }
sig { override.params(node: Prism::ConstantWriteNode).void }
def visit_constant_write_node(node)
name = fully_qualify_name(node.name.to_s)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantOrWriteNode).void }
sig { override.params(node: Prism::ConstantOrWriteNode).void }
def visit_constant_or_write_node(node)
name = fully_qualify_name(node.name.to_s)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantAndWriteNode).void }
sig { override.params(node: Prism::ConstantAndWriteNode).void }
def visit_constant_and_write_node(node)
name = fully_qualify_name(node.name.to_s)
add_constant(node, name)
end

sig { override.params(node: YARP::ConstantOperatorWriteNode).void }
sig { override.params(node: Prism::ConstantOperatorWriteNode).void }
def visit_constant_operator_write_node(node)
name = fully_qualify_name(node.name.to_s)
add_constant(node, name)
end

sig { override.params(node: YARP::CallNode).void }
sig { override.params(node: Prism::CallNode).void }
def visit_call_node(node)
message = node.message
handle_private_constant(node) if message == "private_constant"
end

private

sig { params(node: YARP::CallNode).void }
sig { params(node: Prism::CallNode).void }
def handle_private_constant(node)
arguments = node.arguments&.arguments
return unless arguments

first_argument = arguments.first

name = case first_argument
when YARP::StringNode
when Prism::StringNode
first_argument.content
when YARP::SymbolNode
when Prism::SymbolNode
first_argument.value
end

Expand All @@ -150,37 +150,37 @@ def handle_private_constant(node)
sig do
params(
node: T.any(
YARP::ConstantWriteNode,
YARP::ConstantOrWriteNode,
YARP::ConstantAndWriteNode,
YARP::ConstantOperatorWriteNode,
YARP::ConstantPathWriteNode,
YARP::ConstantPathOrWriteNode,
YARP::ConstantPathOperatorWriteNode,
YARP::ConstantPathAndWriteNode,
YARP::ConstantTargetNode,
YARP::ConstantPathTargetNode,
Prism::ConstantWriteNode,
Prism::ConstantOrWriteNode,
Prism::ConstantAndWriteNode,
Prism::ConstantOperatorWriteNode,
Prism::ConstantPathWriteNode,
Prism::ConstantPathOrWriteNode,
Prism::ConstantPathOperatorWriteNode,
Prism::ConstantPathAndWriteNode,
Prism::ConstantTargetNode,
Prism::ConstantPathTargetNode,
),
name: String,
value: T.nilable(YARP::Node),
value: T.nilable(Prism::Node),
).void
end
def add_constant(node, name, value = nil)
value = node.value unless node.is_a?(YARP::ConstantTargetNode) || node.is_a?(YARP::ConstantPathTargetNode)
value = node.value unless node.is_a?(Prism::ConstantTargetNode) || node.is_a?(Prism::ConstantPathTargetNode)
comments = collect_comments(node)

@index << case value
when YARP::ConstantReadNode, YARP::ConstantPathNode
when Prism::ConstantReadNode, Prism::ConstantPathNode
Index::Entry::UnresolvedAlias.new(value.slice, @stack.dup, name, @file_path, node.location, comments)
when YARP::ConstantWriteNode, YARP::ConstantAndWriteNode, YARP::ConstantOrWriteNode,
YARP::ConstantOperatorWriteNode
when Prism::ConstantWriteNode, Prism::ConstantAndWriteNode, Prism::ConstantOrWriteNode,
Prism::ConstantOperatorWriteNode

# If the right hand side is another constant assignment, we need to visit it because that constant has to be
# indexed too
visit(value)
Index::Entry::UnresolvedAlias.new(value.name.to_s, @stack.dup, name, @file_path, node.location, comments)
when YARP::ConstantPathWriteNode, YARP::ConstantPathOrWriteNode, YARP::ConstantPathOperatorWriteNode,
YARP::ConstantPathAndWriteNode
when Prism::ConstantPathWriteNode, Prism::ConstantPathOrWriteNode, Prism::ConstantPathOperatorWriteNode,
Prism::ConstantPathAndWriteNode

visit(value)
Index::Entry::UnresolvedAlias.new(value.target.slice, @stack.dup, name, @file_path, node.location, comments)
Expand All @@ -189,7 +189,7 @@ def add_constant(node, name, value = nil)
end
end

sig { params(node: T.any(YARP::ClassNode, YARP::ModuleNode), klass: T.class_of(Index::Entry)).void }
sig { params(node: T.any(Prism::ClassNode, Prism::ModuleNode), klass: T.class_of(Index::Entry)).void }
def add_index_entry(node, klass)
name = node.constant_path.location.slice

Expand All @@ -204,7 +204,7 @@ def add_index_entry(node, klass)
@stack.pop
end

sig { params(node: YARP::Node).returns(T::Array[String]) }
sig { params(node: Prism::Node).returns(T::Array[String]) }
def collect_comments(node)
comments = []

Expand Down
Loading

0 comments on commit 28955f3

Please sign in to comment.