Skip to content

Commit

Permalink
Move parsing as a top level concern of Spoom
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <[email protected]>
  • Loading branch information
Morriar committed Apr 3, 2024
1 parent 622da3b commit 531e524
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 23 deletions.
4 changes: 2 additions & 2 deletions lib/spoom/cli/deadcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -87,9 +87,9 @@ def deadcode(*paths)
content = File.read(file)
content = ERB.new(content).src if file.end_with?(".erb")

tree = Spoom::Deadcode.parse_ruby(content, file: file)
tree = Spoom.parse_ruby(content, file: file)
Spoom::Deadcode.index_node(index, tree, content, file: file, plugins: plugins)
rescue Spoom::Deadcode::ParserError => e
rescue ParseError => e
say_error("Error parsing #{file}: #{e.message}")
next
rescue Spoom::Deadcode::IndexerError => e
Expand Down
21 changes: 2 additions & 19 deletions lib/spoom/deadcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

require_relative "visitor"
require_relative "location"
require_relative "parse"

require_relative "deadcode/erb"
require_relative "deadcode/index"
Expand All @@ -25,8 +26,6 @@ class Error < Spoom::Error
abstract!
end

class ParserError < Error; end

class IndexerError < Error
extend T::Sig

Expand All @@ -40,22 +39,6 @@ def initialize(message, parent:)
class << self
extend T::Sig

sig { params(ruby: String, file: String).returns(Prism::Node) }
def parse_ruby(ruby, file:)
result = Prism.parse(ruby)
unless result.success?
message = +"Error while parsing #{file}:\n"

result.errors.each do |e|
message << "- #{e.message} (at #{e.location.start_line}:#{e.location.start_column})\n"
end

raise ParserError, message
end

result.value
end

sig do
params(
index: Index,
Expand All @@ -74,7 +57,7 @@ def index_node(index, node, ruby, file:, plugins: [])

sig { params(index: Index, ruby: String, file: String, plugins: T::Array[Deadcode::Plugins::Base]).void }
def index_ruby(index, ruby, file:, plugins: [])
node = parse_ruby(ruby, file: file)
node = Spoom.parse_ruby(ruby, file: file)
index_node(index, node, ruby, file: file, plugins: plugins)
end

Expand Down
2 changes: 1 addition & 1 deletion lib/spoom/deadcode/remover.rb
Original file line number Diff line number Diff line change
Expand Up @@ -561,7 +561,7 @@ def find(source, location, kind)
"#{e.message} (at #{e.location.start_line}:#{e.location.start_column})."
end.join(" ")

raise ParserError, "Error while parsing #{location.file}: #{message}"
raise ParseError, "Error while parsing #{location.file}: #{message}"
end

visitor = new(location)
Expand Down
28 changes: 28 additions & 0 deletions lib/spoom/parse.rb
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
# typed: strict
# frozen_string_literal: true

require "spoom/visitor"

module Spoom
class ParseError < Error; end

class << self
extend T::Sig

sig { params(ruby: String, file: String).returns(Prism::Node) }
def parse_ruby(ruby, file:)
result = Prism.parse(ruby)
unless result.success?
message = +"Error while parsing #{file}:\n"

result.errors.each do |e|
message << "- #{e.message} (at #{e.location.start_line}:#{e.location.start_column})\n"
end

raise ParseError, message
end

result.value
end
end
end
2 changes: 1 addition & 1 deletion test/spoom/deadcode/index_definitions_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ def test_index_rescue_parser_error
def foo(
RB

exception = assert_raises(ParserError) do
exception = assert_raises(ParseError) do
deadcode_index
end

Expand Down

0 comments on commit 531e524

Please sign in to comment.