Skip to content

Commit 39cd630

Browse files
committed
Refactor comment collection in get_rbs_comment_before
1 parent 93ea246 commit 39cd630

1 file changed

Lines changed: 7 additions & 12 deletions

File tree

lib/typeprof/core/ast/method.rb

Lines changed: 7 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -2,28 +2,23 @@ module TypeProf::Core
22
class AST
33
def self.get_rbs_comment_before(raw_node, lenv)
44
comments = Fiber[:comments]
5-
i = comments.bsearch_index {|comment| comment.location.start_line >= raw_node.location.start_line } || comments.size
6-
lineno = raw_node.location.start_line
5+
node_line = raw_node.location.start_line
6+
last_comment_index = comments.bsearch_index {|c| c.location.start_line >= node_line } || comments.size
77
rbs_comments = []
8-
while i > 0
9-
i -= 1
10-
lineno -= 1
8+
expected_line = node_line - 1
9+
(last_comment_index - 1).downto(0) do |i|
1110
comment = comments[i]
11+
break unless comment.location.start_line == expected_line && comment.location.slice.start_with?("#:")
1212
comment_loc = comment.location
1313
comment_text = comment_loc.slice
14-
if comment_loc.start_line == lineno && comment_text.start_with?("#:")
15-
rbs_comments[comment_loc.start_line - 1] = " " * (comment_loc.start_column + 2) + comment_text[2..]
16-
else
17-
break
18-
end
14+
rbs_comments[comment_loc.start_line - 1] = " " * (comment_loc.start_column + 2) + comment_text[2..]
15+
expected_line -= 1
1916
end
2017
return nil if rbs_comments.empty?
2118
rbs_comments = rbs_comments.map {|line| line || "" }.join("\n")
2219
method_type = RBS::Parser.parse_method_type(rbs_comments)
2320
if method_type
2421
AST.create_rbs_func_type(method_type, method_type.type_params, method_type.block, lenv)
25-
else
26-
nil
2722
end
2823
rescue RBS::ParsingError
2924
# TODO: report the error

0 commit comments

Comments
 (0)