Skip to content

Commit 5fbfe87

Browse files
committed
Refactor parse_ruby to always attach & return comments
1 parent 7f232cf commit 5fbfe87

10 files changed

Lines changed: 11 additions & 31 deletions

File tree

lib/spoom/deadcode/index.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ def index_erb(erb, file:, plugins: [])
4949

5050
#: (String rb, file: String, ?plugins: Array[Plugins::Base]) -> void
5151
def index_ruby(rb, file:, plugins: [])
52-
node = Spoom.parse_ruby(rb, file: file, comments: true)
52+
node, _ = Spoom.parse_ruby(rb, file: file)
5353

5454
# Index definitions
5555
model_builder = Model::Builder.new(@model, file)

lib/spoom/parse.rb

Lines changed: 1 addition & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,26 +7,8 @@ module Spoom
77
class ParseError < Error; end
88

99
class << self
10-
#: (String ruby, file: String, ?comments: bool) -> Prism::Node
11-
def parse_ruby(ruby, file:, comments: false)
12-
result = Prism.parse(ruby)
13-
unless result.success?
14-
message = +"Error while parsing #{file}:\n"
15-
16-
result.errors.each do |e|
17-
message << "- #{e.message} (at #{e.location.start_line}:#{e.location.start_column})\n"
18-
end
19-
20-
raise ParseError, message
21-
end
22-
23-
result.attach_comments! if comments
24-
25-
result.value
26-
end
27-
2810
#: (String ruby, file: String) -> [Prism::Node, Array[Prism::Comment]]
29-
def parse_ruby_with_comments(ruby, file:)
11+
def parse_ruby(ruby, file:)
3012
result = Prism.parse(ruby)
3113

3214
unless result.success?

lib/spoom/sorbet/metrics/code_metrics_visitor.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ def collect_code_metrics(files)
1313
counters.increment("files")
1414

1515
content = File.read(file)
16-
node = Spoom.parse_ruby(content, file: file, comments: true)
16+
node, _ = Spoom.parse_ruby(content, file: file)
1717
visitor = CodeMetricsVisitor.new(counters)
1818
visitor.visit(node)
1919
end

lib/spoom/sorbet/translate/rbs_comments_to_sorbet_sigs.rb

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -349,7 +349,8 @@ def apply_type_aliases(comments)
349349
)
350350

351351
@rewriter << Source::Delete.new(from, to)
352-
@rewriter << Source::Insert.new(insert_pos, "#{indent}#{alias_name} = T.type_alias { #{sorbet_type.to_rbi} }\n")
352+
content = "#{indent}#{alias_name} = T.type_alias { #{sorbet_type.to_rbi} }\n"
353+
@rewriter << Source::Insert.new(insert_pos, content)
353354
rescue ::RBS::ParsingError, ::RBI::Error
354355
# Ignore type aliases with errors
355356
next

lib/spoom/sorbet/translate/translator.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def initialize(ruby_contents, file:)
1919
ruby_contents.encode("UTF-8")
2020
end #: String
2121

22-
node, comments = Spoom.parse_ruby_with_comments(ruby_contents, file: file)
22+
node, comments = Spoom.parse_ruby(ruby_contents, file: file)
2323
@node = node #: Prism::Node
2424
@comments = comments #: Array[Prism::Comment]
2525
@ruby_bytes = ruby_contents.bytes #: Array[Integer]

rbi/spoom.rbi

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,8 @@
66

77
module Spoom
88
class << self
9-
sig { params(ruby: ::String, file: ::String, comments: T::Boolean).returns(::Prism::Node) }
10-
def parse_ruby(ruby, file:, comments: T.unsafe(nil)); end
11-
129
sig { params(ruby: ::String, file: ::String).returns([::Prism::Node, T::Array[::Prism::Comment]]) }
13-
def parse_ruby_with_comments(ruby, file:); end
10+
def parse_ruby(ruby, file:); end
1411
end
1512
end
1613

test/spoom/model/builder_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -464,7 +464,7 @@ def m1; end
464464
#: (String rb) -> Model
465465
def model(rb)
466466
file = "foo.rb"
467-
node = Spoom.parse_ruby(rb, file: file, comments: true)
467+
node, _ = Spoom.parse_ruby(rb, file: file)
468468
model = Model.new
469469
builder = Builder.new(model, file)
470470
builder.visit(node)

test/spoom/model/model_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -104,7 +104,7 @@ module E; end
104104

105105
#: (String rb) -> Model
106106
def model(rb)
107-
node = Spoom.parse_ruby(rb, file: "foo.rb", comments: true)
107+
node, _ = Spoom.parse_ruby(rb, file: "foo.rb")
108108

109109
model = Model.new
110110
builder = Builder.new(model, "foo.rb")

test/spoom/model/namespace_visitor_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -101,7 +101,7 @@ module ::M9::M10; end
101101

102102
#: (String rb) -> Hash[String, String]
103103
def namespaces_for_locs(rb)
104-
node = Spoom.parse_ruby(rb, file: "foo.rb")
104+
node, _ = Spoom.parse_ruby(rb, file: "foo.rb")
105105

106106
visitor = NamespacesForLocs.new
107107
visitor.visit(node)

test/spoom/model/references_visitor_test.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -296,7 +296,7 @@ def test_visit_method_operators
296296
private
297297

298298
def visit(code)
299-
node = Spoom.parse_ruby(code, file: "-")
299+
node, _ = Spoom.parse_ruby(code, file: "-")
300300

301301
v = ReferencesVisitor.new("-")
302302
v.visit(node)

0 commit comments

Comments
 (0)