lib/spoom/deadcode/location.rb in spoom-1.2.3 vs lib/spoom/deadcode/location.rb in spoom-1.2.4

- old
+ new

@@ -11,10 +11,27 @@ class LocationError < Spoom::Error; end class << self extend T::Sig + sig { params(location_string: String).returns(Location) } + def from_string(location_string) + file, rest = location_string.split(":", 2) + raise LocationError, "Invalid location string: #{location_string}" unless file && rest + + start_line, rest = rest.split(":", 2) + raise LocationError, "Invalid location string: #{location_string}" unless start_line && rest + + start_column, rest = rest.split("-", 2) + raise LocationError, "Invalid location string: #{location_string}" unless start_column && rest + + end_line, end_column = rest.split(":", 2) + raise LocationError, "Invalid location string: #{location_string}" unless end_line && end_column + + new(file, start_line.to_i, start_column.to_i, end_line.to_i, end_column.to_i) + end + sig { params(file: String, location: SyntaxTree::Location).returns(Location) } def from_syntax_tree(file, location) new(file, location.start_line, location.start_column, location.end_line, location.end_column) end end @@ -38,9 +55,20 @@ @file = file @start_line = start_line @start_column = start_column @end_line = end_line @end_column = end_column + end + + sig { params(other: Location).returns(T::Boolean) } + def include?(other) + return false unless @file == other.file + return false if @start_line > other.start_line + return false if @start_line == other.start_line && @start_column > other.start_column + return false if @end_line < other.end_line + return false if @end_line == other.end_line && @end_column < other.end_column + + true end sig { override.params(other: BasicObject).returns(T.nilable(Integer)) } def <=>(other) return unless Location === other