Class: Spoom::Deadcode::Location

Inherits:
Object
  • Object
show all
Extended by:
T::Sig
Includes:
Comparable
Defined in:
lib/spoom/deadcode/location.rb

Defined Under Namespace

Classes: LocationError

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(file, start_line, start_column, end_line, end_column) ⇒ Location

Returns a new instance of Location.



54
55
56
57
58
59
60
# File 'lib/spoom/deadcode/location.rb', line 54

def initialize(file, start_line, start_column, end_line, end_column)
  @file = file
  @start_line = start_line
  @start_column = start_column
  @end_line = end_line
  @end_column = end_column
end

Instance Attribute Details

#end_columnObject (readonly)

Returns the value of attribute end_column.



43
44
45
# File 'lib/spoom/deadcode/location.rb', line 43

def end_column
  @end_column
end

#end_lineObject (readonly)

Returns the value of attribute end_line.



43
44
45
# File 'lib/spoom/deadcode/location.rb', line 43

def end_line
  @end_line
end

#fileObject (readonly)

Returns the value of attribute file.



40
41
42
# File 'lib/spoom/deadcode/location.rb', line 40

def file
  @file
end

#start_columnObject (readonly)

Returns the value of attribute start_column.



43
44
45
# File 'lib/spoom/deadcode/location.rb', line 43

def start_column
  @start_column
end

#start_lineObject (readonly)

Returns the value of attribute start_line.



43
44
45
# File 'lib/spoom/deadcode/location.rb', line 43

def start_line
  @start_line
end

Class Method Details

.from_string(location_string) ⇒ Object

Raises:



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/spoom/deadcode/location.rb', line 17

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

.from_syntax_tree(file, location) ⇒ Object



34
35
36
# File 'lib/spoom/deadcode/location.rb', line 34

def from_syntax_tree(file, location)
  new(file, location.start_line, location.start_column, location.end_line, location.end_column)
end

Instance Method Details

#<=>(other) ⇒ Object



74
75
76
77
78
# File 'lib/spoom/deadcode/location.rb', line 74

def <=>(other)
  return unless Location === other

  to_s <=> other.to_s
end

#include?(other) ⇒ Boolean

Returns:

  • (Boolean)


63
64
65
66
67
68
69
70
71
# File 'lib/spoom/deadcode/location.rb', line 63

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

#to_sObject



81
82
83
# File 'lib/spoom/deadcode/location.rb', line 81

def to_s
  "#{@file}:#{@start_line}:#{@start_column}-#{@end_line}:#{@end_column}"
end