Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix Ruby 3.4 #739

Merged
merged 8 commits into from
Jan 18, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/solargraph/parser.rb
Original file line number Diff line number Diff line change
@@ -13,8 +13,8 @@ class SyntaxError < StandardError
# True if the parser can use RubyVM.
#
def self.rubyvm?
!!defined?(RubyVM::AbstractSyntaxTree)
# false
# !!defined?(RubyVM::AbstractSyntaxTree)
false
end

selected = rubyvm? ? Rubyvm : Legacy
4 changes: 3 additions & 1 deletion lib/solargraph/parser/rubyvm/node_methods.rb
Original file line number Diff line number Diff line change
@@ -32,10 +32,12 @@ def pack_name(node)
def infer_literal_node_type node
return nil unless Parser.is_ast_node?(node)
case node.type
when :LIT, :STR
when :LIT, :STR, :SYM
"::#{node.children.first.class.to_s}"
when :DSTR
"::String"
when :INTEGER
'::Integer'
when :ARRAY, :ZARRAY, :LIST, :ZLIST
'::Array'
when :HASH
1 change: 1 addition & 0 deletions lib/solargraph/parser/rubyvm/node_processors.rb
Original file line number Diff line number Diff line change
@@ -58,6 +58,7 @@ module NodeProcessor
register :FOR, Rubyvm::NodeProcessors::BlockNode
register :OP_ASGN_OR, Rubyvm::NodeProcessors::OrasgnNode
register :LIT, Rubyvm::NodeProcessors::LitNode
register :SYM, Rubyvm::NodeProcessors::LitNode
end
end
end
14 changes: 10 additions & 4 deletions lib/solargraph/parser/rubyvm/node_processors/resbody_node.rb
Original file line number Diff line number Diff line change
@@ -22,7 +22,7 @@ def process
locals.push Solargraph::Pin::LocalVariable.new(
location: loc,
closure: region.closure,
name: node.children[1].children.first.children.first.to_s,
name: node.children[1].children.first.to_s,
comments: "@type [#{types.join(',')}]",
presence: presence
)
@@ -34,9 +34,15 @@ def process
private

def exception_variable?
Parser.is_ast_node?(node.children[1]) &&
Parser.is_ast_node?(node.children[1].children.first) &&
node.children[1].children.first.type == :LASGN
if RUBY_VERSION =~ /^3\.4\./
Parser.is_ast_node?(node.children[1]) &&
# Parser.is_ast_node?(node.children[1].children.first) &&
node.children[1].type == :LASGN
else
Parser.is_ast_node?(node.children[1]) &&
Parser.is_ast_node?(node.children[1].children.first) &&
node.children[1].children.first.type == :LASGN
end
end
end
end
4 changes: 2 additions & 2 deletions lib/solargraph/parser/rubyvm/node_processors/send_node.rb
Original file line number Diff line number Diff line change
@@ -43,7 +43,7 @@ def process_visibility
if node.type == :FCALL && Parser.is_ast_node?(node.children.last)
node.children.last.children[0..-2].each do |child|
# next unless child.is_a?(AST::Node) && (child.type == :sym || child.type == :str)
if child.type == :LIT || child.type == :STR
if child.type == :LIT || child.type == :STR || child.type == :SYM
name = child.children[0].to_s
matches = pins.select{ |pin| pin.is_a?(Pin::Method) && pin.name == name && pin.namespace == region.closure.full_context.namespace && pin.context.scope == (region.scope || :instance)}
matches.each do |pin|
@@ -175,7 +175,7 @@ def process_module_function
NodeProcessor.process node.children.last.children[0], region.update(visibility: :module_function), pins, locals
else
node.children.last.children[0..-2].each do |x|
next unless [:LIT, :STR].include?(x.type)
next unless [:LIT, :STR, :SYM].include?(x.type)
cn = x.children[0].to_s
ref = pins.select { |p| p.is_a?(Pin::Method) && p.namespace == region.closure.full_context.namespace && p.name == cn }.first
unless ref.nil?
6 changes: 2 additions & 4 deletions lib/solargraph/range.rb
Original file line number Diff line number Diff line change
@@ -65,10 +65,8 @@ def self.from_to l1, c1, l2, c2
# @param node [RubyVM::AbstractSyntaxTree::Node, Parser::AST::Node]
# @return [Range]
def self.from_node node
if defined?(RubyVM::AbstractSyntaxTree::Node)
if node.is_a?(RubyVM::AbstractSyntaxTree::Node)
Solargraph::Range.from_to(node.first_lineno - 1, node.first_column, node.last_lineno - 1, node.last_column)
end
if Parser.rubyvm? && node.is_a?(RubyVM::AbstractSyntaxTree::Node)
Solargraph::Range.from_to(node.first_lineno - 1, node.first_column, node.last_lineno - 1, node.last_column)
elsif node&.loc && node.loc.expression
from_expr(node.loc.expression)
end
2 changes: 2 additions & 0 deletions lib/solargraph/rbs_map/conversions.rb
Original file line number Diff line number Diff line change
@@ -233,6 +233,8 @@ def method_def_to_sigs decl, pin
end

def parts_of_function type, pin
return [[Solargraph::Pin::Parameter.new(decl: :restarg, name: 'arg', closure: pin)], ComplexType.try_parse(method_type_to_tag(type))] if defined?(RBS::Types::UntypedFunction) && type.type.is_a?(RBS::Types::UntypedFunction)

parameters = []
arg_num = -1
type.type.required_positionals.each do |param|
8 changes: 7 additions & 1 deletion lib/solargraph/rbs_map/core_fills.rb
Original file line number Diff line number Diff line change
@@ -15,6 +15,11 @@ module CoreFills
'then', 'true', 'undef', 'unless', 'until', 'when', 'while', 'yield'
].map { |k| Pin::Keyword.new(k) }

MISSING = [
Solargraph::Pin::Method.new(name: 'tap', scope: :instance, closure: Solargraph::Pin::Namespace.new(name: 'Object')),
Solargraph::Pin::Method.new(name: 'class', scope: :instance, closure: Solargraph::Pin::Namespace.new(name: 'Object'), comments: '@return [Class<self>]')
]

YIELDPARAMS = [
Override.from_comment('Object#tap', %(
@return [self]
@@ -55,7 +60,8 @@ module CoreFills
end
ERRNOS = errnos

ALL = KEYWORDS + YIELDPARAMS + YIELDPARAM_SINGLE_PARAMETERS + CLASS_RETURN_TYPES + ERRNOS
ALL = KEYWORDS + MISSING + YIELDPARAMS + YIELDPARAM_SINGLE_PARAMETERS + CLASS_RETURN_TYPES + ERRNOS
end
end
end

2 changes: 1 addition & 1 deletion lib/solargraph/rbs_map/core_map.rb
Original file line number Diff line number Diff line change
@@ -12,7 +12,7 @@ def initialize
if cache
pins.replace cache
else
loader = RBS::EnvironmentLoader.new(repository: RBS::Repository.new(no_stdlib: true))
loader = RBS::EnvironmentLoader.new(repository: RBS::Repository.new(no_stdlib: false))
environment = RBS::Environment.from_loader(loader).resolve_type_names
environment.declarations.each { |decl| convert_decl_to_pin(decl, Solargraph::Pin::ROOT_PIN) }
pins.concat RbsMap::CoreFills::ALL
3 changes: 3 additions & 0 deletions lib/solargraph/rbs_map/stdlib_map.rb
Original file line number Diff line number Diff line change
@@ -18,6 +18,9 @@ def initialize library
pins.replace cache
else
super
if library == 'yaml'
pins.push Solargraph::Pin::Constant.new(name: 'YAML', comments: '@return [Module<Psych>]', closure: Pin::ROOT_PIN)
end
Cache.save('stdlib', "#{library}.ser", pins)
end
end
118 changes: 59 additions & 59 deletions spec/api_map_spec.rb

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion spec/rbs_map/stdlib_map_spec.rb
Original file line number Diff line number Diff line change
@@ -17,6 +17,6 @@
it 'maps YAML' do
rbs_map = Solargraph::RbsMap::StdlibMap.load('yaml')
pin = rbs_map.path_pin('YAML')
expect(pin).to be_a(Solargraph::Pin::Namespace)
expect(pin).to be_a(Solargraph::Pin::Base)
end
end
6 changes: 3 additions & 3 deletions spec/type_checker/levels/normal_spec.rb
Original file line number Diff line number Diff line change
@@ -868,15 +868,15 @@ def self.foo(*); end

it 'accepts namespace aliases for return tags' do
checker = type_checker(%(
# @return [Mutex]
# @return [Thread::Mutex]
def get_a_mutex; end
))
expect(checker.problems).to be_empty
end

it 'accepts namespace aliases for type tags' do
checker = type_checker(%(
# @type [Mutex]
# @type [Thread::Mutex]
x = get_a_mutex
))
expect(checker.problems).to be_empty
@@ -885,7 +885,7 @@ def get_a_mutex; end
it 'accepts namespace aliases from nested namespaces' do
checker = type_checker(%(
class Foo
# @return [Mutex]
# @return [Thread::Mutex]
def get_a_mutex; end
end
))