Skip to content

Commit

Permalink
Merge pull request #527 from Shopify/at-deadcode-prism
Browse files Browse the repository at this point in the history
Migrate dead code detection to Prism
  • Loading branch information
Morriar committed Mar 15, 2024
2 parents cbb9789 + 35b15d8 commit 6374c3b
Show file tree
Hide file tree
Showing 56 changed files with 37,033 additions and 40,430 deletions.
5 changes: 1 addition & 4 deletions Gemfile.lock
Expand Up @@ -3,8 +3,8 @@ PATH
specs:
spoom (1.2.4)
erubi (>= 1.10.0)
prism (>= 0.19.0)
sorbet-static-and-runtime (>= 0.5.10187)
syntax_tree (>= 6.1.1)
thor (>= 0.19.2)

GEM
Expand Down Expand Up @@ -34,7 +34,6 @@ GEM
parser (3.3.0.5)
ast (~> 2.4.1)
racc
prettier_print (1.2.1)
prism (0.19.0)
psych (5.1.2)
stringio
Expand Down Expand Up @@ -77,8 +76,6 @@ GEM
sorbet (= 0.5.11287)
sorbet-runtime (= 0.5.11287)
stringio (3.1.0)
syntax_tree (6.2.0)
prettier_print (>= 1.2.0)
tapioca (0.12.0)
bundler (>= 2.2.25)
netrc (>= 0.11.0)
Expand Down
32 changes: 22 additions & 10 deletions lib/spoom/deadcode.rb
Expand Up @@ -2,8 +2,9 @@
# frozen_string_literal: true

require "erubi"
require "syntax_tree"
require "prism"

require_relative "deadcode/visitor"
require_relative "deadcode/erb"
require_relative "deadcode/index"
require_relative "deadcode/indexer"
Expand All @@ -18,10 +19,15 @@
module Spoom
module Deadcode
class Error < Spoom::Error
extend T::Sig
extend T::Helpers

abstract!
end

class ParserError < Error; end

class IndexerError < Error
extend T::Sig

sig { params(message: String, parent: Exception).void }
def initialize(message, parent:)
Expand All @@ -30,23 +36,29 @@ def initialize(message, parent:)
end
end

class ParserError < Error; end
class IndexerError < Error; end

class << self
extend T::Sig

sig { params(ruby: String, file: String).returns(SyntaxTree::Node) }
sig { params(ruby: String, file: String).returns(Prism::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)
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,
node: SyntaxTree::Node,
node: Prism::Node,
ruby: String,
file: String,
plugins: T::Array[Deadcode::Plugins::Base],
Expand Down

0 comments on commit 6374c3b

Please sign in to comment.