solargraph_ruby34_fix.rb

 1# frozen_string_literal: true
 2
 3# Monkey-patch to fix Solargraph 0.48.0 bug with Ruby 3.4+
 4# See: https://github.com/castwide/solargraph/issues/733
 5# Fixed in Solargraph PR #735 (merged Jan 7, 2025) but not in 0.48.0
 6# https://github.com/castwide/solargraph/pull/735/files
 7#
 8# Bug: node_range method crashes when passed nil node
 9# This happens, for example,
10# with rescue clauses that don't assign exception to a variable
11# Example: "rescue NameError" instead of "rescue NameError => e"
12
13if defined?(RubyVM::AbstractSyntaxTree)
14	require "solargraph"
15
16	module Solargraph
17		module Parser
18			module Rubyvm
19				module ClassMethods
20					# Fix from PR #735: Add nil check to node_range
21					def node_range(node)
22						if node.nil?
23							nil
24						else
25							st = Position.new(node.first_lineno - 1, node.first_column)
26							en = Position.new(node.last_lineno - 1, node.last_column)
27							Range.new(st, en)
28						end
29					end
30				end
31			end
32		end
33	end
34end