lib/rbi/parser.rb in rbi-0.1.0 vs lib/rbi/parser.rb in rbi-0.1.1
- old
+ new
@@ -93,11 +93,14 @@
sig { params(source: String, file: String).returns(Tree) }
def parse(source, file:)
result = YARP.parse(source)
unless result.success?
- raise ParseError.new(result.errors.map(&:message).join(" "), Loc.from_yarp(file, result.errors.first.location))
+ message = result.errors.map { |e| "#{e.message}." }.join(" ")
+ error = result.errors.first
+ location = Loc.new(file: file, begin_line: error.location.start_line, begin_column: error.location.start_column)
+ raise ParseError.new(message, location)
end
visitor = TreeBuilder.new(source, comments: result.comments, file: file)
visitor.visit(result.value)
visitor.tree
@@ -213,11 +216,11 @@
struct
elsif type_variable_definition?(node.value)
TypeMember.new(
case node
when YARP::ConstantWriteNode
- node.name
+ node.name.to_s
when YARP::ConstantPathWriteNode
node_string!(node.target)
end,
node_string!(node.value),
loc: node_loc(node),
@@ -225,11 +228,11 @@
)
else
Const.new(
case node
when YARP::ConstantWriteNode
- node.name
+ node.name.to_s
when YARP::ConstantPathWriteNode
node_string!(node.target)
end,
node_string!(node.value),
loc: node_loc(node),
@@ -239,11 +242,11 @@
end
sig { override.params(node: YARP::DefNode).void }
def visit_def_node(node)
current_scope << Method.new(
- node.name,
+ node.name.to_s,
params: parse_params(node.parameters),
sigs: current_sigs,
loc: node_loc(node),
comments: current_sigs_comments + node_comments(node),
is_singleton: !!node.receiver,
@@ -334,11 +337,11 @@
body = block.body
return unless body.is_a?(YARP::StatementsNode)
current_scope << TEnumBlock.new(
- body.body.map { |stmt| T.cast(stmt, YARP::ConstantWriteNode).name },
+ body.body.map { |stmt| T.cast(stmt, YARP::ConstantWriteNode).name.to_s },
loc: node_loc(node),
comments: node_comments(node),
)
when "extend"
args = node.arguments
@@ -547,11 +550,11 @@
end
rest = node.rest
if rest.is_a?(YARP::RestParameterNode)
params << RestParam.new(
- rest.name || "*args",
+ rest.name&.to_s || "*args",
loc: node_loc(rest),
comments: node_comments(rest),
)
end
@@ -559,37 +562,37 @@
next unless param.is_a?(YARP::KeywordParameterNode)
value = param.value
params << if value
KwOptParam.new(
- param.name.delete_suffix(":"),
+ param.name.to_s.delete_suffix(":"),
node_string!(value),
loc: node_loc(param),
comments: node_comments(param),
)
else
KwParam.new(
- param.name.delete_suffix(":"),
+ param.name.to_s.delete_suffix(":"),
loc: node_loc(param),
comments: node_comments(param),
)
end
end
rest_kw = node.keyword_rest
if rest_kw.is_a?(YARP::KeywordRestParameterNode)
params << KwRestParam.new(
- rest_kw.name || "**kwargs",
+ rest_kw.name&.to_s || "**kwargs",
loc: node_loc(rest_kw),
comments: node_comments(rest_kw),
)
end
block = node.block
if block.is_a?(YARP::BlockParameterNode)
params << BlockParam.new(
- block.name || "&block",
+ block.name&.to_s || "&block",
loc: node_loc(block),
comments: node_comments(block),
)
end
@@ -640,11 +643,11 @@
end
end
name = case node
when YARP::ConstantWriteNode
- node.name
+ node.name.to_s
when YARP::ConstantPathWriteNode
node_string!(node.target)
end
loc = node_loc(node)
@@ -742,10 +745,10 @@
class SigBuilder < Visitor
extend T::Sig
sig { returns(Sig) }
- attr_accessor :current
+ attr_reader :current
sig { params(content: String, file: String).void }
def initialize(content, file:)
super