Skip to content

Commit

Permalink
Separate parsing and indexing
Browse files Browse the repository at this point in the history
Signed-off-by: Alexandre Terrasa <alexandre.terrasa@shopify.com>
  • Loading branch information
Morriar committed Mar 6, 2024
1 parent 2b12014 commit 9c9aed2
Showing 1 changed file with 23 additions and 5 deletions.
28 changes: 23 additions & 5 deletions lib/spoom/deadcode.rb
Original file line number Diff line number Diff line change
Expand Up @@ -36,17 +36,35 @@ class IndexerError < Error; end
class << self
extend T::Sig

sig { params(index: Index, ruby: String, file: String, plugins: T::Array[Deadcode::Plugins::Base]).void }
def index_ruby(index, ruby, file:, plugins: [])
node = SyntaxTree.parse(ruby)
visitor = Spoom::Deadcode::Indexer.new(file, ruby, index, plugins: plugins)
visitor.visit(node)
sig { params(ruby: String, file: String).returns(SyntaxTree::Node) }
def parse_ruby(ruby, file:)
SyntaxTree.parse(ruby)
rescue SyntaxTree::Parser::ParseError => e
raise ParserError.new("Error while parsing #{file} (#{e.message} at #{e.lineno}:#{e.column})", parent: e)
end

sig do
params(
index: Index,
node: SyntaxTree::Node,
ruby: String,
file: String,
plugins: T::Array[Deadcode::Plugins::Base],
).void
end
def index_node(index, node, ruby, file:, plugins: [])
visitor = Spoom::Deadcode::Indexer.new(file, ruby, index, plugins: plugins)
visitor.visit(node)
rescue => e
raise IndexerError.new("Error while indexing #{file} (#{e.message})", parent: e)
end

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)
index_node(index, node, ruby, file: file, plugins: plugins)
end

sig { params(index: Index, erb: String, file: String, plugins: T::Array[Deadcode::Plugins::Base]).void }
def index_erb(index, erb, file:, plugins: [])
ruby = ERB.new(erb).src
Expand Down

0 comments on commit 9c9aed2

Please sign in to comment.